在Julia中,我想在一个定义在模块内的函数中使用
这会返回一个非常长的错误信息。
我想做的是编写一个包,其中包含一些函数,可以选择性地并行执行操作。因此,像
addprocs
和pmap
。以下是一个愚蠢的例子:module test
using Distributions
export g, f
function g(a, b)
a + rand(Normal(0, b))
end
function f(A, b)
close = false
if length(procs()) == 1 # If there are already extra workers,
addprocs() # use them, otherwise, create your own.
close = true
end
W = pmap(x -> g(x, b), A)
if close == true
rmprocs(workers()) # Remove the workers you created.
end
return W
end
end
test.f(randn(5), 1)
这会返回一个非常长的错误信息。
WARNING: Module test not defined on process 4
WARNING: Module test not defined on process 3
fatal error on fatal error on WARNING: Module test not defined on process 2
43: : WARNING: Module test not defined on process 5
fatal error on fatal error on 5: 2: ERROR: UndefVarError: test not defined
in deserialize at serialize.jl:504
in handle_deserialize at serialize.jl:477
in deserialize at serialize.jl:696
...
in message_handler_loop at multi.jl:878
in process_tcp_streams at multi.jl:867
in anonymous at task.jl:63
Worker 3 terminated.
Worker 2 terminated.ERROR (unhandled task failure): EOFError: read end of file
WARNING: rmprocs: process 1 not removed
Worker 5 terminated.ERROR (unhandled task failure): EOFError: read end of file
4-element Array{Any,1}:Worker 4 terminated.ERROR (unhandled task failure): EOFError: read end of file
ERROR (unhandled task failure): EOFError: read end of file
ProcessExitedException()
ProcessExitedException()
ProcessExitedException()
ProcessExitedException()
我想做的是编写一个包,其中包含一些函数,可以选择性地并行执行操作。因此,像
f
这样的函数可能会接受一个参数par::Bool
,如果用户使用par = true
调用f
,则会执行类似我上面展示的操作,否则将循环执行。因此,在f
的定义内部(以及在模块test
的定义内部),我希望创建工作进程并向它们广播Distributions包和函数g
。
f
是在一个模块中定义的。您发布的代码在REPL中执行是可以工作的。但是在模块内部定义f
,然后调用f
是不起作用的。我会更新问题。 - jcz