使用并行计算时 R 卡住并警告“关闭未使用的连接”

Xin*_*ing 5 memory connection parallel-processing r

我正在 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) 
Run Code Online (Sandbox Code Playgroud)

或者类似的东西

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

这是我的代码:

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*
    }
}
Run Code Online (Sandbox Code Playgroud)

其中 prob 是一个函数,返回一个长度等于 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)
Run Code Online (Sandbox Code Playgroud)

在 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
Run Code Online (Sandbox Code Playgroud)

我使用 3 个内核进行并行计算 (no_cores=3),机器是 Macbook pro 2016。

有人可以帮我吗?谢谢!

Ste*_*ski 6

帮助我了解更多。你在循环,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)
Run Code Online (Sandbox Code Playgroud)

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