Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia - Worker can't find function

I'm currently using the Atom editor to work with Julia 0.5 and somehow fail to make functions available to my worker threads. Here is my testfile test.jl:

module testie
export t1
  function t1()
    a= rand()
    println("a is $a, thread $(myid())")
    return a
  end
end

if nprocs()<2
  addprocs(1)
end
@everywhere println("Hi")
using testie
t1()
println(remotecall_fetch(t1,2))

Executing this file, I get as output a "Hi" from the master and the worker, and the master will also output the "a is ..." line. But the worker won't, and on the remotecall_fetch line it throws the following error msg (shortened)

LoadError: On worker 2:
UndefVarError: testie not defined

http://docs.julialang.org/en/release-0.5/manual/parallel-computing/ states: using DummyModule causes the module to be loaded on all processes; however, the module is brought into scope only on the one executing the statement. Nothing more I could see how to solve this situation. I tried to add an @everywhere before the using line, also tried to add an @everywhere include("test.jl") right before it. Didn't help. This should be really simple but I can't figure it out.

On SO I only found Julia parallel programming - Making existing function available to all workers but this doesn't really answer it to me.

like image 302
Mahrgell Avatar asked Sep 18 '26 13:09

Mahrgell


1 Answers

If you are importing a module yourself with include then you need to tell julia that you want to use t1 from that module by prefixing it with testie.t1

Try this

if nprocs()<2
  addprocs(1)
end
@everywhere include("testie.jl")
println(remotecall_fetch(testie.t1,2)) #NB prefix here

where testie.jl is:

module testie
export t1
  function t1()
    a= rand()
    println("a is $a, thread $(myid())")
    return a
  end
end
like image 181
Alexander Morley Avatar answered Sep 21 '26 02:09

Alexander Morley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!