The $sample_mpi()
method of a CmdStanModel
object is
identical to the $sample()
method but with support for
MPI. The target audience for MPI are
those with large computer clusters. For other users, the
$sample()
method provides both parallelization of
chains and threading support for within-chain parallelization.
In order to use MPI with Stan, an MPI implementation must be installed. For Unix systems the most commonly used implementations are MPICH and OpenMPI. The implementations provide an MPI C++ compiler wrapper (for example mpicxx), which is required to compile the model.
An example of compiling with MPI:
mpi_options = list(STAN_MPI=TRUE, CXX="mpicxx", TBB_CXX_TYPE="gcc") mod = cmdstan_model("model.stan", cpp_options = mpi_options)
The C++ options that must be supplied to the compile call are:
STAN_MPI
: Enables the use of MPI with Stan if TRUE
.
CXX
: The name of the MPI C++ compiler wrapper. Typically "mpicxx"
.
TBB_CXX_TYPE
: The C++ compiler the MPI wrapper wraps. Typically "gcc"
on Linux and "clang"
on macOS.
In the call to the $sample_mpi()
method it is also possible to provide
the name of the MPI launcher (mpi_cmd
, defaulting to "mpiexec"
) and any
other MPI launch arguments (mpi_args
). In most cases, it is enough to
only define the number of processes. To use n_procs
processes specify
mpi_args = list("n" = n_procs)
.
sample_mpi( data = NULL, mpi_cmd = "mpiexec", mpi_args = NULL, seed = NULL, refresh = NULL, init = NULL, save_latent_dynamics = FALSE, output_dir = NULL, chains = 1, chain_ids = seq_len(chains), iter_warmup = NULL, iter_sampling = NULL, save_warmup = FALSE, thin = NULL, max_treedepth = NULL, adapt_engaged = TRUE, adapt_delta = NULL, step_size = NULL, metric = NULL, metric_file = NULL, inv_metric = NULL, init_buffer = NULL, term_buffer = NULL, window = NULL, fixed_param = FALSE, sig_figs = NULL, validate_csv = TRUE, show_messages = TRUE )
data | (multiple options) The data to use for the variables specified in
the
|
---|---|
mpi_cmd | (character vector) The MPI launcher used for launching MPI
processes. The default launcher is |
mpi_args | (list) A list of arguments to use when launching MPI
processes. For example, |
seed | (positive integer) A seed for the (P)RNG to pass to CmdStan. |
refresh | (non-negative integer) The number of iterations between
printed screen updates. If |
init | (multiple options) The initialization method to use for the
variables declared in the
|
save_latent_dynamics | (logical) Should auxiliary diagnostic information
about the latent dynamics be written to temporary diagnostic CSV files?
This argument replaces CmdStan's |
output_dir | (string) A path to a directory where CmdStan should write
its output CSV files. For interactive use this can typically be left at
|
chains | (positive integer) The number of Markov chains to run. The default is 4. |
chain_ids | (vector) A vector of chain IDs. Must contain |
iter_warmup | (positive integer) The number of warmup iterations to run
per chain. Note: in the CmdStan User's Guide this is referred to as
|
iter_sampling | (positive integer) The number of post-warmup iterations
to run per chain. Note: in the CmdStan User's Guide this is referred to as
|
save_warmup | (logical) Should warmup iterations be saved? The default
is |
thin | (positive integer) The period between saved samples. This should typically be left at its default (no thinning) unless memory is a problem. |
max_treedepth | (positive integer) The maximum allowed tree depth for the NUTS engine. See the Tree Depth section of the CmdStan User's Guide for more details. |
adapt_engaged | (logical) Do warmup adaptation? The default is |
adapt_delta | (real in |
step_size | (positive real) The initial step size for the discrete approximation to continuous Hamiltonian dynamics. This is further tuned during warmup. |
metric | (character) One of |
metric_file | (character) A character vector containing paths to JSON or
Rdump files (one per chain) compatible with CmdStan that contain
precomputed inverse metrics. The |
inv_metric | (vector, matrix) A vector (if |
init_buffer | (nonnegative integer) Width of initial fast timestep adaptation interval during warmup. |
term_buffer | (nonnegative integer) Width of final fast timestep adaptation interval during warmup. |
window | (nonnegative integer) Initial width of slow timestep/metric adaptation interval. |
fixed_param | (logical) When |
sig_figs | (positive integer) The number of significant figures used
when storing the output values. By default, CmdStan represent the output
values with 6 significant figures. The upper limit for |
validate_csv | (logical) When |
show_messages | (logical) When |
A CmdStanMCMC
object.
The CmdStanR website (mc-stan.org/cmdstanr) for online documentation and tutorials.
The Stan and CmdStan documentation:
Stan documentation: mc-stan.org/users/documentation
CmdStan User’s Guide: mc-stan.org/docs/cmdstan-guide
The Stan Math Library's MPI documentation (mc-stan.org/math/mpi) for more details on MPI support in Stan.
Other CmdStanModel methods:
model-method-check_syntax
,
model-method-compile
,
model-method-generate-quantities
,
model-method-optimize
,
model-method-sample
,
model-method-variational
# \dontrun{ # mpi_options <- list(STAN_MPI=TRUE, CXX="mpicxx", TBB_CXX_TYPE="gcc") # mod <- cmdstan_model("model.stan", cpp_options = mpi_options) # fit <- mod$sample_mpi(..., mpi_args = list("n" = 4)) # }