Often times in parallel code, I find myself in need of populating an empty array. I have gone back and forth between using a vcat operation versus a SharedArray. A typical pseudo-code would look like:
function my_parallel_sharedarray()
LL = SharedArray{Float64}(10, 1)
@distributed for i in 1:10
LL[i] = #value
end
return sum(LL)
end
function my_parallel_vcat()
z = @distributed (vcat) for i in 1:10
LL = #value
end
return sum(z)
end
What would be a preferred way to allocate an empty array (entry-by-entry, row-by-row or column-by-column) in parallel especially when the final array to be allocated in large (~1000 x 1 or 1000 * 100 etc.)? If one of the above ways is strictly preferred, could someone please point me to the downside of the other sub-optimal way, ideally with the help of a small toy example?
Is it required that you start with an empty array? If not, I suggest instead doing something like
using Distributed: pmap, addprocs
addprocs(5)
pmap([(i, j) for i in 1:10, j in 1:10]) do (i, j)
(factorial(i) + factorial(j))/(factorial(i)*factorial(j))
end
which will create and fill the following matrix in parallel:
Thanks for the very helpful suggestion! Yes, I would prefer to do it by first pre-allocating an empty array. The reason is that each value is tied to a unique id and if there are issues in a value (say, I get something like a NaN, -Inf or Inf), I can look at the id corresponding to that value and then look back at all the data linked to that id. It would be simpler to examine issues if I can easily track ids from values and the pre-allocated array is a very simple way to achieve that. My worry is that if vcat keeps creating a copy then the vcat approach will allocate unnecessarily for large matrices but I want to better understand what vcat would do relative to a pre-allocated SharedArray in parallel mode drawing on the experience of julians here!