Apply a function to a list using the cluster
clusterApply(cluster, x, fun, ..., stopOnError = FALSE, progressBar = TRUE)
The cluster of threads to run the function.
The list on which the function will be applied.
The function to apply. Note that the context in which the function is specifies matters (see details).
Additional parameters for the function.
Stop when one of the threads reports an error? If FALSE, all errors will be reported at the end.
Show a progress bar?
A list with the result of the function on each item in x.
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.
fun <- function(x) {
return (x^2)
}
cluster <- makeCluster(numberOfThreads = 3)
clusterApply(cluster, 1:10, fun)
#> | | | 0% | |======= | 10% | |============== | 20% | |===================== | 30% | |============================ | 40% | |=================================== | 50% | |========================================== | 60% | |================================================= | 70% | |======================================================== | 80% | |=============================================================== | 90% | |======================================================================| 100%
#> [[1]]
#> [1] 1
#>
#> [[2]]
#> [1] 4
#>
#> [[3]]
#> [1] 9
#>
#> [[4]]
#> [1] 16
#>
#> [[5]]
#> [1] 25
#>
#> [[6]]
#> [1] 36
#>
#> [[7]]
#> [1] 49
#>
#> [[8]]
#> [1] 64
#>
#> [[9]]
#> [1] 81
#>
#> [[10]]
#> [1] 100
#>
stopCluster(cluster)