Skip to content

Commit c16052e

Browse files
committed
Refine init.R for core setting
1 parent da9cb79 commit c16052e

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

R/init.R

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,27 @@
2020
.pkgenv <- new.env(parent=emptyenv())
2121

2222
.onLoad <- function(libname, pkgname) {
23-
.pkgenv[["nb_threads"]] <- EigenNbThreads() # #nocov
23+
## simple fallback: 'Ncpus' (if set) or else all cpus seen by OpenMP
24+
ncores <- getOption("Ncpus", EigenNbThreads())
25+
## consider OMP_THREAD_LIMIT (cf Writing R Extensions), gets NA if envvar unset
26+
ompcores <- as.integer(Sys.getenv("OMP_THREAD_LIMIT"))
27+
## keep the smaller value, omitting NA
28+
ncores <- min(na.omit(c(ncores, ompcores)))
29+
.pkgenv[["nb_threads"]] <- ncores # #nocov
30+
RcppEigen_throttle_cores(ncores)
2431
}
2532

2633
##' Throttle (or Reset) (Rcpp)Eigen to Two Cores
2734
##'
2835
##' Helper functions to throttle use of cores by RcppEigen-internal code.
2936
##' On package load, the initial value is saved and used to reset the value.
3037
##' @param n Integer value of desired cores, default is two
31-
RcppEigen_throttle_cores <- function(n = 2) {
38+
RcppEigen_throttle_cores <- function(n) {
39+
if (missing(n)) n <- .pkgenv[["nb_threads"]]
3240
EigenSetNbThreads(n)
3341
}
3442

35-
##' @rdname RcppEigen_throttle_cores
36-
eigen_reset_cores <- function() {
37-
n <- .pkgenv[["nb_threads"]]
38-
EigenSetNbThreads(n)
43+
##'@ rdname RcppEigen_throttle_cores
44+
RcppEigen_reset_cores <- function() {
45+
EigenSetNbThreads(.pkgenv[["nb_threads"]])
3946
}

0 commit comments

Comments
 (0)