How do I push a value to all processes?

First what I’m trying to do:

using Distributed
addprocs(1)

target = string(@__DIR__, "/dir2/d2.jl")
@everywhere include(target)

With a directory structure like:

  • /dir1
    • m1.jl
    • /dir 2
      • d2.jl

The above code is in m1.jl. This is giving me an error UndefVarError: target not defined on worker 2. For development I’m running this under Atom, which means the root directory from which julia is started is the root of the project. However when running “normally” I would expect my current directory to be “dir1”…probably.

The above could (if it worked) also handle the situation if the current directory is NOT dir1, like if I executed: julia ~/project/dir1/m1.jl from the console. If I just did @everywhere include("dir2/d2.jl") I’m restricted to only being able to run if my current directory is dir1.

Try:

@everywhere target = string(@__DIR__, "/dir2/d2.jl")
@everywhere include(target)
1 Like

Well that’s embarrassing, that worked. I didn’t try it because I just assumed @__DIR__ would expand on the distributed process…

Thanks for the help.

I think @__DIR__ is not the issue in your code.
The problem is that target is only defined in the main process.

1 Like

Or interpolate the value of target:

target = string(@__DIR__, "/dir2/d2.jl")
@everywhere include($target)