Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make R use all processors?

I have a quad-core laptop running Windows XP, but looking at Task Manager R only ever seems to use one processor at a time. How can I make R use all four processors and speed up my R programs?

like image 680
David Smith Avatar asked Sep 08 '09 17:09

David Smith


People also ask

Can R use multiple cores?

Unfortunately, R is not natively able to use several cores at the same time!

How many cores does r studio use?

As mentioned in the previous section, R only uses one core at the time, even if there are more available.

Does R use CPU?

Base R is single-threaded so that 25% of usage is expected on 4-core CPU. On a single Windows machine, it is possible to spread processing across clusters (or cores if you like) using either the parallel package and the foreach package.


4 Answers

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) 
like image 103
hangmanwa7id Avatar answered Sep 28 '22 01:09

hangmanwa7id


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.

like image 37
Dirk Eddelbuettel Avatar answered Sep 28 '22 01:09

Dirk Eddelbuettel


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")
like image 26
csgillespie Avatar answered Sep 28 '22 03:09

csgillespie


I hear tell that REvolution R supports better multi-threading then the typical CRAN version of R and REvolution also supports 64 bit R in windows. I have been considering buying a copy but I found their pricing opaque. There's no price list on their web site. Very odd.

like image 25
JD Long Avatar answered Sep 28 '22 01:09

JD Long