JuliaLang / Distributed.jl

Create and control multiple Julia processes remotely for distributed computing. Ships as a Julia stdlib.
https://docs.julialang.org/en/v1/stdlib/Distributed/
MIT License
31 stars 11 forks source link

module globals and parallel methods #19

Open amitmurthy opened 9 years ago

amitmurthy commented 9 years ago

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?

ArchRobison commented 9 years ago

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?

toivoh commented 9 years ago

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

malmaud commented 9 years ago

@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 commented 9 years ago

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