The code presented above did not consistently work for me. Sometimes it did, and sometimes it didn’t. With parallel::mclapply, it fails and prints
Warning message:
In parallel::mclapply(1:4, model$sample, data = data_list, num_chains = 1, :
all scheduled cores encountered errors in user code
I tried doParallel solutions, too, but with no luck. Same sort of issue, sometimes it works, sometimes it doesn’t. Here’s a reproducible example based off of the code above, but copied for completeness:
library(cmdstanr)
set_cmdstan_path("~/cmdstan")
modfile <- file.path(cmdstan_path(), "examples", "bernoulli", "bernoulli.stan")
model <- cmdstan_model(modfile)
data_list <- list(N = 10, y =c(0,1,0,0,0,0,0,0,0,1))
library(doParallel)
registerDoParallel(2)
fit_list <- foreach(i = 1:4) %dopar% {
model$sample(data = data_list,
num_chains = 1,
num_samples = 1e3)
}
The error message here is somehow even more vague
Error in { : task 1 failed - "invalid 'type' (character) of argument"
Both of these work if you remove the parallel bits, and both fail more often if you add more cores and/or more repetitions.
I’m optimistic we can get something like the following to work,
fit_list <- foreach(i = 1:100) %dopar% {
lapply(1:4,
model$sample,
data = data_list,
num_chains = 1,
num_samples = 1e3
)
}
Here’s my session in
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] parallel stats graphics grDevices utils
[6] datasets methods base
other attached packages:
[1] doParallel_1.0.15 iterators_1.0.12
[3] foreach_1.5.0 cmdstanr_0.0.0.9000
loaded via a namespace (and not attached):
[1] codetools_0.2-16 ps_1.3.2 crayon_1.3.4
[4] R6_2.4.1 lifecycle_0.2.0 jsonlite_1.6.1
[7] backports_1.1.6 magrittr_1.5 posterior_0.0.2
[10] pillar_1.4.3 rlang_0.4.5 rstudioapi_0.11
[13] vctrs_0.2.4 ellipsis_0.3.0 checkmate_2.0.0
[16] tools_3.6.1 abind_1.4-5 compiler_3.6.1
[19] processx_3.4.2 pkgconfig_2.0.3 tibble_3.0.1
I’ve tried this with both CmdStan v2.22.1 and CmdStan v2.23.0.
Any thoughts?