使用foreach包安全地终止R中的完成过程

时间:2014-05-29 13:08:46

标签: r foreach parallel-processing random-forest

我编写了以下脚本,使用R foreach包并行训练随机森林模型,最初我使用20个处理器并行运行训练阶段,整个训练过程是嵌套的双循环,但是,我注意到当训练结束时,20个处理器仍处于睡眠状态,现在消耗大量内存,我不得不手动杀死它们,因为它们已经不再使用,因为结果已经保存并且是一个新阶段训练是从嵌套循环开始的,所以我想知道是否有一个命令可以在保存训练模型的结果后终止进程:为了更清楚我想要做什么,以下是我的脚本:

library("foreach")
library("randomForest")
library("doSNOW")
for(len in 7:24){ 
   for(clus in 1:3){    
    load(paste("mySCOP_myfull_",paste(len,paste("_Clus_",paste(clus,".RData",sep=""),sep=""),sep=""),sep=""))
    x=as.data.frame(full[,4:(dim(full)[2]-13)])
    y=as.numeric(as.character((as.numeric(as.character(full[,1])))))
    registerDoSNOW(makeCluster(20, type="SOCK"))
    mytr_all = foreach(ntree = rep(25, 20), .combine = combine, .packages = "randomForest") %dopar% randomForest(x, y, ntree = ntree,corr.bias=TRUE,na.action=na.omit)
    outFile=paste("mySCOP_mytr_",paste(len,paste("_Clus_",paste(clus,".RData",sep=""),sep=""),sep=""),sep="")
    save(mytr_all,file=outFile)

    # I want to release the finished "sleeping" processes since reaching here means that the 20 processes finished their tasks and saved the final trained model. in reality the 20 processes are now in the "sleeping mode" which means they are not running but still occupy the cash
}
}

1 个答案:

答案 0 :(得分:1)

你有没有尝试过'stopCluster' doSNOW包中的命令?您需要命名群集以阻止它。类似于以下内容:

cluster_name <- makeCluster(20, type="SOCK")
registerDoSNOW(cluster_name)
...
stopCluster(cluster_name)

如果我能正确理解这个问题,这应该有用。