[Rd] Multiple cores are used in simple for loop

Daniel Kaschek daniel.kaschek at physik.uni-freiburg.de
Fri Jan 15 15:03:40 CET 2016

Dear all,

I run different R versions (3.2.1, 3.2.2 and 3.2.3) on different 
platforms (Arch, Ubuntu, Debian) with a different number of available 
cores (24, 4, 24). The following line produces very different behavior 
on the three machines:

for(i in 1:1e6) {n <- 100; M <- matrix(rnorm(n^2), n, n); M %*% M}

On the Ubuntu and Arch machine one core is used, but on the Debian 
machine ALL cores are used with heavy "kernel time" vs. "normal time" 
(red vs. green in htop). It seems that the number of cores used on 
Debian is related to the size of the matrix. Reducing n from 100 to 4 
causes four cores to work.

A similar problem persists with the parallel package and mclapply():

out <- mclapply(1:1e6, function(i) { n <- 100; M <- matrix(rnorm(n^2), 
n, n); M %*% M }, mc.cores = 24)

On Arch and Debian all 24 cores run and show a high kernel time vs. 
normal time (all CPU bars in htop are 80% red). With mc.cores = 4 on 
the Ubuntu system however, all four cores run at full load with almost 
no kernel time but full normal time (all bars are green).

Have you seen this problem before? Does anybody know how to fix it?


More information about the R-devel mailing list