[R-sig-Debian] R, OpenBLAS and OMP_NUM_THREEADS

Gordon Ball gordon at chronitis.net
Mon Aug 1 13:06:02 CEST 2016


What is the correct way to globally configure R to default to single (or
at least, << NUM_CPUS) threaded operation?


Using R 3.3.1 (both in debian unstable or using the CRAN repository for
xenial) with OpenBLAS (0.2.18) defaults to using one thread per
available CPU, which isn't ideal for machines more than a couple of CPUs.

Setting the environment (OMP_NUM_THREADS or OPENBLAS_NUM_THREADS) in the
shell works:

$ OMP_NUM_THREADS=1 R
> Sys.getenv("OMP_NUM_THREADS")
[1] "1"
> system.time({x <- replicate(5e3, rnorm(5e3)); tcrossprod(x) })
[runs in one thread]

but adding it to /etc/R/Renviron.site doesn't:

$ R
> Sys.getenv("OMP_NUM_THREADS")
[1] "1"
> system.time({x <- replicate(5e3, rnorm(5e3)); tcrossprod(x) })
[runs multi-threaded]

(nor does setting the variable at runtime with `Sys.setenv`)

Presumably Renviron is read after the library is already loaded and so
the environment variable is set too late to matter.




I can think of these solutions, but none of them are ideal:

 * remove OpenBLAS (but even single threaded it performs quite a lot
better than the basic libblas)

 * set OMP_NUM_THREADS globally in people's shells with the system
bashrc (but this doesn't work for non-shell, RStudio server sessions)

 * use a library like RhpcBLASctl to set the number of threads in the
global Rprofile

 * compile a custom openblas with threading disabled, or at least a
small default number of threads


Any better ideas?


Thanks

Gordon



More information about the R-SIG-Debian mailing list