-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathinit.R
More file actions
56 lines (51 loc) · 2.21 KB
/
init.R
File metadata and controls
56 lines (51 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
## init.R: Startup
##
## Copyright (C) 2023-2026 Dirk Eddelbuettel
##
## This file is part of RcppArmadillo.
##
## RcppArmadillo is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 2 of the License, or
## (at your option) any later version.
##
## RcppArmadillo is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with RcppArmadillo. If not, see <http://www.gnu.org/licenses/>.
.pkgenv <- new.env(parent=emptyenv())
.onLoad <- function(libname, pkgname) { # nocov start
## simple fallback: 'Ncpus' (if set) or else all cpus seen by OpenMP
ncores <- getOption("Ncpus", armadillo_get_number_of_omp_threads())
## consider OMP_THREAD_LIMIT (cf Writing R Extensions), gets NA if envvar unset
ompcores <- as.integer(Sys.getenv("OMP_THREAD_LIMIT"))
## keep the smaller value, omitting NA
ncores <- min(na.omit(c(ncores, ompcores)))
.pkgenv[["omp_threads"]] <- ncores
armadillo_throttle_cores(ncores)
}
.onAttach <- function(libname, pkgname) {
if (interactive()) {
packageStartupMessage("RcppArmadillo ",
packageDescription("RcppArmadillo", fields="Version"),
" using ", .pkgenv[["omp_threads"]], " cores. See ",
"'help(\"RcppArmadillo-package\")' for details.")
}
}
##' Throttle (or Reset) (Rcpp)Armadillo to Two Cores
##'
##' Helper functions to throttle use of cores by RcppArmadillo-internal
##' code on systems with OpenMP. On package load, the initial value is
##' saved and used to reset the value.
##' @param n Integer value of desired cores, default is two
armadillo_throttle_cores <- function(n = 2) {
armadillo_set_number_of_omp_threads(n)
}
##' @rdname armadillo_throttle_cores
armadillo_reset_cores <- function() {
n <- .pkgenv[["omp_threads"]]
armadillo_set_number_of_omp_threads(n)
} # nocov end