I noticed a strange thing while I was playing with distributed arrays. Here is an example of the code:
using Distributed; addprocs(2) @everywhere using DistributedArrays function create_big_darray(n) darr = dzeros( Int, (n, nworkers()), workers(), [1, nworkers()] ) end function do_something_with_darray!(darr) @sync for worker in workers() @spawnat worker fill!(darr.localpart, 1) end end function fetch_result!(result, index, darr) result .= darr[:, index] end n = 2^27 darr = create_big_darray(n) do_something_with_darray!(darr) result = zeros(Int, n) @allocated fetch_result!(result, 1, darr) #2.4G allocations
I find strange that
fetch_result! allocates memory. I think it should just copy the values to the preallocated array.
Is there a way to copy the result from a worker to an existing array without any allocations?