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

Evan Biederstedt ev@n@b|eder@tedt @end|ng |rom gm@||@com
Fri Mar 18 02:19:29 CET 2022


Hi Wolfgang

Thank you for the help; this is a very helpful suggestion:


*> Check omp_set_num_threads() from the RhpcBLASctl package. I know from
experience that it works for limiting the number of threads for BLAS inside
a running R session with blas_set_num_threads(1) (instead of setting
OPENBLAS_NUM_THREADS=1 before running R). I assume it should work the same
for omp_set_num_threads().*
I've experienced BLAS issues similar to this in the past; limiting the
number of threads for BLAS may be what we need; I'll try it and update you.

Very much appreciated
Thanks, Evan



On Thu, Mar 17, 2022 at 7:06 PM Viechtbauer, Wolfgang (SP) <
wolfgang.viechtbauer using maastrichtuniversity.nl> wrote:

> Hi Evan,
>
> Check omp_set_num_threads() from the RhpcBLASctl package. I know from
> experience that it works for limiting the number of threads for BLAS inside
> a running R session with blas_set_num_threads(1) (instead of setting
> OPENBLAS_NUM_THREADS=1 before running R). I assume it should work the same
> for omp_set_num_threads().
>
> Best,
> Wolfgang
>
> >-----Original Message-----
> >From: R-package-devel [mailto:r-package-devel-bounces using r-project.org] On
> Behalf Of
> >Evan Biederstedt
> >Sent: Thursday, 17 March, 2022 14:52
> >To: R Package Development
> >Subject: [R-pkg-devel] Setting OpenMP threads (globally) for an R package
> >
> >Hi R-package-devel
> >
> >I'm developing an R package which uses `parallel::mclapply` and several
> >other library dependencies which possibly rely upon OpenMP. Unfortunately,
> >some functions explode the amount of memory used.
> >
> >I've noticed that if I set `export OMP_NUM_THREADS=1` before starting R,
> >the memory is far more manageable.
> >
> >My question is, if there a way for me to achieve this behavior within the
> R
> >package itself?
> >
> >My initial try was to use `R/zzz.R` and an `.onLoad()` function to load
> >these global variables upon loading the library.
> >
> >```
> >.onLoad <- function(libname, pkgname){
> >  Sys.setenv(OMP_NUM_THREADS=1)
> >}
> >```
> >
> >But this doesn't work. The memory still explodes. In fact, I'm worried
> that
> >this cannot be done within an R package itself, as R has already started,
> >e.g.  https://stackoverflow.com/a/27320691/5269850
> >
> >Is there a recommended approach for this problem when writing R packages?
> >
> >Package here: https://github.com/kharchenkolab/numbat
> >
> >Related question on SO:
> >https://stackoverflow.com/questions/71507979/set-openmp-threads-for-all-
> >dependencies-in-r-package
> >
> >Any help appreciated. Thanks, Evan
>

	[[alternative HTML version deleted]]



More information about the R-package-devel mailing list