gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:14:58 26 4
gpt4 key购买 nike

我正在 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 是一个函数,返回一个长度等于 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)

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

关于使用并行计算时 R 卡住,警告 'closing unused connection',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46367204/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com