如何使用 doSMP 初始化工作程序?
有没有一种方法可以初始化类似于snow包中的clusterEvalQ
和clusterExport
的doSMP集群?例如:
x <- 1:10
y <- 10:1
z <- rnorm(10)
cl <- makeSOCKcluster(2)
clusterEvalQ(cl, library(quantmod))
clusterExport(cl, list("x","y","z"))
clusterEvalQ(cl, ls())
clusterEvalQ(cl, search())
doSMP 有一个 initEnvir
选项,但是 ?doSMP
说
'initEnvir' 是每个工作线程在执行任何操作之前执行的函数 任务的执行与 foreach 相关。其目的是 初始化执行环境,或者一般的工作线程。 仅当该工作人员至少执行时,它才会由该工作人员执行 与 foreach 相关的一项任务。
每个工作人员都需要几个大对象的副本才能运行我发送到 foreach
的表达式。此外,我需要使用这些大对象的相同版本调用 foreach
数百次。每次调用 foreach
时都复制这些对象是低效的。
即使没有现成的方法来做到这一点,我也会很感激。
Is there a way to initialize a doSMP cluster similar to clusterEvalQ
and clusterExport
in the snow package? For example:
x <- 1:10
y <- 10:1
z <- rnorm(10)
cl <- makeSOCKcluster(2)
clusterEvalQ(cl, library(quantmod))
clusterExport(cl, list("x","y","z"))
clusterEvalQ(cl, ls())
clusterEvalQ(cl, search())
There is an initEnvir
option to doSMP, but ?doSMP
says
‘initEnvir’ is a function to be executed by each worker before any tasks are executed associated with a foreach. Its purpose is to initialize the execution environment, or the worker in general. It is only executed by a worker if that worker executes at least one task associated with the foreach.
Each worker needs a copy of several large objects in order to run the expression I send to foreach
. Additionally, I need to call foreach
several hundred times, with identical versions of these large objects. It would be inefficient to copy these objects for every call to foreach
.
Even if there isn't a ready-made way to do this, I'd appreciate a kludge.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
foreach
的第一个参数是...
,它传输迭代对象以评估目标表达式。如果您只需要对象的一部分,那么使用 iter 只传递对象的一部分可能会更有效。调用环境中的对象仍然可用:
The first argument to
foreach
is...
which transports the iterated objects for the evaluation of target expression. If you only need part of the object, then it may be more efficient to useiter
to only pass portions of an object.The objects in the calling environment are still available:
试试这个:
或者你可以使用 doSNOW 和
http://cran.r- project.org/web/packages/doSNOW/doSNOW.pdf
最后,还有 doMC(不适用于 Windows)
所有这些技术都在 2.14 中的新并行包之前工作,我相信它已经对这些技术进行了一些统一。
Try this:
Or you could use doSNOW and
http://cran.r-project.org/web/packages/doSNOW/doSNOW.pdf
Finally, there is doMC (not for windows)
All these techniques work before the new parallel package in 2.14, which I believe has done some unification of these techniques.