Tuesday, April 26, 2016

Parallel Computing for R: A convenient wrapper function for the R package "parallel"


I wrote the following wrapper function that makes it easy to call parallel computing in R.  You may want to change the "nCores" parameter to the number of CPU cores you have and want to utilize.


my.parallel <- function(X, FUN,
clFUN=c("parSapply", "parLapply", "parRapply", "parCapply"),
nCores=2, ...)
{
## PURPOSE: Wrapper for R Parallel Computing Package "parallel"
## ARGUMENT:
## X: a list or vector of data
## FUN: a function to be applied over the data X.
## clFUN: a string for the parallel version of the "apply" function
## varieties available from the R package "parallel".
## Only four versions are allowed.
## nCores: Numer of CPU cores to be utilized for parallel computing
## ...: other arguments that will be passed the function "clFUN".
## RETURN: Computed data that are returned from the "clFUN" call.
## DATE: 26 Apr 2016, 12:43
## -----------------------------------------------------
clFUN <- match.arg(clFUN)
require(parallel)
cl <- makeCluster(nCores, type="FORK")
res <- do.call(clFUN, list(cl=cl, X, FUN, ...))
stopCluster(cl)
res
}
if (F) { # Unit Test 
    parallel::detectCores()       # check how many CPU cores are available
base <- 2
f <- function(exponent) base^exponent
my.parallel(2:4, f) # 4 8 16
my.parallel(matrix(1:9, 3), sum, "parRapply") # 12 15 18
}