Distributed arrays in for loop

Hi, I’m trying to get distributed arrays to work correctly for my distributed for loop. I’m getting this weird outcome where not all workers do their task always, even though I know they’ve been assigned correctly (using procs). Not sure what’s going on. Here a MWE:

using Distributed

addprocs(4)
@everywhere begin
    using DistributedArrays
    result_D = dzeros(Float64,(2,4), workers()[1:4],[1,4])
    initq = [i*ones(2,1) for i in 1:4]  
    @sync @distributed for ele in initq 
        res_local = localpart(result_D)
        res_local .= ele
    end
end

result = convert(Array,result_D)

When I run this, the results are inconsistent: sometimes the whole array stays empty, sometimes one worker does a job, sometimes a few. What could be the issue?

I think you shouldn’t define the distributed array and run the @distributed loop inside the @everywhere block. This might cause the issues you see, since somehow every worker ends up allocating their own array and I am not sure which worker will then write where.

Thank you @abraemer ! This indeed did the trick. Here the working version:

using Distributed

addprocs(4)
@everywhere begin

    using Distributed, DistributedArrays
    initq = [i*ones(2,1) for i in 1:4] 
end
result_D = dzeros(Float64,(2,8), workers()[1:4],[1,4])
    
@sync @distributed for ele in initq 
    res_local = localpart(result_D)
    res_local .= ele
end
result = convert(Array,result_D)