“取消注册"一个 doParallel 集群 [英] "un-register" a doParallel cluster

查看:24
本文介绍了“取消注册"一个 doParallel 集群的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在没有注册集群的情况下运行 foreach...%dopar%,foreach 会发出警告,并按顺序执行代码:

If I run foreach... %dopar% without registering a cluster, foreach raises a warning, and executes the code sequentially:

library("doParallel")
foreach(i=1:3) %dopar%
  sqrt(i)

产量:

Warning message:
executing %dopar% sequentially: no parallel backend registered 

但是,如果我在启动、注册和停止集群后运行相同的代码,它会失败:

However, if I run this same code after starting, registering, and stopping a cluster, it fails:

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 会话之前,我是否一直在使用旧集群的鬼魂?

Is there an opposite of registerDoParallel() that cleans up the cluster registration? Or am I stuck with the ghost of the old cluster until I re-start my R session?

/edit:一些谷歌搜索揭示了 bumphunter Biocondoctor 包:

/edit: some googling reveals the bumphunter:::foreachCleanup() function in the bumphunter Biocondoctor package:

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>

然而,这个功能似乎并没有解决问题.

However, this function doesn't seem to fix the problem.

library(bumphunter)
cl <- makeCluster(2)
registerDoParallel(cl)
stopCluster(cl)
rm(cl)
bumphunter:::foreachCleanup()
foreach(i=1:3) %dopar%
  sqrt(i)

foreach 在哪里保存注册集群的信息?

Where does foreach keep the information on the registered cluster?

推荐答案

取消注册" foreach 后端的唯一官方方法是注册顺序后端:

The only official way to "unregister" a foreach backend is to register the sequential backend:

registerDoSEQ()

这对我来说很有意义,因为您应该声明要使用哪个后端,所以我认为提供一种取消声明"要使用的后端的方法没有任何意义.相反,您声明要使用顺序后端,这是默认设置.

This makes sense to me because you're supposed to declare which backend to use, so I didn't see any point in providing a way to "undeclare" which backend to use. Instead, you declare that you want to use the sequential backend, which is the default.

我最初考虑包含一个取消注册"函数,但由于我无法说服自己它很有用,所以我决定不使用它,因为添加一个函数比删除一个函数要容易得多.

I originally considered including an "unregister" function, but since I couldn't convince myself that it was useful, I decided to leave it out since it's much easier to add a function than to remove one.

话虽如此,我认为您需要做的就是从 foreach::.foreachGlobals 中删除所有变量,这是 foreach 保持其所有状态的地方:

That being said, I think all you need to do is to remove all of the variables from foreach:::.foreachGlobals which is where foreach keeps all of its state:

unregister <- function() {
  env <- foreach:::.foreachGlobals
  rm(list=ls(name=env), pos=env)
}

调用此函数后,如果调用%dopar%,任何并行后端将被注销并再次发出警告.

After calling this function, any parallel backend will be deregistered and the warning will be issued again if %dopar% is called.

这篇关于“取消注册"一个 doParallel 集群的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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