我有一个案例,其中 foreach
使用 doMC
作为后端在不同的机器上产生不同的行为。
在运行 Ubuntu 12.04.4 LTS 的 linux 服务器上,以下代码(改编自 foreach vingette)在单个内核上同时运行 5 个作业,这不是所需的行为。
library(foreach)
library(doMC)
registerDoMC(cores=5)
getDoParWorkers()
x <- iris[which(iris[,5] != "setosa"), c(1,5)]
trials <- 10000
r <- foreach(icount(trials), .combine=cbind) %dopar% {
ind <- sample(100, 100, replace=TRUE)
result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
coefficients(result1)
}
session 信息:
> sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=C LC_COLLATE=C LC_MONETARY=C
[6] LC_MESSAGES=C LC_PAPER=C LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=C LC_IDENTIFICATION=C
attached base packages:
[1] parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] doMC_1.3.3 iterators_1.0.7 foreach_1.4.2
loaded via a namespace (and not attached):
[1] codetools_0.2-8 compiler_3.1.0 tools_3.1.0
在运行 OSX 10.7.5 的 Mac 上运行相同的代码会产生在 5 个不同内核上运行 5 个作业的期望和预期行为。
session 信息:
> sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] C
attached base packages:
[1] parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] doMC_1.3.2 iterators_1.0.6 foreach_1.4.1
loaded via a namespace (and not attached):
[1] codetools_0.2-8 compiler_3.0.1 tools_3.0.1
我还观察到使用其他并行后端的相同行为。两台机器都有 20 多个内核。关于发生了什么的任何想法?
最佳答案
该问题是由 OpenBLAS 引起的。切换到 ATLAS 解决了这个问题。在 Linux 中切换 BLAS 库的方法在 Nathan VanHoudnos's blog :
在 BLAS 库之间切换
现在我们可以在安装的不同 BLAS 选项之间切换:sudo update-alternatives --config libblas.so.3gf
替代 libblas.so.3gf 有 3 种选择(提供/usr/lib/libblas.so.3gf)。
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/openblas-base/libopenblas.so.0 40 auto mode
1 /usr/lib/atlas-base/atlas/libblas.so.3gf 35 manual mode
2 /usr/lib/libblas/libblas.so.3gf 10 manual mode
3 /usr/lib/openblas-base/libopenblas.so.0 40 manual mode
Press enter to keep the current choice[*], or type selection number:
旁注:如果以上返回:
update-alternatives: error: no alternatives for libblas.so.3gf
尝试
sudo update-alternatives --config libblas.so.3
关于R foreach 不使用多核,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24145693/