[Rd] Multiple cores are used in simple for loop

Martyn Plummer plummerm at iarc.fr
Fri Jan 15 16:01:51 CET 2016

On Fri, 2016-01-15 at 15:03 +0100, Daniel Kaschek wrote:
> 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.

It depends on what backend R is using for linear algebra. Some will
split large matrix calculations over multiple threads. On Debian, you
can set the blas and lapack libraries to the implementation of your


As far as I know reference blas and lapack are still single threaded.

Alternatively, you may be able to control the maximum number of threads
by setting and exporting an appropriate environment variable depending
on what backend you are using, e.g. OPENBLAS_NUM_THREADS or


> A similar problem persists with the parallel package and mclapply():
> library(parallel)
> 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?
> Cheers,
> Daniel
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

This message and its attachments are strictly confidenti...{{dropped:8}}

More information about the R-devel mailing list