Multi-dimensional SharedArrays.jl don't work in @distributed loops

I’m following the documentation for parallel code in Julia v1.0. The simple example in the docs for using a 1D SharedArrays and @distributed does execute correctly for me. However, if I modify the example to make the SharedArray be 2D instead of 1D, it no longer works correctly.

using SharedArrays, Distributed

a = SharedArray{Float64}(5,5)
@distributed for i = 1:5
    a[i,:] = i
end
a

5×5 SharedArray{Float64,2}:
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0

I found that explicitly doing the assignment with another inner for loop will work correctly. This is inconvenient though :frowning:

a = SharedArray{Float64}(5, 5)
@distributed for i = 1:5
    for j in 1:5
        a[i,j] = i
    end
end
a

5×5 SharedArray{Float64,2}:
1.0 1.0 1.0 1.0 1.0
2.0 2.0 2.0 2.0 2.0
3.0 3.0 3.0 3.0 3.0
4.0 4.0 4.0 4.0 4.0
5.0 5.0 5.0 5.0 5.0

I think what you’re looking for is:

using SharedArrays, Distributed

a = SharedArray{Float64}(5,5)
@distributed for i = 1:5
    a[i,:] .= i
end
a

Right now you are trying to assign a scalar value to a slice, which is actually a vector. You could also write a[i,:] = fill(i, 5), which would create an Array with 5 elements, but using broadcasting is neater and doesn’t allocate a whole Array each time.

1 Like

That works. Thanks!