doMC vs doSNOW vs doSMP vs doMPI:为什么“foreach"的各种并行后端在功能上不相同? [英] doMC vs doSNOW vs doSMP vs doMPI: why aren't the various parallel backends for 'foreach' functionally equivalent?
问题描述
我有一些代码测试片段,我在不同的机器上运行过,总是得到相同的结果.我认为各种 do... 包背后的哲学是它们可以互换用作 foreach 的 %dopar% 的后端.为什么不是这样?
I've got a few test pieces of code that I've been running on various machines, always with the same results. I thought the philosophy behind the various do... packages was that they could be used interchangeably as a backend for foreach's %dopar%. Why is this not the case?
例如,此代码片段有效:
For example, this code snippet works:
library(plyr)
library(doMC)
registerDoMC()
x <- data.frame(V= c("X", "Y", "X", "Y", "Z" ), Z = 1:5)
ddply(x, .(V), function(df) sum(df$Z),.parallel=TRUE)
虽然这些代码片段中的每一个都失败了:
While each of these code snippets fail:
library(plyr)
library(doSMP)
workers <- startWorkers(2)
registerDoSMP(workers)
x <- data.frame(V= c("X", "Y", "X", "Y", "Z" ), Z = 1:5)
ddply(x, .(V), function(df) sum(df$Z),.parallel=TRUE)
stopWorkers(workers)
library(plyr)
library(snow)
library(doSNOW)
cl <- makeCluster(2, type = "SOCK")
registerDoSNOW(cl)
x <- data.frame(V= c("X", "Y", "X", "Y", "Z" ), Z = 1:5)
ddply(x, .(V), function(df) sum(df$Z),.parallel=TRUE)
stopCluster(cl)
library(plyr)
library(doMPI)
cl <- startMPIcluster(count = 2)
registerDoMPI(cl)
x <- data.frame(V= c("X", "Y", "X", "Y", "Z" ), Z = 1:5)
ddply(x, .(V), function(df) sum(df$Z),.parallel=TRUE)
closeCluster(cl)
在所有四种情况下,foreach(i = 1:3,.combine = "c") %dopar% {sqrt(i)}
产生完全相同的结果,所以我知道我有这些软件包在我测试过的每台机器上都安装并正常工作.
In all four cases, foreach(i = 1:3,.combine = "c") %dopar% {sqrt(i)}
yields the exact same result, so I know I have the packages installed and working properly on each machine I've tested them on.
doMC 与 doSMP、doSNOW 和 doMPI 有何不同?
What is doMC doing differently from doSMP, doSNOW, and doMPI?
推荐答案
doMC
fork 当前 R 进程,因此它继承所有现有变量.所有其他后端只传递显式请求的变量.不幸的是,我没有意识到这一点,只使用 doMC
进行了测试——这是我希望在下一版 plyr 中修复的问题.
doMC
forks the current R process so it inherits all the existing variables. All the other do backends only pass on explicitly requested variables. Unfortunately I didn't realise that, and only tested with doMC
- this is something I hope to fix in the next version of plyr.
这篇关于doMC vs doSNOW vs doSMP vs doMPI:为什么“foreach"的各种并行后端在功能上不相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!