I try to keep some computation results in each workers and fetch them together after all computation is done. However, I could not actually modify the variable of the workers.
Here is a simplified example
using Distributed
addprocs(2)
@everywhere function modify_x()
global x
x += 1
println(x) # x will increase as expected
end
@everywhere x = 0
@sync @distributed for i in 1:10
modify_x()
end
fetch(@spawnat 2 x) # gives 0
This sample tries to modify x
contained in each worker. I expect x
to be like 5
, but the final fetch
gives the initial value 0