How to NOT interpolate on remote worker?

julia> using Distributed

julia> addprocs(2);

julia> @everywhere a = [1,2]

julia> @everywhere println(a)
[1, 2]
      From worker 3:    [1, 2]
      From worker 2:    [1, 2]

julia> a = 5
5

julia> @everywhere println(a)
5
      From worker 2:    [1, 2]
      From worker 3:    [1, 2]

julia> @fetchfrom 3 a[2]
ERROR: On worker 3:
BoundsError
Stacktrace:

julia> @everywhere println(a)
5
      From worker 2:    [1, 2]
      From worker 3:    5

this above behavior is weird, how do I access the a on the remote?

My real use case is if I have a @distributed for loop, how can I ask Julia NOT to use my local (main process) variable in the body of the loop?

1 Like

for my second question, it seems to be a scoping problem:

as long as X does not exist in my main process, the loop body will use the remote variable correctly

Sounds like you got it figured out, but in case its useful, this is the relevant section, and @everywhere println(a) is indeed the correct way to access the value of the global variable a on the worker without triggering an auto-send from a on the local process (conversely, @fetchfrom 3 a[2] triggered the auto-send, hence why it ended up being 5 on that worker).

1 Like

The other thing you can do is “hide” accessing the global from the auto-shipping mechanism (and hence prevent auto-sending it) inside an @eval, e.g.

@fetchfrom 3 @eval a[2]

will not send a, it’ll return the global a[2] exactly as it currently exists on the worker.

1 Like