Module 如何在Julia中的函数中加载模块@everywhere
在使用Module 如何在Julia中的函数中加载模块@everywhere,module,julia,distributed-computing,Module,Julia,Distributed Computing,在使用addprocs创建workers之后,我正在尝试在workers中加载模块。在顶层调用addprocs时,一切正常。但是,当我将代码包装到函数中时,我不能做同样的事情 在我的例子中,我是动态添加工人的,因此使用XXX始终在顶层调用@无处不在是不可行的,我需要在函数中这样做 简言之,这是可行的: addprocs(1) @everywhere using XXX 但这并不是: function myaddprocs() addprocs(1) @everywhere us
addprocs
创建workers之后,我正在尝试在workers中加载模块。在顶层调用addprocs
时,一切正常。但是,当我将代码包装到函数中时,我不能做同样的事情
在我的例子中,我是动态添加工人的,因此使用XXX始终在顶层调用@无处不在是不可行的,我需要在函数中这样做
简言之,这是可行的:
addprocs(1)
@everywhere using XXX
但这并不是:
function myaddprocs()
addprocs(1)
@everywhere using XXX
end
有什么想法吗?你不需要到处都是。这对我很有用:
addprocs()
using mymodule # loads code on all procs but brings in to scope only on master process.
如果您想执行pmap(x->fun(x),workers())
,并且从mymodule
导出fun
,那么这就是您想要的。您可以在这里阅读:经过进一步的调查,我已经指出了一些使我的代码无法工作的问题
导入必须在addprocs
之后进行。如果以前进行过导入,则导入的前缀必须为@everywhere
函数内部的顶级表达式(例如使用
)不起作用,除非包装在eval
语句中
对我的代码的修复将是:
function myaddprocs()
addprocs(1)
eval(macroexpand(quote @everywhere using XXX end))
end
例子
我已经在Julia 0.6.1上测试了以下代码片段。我还在SGE集群(OGS/GE 2011.11p1)上使用相同的版本对它们进行了测试,将所有addprocs
替换为addprocs\u SGE
,并导入ClusterManagers.jl
。以下代码段可以工作:
在addprocs
之后使用:
addprocs(1)
using SpecialFunctions
pmap(x->SpecialFunctions.sinint(1), workers())
在addprocs
之前和之后使用,第二个使用@where
:
using SpecialFunctions
addprocs(1)
@everywhere using SpecialFunctions
pmap(x->sinint(1), workers())
使用包装在eval
后addprocs
函数中
function getprocs()
addprocs(1)
eval(Expr(:using,:SpecialFunctions))
pmap(x->SpecialFunctions.sinint(1), workers())
end
getprocs()
using SpecialFunctions
function getprocs()
addprocs(1)
@everywhere using SpecialFunctions
pmap(x->sinint(1), workers())
end
getprocs()
function getprocs()
addprocs(1)
using SpecialFunctions
pmap(x->SpecialFunctions.sinint(1), workers())
end
getprocs()
- 与前面相同,with
@everywhere
应用于eval
function getprocs()
addprocs(1)
@everywhere eval(Expr(:using,:SpecialFunctions))
pmap(x->sinint(1), workers())
end
getprocs()
- 与之前相同,改为在
eval
中使用@everywhere
function getprocs()
addprocs(1)
eval(macroexpand(quote @everywhere using SpecialFunctions end))
pmap(x->sinint(1), workers())
end
getprocs()
另一方面,这些代码段不起作用:
在addprocs
using SpecialFunctions
addprocs(1)
pmap(x->SpecialFunctions.sinint(1), workers())
using SpecialFunctions
addprocs(1)
using SpecialFunctions
pmap(x->SpecialFunctions.sinint(1), workers())
在addprocs
using SpecialFunctions
addprocs(1)
pmap(x->SpecialFunctions.sinint(1), workers())
using SpecialFunctions
addprocs(1)
using SpecialFunctions
pmap(x->SpecialFunctions.sinint(1), workers())
在函数中使用
function getprocs()
addprocs(1)
eval(Expr(:using,:SpecialFunctions))
pmap(x->SpecialFunctions.sinint(1), workers())
end
getprocs()
using SpecialFunctions
function getprocs()
addprocs(1)
@everywhere using SpecialFunctions
pmap(x->sinint(1), workers())
end
getprocs()
function getprocs()
addprocs(1)
using SpecialFunctions
pmap(x->SpecialFunctions.sinint(1), workers())
end
getprocs()
在函数中使用
function getprocs()
addprocs(1)
eval(Expr(:using,:SpecialFunctions))
pmap(x->SpecialFunctions.sinint(1), workers())
end
getprocs()
using SpecialFunctions
function getprocs()
addprocs(1)
@everywhere using SpecialFunctions
pmap(x->sinint(1), workers())
end
getprocs()
function getprocs()
addprocs(1)
using SpecialFunctions
pmap(x->SpecialFunctions.sinint(1), workers())
end
getprocs()
Cako的解决方案很有帮助,但我不得不添加模块作为macroexpand的第一个参数:
eval(macroexpand(Distributed,quote @everywhere using DistributedArrays end))
嗨,弗洛里安,你说得对,这很有效!然而,我不得不使用mymodule.fun
。不幸的是,我刚刚意识到,我还必须从ClusterManager获得addprocs_sge的这项工作,而当前的解决方案不起作用。我将提交一份关于他们项目的报告。谢谢如果你不导出fun
,那么你就必须执行mymodule.fun。为什么addprocs\u sge
不起作用(到底什么不起作用)?如果你在问题的底部添加一个可复制的例子,你将有更好的机会获得帮助!我花了更多的时间整理这件事,并把我的结论贴出来作为答案。我认为现在一切都有意义了,谢谢你的帮助