How to let R use all the cores of the computer?

Dail picture Dail · Nov 11, 2011 · Viewed 10.4k times · Source

I have read that R uses only a single CPU. How can I let R use all the available cores to run statistical algorithms?

Answer

Gavin Simpson picture Gavin Simpson · Nov 11, 2011

Yes, for starters, see the High Performance Computing Task View on CRAN. This lists details of packages that can be used in support of parallel computing on a single machine.

From R version 2.14.0, there is inbuilt support for parallel computing via the parallel package, which includes slightly modified versions of the existing snow and multicore packages. The parallel package has a vignette that you should read. You can view it using:

vignette(package="parallel", topic = "parallel")

There are other ways to exploit your multiple cores, for example via use of a multi-threaded BLAS for linear algebra computations.

Whether any of this will speed up the "statistics calculations" you want to do will depend on what those "statistics calculations" are. Spawning off multiple threads or workers entails an overhead cost to set them up, manage them and collect the results. Some operations see a benefit (some large, some small) of using multiple cores/threads, others are slowed down because of this extra overhead.

In short, do not expect to get an n times decrease in your compute time by using n cores instead of just 1.