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,
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
)

## Arguments

data (multiple options) The data to use for the variables specified in the data block of the Stan program. One of the following: A named list of R objects (like for RStan). Internally this list is then written to JSON for CmdStan using write_stan_json(). A path to a data file compatible with CmdStan (JSON or R dump). See the appendices in the CmdStan manual for details on using these formats. NULL or an empty list if the Stan program has no data block. (character vector) The MPI launcher used for launching MPI processes. The default launcher is "mpiexec". (list) A list of arguments to use when launching MPI processes. For example, mpi_args = list("n" = 4) launches the executable as mpiexec -n 4 model_executable, followed by CmdStan arguments for the model executable. (positive integer) A seed for the (P)RNG to pass to CmdStan. (non-negative integer) The number of iterations between printed screen updates. If refresh = 0, only error messages will be printed. (multiple options) The initialization method to use for the variables declared in the parameters block of the Stan program: A real number x>0. This initializes all parameters randomly between [-x,x] (on the unconstrained parameter space); The number 0. This initializes all parameters to 0; A character vector of paths (one per chain) to JSON or Rdump files containing initial values for all or some parameters. See write_stan_json() to write R objects to JSON files compatible with CmdStan. A list of lists containing initial values for all or some parameters. For MCMC the list should contain a sublist for each chain. For optimization and variational inference there should be just one sublist. The sublists should have named elements corresponding to the parameters for which you are specifying initial values. See Examples. A function that returns a single list with names corresponding to the parameters for which you are specifying initial values. The function can take no arguments or a single argument chain_id. For MCMC, if the function has argument chain_id it will be supplied with the chain id (from 1 to number of chains) when called to generate the initial values. See Examples. (logical) Should auxiliary diagnostic information about the latent dynamics be written to temporary diagnostic CSV files? This argument replaces CmdStan's diagnostic_file argument and the content written to CSV is controlled by the user's CmdStan installation and not CmdStanR (for some algorithms no content may be written). The default is FALSE, which is appropriate for almost every use case. To save the temporary files created when save_latent_dynamics=TRUE see the $save_latent_dynamics_files() method. (string) A path to a directory where CmdStan should write its output CSV files. For interactive use this can typically be left at NULL (temporary directory) since CmdStanR makes the CmdStan output (posterior draws and diagnostics) available in R via methods of the fitted model objects. The behavior of output_dir is as follows: If NULL (the default), then the CSV files are written to a temporary directory and only saved permanently if the user calls one of the $save_* methods of the fitted model object (e.g., $save_output_files()). These temporary files are removed when the fitted model object is garbage collected (manually or automatically). If a path, then the files are created in output_dir with names corresponding to the defaults used by $save_output_files(). (positive integer) The number of Markov chains to run. The default is 4. (vector) A vector of chain IDs. Must contain chains unique positive integers. If not set, the default chain IDs are used (integers starting from 1). (positive integer) The number of warmup iterations to run per chain. Note: in the CmdStan User's Guide this is referred to as num_warmup. (positive integer) The number of post-warmup iterations to run per chain. Note: in the CmdStan User's Guide this is referred to as num_samples. (logical) Should warmup iterations be saved? The default is FALSE. If save_warmup=TRUE then you can use $draws(inc_warmup=TRUE) to include warmup when accessing the draws. (positive integer) The period between saved samples. This should typically be left at its default (no thinning) unless memory is a problem. (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. (logical) Do warmup adaptation? The default is TRUE. If a precomputed inverse metric is specified via the inv_metric argument (or metric_file) then, if adapt_engaged=TRUE, Stan will use the provided inverse metric just as an initial guess during adaptation. To turn off adaptation when using a precomputed inverse metric set adapt_engaged=FALSE. (real in (0,1)) The adaptation target acceptance statistic. (positive real) The initial step size for the discrete approximation to continuous Hamiltonian dynamics. This is further tuned during warmup. (character) One of "diag_e", "dense_e", or "unit_e", specifying the geometry of the base manifold. See the Euclidean Metric section of the CmdStan User's Guide for more details. To specify a precomputed (inverse) metric, see the inv_metric argument below. (character) A character vector containing paths to JSON or Rdump files (one per chain) compatible with CmdStan that contain precomputed inverse metrics. The metric_file argument is inherited from CmdStan but is confusing in that the entry in JSON or Rdump file(s) must be named inv_metric, referring to the inverse metric. We recommend instead using CmdStanR's inv_metric argument (see below) to specify an inverse metric directly using a vector or matrix from your R session. (vector, matrix) A vector (if metric='diag_e') or a matrix (if metric='dense_e') for initializing the inverse metric. This can be used as an alternative to the metric_file argument. A vector is interpreted as a diagonal metric. The inverse metric is usually set to an estimate of the posterior covariance. See the adapt_engaged argument above for details about (and control over) how specifying a precomputed inverse metric interacts with adaptation. (nonnegative integer) Width of initial fast timestep adaptation interval during warmup. (nonnegative integer) Width of final fast timestep adaptation interval during warmup. (nonnegative integer) Initial width of slow timestep/metric adaptation interval. (logical) When TRUE, call CmdStan with argument "algorithm=fixed_param". The default is FALSE. The fixed parameter sampler generates a new sample without changing the current state of the Markov chain; only generated quantities may change. This can be useful when, for example, trying to generate pseudo-data using the generated quantities block. If the parameters block is empty then using fixed_param=TRUE is mandatory. When fixed_param=TRUE the chains and parallel_chains arguments will be set to 1. (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 sig_figs is 18. Increasing this value will result in larger output CSV files and thus an increased usage of disk space. (logical) When TRUE (the default), validate the sampling results in the csv files. Disable if you wish to manually read in the sampling results and validate them yourself, for example using read_cmdstan_csv(). (logical) When TRUE (the default), prints all informational messages, for example rejection of the current proposal. Disable if you wish silence these messages, but this is not recommended unless you are very sure that the model is correct up to numerical error. If the messages are silenced then the $output() method of the resulting fit object can be used to display all the silenced messages.

## Value

A CmdStanMCMC object.

The CmdStanR website (mc-stan.org/cmdstanr) for online documentation and tutorials.

The Stan and CmdStan documentation:

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

## Examples

# \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))
# }