Recently, I am trying to use reference class with parallel
. I tried 4 different schemes, multicore, MPI, Socket and Forking. However, only multicore is able to produce correct result. and MPI, PSOCK and Forking all produce errors.
PS: I was running this script on a computer cluster with MPI support.
library(doParallel)
np = 4L
# cl <- makeCluster(np, type="MPI", outfile = "")
# cl <- makeCluster(np, type="PSOCK", outfile = "")
# cl <- makeCluster(np, type="FORK", outfile = "")
cl <- np # multicore
registerDoParallel(cl)
myClass = setRefClass("myClass",
fields = c("a"),
methods = list(
hello = function(){cat("hello\n")},
show = function(){cat("show\n")}
)
)
objs = foreach(i = 1:4) %dopar% {
obj = new("myClass")
obj$a=i
obj
}
It may be related to parallel computations on Reference Classes
Update:
More investigation reveals that reference class instances are cloned but not the reference class definition.
library(doParallel)
np = 4L
cl <- makeCluster(np, type="MPI", outfile = "")
# cl <- makeCluster(np, type="PSOCK", outfile = "")
# cl <- makeCluster(np, type="FORK", outfile = "")
# cl <- np # multicore
registerDoParallel(cl)
myClass = setRefClass("myClass",
fields = c("a"),
methods = list(
hello = function(){cat("hello\n")},
show = function(){cat("show\n")}
)
)
obj = new("myClass")
obj$a = 0
results = foreach(i = 1:4) %dopar% {
obj$a # no error
newobj = new("myClass") # error
}