Skip to content
Prev 2583 / 3656 Next

R, OpenBLAS and OMP_NUM_THREEADS

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
[1] "1"
[runs in one thread]

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

$ R
[1] "1"
[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