Error in R parallel:Error in checkForRemoteErrors(val) : 2 nodes produced errors; first error: cannot open the connection

独自空忆成欢 提交于 2021-01-05 06:59:44

问题


I wrote a function to run R parallel, but it doesn't seem to work. The code is '''

rm(list=ls())
square<-function(x){
  library(Iso)
  y=ufit(x,lmode<-2,x<-c(1:length(x)),type="b")[[2]]
  return(y)
}
num<-c(1,2,1,4)
cl <- makeCluster(getOption("cl.cores",2))
clusterExport(cl,"square")
results<-parLapply(cl,num,square)
stopCluster(cl)

''' and the error is: Error in checkForRemoteErrors(val) : 2 nodes produced errors; first error: cannot open the connection I think a possible reason is that I used the Iso package in the function. BUT I DON'T KNOW how to solve it.


回答1:


You have to export your functions/whole packages to each cluster if you want to do it in parallel:

library(doSNOW)
## the rest is the same
rm(list=ls())
square<-function(x){
  y=ufit(x,lmode<-2,x<-c(1:length(x)),type="b")[[2]]
  return(y)
}
num<-c(1,2,1,4)
cl <- makeCluster(getOption("cl.cores",2))
clusterExport(cl,"square")
clusterEvalQ(cl,library(Iso))
## here you should see smth like this, where each cluster prints attached libraries
[[1]]
[1] "Iso"       "snow"      "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base"     

[[2]]
[1] "Iso"       "snow"      "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base" 

## then just call the same as with parallel

results<-parLapply(cl,num,square)
stopCluster(cl)


## alternative is to use Iso::ufit


来源:https://stackoverflow.com/questions/59088824/error-in-r-parallelerror-in-checkforremoteerrorsval-2-nodes-produced-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!