I’m trying to fit multiple models in parallel using the foreach and doParallel libraries. However, I get the empty model returned instead of the results!
Because the parallel package in multicore mode starts its workers using fork
without doing a subsequent exec, it has some limitations. Some operations
cannot be performed properly by forked processes. For example, connection
objects very likely won’t work. In some cases, this could cause an object to
become corrupted, and the R session to crash.
So maybe try do your require statement for rstan inside of the dopar loop so that it all loads after the fork.
I recommend trying what @emiruz suggested but also which operating system do you have? On my mac, even without doing what @emiruz suggested it seems to work ok, for example:
library(parallel)
library(doParallel)
library(rstan)
code <-"
data {
int N;
}
parameters {
real x[N];
}
model {
x ~ normal(0, 1);
}
"
model <- stan_model(model_code = code)
data <- list(N = 5)
cl <- makeCluster(2)
registerDoParallel(cl)
result <- foreach(i=1:5, .export=c("optimizing")) %dopar% {
posterior <- optimizing(model, data = data, algorithm="LBFGS")
}
str(result)
Which gives
> str(result)
List of 5
$ :List of 4
..$ par : Named num [1:5] 1 1 1 1 1
.. ..- attr(*, "names")= chr [1:5] "x[1]" "x[2]" "x[3]" "x[4]" ...
..$ value : num -3.2e-30
..$ return_code: int 0
..$ theta_tilde: num [1, 1:5] 1 1 1 1 1
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:5] "x[1]" "x[2]" "x[3]" "x[4]" ...
$ :List of 4
..$ par : Named num [1:5] 1 1 1 1 1
.. ..- attr(*, "names")= chr [1:5] "x[1]" "x[2]" "x[3]" "x[4]" ...
..$ value : num -1.66e-30
..$ return_code: int 0
..$ theta_tilde: num [1, 1:5] 1 1 1 1 1
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:5] "x[1]" "x[2]" "x[3]" "x[4]" ...
$ :List of 4
..$ par : Named num [1:5] 1 1 1 1 1
.. ..- attr(*, "names")= chr [1:5] "x[1]" "x[2]" "x[3]" "x[4]" ...
..$ value : num -4.82e-30
..$ return_code: int 0
..$ theta_tilde: num [1, 1:5] 1 1 1 1 1
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:5] "x[1]" "x[2]" "x[3]" "x[4]" ...
$ :List of 4
..$ par : Named num [1:5] 1 1 1 1 1
.. ..- attr(*, "names")= chr [1:5] "x[1]" "x[2]" "x[3]" "x[4]" ...
..$ value : num -4.19e-31
..$ return_code: int 0
..$ theta_tilde: num [1, 1:5] 1 1 1 1 1
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:5] "x[1]" "x[2]" "x[3]" "x[4]" ...
$ :List of 4
..$ par : Named num [1:5] 1 1 1 1 1
.. ..- attr(*, "names")= chr [1:5] "x[1]" "x[2]" "x[3]" "x[4]" ...
..$ value : num -1.05e-31
..$ return_code: int 0
..$ theta_tilde: num [1, 1:5] 1 1 1 1 1
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:5] "x[1]" "x[2]" "x[3]" "x[4]" ...
But perhaps if you’re on Windows then more is required to get it working, e.g. what @emiruz suggested.
Thanks so much guys! I suspect it is a Windows thing.
Emiruz, thanks for the suggestion. Since I use Windows, the doParallel package should use “snow” functionality instead of Multicore.
Johan - running your code as-is produces an output of models again.
However, if I try your suggestions anyway and reload the package inside the foreach loop or simply use the ".packages=c(“rstan”)) " option, it now works correctly.