允许每个工作人员注册和分发其他工作人员 [英] Allow foreach workers to register and distribute sub-tasks to other workers

查看:179
本文介绍了允许每个工作人员注册和分发其他工作人员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个R代码,涉及几个foreach工作者并行执行一些任务。我正在使用foreach和doMC来达到这个目的。我想让每个foreach工作人员招募一些新的工作人员,并将他们的代码的一些部分分发给他们。



当前的代码如下所示: / b>

  require(doMC)
require(foreach)
registerDoMC(cores = 8)

foreach(i =(1:8))%dopar%{
<<<一些代码在这里>>
for(j in c(1:4)){
<<<<其他一些代码>>




$ b $ p
$ b我正在寻找一个理想的代码, :
$ b $ pre $ require(doMC)
require(foreach)
registerDoMC(cores = 8)

foreach(i =(1:8))%dopar%{
<<<这里的一些代码>>
foreach(j =(1:4))%dopar%{
<<<其他一些代码>>




$ b我看到了一个使用doSNOW进行多范式并行的例子和doMC here(https://www.rmetrics.org/文件/ Meielisalp2009 /简报/ Lewis.pdf#页= 17)。但是,我不知道它是否做我想要的。

另外,它似乎嵌套的foreach 不适用,因为它需要合并两个循环(请参阅此处),而在我的情况下,这是不优选的;第二个循环只能帮助第一个代码的一部分。如果我错了,请纠正。



谢谢。

在foreach循环中有一个foreach循环没有特别的问题。下面是一个doSNOW循环中doMC循环的例子:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 'host-1','host-2')
cl < - makeSOCKcluster(hosts)
registerDoSNOW(cl)
r < - foreach(i = 1:4,.packages = 'doMC')%dopar%{
registerDoMC(2)
foreach(j = 1:8,.combine ='c')%dopar%{
i * j
}
}
stopCluster(cl)

为内部循环,但你可以做任何你想要的。你也可以在两个循环中使用doSNOW,但是你需要在外部的foreach循环中创建和停止积雪。



下面是一个在doMC循环:

  library(doMC)
registerDoMC(2)
r < - foreach(i = 1:2,.packages ='doMC')%dopar%{
ppid < - Sys.getpid()
registerDoMC(2)
foreach(j = 1:2)%dopar %{
c(ppid,Sys.getpid())
}
}

结果表明,共有六个进程由doMC包分配,尽管只有四个进程执行了内部循环的主体:

 > r 
[[1]]
[[1]] [[1]]
[1] 14946 14949

[[1]] [[2] ]
[1] 14946 14951

$ b [[2]]
[[2]] [[1]]
[1] 14947 14948

[[2]] [[2]]
[1] 14947 14950

当然,您需要小心,不要在单个节点上启动太多的进程。我发现这种嵌套有点尴尬,这导致了嵌套操作符的发展。

I have an R code that involves several foreach workers to perform some tasks in parallel. I am using foreach and doMC for this purpose. I want to let each of the foreach workers recruits some new workers and distribute some parts of their code, which is parallelizable, to them.

The current code looks like:

require(doMC)
require(foreach)
registerDoMC(cores = 8)

foreach (i = (1:8)) %dopar% {
<<some code here>>
    for (j in c(1:4))  {
    <<some other code here>>
    }
}

I am looking for an ideal code that would look like:

require(doMC)
require(foreach)
registerDoMC(cores = 8)

foreach (i = (1:8)) %dopar% {
<<some code here>>
    foreach (j = (1:4)) %dopar% {
    <<some other code here>>
    }
}

I saw an example of multi-paradigm parallelism using doSNOW and doMC here (https://www.rmetrics.org/files/Meielisalp2009/Presentations/Lewis.pdf#page=17). However, I do not know whether it does what I want or not.

Also, it seems Nested foreach is not applicable because it requires merging the two loops (see here), while in my case this is not preferred; the second loop only helps the first one for a portion of the code. Please correct me if I am wrong.

Thanks.

解决方案

There's no particular problem with having a foreach loop inside of a foreach loop. Here's an example of a doMC loop inside a doSNOW loop:

library(doSNOW)
hosts <- c('host-1', 'host-2')
cl <- makeSOCKcluster(hosts)
registerDoSNOW(cl)
r <- foreach(i=1:4, .packages='doMC') %dopar% {
  registerDoMC(2)
  foreach(j=1:8, .combine='c') %dopar% {
    i * j
  }
}
stopCluster(cl)

It seems natural to me to use doMC for the inner loop, but you can do it anyway you want. You could also use doSNOW for both loops, but then you would need to create and stop the snow cluster inside the outer foreach loop.

Here's an example of using doMC inside a doMC loop:

library(doMC)
registerDoMC(2)
r <- foreach(i=1:2, .packages='doMC') %dopar% {
  ppid <- Sys.getpid()
  registerDoMC(2)
  foreach(j=1:2) %dopar% {
    c(ppid, Sys.getpid())
  }
}

The results demonstrate that a total of six processes are forked by the doMC package, although only four execute the body of the inner loop:

> r
[[1]]
[[1]][[1]]
[1] 14946 14949

[[1]][[2]]
[1] 14946 14951


[[2]]
[[2]][[1]]
[1] 14947 14948

[[2]][[2]]
[1] 14947 14950

Of course, you need to be careful not to start too many processes on a single node. I found this kind of nesting a bit awkward, which led to the development of the nesting operator.

这篇关于允许每个工作人员注册和分发其他工作人员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆