## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set(collapse = TRUE, comment = "#>", eval = FALSE) library(flexstanr) ## ----------------------------------------------------------------------------- # stan_options(threading = TRUE) ## ----------------------------------------------------------------------------- # opts <- stan_options(chains = 4, threading = TRUE) # #> flexstanr: threading enabled. Using 16 of 16 available cores: 4 chains in # #> parallel, 4 threads per chain. Pass max_cores to leave some cores free. ## ----------------------------------------------------------------------------- # fit <- fit_model("my_model", dat_stan = dat, init = init, stan_opts = opts) ## ----------------------------------------------------------------------------- # # use at most 4 cores, however many the machine has # stan_options(chains = 4, threading = TRUE, max_cores = 4) ## ----------------------------------------------------------------------------- # run_my_fit <- function(dat_stan, init, stan_opts = stan_options()) { # if (test_threaded(stan_opts) && !model_is_threaded) { # warning( # "threads were requested but this model does not use reduce_sum(); ", # "it will run one thread per chain. Run more chains, use the threaded ", # "model variant, or set threading = FALSE.", # call. = FALSE # ) # } # fit_model("my_model", dat_stan = dat_stan, init = init, stan_opts = stan_opts) # } ## ----------------------------------------------------------------------------- # options(mc.cores = 6) # stan_options(chains = 4, threading = TRUE) # sees at most 6 cores ## ----------------------------------------------------------------------------- # # fit.R -- no core arithmetic; availableCores() picks up SLURM_CPUS_PER_TASK = 8 # opts <- stan_options(chains = 4, iter = 2000, threading = TRUE) # fit <- fit_model("my_model", dat_stan = dat, init = init, stan_opts = opts) # saveRDS(fit, "fit.rds") ## ----------------------------------------------------------------------------- # i <- Sys.getenv("SLURM_ARRAY_TASK_ID") # opts <- stan_options(chains = 1, seed = as.integer(i), threading = TRUE) # fit <- fit_model("my_model", dat_stan = dat, init = init, stan_opts = opts) # saveRDS(fit, sprintf("chain-%s.rds", i))