我目前正在使用 Atom 编辑器与 Julia 0.5 一起工作,但不知何故未能使我的工作线程可以使用函数。这是我的测试文件 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))
执行这个文件,我从 master 和 worker 得到一个“Hi”作为输出,master 也会输出“a is ...”行。但是工作人员不会,并且在 remotecall_fetch 行上它会抛出以下错误消息(缩短)
LoadError: On worker 2:
UndefVarError: testie not defined
http://docs.julialang.org/en/release-0.5/manual/parallel-computing/状态:使用 DummyModule 会导致模块在所有进程上加载;但是,该模块仅在执行该语句的模块中被纳入范围。仅此而已,我看不到如何解决这种情况。我尝试在 using 行之前添加一个 @everywhere,还尝试添加一个
@everywhere include("test.jl")
就在它之前。没有帮助。这应该很简单,但我无法弄清楚。在 SO 我只找到 Julia parallel programming - Making existing function available to all workers但这并没有真正回答我。
最佳答案
如果您使用 include
自己导入模块那么你需要告诉 Julia 你想使用t1
通过在该模块前加上 testie.t1
试试这个
if nprocs()<2
addprocs(1)
end
@everywhere include("testie.jl")
println(remotecall_fetch(testie.t1,2)) #NB prefix here
在哪里
testie.jl
是:module testie
export t1
function t1()
a= rand()
println("a is $a, thread $(myid())")
return a
end
end
关于multithreading - Julia - worker 找不到功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40389039/