I'm developing an R package which uses parallel::mclapply and several other library dependencies which possibly rely upon OpenMP.
Unfortunately, some functions explode with memory usage, making the R package practically unusable.
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.
.onLoad <- function(libname, pkgname){
Sys.setenv(OMP_NUM_THREADS=1)
}
But this doesn't work. The memory still explodes.
I'm worried that this cannot be done within an R package itself: https://stackoverflow.com/a/27320691/5269850
Modifications to the environment variables after the program has started, even if modified by the program itself, are ignored by the OpenMP implementation.
Is there a recommended approach for this problem when writing R packages?