2

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.

kulala
  • 39
  • 1
  • 6

1 Answers1

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
JacobJacox
  • 917
  • 5
  • 14
  • this error happens again when using this. > clusterEvalQ(cl,library(Iso)) Error in checkForRemoteErrors(lapply(cl, recvResult)) : 2 nodes produced errors; first error: cannot open the connection – kulala Nov 28 '19 at 12:38
  • Then it is not the problem with libraries, but with the sockets. Try reinstalling parallel packages, the code above worked fine for me. I am running on Windows though. – JacobJacox Nov 28 '19 at 12:41
  • Many thanks. But there is a silly question, how can I reinstall this package since it's a base package in R – kulala Nov 29 '19 at 09:44
  • I like DoSnow package, it is similiar to parallel, try that one. – JacobJacox Nov 29 '19 at 10:40
  • Can you please give me a example of how to use DoSnow in mycode? – kulala Nov 29 '19 at 12:24