如果我在foreach... %dopar%没有注册集群的情况下运行,foreach会发出警告,并按顺序执行代码:
library("doParallel")
foreach(i=1:3) %dopar%
sqrt(i)
Run Code Online (Sandbox Code Playgroud)
产量:
Warning message:
executing %dopar% sequentially: no parallel backend registered
Run Code Online (Sandbox Code Playgroud)
但是,如果我在启动,注册和停止集群后运行相同的代码,则会失败:
cl <- makeCluster(2)
registerDoParallel(cl)
stopCluster(cl)
rm(cl)
foreach(i=1:3) %dopar%
sqrt(i)
Run Code Online (Sandbox Code Playgroud)
产量:
Error in summary.connection(connection) : invalid connection
Run Code Online (Sandbox Code Playgroud)
有没有相反的registerDoParallel()清理群集注册?还是我坚持使用旧集群的鬼魂,直到我重新开始我的R会话?
/编辑:一些谷歌搜索揭示bumphunter:::foreachCleanup()了bumphunter Biocondoctor包中的功能:
function ()
{
if (exists(".revoDoParCluster", where = doParallel:::.options)) {
if (!is.null(doParallel:::.options$.revoDoParCluster))
stopCluster(doParallel:::.options$.revoDoParCluster)
remove(".revoDoParCluster", envir = doParallel:::.options)
}
}
<environment: namespace:bumphunter>
Run Code Online (Sandbox Code Playgroud)
但是,此功能似乎无法解决问题.
library(bumphunter)
cl <- makeCluster(2)
registerDoParallel(cl)
stopCluster(cl)
rm(cl)
bumphunter:::foreachCleanup()
foreach(i=1:3) %dopar%
sqrt(i)
Run Code Online (Sandbox Code Playgroud)
foreach在哪里保留注册集群的信息?
我曾试图用20 CPU运行在Unix机器上下面的代码,使用[R ,foreach,parallel,doParallel和party包(我的目标是让党/ varimp功能上并行多个CPU的工作):
parallel_compute_varimp <- function (object, mincriterion = 0, conditional = FALSE, threshold = 0.2,
nperm = 1, OOB = TRUE, pre1.0_0 = conditional)
{
response <- object@responses
input <- object@data@get("input")
xnames <- colnames(input)
inp <- initVariableFrame(input, trafo = NULL)
y <- object@responses@variables[[1]]
error <- function(x, oob) mean((levels(y)[sapply(x, which.max)] != y)[oob])
w <- object@initweights
perror <- matrix(0, nrow = nperm * length(object@ensemble), ncol = length(xnames))
colnames(perror) <- xnames
data = …Run Code Online (Sandbox Code Playgroud)