Groups 210 of 99+ julia-users › copy assignment opeartion on existing and valid destination arrays 9 posts by 3 authors Roy Wang 10 2 14 I often need a copy assignment type of operation to an existing destination array of the exact same element type and size. Let's only talk about arrays of concrete types, like a multi-dimensional array of floats. This is useful when I write optimization solvers, and I need to store vectors or matrices from the previous step. I usually pre-allocate a pair of arrays of the same type and size, x and x_next, then do: x_next x; at the end of each iteration of my solver. At first, I thought using copy shallow copy on them is fine to make sure they are separate entities, since floating point numbers and integers are concrete types in Julia. While I verified this is true at least on arrays of Float64s , I looked at around line 202 at the time of this post , and copy seems to call copy! similar a , a . To my understanding, this allocates a new destination array, fills it with the corresponding values from the source array, then assigns the pointer of this new destination array to x_next, and the garbage collector removes the old array that x_next was pointing to. This is a lot of work when I just want to traverse through x_next, and assign it the corresponding values from x. Please correct me if my understanding is wrong! This is a really common operation. I'd appreciate it if someone can advise me whether there is already an existing method for doing this or a better solution before I write my own. Cheers, Roy Roy Wang 10 2 14 This kind of routine is what I'm talking about... copy assignment for vectors function copyassignment! a::Vector,b::Vector assert length a length b for n 1:length a a n b n ; end end My questions: 1 Is there a standard function that does this? 2 Is there a better way to do this so it'll handle any type of multi-dimensional array of integers and floats without performance penalty? John Myles White 10 2 14 Re: julia-users Re: copy assignment opeartion on existing and valid destination arrays Why not use copy! -- John Roy Wang 10 2 14 oops, add assert eltype a eltype b in the checks too, otherwise there might be an inexact error when mixing integers and floats. Roy Wang 10 2 14 Re: julia-users Re: copy assignment opeartion on existing and valid destination arrays Hey John, Ah geez, copy! was only 2 lines lower than copy in abstractarray-jl. Thanks! Roy Wang 10 2 14 Re: julia-users Re: copy assignment opeartion on existing and valid destination arrays Is there a version of copy! that uses parallel? My x and x_next are usually huge. David P. Sanders 10 4 14 Re: julia-users Re: copy assignment opeartion on existing and valid destination arrays Hi, Are you sure that you need a copy operation? If I understand correctly what you are doing, you just need access to the x from the previous iteration. Could you not just swap x and x_next and avoid the copy : X, x_next x_next, x so you create them once only . Roy Wang 10 21 14 Re: julia-users Re: copy assignment opeartion on existing and valid destination arrays This is great, thanks! I verified pointer X is the same as what pointer x_next used to be before running your line of code, and pointer x_next after running your line of code is the same as pointer x . I did not know of this about Julia! David P. Sanders 10 21 14 Re: julia-users Re: copy assignment opeartion on existing and valid destination arrays El martes, 21 de octubre de 2014 14:25:33 UTC-5, Roy Wang escribió: This is great, thanks! I verified pointer X is the same as what pointer x_next used to be before running your line of code, and pointer x_next after running your line of code is the same as pointer x . I did not know of this about Julia! I'm glad this was useful! This is a common idiom programming style trick in any language. Unfortunately, often people don't know that this is possible or practical, and they waste a lot of time copying arrays unnecessarily when they could just be swapping the pointers, as you put it. In most languages you would need to call an explicit function, like `swap` in Cpp, in order to do this. This idiom, x, x_next x_next, x works in both Python and Julia. Yes, it is quite neat ; The capital X that was in my original post was due to an auto-correct when typing from my phone. It should be small x to work with the code in your loop. David. On Saturday, 4 October 2014 11:05:33 UTC-4, David P. Sanders wrote: Hi, Are you sure that you need a copy operation? If I understand correctly what you are doing, you just need access to the x from the previous iteration. Could you not just swap x and x_next and avoid the copy : X, x_next x_next, x so you create them once only .