krcools
krcools

Reputation: 927

Evaluation context of @everywhere

Consider the following code:

module Mod

    function f()
        @everywhere store(v) = println(12+v)
        g(3,Main.store)
    end

    function g(n,p)
        for i in 1:n
            p(8)
        end
    end

end

Mod.f()

The line of interest is g(3,Main.store). Without specifying the module, execution of this snippet at the REPL (w.r.t Main) results in an UnDefVarErrror: store.

Is it per design that the line is not evaluated in the current Module? Why? The serial counterpart (without @everywhere runs fine without specifying Main).

Upvotes: 1

Views: 82

Answers (2)

krcools
krcools

Reputation: 927

This recently posted issue resolved all my issues. As it turns out I do not need to define the closure store @everywhere. It can simply be passed as data in remotecall_wait.

In other words the code posted by Tasos will simply work when additional processes are added before using the containing module.

Upvotes: 1

Tasos Papastylianou
Tasos Papastylianou

Reputation: 22255

If my understanding is correct, the @everywhere macro essentially tells julia to execute the instruction store(v) = println(12+v) on all the processes (including the main process, i.e. the one running the REPL). In other words, this happens at global scope per process. Therefore it makes sense that after this step, you can access the store function via Main.store, i.e. at the outermost scope for the process, since it does not exist in the current scope (i.e. inside your module / f function).

I'm assuming by "serial counterpart" you mean directly typing in the REPL:

function f()
    store(v) = println(12+v)
    g(3, store)
end

function g(n,p)
    for i in 1:n
    p(8)
end

f()

in which case, yes this is a completely different scenario. The store function is now local to f(), and that's where you use it, so it wouldn't make sense to try and use a namespace qualifier to access it from somewhere else.

Upvotes: 2

Related Questions