Configure and run HMC/NUTS safely in true parallel (one chain per worker)
Source:R/diagnostics.R
configure_hmc_safely_parallel.RdParallel counterpart of configure_hmc_safely_bis, implementing true
parallelism by running one MCMC chain per PSOCK worker
(parallel::makeCluster). Each chain is executed in an isolated R
session, and results are aggregated on the master into a single
coda::mcmc.list.
Usage
configure_hmc_safely_parallel(
build_fn,
niter,
nburnin,
thin,
nchains,
monitors,
inits = NULL,
enable_WAIC = FALSE,
show_compiler_output = FALSE,
log_dir = "log",
seed = 1L
)Arguments
- build_fn
Function; model builder used to construct the NIMBLE model and base MCMC configuration (e.g.
build_M). If.fresh_build()is available on workers, it is called as.fresh_build(build_fn, monitors, thin); otherwise,build_fn()must return a list with at leastmodelandconf. Ifbuild_fnaccepts achain_idargument, it is passed automatically.- niter
Integer; total number of MCMC iterations per chain.
- nburnin
Integer (>= 0); number of burn-in iterations.
- thin
Integer (>= 1); thinning interval.
- nchains
Integer (>= 1); number of chains (one PSOCK worker per chain).
- monitors
Character vector or
NULL; nodes to monitor, passed to model building and/or HMC configuration.- inits
NULLor a list of per-chain initial values. If provided, its length must matchnchains; each element is passed to the corresponding worker.- enable_WAIC
Logical; whether to enable WAIC computation (if supported).
- show_compiler_output
Logical; forwarded to
nimbleOptions(showCompilerOutput = ...)on each worker.- log_dir
Character; directory for worker-level logs (if enabled).
- seed
Integer; base RNG seed. Chain
iusesseed + i - 1.
Value
A list with components:
conf: final MCMC configuration (from chain 1),cmcmc: compiled MCMC object (from chain 1),samples: combinedcoda::mcmc.list(one chain per worker),runtime_s: wall-clock time in seconds (maximum over chains),build: build object returned bybuild_fnor.fresh_build()(from chain 1).
Details
Relative to the sequential implementation, this function:
launches a dedicated PSOCK worker for each chain, ensuring full memory and RNG isolation (no forking),
initializes each worker by attaching nimble (and nimbleHMC when available) and defining internal helpers that compiled or lifted NIMBLE code may call unqualified,
runs
nimble::runMCMC(..., nchains = 1L)independently on each worker and combines chains on the master,reports
runtime_sas the maximum elapsed time across chains, corresponding to the effective wall-clock time of the parallel execution,returns compiled objects (
conf,cmcmc,build) from the first chain only, avoiding the transfer of large compiled objects from all workers.
In contrast to mixed or fallback strategies, this function performs a
strict HMC/NUTS configuration: no per-node fallback (e.g.
addSampler(type = "slice")) is attempted. Any failure during HMC
configuration or execution results in an error.