Skip to content

Commit da9cb79

Browse files
committed
Add basic (unthrottled) OpenMP support
1 parent 58df934 commit da9cb79

File tree

6 files changed

+89
-4
lines changed

6 files changed

+89
-4
lines changed

R/RcppExports.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ 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
}
@@ -13,6 +17,10 @@ EigenNbThreads <- function() {
1317
.Call(`_RcppEigen_EigenNbThreads`)
1418
}
1519

20+
EigenSetNbThreads <- function(n) {
21+
invisible(.Call(`_RcppEigen_EigenSetNbThreads`, n))
22+
}
23+
1624
fastLm_Impl <- function(X, y, type) {
1725
.Call(`_RcppEigen_fastLm_Impl`, X, y, type)
1826
}

R/init.R

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
.pkgenv[["nb_threads"]] <- EigenNbThreads() # #nocov
24+
}
25+
26+
##' Throttle (or Reset) (Rcpp)Eigen to Two Cores
27+
##'
28+
##' Helper functions to throttle use of cores by RcppEigen-internal code.
29+
##' On package load, the initial value is saved and used to reset the value.
30+
##' @param n Integer value of desired cores, default is two
31+
RcppEigen_throttle_cores <- function(n = 2) {
32+
EigenSetNbThreads(n)
33+
}
34+
35+
##' @rdname RcppEigen_throttle_cores
36+
eigen_reset_cores <- function() {
37+
n <- .pkgenv[["nb_threads"]]
38+
EigenSetNbThreads(n)
39+
}

src/Makevars

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
## -*- mode: makefile; -*-
22

33
## One could add '-fopenmp' here (and below) for multithreaded operations
4-
PKG_CXXFLAGS = -I../inst/include
4+
PKG_CXXFLAGS = -I../inst/include $(SHLIB_OPENMP_CXXFLAGS)
55

66
## One could add '-fopenmp' here (and above) for multithreaded operations
7-
PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
7+
PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS) $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
88

99
## We are not enabling multithreaded operations by default because this would be
1010
## a change in behaviour that would like create trouble for packages using RcppEigen

src/Makevars.win

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
## -*- mode: makefile; -*-
22

33
## One could add '-fopenmp' here (and below) for multithreaded operations
4-
PKG_CXXFLAGS = -I../inst/include
4+
PKG_CXXFLAGS = $(SHLIB_OPENMP_CXXFLAGS) -I../inst/include
55

66
## One could add '-fopenmp' here (and above) for multithreaded operations
7-
PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
7+
PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS) $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
88

99
## We are not enabling multithreaded operations by default because this would be
1010
## a change in behaviour that would like create trouble for packages using RcppEigen

src/RcppEigen.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ Rcpp::IntegerVector eigen_version(bool single) {
3737
_["patch"] = EIGEN_MINOR_VERSION);
3838
}
3939

40+
// [[Rcpp::export]]
41+
Rcpp::List eigen_version_typed() {
42+
// create a vector of major, minor, patch
43+
auto v = Rcpp::IntegerVector::create(EIGEN_WORLD_VERSION, EIGEN_MAJOR_VERSION, EIGEN_MINOR_VERSION);
44+
// and place it in a list (as e.g. packageVersion() in R returns)
45+
auto l = Rcpp::List::create(v);
46+
// and class it as 'package_version' accessing print() etc methods
47+
l.attr("class") = Rcpp::CharacterVector::create("package_version", "numeric_version");
48+
return l;
49+
}
50+
4051
// [[Rcpp::export]]
4152
bool Eigen_SSE() {
4253
return Rcpp::wrap(Eigen::SimdInstructionSetsInUse());
@@ -46,3 +57,8 @@ bool Eigen_SSE() {
4657
int EigenNbThreads() {
4758
return Eigen::nbThreads();
4859
}
60+
61+
// [[Rcpp::export]]
62+
void EigenSetNbThreads(int n) {
63+
Eigen::setNbThreads(n);
64+
}

src/RcppExports.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ BEGIN_RCPP
2222
return rcpp_result_gen;
2323
END_RCPP
2424
}
25+
// eigen_version_typed
26+
Rcpp::List eigen_version_typed();
27+
RcppExport SEXP _RcppEigen_eigen_version_typed() {
28+
BEGIN_RCPP
29+
Rcpp::RObject rcpp_result_gen;
30+
Rcpp::RNGScope rcpp_rngScope_gen;
31+
rcpp_result_gen = Rcpp::wrap(eigen_version_typed());
32+
return rcpp_result_gen;
33+
END_RCPP
34+
}
2535
// Eigen_SSE
2636
bool Eigen_SSE();
2737
RcppExport SEXP _RcppEigen_Eigen_SSE() {
@@ -42,6 +52,16 @@ BEGIN_RCPP
4252
return rcpp_result_gen;
4353
END_RCPP
4454
}
55+
// EigenSetNbThreads
56+
void EigenSetNbThreads(int n);
57+
RcppExport SEXP _RcppEigen_EigenSetNbThreads(SEXP nSEXP) {
58+
BEGIN_RCPP
59+
Rcpp::RNGScope rcpp_rngScope_gen;
60+
Rcpp::traits::input_parameter< int >::type n(nSEXP);
61+
EigenSetNbThreads(n);
62+
return R_NilValue;
63+
END_RCPP
64+
}
4565
// fastLm_Impl
4666
Rcpp::List fastLm_Impl(Rcpp::NumericMatrix X, Rcpp::NumericVector y, int type);
4767
RcppExport SEXP _RcppEigen_fastLm_Impl(SEXP XSEXP, SEXP ySEXP, SEXP typeSEXP) {
@@ -58,8 +78,10 @@ END_RCPP
5878

5979
static const R_CallMethodDef CallEntries[] = {
6080
{"_RcppEigen_eigen_version", (DL_FUNC) &_RcppEigen_eigen_version, 1},
81+
{"_RcppEigen_eigen_version_typed", (DL_FUNC) &_RcppEigen_eigen_version_typed, 0},
6182
{"_RcppEigen_Eigen_SSE", (DL_FUNC) &_RcppEigen_Eigen_SSE, 0},
6283
{"_RcppEigen_EigenNbThreads", (DL_FUNC) &_RcppEigen_EigenNbThreads, 0},
84+
{"_RcppEigen_EigenSetNbThreads", (DL_FUNC) &_RcppEigen_EigenSetNbThreads, 1},
6385
{"_RcppEigen_fastLm_Impl", (DL_FUNC) &_RcppEigen_fastLm_Impl, 3},
6486
{NULL, NULL, 0}
6587
};

0 commit comments

Comments
 (0)