Configure + run HMC/NUTS in parallel (large-model safe, PSOCK)
Source:R/diagnostics.R
configure_hmc_safely_parallel_bis.RdParallel wrapper around nimbleHMC::configureHMC() aligned with the worker strategy of
run_baseline_config_parallel_bis: each worker resolves build_fn, builds the R model,
compiles the project (compiled model), configures HMC, builds & compiles the MCMC, then runs
and returns samples + timing.
Important technical points (to avoid “repetitions” and post-run hangs):
The model is compiled once per worker (
cmodel), then the MCMC is compiled against that compiled project.No monitor mutation after MCMC build (monitors are passed only via
configureHMC()).Returning
confobjects can fail due to serialization (external pointers). This function supports safe modes viaopts$conf_return.
Usage
configure_hmc_safely_parallel_bis(
build_fn,
niter,
nburnin,
thin,
nchains,
monitors,
inits = NULL,
enable_WAIC = FALSE,
show_compiler_output = FALSE,
seed = 1L,
n_cores = nchains,
samplesAsCodaMCMC = FALSE,
opts = NULL
)Arguments
- build_fn
Function OR single string (function name). The builder is resolved on workers using namespace/globalEnv/object fallback (same philosophy as baseline_bis). It must return either a nimble model, or a list with at least
$model(and optionally$cmodel).- niter, nburnin, thin
MCMC run controls.
- nchains
Number of chains.
- monitors
Character vector of monitors passed to
configureHMC().- inits
Optional list of inits (length = nchains) or a single init list. If provided, the worker will try calling the builder with
inits=(fallback to builder without it if not supported).- enable_WAIC
Logical passed to
configureHMC().- show_compiler_output
Logical passed to Nimble compilation.
- seed
Integer seed.
- n_cores
Number of PSOCK workers (defaults to nchains).
- samplesAsCodaMCMC
Logical; if TRUE returns
coda::mcmc.list.- opts
Advanced options (same family as baseline_bis):
cluster_type ("PSOCK"), outfile (""), worker_libpaths, worker_packages
build_fn_text, build_fn_name, build_fn_ns
build_fn_object, builder_env_names
resetFunctions (default FALSE recommended here), gc_worker (TRUE)
mcmc_time (TRUE), progressBar (FALSE)
conf_return: "none" | "chain1" | "by_chain" | "rds"
"chain1"/"by_chain": attempts to return
conf_hmcdirectly (may fail serialization on some setups)"rds": saves conf to disk on worker and returns paths (recommended if you need conf)
conf_dir: directory used when conf_return="rds" (default tempdir()).