mclapply clusterApply
.
:
R ,
( wait, Unix,
).
library(parallel)
tasks <- list(
job1 = function() cov(mtcars[1:10,], use="complete.obs"),
job2 = function() cov(mtcars[11:20,], use="complete.obs"),
job3 = function() cov(mtcars[21:32,], use="complete.obs"),
# To check that the computations are indeed running in parallel.
job4 = function() for (i in 1:5) { cat("4"); Sys.sleep(1) },
job5 = function() for (i in 1:5) { cat("5"); Sys.sleep(1) },
job6 = function() for (i in 1:5) { cat("6"); Sys.sleep(1) }
)
out <- mclapply(
tasks,
function(f) f(),
mc.cores = length(tasks)
)
# Equivalently: create a cluster and destroy it.
# (This may work on Windows as well.)
cl <- makeCluster( length(tasks) )
out <- clusterApply(
cl,
tasks,
function(f) f()
)
stopCluster(cl)