How to make R use all processors? How to make R use all processors? r r

How to make R use all processors?


I have a basic system I use where I parallelize my programs on the "for" loops. This method is simple once you understand what needs to be done. It only works for local computing, but that seems to be what you're after.

You'll need these libraries installed:

library("parallel")library("foreach")library("doParallel")

First you need to create your computing cluster. I usually do other stuff while running parallel programs, so I like to leave one open. The "detectCores" function will return the number of cores in your computer.

cl <- makeCluster(detectCores() - 1)registerDoParallel(cl, cores = detectCores() - 1)

Next, call your for loop with the "foreach" command, along with the %dopar% operator. I always use a "try" wrapper to make sure that any iterations where the operations fail are discarded, and don't disrupt the otherwise good data. You will need to specify the ".combine" parameter, and pass any necessary packages into the loop. Note that "i" is defined with an equals sign, not an "in" operator!

data = foreach(i = 1:length(filenames), .packages = c("ncdf","chron","stats"),               .combine = rbind) %dopar% {  try({       # your operations; line 1...       # your operations; line 2...       # your output     })}

Once you're done, clean up with:

stopCluster(cl)


The CRAN Task View on High-Performance Compting with R lists several options. XP is a restriction, but you still get something like snow to work using sockets within minutes.


As of version 2.15, R now comes with native support for multi-core computations. Just load the parallel package

library("parallel")

and check out the associated vignette

vignette("parallel")