Learn R Programming

Smisc (version 0.3.9.1)

doCallParallel: Call a function with a vectorized input in parallel

Description

Call a function with a vectorized input in parallel, where the function is computationally intensive.

Usage

doCallParallel(fun, x, ..., njobs = parallel::detectCores() - 1,
  random.seed = NULL)

Arguments

fun

A function, or a text string with the name of the function, whose first argument is a vector and returns a corresponding vector

x

A vector of values that is the first argument to fun

Additional named arguments for fun

njobs

The number of parallel jobs to spawn using parLapplyW.

random.seed

If a numeric value is provided, x is randomized to better distribute the work among the jobs if some values of x take longer to evaluate than others. The original ordering is restored before fun(x, ...) is returned. If NULL, no randomization is performed.

Value

The same result that would be had by calling fun(x, ...), except calculated in parallel

Details

This function is a parallelized wrapper for do.call designed for the case where fun is computationally intensive. Each element of x is evaluated independently of the other elements of x. Thus, fun(c(x1,x2)) must be equivalent to c(fun(x1), fun(x2)) in order for doCallParallel to work properly.

Examples

Run this code
# NOT RUN {
# Get a vector of x's
x <- rnorm(18, mean = 2, sd = 2)

# 2 cores
y1 <- doCallParallel("pnorm", x, mean = 2, sd = 2, njobs = 2)

# 2 cores and randomization
y2 <- doCallParallel(pnorm, x, mean = 2, sd = 2, njobs = 2, random.seed = 1)

# Without using doCallParallel()
y3 <- pnorm(x, mean = 2, sd = 2)

# Comparisons
identical(y1, y2)
identical(y1, y3)
# }

Run the code above in your browser using DataLab