Parallel computing in both windows and Unix computer

In the package parallel, a very useful function is mclapply(); it can be used directly as an lapply() but it runs the code in parallel in different cores.

However, do not forget the to set the number of cores to be used because the default is only 2.

Then you can do:
options(mc.cores = detectCores())
system.time(out <- mclapply(as.list(1:10), FUN=function(x) {x*2}))
or directly:
system.time(out <- mclapply(as.list(1:10), FUN=function(x) {x*2}, mc.cores = detectCores()))

However it does not work in Windows because forking cannot be used in windows.

In windows you can use:

options(cl.cores = detectCores())
cl <- makeCluster(getOption("cl.cores", 2))
# If you must use other package in the parallel function; use
# invisible(clusterEvalQ(cl = cl , library(xxxxxx)))
system.time(out <- parLapply(cl=cl, X=as.list(1:10), fun=function(x) {x*2}))
stopCluster(cl)  

Don't forget to stop the cluster before to open another one.

Commentaires

Posts les plus consultés de ce blog

Standard error from Hessian Matrix... what can be done when problem occurs

stepAIC from package MASS with AICc

Install treemix in ubuntu 20.04