mclapply {parallel} | R Documentation

## Parallel Versions of `lapply` and `mapply` using Forking

### Description

`mclapply` is a parallelized version of `lapply`, it returns a list of the same length as `X`, each element of which is the result of applying `FUN` to the corresponding element of `X`.

It relies on forking and hence is not available on Windows unless `mc.cores = 1`.

`mcmapply` is a parallelized version of `mapply`, and `mcMap` corresponds to `Map`.

### Usage

    mclapply(X, FUN, ..., mc.preschedule = TRUE, mc.set.seed = TRUE,
             mc.silent = FALSE, mc.cores = getOption("mc.cores", 2L),
             mc.cleanup = TRUE, mc.allow.recursive = TRUE)

    mcmapply(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE,
            mc.preschedule = TRUE, mc.set.seed = TRUE,
            mc.silent = FALSE, mc.cores = getOption("mc.cores", 2L),
            mc.cleanup = TRUE)

    mcMap(f, ...)

### Arguments

`X` | a vector (atomic or list) or an expressions vector. Other objects (including classed objects) will be coerced by `as.list`.
`FUN` | the function to be applied to (`mclapply`) each element of `X` or (`mcmapply`) in parallel to `...`.
`f` | the function to be applied in parallel to `...`.
`...` | For `mclapply`, optional arguments to `FUN`. For `mcmapply` and `mcMap`, vector or list inputs: see `mapply`.
`MoreArgs, SIMPLIFY, USE.NAMES` | see `mapply`.

`mc.preschedule`

if set to `TRUE` then the computation is first divided to (at most) as many jobs are there are cores and then the jobs are started, each job possibly covering more than one value. If set to `FALSE` then one job is forked for each value of `X`. The former is better for short computations or large number of values in `X`, the latter is better for jobs that have high variance of completion time and not too many values of `X` compared to `mc.cores`.

`mc.set.seed` | See `mcparallel`.
`mc.silent` | if set to `TRUE` then all output on ‘stdout’ will be suppressed for all parallel processes forked (‘stderr’ is not affected).
`mc.cores` | The number of cores to use, i.e. at most how many child processes will be run simultaneously. The option is initialized from environment variable MC_CORES if set. Must be at least one, and parallelization requires at least two cores.

`mc.cleanup`

if set to `TRUE` then all children that have been forked by this function will be killed (by sending `SIGTERM`) before this function returns. Under normal circumstances `mclapply` waits for the children to deliver results, so this option usually has only effect when `mclapply` is interrupted. If set to `FALSE` then child processes are collected, but not forcefully terminated. As a special case this argument can be set to the number of the signal that should be used to kill the children instead of `SIGTERM`.

`mc.allow.recursive` | Unless true, calling `mclapply` in a child process will use the child and not fork again.

### Details

`mclapply` is a parallelized version of `lapply`, provided `mc.cores > 1`: for `mc.cores == 1` it simply calls `lapply`.

By default (`mc.preschedule = TRUE`) the input `X` is split into as many parts as there are cores (currently the values are spread across the cores sequentially, i.e. first value to core 1, second to core 2, ... (core + 1)-th value to core 1 etc.) and then one process is forked to each core and the results are collected.

Without prescheduling, a separate job is forked for each value of `X`. To ensure that no more than `mc.cores` jobs are running at once, once that number has been forked the master process waits for a child to complete before the next fork.

Due to the parallel nature of the execution random numbers are not sequential (in the random number sequence) as they would be when using `lapply`. They are sequential for each forked process, but not all jobs as a whole. See `mcparallel` or the package's vignette for ways to make the results reproducible with `mc.preschedule = TRUE`.

Note: the number of file descriptors (and processes) is usually limited by the operating system, so you may have trouble using more than 100 cores or so (see `ulimit -n` or similar in your OS documentation) unless you raise the limit of permissible open file descriptors (fork will fail with error `"unable to create a pipe"`).

### Value

For `mclapply`, a list of the same length as `X` and named by `X`.

For `mcmapply`, a list, vector or array: see `mapply`.

For `mcMap`, a list.

Each forked process runs its job inside `try(..., silent = TRUE)` so if errors occur they will be stored as class `"try-error"` objects in the return value and a warning will be given.

### Warning

It is strongly discouraged to use these functions in GUI or embedded environments, because it leads to several processes sharing the same GUI which will likely cause chaos (and possibly crashes). Child processes should never use on-screen graphics devices.

Some precautions have been taken to make this usable in `R.app` on Mac OS X, but users of third-party front-ends should consult their documentation.

### Author(s)

Simon Urbanek and R Core.

Derived from the multicore package.

### See Also

`mcparallel`, `pvec`, `parLapply`, `clusterMap`.

`simplify2array` for results like `sapply`.

### Examples

    simplify2array(mclapply(rep(4, 5), rnorm))
    # use the same random numbers for all values
    set.seed(1)
    simplify2array(mclapply(rep(4, 5), rnorm, mc.preschedule = FALSE,
                    mc.set.seed = FALSE))

    ## Contrast this with the examples for clusterCall
    library(boot)
    cd4.rg <- function(data, mle) MASS::mvrnorm(nrow(data), mle$m, mle$v)
    cd4.mle <- list(m = colMeans(cd4), v = var(cd4))
    mc <- getOption("mc.cores", 2)
    run1 <- function(...) boot(cd4, corr, R = 500, sim = "parametric",
                               ran.gen = cd4.rg, mle = cd4.mle)
    ## To make this reproducible:
    set.seed(123, "L'Ecuyer")
    res <- mclapply(seq_len(mc), run1)
    cd4.boot <- do.call(c, res)
    boot.ci(cd4.boot,  type = c("norm", "basic", "perc"),
            conf = 0.9, h = atanh, hinv = tanh)

[Package parallel version 2.15.1 Index]