Skip to content

Multiple cores are used in simple for loop

4 messages · Martyn Plummer, Daniel Kaschek, Henrik Bengtsson

#
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():

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

https://wiki.debian.org/DebianScience/LinearAlgebraLibraries

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
MKL_NUM_THREADS.

Martyn
-----------------------------------------------------------------------
This message and its attachments are strictly confidenti...{{dropped:8}}
#
Dear Martyn,
On Fr, Jan 15, 2016 at 4:01 , Martyn Plummer <plummerm at iarc.fr> wrote:
Thanks a lot. Running

export OPENBLAS_NUM_THREADS = 1

in the bash before starting R solves both problems!


Cheers,
Daniel
#
On Fri, Jan 15, 2016 at 10:15 AM, Daniel Kaschek
<daniel.kaschek at physik.uni-freiburg.de> wrote:
I don't have builds so I can try myself, but as an alternative, is it
possible to set this environment variable in ~/.Renviron, or is that
too late in the R startup process?  What about
Sys.setenv(OPENBLAS_NUM_THREADS=1) in ~/.Rprofile?

/Henrik