R 在使用并行计算时卡住并发出警告 'closing unused connection'

R gets stuck when using parallel computing with warning 'closing unused connection'

我 运行 在 R 中进行一些模拟,代码在不使用并行计算的情况下运行良好。然而,当我修改我的一行代码并尝试使用并行计算时,R 卡住了,并且每次都卡在不同的迭代时间。当 R 卡住时,我必须从 运行 手动停止它,有时会有一些警告说

Warning messages:
1: closing unused connection 13 (<-localhost:11688)
2: closing unused connection 12 (<-localhost:11688) 
3: closing unused connection 9 (<-localhost:11688) 
4: closing unused connection 8 (<-localhost:11688) 
5: closing unused connection 7 (<-localhost:11688) 
6: closing unused connection 6 (<-localhost:11688) 

或者类似

Warning message:
In .Internal(get(x, envir, mode, inherits)) :
closing unused connection 6 (<-localhost:11688)

这是我的代码:

for (iter in 1:100){
    *Simulate data matrix X and Y, and initial start Z0*

    for (i in 1:100){
    *Calculate input matrix Z based on Z0*

    cl <- makeCluster(no_cores, type="FORK")
    Z <-cbind(Z,unlist(parLapply(cl,
                                 as.list(data.frame(t(Z))),
                                 function(x) prob(x,X,Y))))
    stopCluster(cl)

    result <- rbind(result,Z)
    result <- result[!duplicated(result),]
    result <- result[order(-result[,dim(result)[2]]),][1:10,]
    *Calculate a new Z0 based on Z*
    }
}

其中 prob 是函数 returns 长度等于 Z 的行数的向量。

由于代码在不使用并行计算的情况下运行良好,我认为问题出在并行计算中。我没有使用 parLapply,而是在迭代中尝试了 foreach

cl <- makeCluster(no_cores, type="FORK")
Z <- cbind(Z,foreach(tmp=as.list(data.frame(t(Z))),
                             .combine = c)  %dopar%
                     prob(tmp,X,Y))
stopCluster(cl)

R 卡住后,我从 运行 手动停止它,我收到类似的警告:

Warning messages:
1: closing unused connection 13 (<-localhost:11688) 
2: closing unused connection 12 (<-localhost:11688) 
3: closing unused connection 9 (<-localhost:11688) 
4: closing unused connection 8 (<-localhost:11688) 
5: closing unused connection 7 (<-localhost:11688) 
6: closing unused connection 6 (<-localhost:11688) 
7: In doTryCatch(return(expr), name, parentenv, handler) :
  restarting interrupted promise evaluation
8: In doTryCatch(return(expr), name, parentenv, handler) :
  restarting interrupted promise evaluation
9: In doTryCatch(return(expr), name, parentenv, handler) :
  restarting interrupted promise evaluation

我用的是3核并行计算(no_cores=3),机器是Macbook pro 2016

有人可以帮帮我吗?谢谢!

帮助我了解更多。您正在使用 i 进行循环,但我没有看到您使用它?我错过了什么吗?

此外,我建议在循环之外启动集群。您有效地创建和停止了 10,000 次集群。

我在循环外调用 makeCluster,然后在循环结束后停止它。

尝试这样的事情:

cl <- makeCluster(no_cores, type="FORK")

clusterExport(cl, 'Z') # need to export the variable to cluster? 

for (iter in 1:100){
*Simulate data matrix X and Y, and initial start Z0*

for (i in 1:100){
*Calculate input matrix Z based on Z0*

Z <-cbind(Z,unlist(parLapply(cl,
                             as.list(data.frame(t(Z))),
                             function(x) prob(x,X,Y))))


result <- rbind(result,Z)
result <- result[!duplicated(result),]
result <- result[order(-result[,dim(result)[2]]),][1:10,]
*Calculate a new Z0*
    }
}
stopCluster(cl)

如果这不起作用,请告诉我。我会尽力帮忙的!