17

No matter how intensive the R computation is, it doesn't use more than 25% of the CPU. I have tried setting the priority of the rsession.exe to High and even Realtime but the usage remains the same. Is there any way to increase the CPU usage of R to utilize the full potential of my system or is there is any misunderstanding in my understanding of the problem? Thanks in advance for the help.

P.S.: Below is a screenshot of the CPU usage. Screenshot of the CPU usage

2
  • Do you have 4 cores? Check the "performance" tab, is 1 core at 100%?
    – RaGe
    May 2, 2015 at 5:18
  • What are your computer's specs? What version of R are you using? May 2, 2015 at 5:33

2 Answers 2

20

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.

First of all, the parallel package (included in R 2.8.0+, no need to install) provides functions based on the snow package - these functions are extensions of lapply(). And the foreach package provides an extension of for-loop construct - note that it has to be used with the doParallel package.

Below is a quick example of k-means clustering using both the packages. The idea is simple, which is (1) fitting kmeans() in each cluster, (2) combining the outcomes and (3) seleting minimum tot.withiness.

library(parallel)
library(iterators)
library(foreach)
library(doParallel)

# parallel
split = detectCores()
eachStart = 25

cl = makeCluster(split)
init = clusterEvalQ(cl, { library(MASS); NULL })
results = parLapplyLB(cl
                      ,rep(eachStart, split)
                      ,function(nstart) kmeans(Boston, 4, nstart=nstart))
withinss = sapply(results, function(result) result$tot.withinss)
result = results[[which.min(withinss)]]
stopCluster(cl)

result$tot.withinss
#[1] 1814438

# foreach
split = detectCores()
eachStart = 25
# set up iterators
iters = iter(rep(eachStart, split))
# set up combine function
comb = function(res1, res2) {
  if(res1$tot.withinss < res2$tot.withinss) res1 else res2
}

cl = makeCluster(split)
registerDoParallel(cl)
result = foreach(nstart=iters, .combine="comb", .packages="MASS") %dopar%
  kmeans(Boston, 4, nstart=nstart)
stopCluster(cl)

result$tot.withinss
#[1] 1814438

Further details of those packages and more examples can be found in the following posts.

4
  • 1
    Thanks for the reply. So the changes are to be brought in the program instead of the R configuration? Is there any way that R can be configured to use all the cores?
    – Suraj
    May 3, 2015 at 3:39
  • detectCores() in the parallel package identifies # clusters available on a machine. The above example is done on 4-cored laptop and its single thread equivalent is kmeans(Boston, 4, nstart = 100). Instead of running the algorithm 100 times sequentially, the parallel version runs 25 times at each cluster. Then the results are combined. | As far as I've searched, code has to be modified if the R distributed by CRAN is used. You may search that by Revolution Analytics but I'm not sure. May 3, 2015 at 4:03
  • Links are dead! Nov 13, 2016 at 2:51
  • 1
    Links are recovered! Nov 14, 2016 at 8:05
4

R is, in most cases, single threaded. Unless you set it up properly, you will only use 1 core to 100%. My guess is you are using a quad core machine so 1 core at 100% will look like 25% CPU usage.

1
  • Thanks for the reply. But how can I configure it to use all the cores? I'm using Intel 2nd Gen i5 2440M machine. Do I make changes in the program or configure R in some way?
    – Suraj
    May 3, 2015 at 3:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.