Distributed.jl icon indicating copy to clipboard operation
Distributed.jl copied to clipboard

module globals and parallel methods

Open amitmurthy opened this issue 10 years ago • 4 comments

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?

amitmurthy avatar Dec 02 '14 05:12 amitmurthy

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?

ArchRobison avatar Dec 11 '14 20:12 ArchRobison

For the second question: I belive that eval(MyModule, expr) should work, maybe also MyModule.eval(expr).

toivoh avatar Dec 11 '14 21:12 toivoh

@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.

malmaud avatar Oct 14 '15 07:10 malmaud

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

malmaud avatar Oct 16 '15 21:10 malmaud