|
| 1 | +## init.R: Startup |
| 2 | +## |
| 3 | +## Copyright (C) 2025 Dirk Eddelbuettel |
| 4 | +## |
| 5 | +## This file is part of RcppEigen. |
| 6 | +## |
| 7 | +## RcppEigen is free software: you can redistribute it and/or modify it |
| 8 | +## under the terms of the GNU General Public License as published by |
| 9 | +## the Free Software Foundation, either version 2 of the License, or |
| 10 | +## (at your option) any later version. |
| 11 | +## |
| 12 | +## RcppEigen is distributed in the hope that it will be useful, but |
| 13 | +## WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +## GNU General Public License for more details. |
| 16 | +## |
| 17 | +## You should have received a copy of the GNU General Public License |
| 18 | +## along with RcppEigen. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +.pkgenv <- new.env(parent=emptyenv()) |
| 21 | + |
| 22 | +.onLoad <- function(libname, pkgname) { |
| 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) |
| 31 | +} |
| 32 | + |
| 33 | +.onAttach <- function(libname, pkgname) { |
| 34 | + if (interactive()) { |
| 35 | + packageStartupMessage("RcppEigen ", packageVersion("RcppEigen"), |
| 36 | + " using ", .pkgenv[["nb_threads"]], " cores. See ", |
| 37 | + "'help(\"RcppEigen-package\")' for details.") |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +##' Throttle (or Reset) (Rcpp)Eigen Core Usage |
| 42 | +##' |
| 43 | +##' Helper functions to throttle use of cores by RcppEigen-internal code. |
| 44 | +##' On package load, the initial value is saved and used to reset the value. |
| 45 | +##' @param n Integer value of desired cores, default is the value set at package |
| 46 | +##' startup reflecting the smallest value among the total number of available |
| 47 | +##' cores (or one if compiled without OpenMP support), the value of option |
| 48 | +##' \code{Ncpus} and the value of environment variable \code{OMP_THREAD_LIMIT}. |
| 49 | +##' @return Only \code{EigenNbThreads()} returns a value, the current value of |
| 50 | +##' the number of cores used. The other functions are invoked for their side |
| 51 | +##' effect of affecting the count of cores used. |
| 52 | +##' @seealso \code{\link{RcppEigen-package}} |
| 53 | +RcppEigen_throttle_cores <- function(n) { |
| 54 | + if (missing(n)) n <- .pkgenv[["nb_threads"]] |
| 55 | + EigenSetNbThreads(n) |
| 56 | +} |
| 57 | + |
| 58 | +##' @rdname RcppEigen_throttle_cores |
| 59 | +RcppEigen_reset_cores <- function() { |
| 60 | + EigenSetNbThreads(.pkgenv[["nb_threads"]]) |
| 61 | +} |
0 commit comments