如何使用 doSMP 初始化工作程序?

发布于 2024-12-07 22:02:12 字数 740 浏览 0 评论 0原文

有没有一种方法可以初始化类似于snow包中的clusterEvalQclusterExport的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿越时光隧道 2024-12-14 22:02:12

foreach 的第一个参数是 ...,它传输迭代对象以评估目标表达式。如果您只需要对象的一部分,那么使用 iter 只传递对象的一部分可能会更有效。

w <- startWorkers(workerCount = 4)
registerDoSMP(w)

foreach(x=iter(x),y=iter(y),z=iter(z) ) %dopar% (x*y*z)

调用环境中的对象仍然可用:

foreach(1:10 ) %dopar% (x*y*z)  # Somewhat repetitious # 

zed <- 20:1
foreach(x=iter(x) ) %dopar% (x*zed)

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 use iter to only pass portions of an object.

w <- startWorkers(workerCount = 4)
registerDoSMP(w)

foreach(x=iter(x),y=iter(y),z=iter(z) ) %dopar% (x*y*z)

The objects in the calling environment are still available:

foreach(1:10 ) %dopar% (x*y*z)  # Somewhat repetitious # 

zed <- 20:1
foreach(x=iter(x) ) %dopar% (x*zed)
屋檐 2024-12-14 22:02:12

试试这个:

library(doSMP)
library(foreach)
w <- startWorkers(workerCount = 4)
registerDoSMP(w)
foreach(i = 1:3) %dopar% sqrt(i)

或者你可以使用 doSNOW 和

registerDoSNOW(cl)

http://cran.r- project.org/web/packages/doSNOW/doSNOW.pdf

最后,还有 doMC(不适用于 Windows)

library(doMC)
registerDoMC()
foreach(i = 1:3) %dopar% sqrt(i)

所有这些技术都在 2.14 中的新并行包之前工作,我相信它已经对这些技术进行了一些统一。

Try this:

library(doSMP)
library(foreach)
w <- startWorkers(workerCount = 4)
registerDoSMP(w)
foreach(i = 1:3) %dopar% sqrt(i)

Or you could use doSNOW and

registerDoSNOW(cl)

http://cran.r-project.org/web/packages/doSNOW/doSNOW.pdf

Finally, there is doMC (not for windows)

library(doMC)
registerDoMC()
foreach(i = 1:3) %dopar% sqrt(i)

All these techniques work before the new parallel package in 2.14, which I believe has done some unification of these techniques.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文