14

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

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
Dail
  • 4,622
  • 16
  • 74
  • 109

2 Answers2

23

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.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • Note that to view the vignette, you might have to do `vignette(package="parallel",topic="parallel")`. For me, if I don't add `topic=` it just lists the vignette. – Xu Wang Nov 11 '11 at 10:05
  • @XuWang Yes, indeed. I meant to say you can how it is listed via that code - I couldn't remember the topic name and I have yet to switch to 2.14.0 at the office. Just being lazy. But will edit the above. Thanks. – Gavin Simpson Nov 11 '11 at 10:13
  • No worries. Judging from your many detailed answers, you are the opposite of lazy! – Xu Wang Nov 11 '11 at 10:25
  • I don't think that the vignette is available (at least in R and parallel v 3.0.2) – Abe Mar 10 '14 at 20:07
  • @Abe What makes you say that? The `vignnette` line of code I show is working for me, at least under R v 3.0.3 – Gavin Simpson Mar 10 '14 at 21:16
  • because when I type in the line I get `Warning message: vignette parallel not found` – Abe Mar 17 '14 at 21:54
  • @Abe well, it is working for me. Did you compile R with the recommended packages? You might need to check your installation as **parallel** is a recommended package shipped with R so we should expect that it is available, and the vignette *is* part of that package. – Gavin Simpson Mar 17 '14 at 23:53
9

If you happen to do few* iterations of the same thing (or same code for few* different parameters), the easiest way to go is to run several copies of R -- OS will allocate the work on different cores.
In the opposite case, go and learn how to use real parallel extensions.

For the sake of this answer, few means less or equal the number of cores.

mbq
  • 18,510
  • 6
  • 49
  • 72