2
votes

I was wondering how can I update an array in a @parallel for loop in a function and return the results. Here is a simple example:

addprocs(2)

function parallel_func()
   a = Dict{Int64, Int64}()
   @sync @parallel for i in 1:10
      a[i] = 2*i
   end
   println(length(a))
   return a
end 

a = parallel_func()
println(length(a)) 

Here, a is empty after running the for loop with @parallel macro. I know @parallel copies the data on each worker and does not touch the original data, but I thought there might be a way to fetch the data from all the workers. I appreciate if you can comment on any alternatives to expedite a for loop like the example above.

1

1 Answers

0
votes

@parallel has a return. You can return an array by reducing with vcat or hcat:

arr = @sync @parallel (vcat) for i in 1:10
  ...
  element
end

You can cat to an array of Pairs and build a Dict from that.

Edit

I noticed that you're actually looking to mutate an array. In that case you should use a SharedArray or a DistributedArray.