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)
Yields:
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)
Yields:
Error in summary.connection(connection) : invalid connection
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: 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)
Where does foreach keep the information on the registered cluster?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…