Julia: Importing SharedArrays module work without @everywhere?

Created on 15 Aug 2020  路  5Comments  路  Source: JuliaLang/julia

I expected that we need @everywhere macro before importing SharedArrays module, i.e., @everywhere using SharedArrays, but the code below seems to be working perfectly fine with or without @everywhere.
Secondly, I also expected that the variable y below needs to be defined @everywhere, i.e.,@everywhere y=5 for it to be recognized by all processes, but it worked fine without it.

using Distributed
addprocs(4)
using SharedArrays #I expected that we need @everywhere using SharedArrays

x = SharedArray{Int}(10)
y = 5 #this is available to all processes even without adding @everywhere macro
@distributed for i = 1:10
    x[i] = i + y
end

Note: If I append @everywhere in the above two cases, the code still works fine.
I am not sure if this is a bug, but I expected the approach without @everywhere to throw an error. Is it because SharedArrays.jl is in stdlib? I suppose we should have more consistent way of importing ShareadArrays module or declaring a variable in this case?

I am using Julia version 1.5.0

All 5 comments

The first behavior is expected. After adding processes using loads the packages on all processes to support serialization and deserialization of datatypes.

@distributed creates a lambda with the body of the for-loop. That lambda is serialized and send over the wire to the other process, and since y is only read from it's value is send over as well.

The point regarding y makes sense. Also, I agree with the first point that after adding processes using loads the packages on all processes. However, if I add the processes after importing SharedArrays, then too the code works perfectly (the SharredArray module is available to all processes). I am not sure why this works.

using Distributed
using SharedArrays #I expected that we need @everywhere using SharedArrays

addprocs(4)

x = SharedArray{Int}(10)
y = 5 
@distributed for i = 1:10
    x[i] = i + y
end

This works because because SharedArray{T}(dims) calls SharedArrays.shared_pids([]) which gives the process id's of processes in the same node at the time of calling SharedArray{T}(dims). Since your code doesn't use any SharedArray specific method or type on any of the worker processes, this doesn't require loading SharedArray package on worker processes

@OkonSamuel yes that makes sense. So, SharedArrays is just like a normal package which if required by any method or type on any worker processes should have @everywhere before it, i.e., @everywhere using SharedArrays, else @everywhere isn't required. Thank you!

Yes you got the point. Let me add this.
As earlier mentioned by @vchuravy. If you have spawned some processes before loading a package, then the package to be loaded is loaded on all processes (even if you didn't use @everywhere using to load the package).
i.e.

using Distributed
addprocs(4)
using SharedArrays 

and

using Distributed
addprocs(4)
@everywhere using SharedArrays 

both load SharedArrays package on all processes. The only difference is that the first case only bring the methods, variables and types exported by SharedArrays into scope only on the master process. see second example in https://docs.julialang.org/en/v1/manual/distributed-computing/#code-availability-1.
Hope this helps

Was this page helpful?
0 / 5 - 0 ratings