Memory allocation and SharedArrays

Hi all,

I am new to parallel computing and I had a question regarding memory allocation. Is it possible to update an array in place while doing parallel computing? I tried using SharedArrays and DistributedArrays without success.

As an example,

# initializes the array to be updated in place 
T = SharedArray{Float64,2}(10, 10, pids = workers());
for (i, ind) in enumerate(CartesianIndices(T))
    T[ind] = i 
end # works as intended 
# reset all entries to zero
T .= 0.0;
@async @distributed for (i, ind) in enumerate(CartesianIndices(T))
    T[ind] = i 
end # T is only filled with zeros

The question has already been asked on this forum but I could not find a definitive answer.

Thank you very much in advance!

3 Likes

Trying to get an MWE this seems to work:

julia> using Distributed, SharedArrays

julia> addprocs(4)
4-element Array{Int64,1}:
 2
 3
 4
 5

julia> T = SharedArray{Float64, 2}(10,10, pids = workers());

julia> @sync @distributed for i in 1:100   
          T[i] = i
       end
Task (done) @0x0000000011642850

julia> T
10Ă—10 SharedArray{Float64,2}:
  1.0  11.0  21.0  31.0  41.0  51.0  61.0  71.0  81.0   91.0
  2.0  12.0  22.0  32.0  42.0  52.0  62.0  72.0  82.0   92.0
  3.0  13.0  23.0  33.0  43.0  53.0  63.0  73.0  83.0   93.0
  4.0  14.0  24.0  34.0  44.0  54.0  64.0  74.0  84.0   94.0
  5.0  15.0  25.0  35.0  45.0  55.0  65.0  75.0  85.0   95.0
  6.0  16.0  26.0  36.0  46.0  56.0  66.0  76.0  86.0   96.0
  7.0  17.0  27.0  37.0  47.0  57.0  67.0  77.0  87.0   97.0
  8.0  18.0  28.0  38.0  48.0  58.0  68.0  78.0  88.0   98.0
  9.0  19.0  29.0  39.0  49.0  59.0  69.0  79.0  89.0   99.0
 10.0  20.0  30.0  40.0  50.0  60.0  70.0  80.0  90.0  100.0
2 Likes

That is indeed working! Surprising difference…

You should wait() on that @async block to ensure it runs, and doesn’t throw any errors (which you won’t find out about until you wait() on it, anyway).