如果我在没有注册集群的情况下运行foreach... %dopar%,foreach会抛出一个警告,并按顺序执行代码:
library("doParallel")
foreach(i=1:3) %dopar%
sqrt(i)收益率:
Warning message:
executing %dopar% sequentially: no parallel backend registered 但是,如果我在启动、注册和停止集群后运行相同的代码,它将失败:
cl <- makeCluster(2)
registerDoParallel(cl)
stopCluster(cl)
rm(cl)
foreach(i=1:3) %dopar%
sqrt(i)收益率:
Error in summary.connection(connection) : invalid connection有没有registerDoParallel()的对立面来清理集群注册?或者,在我重新启动R会话之前,我会一直使用旧集群的幽灵吗?
/edit:一些谷歌搜索显示了bumphunter Biocondoctor包中的bumphunter:::foreachCleanup()函数:
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>然而,这个函数似乎并没有解决这个问题。
library(bumphunter)
cl <- makeCluster(2)
registerDoParallel(cl)
stopCluster(cl)
rm(cl)
bumphunter:::foreachCleanup()
foreach(i=1:3) %dopar%
sqrt(i)foreach将已注册集群的信息保存在哪里?
发布于 2014-08-04 08:01:29
“注销”foreach后端的唯一官方方法是注册顺序后端:
registerDoSEQ()这对我来说是有意义的,因为你应该声明使用哪个后端,所以我看不出提供一种“取消声明”使用哪个后端的方法有任何意义。相反,您声明要使用顺序后端,这是默认的。
我最初考虑加入一个“注销”函数,但是因为我不能说服自己它是有用的,所以我决定去掉它,因为添加一个函数比删除一个容易得多。
也就是说,我认为你需要做的就是从foreach:::.foreachGlobals中删除所有的变量,这是foreach保存其所有状态的地方:
unregister <- function() {
env <- foreach:::.foreachGlobals
rm(list=ls(name=env), pos=env)
}调用此函数后,任何并行后端都将被注销,如果调用%dopar%,将再次发出警告。
发布于 2016-01-20 12:23:38
cl <- makeCluster(2)
registerDoParallel(cl)
on.exit(stopCluster(cl))这对我来说很好。
https://stackoverflow.com/questions/25097729
复制相似问题