How to write to multiple indices of an array at the same time in Julia?

Viewed 118

I would like to see something like this working in Julia:

using Distributed
addprocs(4)

@everywhere arr = Array{Int}(undef, 10)
for i = 1:10
    @spawn arr[i] = i
end

What is the proper way of doing this?

1 Answers

You have the following ways to parallelize the process.

  1. Threads (requires setting JULIA_NUM_THREADS system variable)

    arr = Array{Int}(undef, 10)
    Threads.@threads for i = 1:10
        arr[i] = i
    end
    
  2. SharedArrays

    using Distributed, SharedArrays
    addprocs(4)
    arr = SharedVector{Int}(10)
    @sync @distributed for i in 1:10
        arr[i] = i 
    end
    

    Note that a common error is to forget to place @sync before @distributed that does not have an aggregator function (see the last example).

  3. Aggregate the results of your distributed computation

    using Distributed
    addprocs(4)
    arr = @distributed (append!) for i in 1:10
        [i]
    end
    
Related