Bug in DistributedArrays? Pushing to first local part pushes to all local parts

I found what I believe to be a bug in DistributedArrays. I am trying to push to the first local part of an array but it ends up pushing to all of the local parts.

using Distributed
addprocs(4)
@everywhere using DistributedArrays

a = dfill([], 10)

s = @spawnat 2 push!(localpart(a)[1], 999)

a

I would expect the a to be [[999], [], [], [] ...], but it actually is [[999], [999], [999], [], ...]. This is unexpected and against my intuition for how it should behave.

This behavior is not specific to Distributed, but the way (d)fill operates. From the docstring

If x is an object reference, all elements will refer to the same object:

  julia> A = fill(zeros(2), 2);

  julia> A[1][1] = 42; # modifies both A[1][1] and A[2][1]

  julia> A
  2-element Vector{Vector{Float64}}:
   [42.0, 0.0]
   [42.0, 0.0]
end

Create the array locally first and then distribute

a = distribute([ [] for _ in 1:10 ])

There is no additional overhead since dfill allocates locally first anyway.

1 Like