Julia Distributed, failed to modify the global variable of the worker

295 views Asked by At

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

1

There are 1 answers

1
Mark Kittisopikul On BEST ANSWER

By running fetch(@spawnat 2 x) you unintentionally transferred the value of x from the current worker to worker 2.

See this example:

julia> x = 3
3

julia> fetch(@spawnat 2 x)
3

If you want to retrieve the value of x, you could try the following:

julia> @everywhere x = 0

julia> @sync @distributed for i in 1:10
           modify_x()
       end
      From worker 3:    1
      From worker 3:    2
      From worker 3:    3
      From worker 3:    4
      From worker 3:    5
      From worker 2:    1
      From worker 2:    2
      From worker 2:    3
      From worker 2:    4
Task (done) @0x000000000d34a6d0      From worker 2:     5


julia> @everywhere function fetch_x()
           return x
       end

julia> fetch(@spawnat 2 fetch_x())
5

See https://docs.julialang.org/en/v1/manual/distributed-computing/#Global-variables