Distributed.jl
Distributed.jl copied to clipboard
module globals and parallel methods
Consider the following:
julia> addprocs(1)
1-element Array{Int64,1}:
2
julia> @everywhere module Foo
foo() = remotecall(2, ()->(global X=[1]))
bar() = @everywhere X[1]=2
end
julia> Foo.foo()
RemoteRef(2,1,6)
julia> Foo.bar()
exception on 1: exception on 2: ERROR: X not defined
in eval at /home/amitm/Work/julia/julia/base/sysimg.jl:7
in anonymous at multi.jl:1439
in run_work_thunk at multi.jl:598
in run_work_thunk at multi.jl:607
in anonymous at task.jl:6
This is because foo() creates X as module global, while the @everywhere call refers to Main
Would it be appropriate to have the remote part of all parallel methods execute under the same module as the calling module? Is information about the calling module available to a function in Base?
Thinking about this revealed how fuzzy I am on the module system. Modules seem to be objects. For example, a==b works for two modules a and b. But if I have two modules in different processes, what makes them the "same"? The path of names to reach them? Also, just for serial code, is there a way to "inject" evaluation of an expression into a module, or is that a meaningless question?
For the second question: I belive that eval(MyModule, expr) should work, maybe also MyModule.eval(expr).
@ArchRobison The only built-in notion of 'sameness' for modules is if the two module instances are "==="; ie have the same memory address. Each invocation of module X ... end will create a new module object, so module equality has nothing to do with the name of the module.
Related:
julia> module M
x=1
y=@fetch x+1
end
WARNING: Module M not defined on process 2
fatal error on 2: ERROR: UndefVarError: M not defined