问题
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