Skip to content
Prev 7867 / 12125 Next

[R-pkg-devel] Setting OpenMP threads (globally) for an R package

I agree with Henrik's assessment.




*>  However, then one of the direct or indirect dependenciesis updated and
introduced non-fork-safe code, and Boom! - a "Boom!"that is often
semi-random, some times rare, and hard to narrow down.*
This really is a problem, as they're often dependencies of dependencies. I
found myself trying to read through DESCRIPTION files + Makevas statements
in dozens of packages to pin down BLAS linking, just trying to get some
sense of what could possibly be going on here. Even then, you'd notice some
packages had this behavior when isolated (probably because of dependencies
these packages utilized).




*> But the 'parallel' package, or R in general,doesn't provide a way for
that developer to detect this. This is areal problem that already exists
for some packages out there.*
I agree that this would be exceptionally valuable.

Consider data.table getDTthreads():
https://rdrr.io/cran/data.table/man/openmp-utils.html

Couldn't/shouldn't something like exist in `parallel()`?

Better yet would be something within "base R" to set this.


*RE: solution*
It doesn't appear to reduce memory usage as much as setting `export
OMP_NUM_THREADS=1` before starting R, but everyone's suggestion here
helped; if I set

"""
RhpcBLASctl::blas_set_num_threads(1)
RhpcBLASctl::omp_set_num_threads(1)
data.table::setDTthreads(1)
""""

I don't notice this problem with forking + memory surges. It applies to
various installations of BLAS, e.g. "traditional" BLAS, OpenBLAS, etc.

I really want to thank everyone for the help with this! At least I have a
better understanding what happened here + a decent way forward.

Best, Evan Biederstedt

On Fri, Mar 18, 2022 at 12:53 PM Henrik Bengtsson <
henrik.bengtsson at gmail.com> wrote: