Learn R Programming

spaMM (version 3.9.0)

dopar: Interface for parallel computations

Description

An interface to apply some function fn in parallel on columns of a matrix. dopar is used internally in spaMM, but is not logically restricted to mixed-effect applications, hence it can be used more widely. Depending on the nb_cores argument, parallel or serial computation is performed. A socket cluster is used by default for parallel computations, but a fork cluster can be requested on linux and alike operating systems by using argument cluster_args=list(type="FORK").

Usage

dopar(newresp, fn, nb_cores = NULL, fit_env, control = list(), 
      cluster_args = NULL, debug. = FALSE, iseed = NULL, 
      showpbar = eval(spaMM.getOption("barstyle")), 
      pretest_cores =NULL, ...)

Arguments

newresp

A matrix on whose columns fn will be applied (e.g., as used internally in spaMM, the return value of a simulate.HLfit() call).

fn

Function, whose first argument is named y, to be applied to each column of newresp.

nb_cores

Integer. Number of cores to use for parallel computations. If >1, a cluster of nb_cores nodes is used. Otherwise, no parallel computation is performed.

fit_env

(for socket clusters only:) An environment, or a list, containing variables to be exported on the nodes of the cluster (by parallel::clusterExport).

control

A list. In particular if the doSNOW package is attached, foreach is called with default arguments including i = 1:ncol(newresp), .combine = "cbind", .inorder = TRUE, .errorhandling = "remove", .packages = "spaMM". control may be used to provide non-default values of these arguments. For example, .errorhandling = "pass" is useful to get error messages from the nodes. If doSNOW is not attached, the result is still in the format returned by foreach with default .combine="cbind" or possible non-default .combine="rbind".

Alternatively, if a for cluster is used, control$mc.silent can be used to control the mc.silent argument of mclapply.

cluster_args

A list of arguments passed to parallel::makeCluster. E.g., outfile="log.txt" may be useful to collect output from the nodes, and type="FORK" to force a fork cluster on linux(-alikes).

debug.

(for socket clusters only:) For debugging purposes. Effect, if any, is to be defined by the fn as provided by the user.

iseed

(all parallel contexts:) Integer, or NULL. If an integer, it is used to initialize "L'Ecuyer-CMRG" random-number generator (iseed argument of clusterSetRNGStream), yet not ensuring identical control of random number generator accross different parallelisation options (see alternative function dofuture for such control). If iseed is NULL, the default generator is selected on each node, where its seed is not controlled.

showpbar

(for socket clusters only:) Controls display of progress bar. See barstyle option for details.

pretest_cores

(for socket clusters only:) A function to run on the cores before running fn. It may be used to check that all arguments of the fn can be evaluated in the cores' environments (the internal function .pretest_fn_on_cores provides an example).

Further arguments to be passed (unevaluated) to fn.

Value

The result of calling foreach, pbapply or mclapply, as dependent on the control argument. A side-effect of dopar is to show a progress bar that informs about the type of parallelisation performed: a default "=" character for fork clusters, and otherwise"P" for parallel computation via foreach and doSNOW, "p" for parallel computation via pbapply, and "s" for serial computation via pbapply.

See Also

dofuture for an alternative implementation of (essentially) the same functionalities, and wrap_parallel for its differences from dopar.

Examples

Run this code
# NOT RUN {
## See source code of spaMM_boot()

# }
# NOT RUN {
# Useless function, but requiring some argument beyond the first
foo <- function(y, somearg, ...) {
  if ( is.null(somearg) || TRUE ) length(y)
}

# Whether FORK can be used depends on OS and whether Rstudio is used:
dopar(matrix(1,ncol=4,nrow=3), foo, fit_env=list(), somearg=NULL, 
  nb_cores=2, cluster_args=list(type="FORK"))
# }

Run the code above in your browser using DataLab