Learn R Programming

dclone (version 1.2-0)

snowWrapper: Parallel wrapper function to call from within a function

Description

snowWrapper is a wrapper function around many snow, designed to work closely with MCMC fitting functions (can be called from inside of a function).

Usage

snowWrapper(cl, seq, fun, cldata, name = "cldata", lib = NULL, evalq=NULL,
size = 1, balancing = c("none", "load", "size", "both"), dir = getwd(), ...)

Arguments

cl
A cluster object created by makeCluster.
seq
A vector to split.
fun
A function or character string naming a function.
cldata
A list containing data. This will be assigned to an object name in the global environment (an already existing object with same name will be saved and replaced back in the end). This list is then exported to the cluster by
name
Character, the name of cldata as to be assigned to the global environment and used in fun.
lib
Character, name of package(s). Optionally packages can be loaded onto the cluster. More than one package can be specified as character vector.
evalq
Character, expressions to evaluate, e.g. for changing global options (passed to clusterEvalQ). More than one expressions can be specified as character vector.
balancing
Character, type of balancing to perform (see Details).
size
Vector of problem sizes (or relative performance information) corresponding to elements of seq (recycled if needed). The default 1 indicates equality of problem sizes.
dir
Working directory to use.
...
Other arguments of fun, that are simple values and not objects. (Arguments passed as objects should be specified in cldata, otherwise not exported to the cluster.)

Value

  • Usually a list with results returned by the cluster.

Details

The function sets the random seeds, loads packages lib onto the cluster, sets the working directory as dir, exports cldata and evaluates fun on seq. No balancing (balancing = "none") means, that the problem is splitted into roughly equal subsets, without respect to size (see clusterSplit). This splitting is deterministic (reproducible). Load balancing (balancing = "load") means, that the problem is not splitted into subsets a priori, but subsequent items are placed on the worker which is empty (see clusterApplyLB for load balancing). This splitting is non-deterministic (might not be reproducible). Size balancing (balancing = "size") means, that the problem is splitted into subsets, with respect to size (see clusterSplitSB and parLapplySB). In size balancing, the problem is re-ordered from largest to smallest, and then subsets are determined by minimizing the total approximate processing time. This splitting is deterministic (reproducible). Size and load balancing (balancing = "both") means, that the problem is re-ordered from largest to smallest, and then undeterministic load balancing is used (see parLapplySLB). If size is correct, this is identical to size balancing. This splitting is non-deterministic (might not be reproducible).

See Also

Size balancing: parLapplySB, parLapplySLB Optimizing the number of workers: clusterSize, plotClusterSize. snowWrapper is used internally by jags.parfit and dc.parfit.

Examples

Run this code
cl <- makeSOCKcluster(2)
## wrapper
fun <- function(i) cldata$a * i - cldata$b
cldata <- list(a=10, b=5)
snowWrapper(cl, 1:5, fun, cldata)
stopCluster(cl)

Run the code above in your browser using DataLab