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

查看:280
本文介绍了“取消注册"一个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中的bumphunter:::foreachCleanup()功能包装:

/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天全站免登陆