clusterApply: Apply a function to a list using the cluster

View source: R/Cluster.R

clusterApplyR Documentation

Apply a function to a list using the cluster

Description

Apply a function to a list using the cluster

Usage

clusterApply(cluster, x, fun, ..., stopOnError = FALSE, progressBar = TRUE)

Arguments

cluster

The cluster of threads to run the function.

x

The list on which the function will be applied.

fun

The function to apply. Note that the context in which the function is specifies matters (see details).

...

Additional parameters for the function.

stopOnError

Stop when one of the threads reports an error? If FALSE, all errors will be reported at the end.

progressBar

Show a progress bar?

Details

The function will be executed on each element of x in the threads of the cluster. If there are more elements than threads, the elements will be queued. The progress bar will show the number of elements that have been completed. It can sometimes be important to realize that the context in which a function is created is also transmitted to the worker node. If a function is defined inside another function, and that outer function is called with a large argument, that argument will be transmitted to the worker node each time the function is executed. It can therefore make sense to define the function to be called at the package level rather than inside a function, to save overhead.

Value

A list with the result of the function on each item in x.

Examples

fun <- function(x) {
  return (x^2)
}

cluster <- makeCluster(numberOfThreads = 3)
clusterApply(cluster, 1:10, fun)
stopCluster(cluster)

ParallelLogger documentation built on June 7, 2022, 5:05 p.m.