Skip to content

Commit 6108a3a

Browse files
authored
Support OpenMP (#145)
* Add basic (unthrottled) OpenMP support * Refine init.R for core setting * Simplify Makevars{,.win} removing comments, minor code cleanup * Add configure logic (relying on recent RcppArmadillo change) * Export and document threading helpers * Add package startup message, expand documentation * Minor documentation correction from code review [ci skip] * Add ChangeLog entry * Set a distinct version number for the branch
1 parent 58df934 commit 6108a3a

File tree

15 files changed

+4948
-40
lines changed

15 files changed

+4948
-40
lines changed

ChangeLog

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
2025-12-28 Dirk Eddelbuettel <edd@debian.org>
2+
3+
* configure.ac: Detect OpenMP compile and link options
4+
* configure.: Idem
5+
* R/init.R: Set multithreading core count on startup, show startup
6+
message, add two helper accessor functions
7+
* src/Makevars.in: Support OpenMP use for multithreading
8+
* src/Makevars.win: Idem
9+
* src/RcppEigen.cpp: Add thread setter, add typed version getter,
10+
edits to old version getter
11+
* src/RcppExport.cpp: Export new compiled functions
12+
* R/RcppExport.R: Idem
13+
* NAMESPACE: Export several helper functions
14+
* man/RcppEigen-package.Rd: Document thread count setting options
15+
* man/RcppEigen_throttle_cores.Rd: Document additional helpers
16+
* cleanup: Minor extension
17+
118
2025-08-26 Dirk Eddelbuettel <edd@debian.org>
219

320
* inst/tinytest/test_misc.R: Add minimal test for EigenNbThreads()

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: RcppEigen
22
Type: Package
33
Title: 'Rcpp' Integration for the 'Eigen' Templated Linear Algebra Library
4-
Version: 0.3.4.0.2.1
5-
Date: 2025-08-26
4+
Version: 0.4.9.8-0
5+
Date: 2025-12-28
66
Authors@R: c(person("Doug", "Bates", role = "aut",
77
comment = c(ORCID = "0000-0001-8316-9503")),
88
person("Dirk", "Eddelbuettel", role = c("aut", "cre"), email = "edd@debian.org",
@@ -35,3 +35,4 @@ Imports: Rcpp (>= 0.11.0), stats, utils
3535
Suggests: Matrix, inline, tinytest, pkgKitten, microbenchmark
3636
URL: https://github.com/RcppCore/RcppEigen, https://dirk.eddelbuettel.com/code/rcpp.eigen.html
3737
BugReports: https://github.com/RcppCore/RcppEigen/issues
38+
RoxygenNote: 6.0.1

NAMESPACE

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
useDynLib("RcppEigen", .registration=TRUE)
22

33
importFrom("Rcpp", "evalCpp")
4-
importFrom("utils", "packageDescription", "package.skeleton")
5-
importFrom("stats", "model.frame", "model.matrix", "model.response", "fitted", "coef", "printCoefmat", "pt")
4+
importFrom("utils", "packageDescription", "package.skeleton", "packageVersion")
5+
importFrom("stats", "model.frame", "model.matrix", "model.response", "fitted", "coef", "printCoefmat", "pt", "na.omit")
66
export("fastLm",
77
"fastLmPure",
8-
"RcppEigen.package.skeleton"
8+
"RcppEigen.package.skeleton",
9+
"EigenNbThreads",
10+
"EigenSetNbThreads",
11+
"RcppEigen_throttle_cores",
12+
"RcppEigen_reset_cores"
913
)
1014

1115
S3method("fastLm", "default")

R/RcppExports.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,24 @@ eigen_version <- function(single) {
55
.Call(`_RcppEigen_eigen_version`, single)
66
}
77

8+
eigen_version_typed <- function() {
9+
.Call(`_RcppEigen_eigen_version_typed`)
10+
}
11+
812
Eigen_SSE <- function() {
913
.Call(`_RcppEigen_Eigen_SSE`)
1014
}
1115

16+
#' @rdname RcppEigen_throttle_cores
1217
EigenNbThreads <- function() {
1318
.Call(`_RcppEigen_EigenNbThreads`)
1419
}
1520

21+
#' @rdname RcppEigen_throttle_cores
22+
EigenSetNbThreads <- function(n) {
23+
invisible(.Call(`_RcppEigen_EigenSetNbThreads`, n))
24+
}
25+
1626
fastLm_Impl <- function(X, y, type) {
1727
.Call(`_RcppEigen_fastLm_Impl`, X, y, type)
1828
}

R/init.R

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
}

cleanup

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ rm -f src/*.o src/*.so \
44
inst/doc/RcppEigen-unitTests.out \
55
inst/doc/RcppEigen-unitTests.aux \
66
inst/doc/RcppEigen-unitTests.log \
7-
*/*~ *~
7+
*/*~ *~ \
8+
config.log config.status src/Makevars
89
rm -rf autom4te.cache

0 commit comments

Comments
 (0)