|
20 | 20 | .pkgenv <- new.env(parent=emptyenv()) |
21 | 21 |
|
22 | 22 | .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) |
24 | 31 | } |
25 | 32 |
|
26 | 33 | ##' Throttle (or Reset) (Rcpp)Eigen to Two Cores |
27 | 34 | ##' |
28 | 35 | ##' Helper functions to throttle use of cores by RcppEigen-internal code. |
29 | 36 | ##' On package load, the initial value is saved and used to reset the value. |
30 | 37 | ##' @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"]] |
32 | 40 | EigenSetNbThreads(n) |
33 | 41 | } |
34 | 42 |
|
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"]]) |
39 | 46 | } |
0 commit comments