Skip to content

Commit efe5fe6

Browse files
committed
Support throttling and resetting thread use via OpenMP functions
1 parent 5176e7d commit efe5fe6

10 files changed

+173
-4
lines changed

ChangeLog

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
2023-10-29 Dirk Eddelbuettel <[email protected]>
2+
3+
* src/RcppArmadillo.cpp (armadillo_get_number_of_omp_threads)
4+
(armadillo_set_number_of_omp_threads): New helper functions
5+
* src/RcppExports.cpp: Regenerated
6+
* R/RcppExports.R: Idem
7+
* man/armadillo_get_number_of_omp_threads.Rd: Documentation
8+
9+
* R/init.R (.onLoad): Store initial thread count
10+
* R/init.R (armadillo_throttle_cores, armadillo_reset_cores):
11+
Tread throtte and reset helper functions
12+
* man/armadillo_throttle_cores.Rd: Documentation
13+
14+
* man/fastLm.Rd: Illustration of use of throttle and reset function
15+
* NAMESPACE: Export new functions
16+
117
2023-10-14 Dirk Eddelbuettel <[email protected]>
218

319
* DESCRIPTION (Version, Date): RcppArmadillo 0.12.6.5.0

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ Imports: Rcpp (>= 0.11.0), stats, utils, methods
2626
Suggests: tinytest, Matrix (>= 1.3.0), pkgKitten, reticulate, slam
2727
URL: https://github.com/RcppCore/RcppArmadillo, https://dirk.eddelbuettel.com/code/rcpp.armadillo.html
2828
BugReports: https://github.com/RcppCore/RcppArmadillo/issues
29+
RoxygenNote: 6.0.1

NAMESPACE

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ export("fastLmPure",
1010
"RcppArmadillo.package.skeleton",
1111
"armadillo_version",
1212
"armadillo_set_seed",
13-
"armadillo_set_seed_random")
13+
"armadillo_set_seed_random",
14+
15+
"armadillo_throttle_cores",
16+
"armadillo_reset_cores",
17+
"armadillo_set_number_of_omp_threads",
18+
"armadillo_set_number_of_omp_threads"
19+
)
1420
S3method("fastLm", "default")
1521
S3method("fastLm", "formula")
1622
S3method("predict", "fastLm")
1723
S3method("print", "fastLm")
1824
S3method("summary", "fastLm")
1925
S3method("print", "summary.fastLm")
20-
21-

R/RcppExports.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ armadillo_set_seed <- function(val) {
5353
invisible(.Call(`_RcppArmadillo_armadillo_set_seed`, val))
5454
}
5555

56+
#' Report (or Set) Maximum Number of OpenMP Threads
57+
#'
58+
#' @param n Number of threads to be set
59+
#' @return For the getter, and on a system with OpenMP, the maximum
60+
#' number of threads that OpenMP may be using and on systems without it,
61+
#' one. The setter does not return a value.
62+
armadillo_get_number_of_omp_threads <- function() {
63+
.Call(`_RcppArmadillo_armadillo_get_number_of_omp_threads`)
64+
}
65+
66+
#' @rdname armadillo_get_number_of_omp_threads
67+
armadillo_set_number_of_omp_threads <- function(n) {
68+
invisible(.Call(`_RcppArmadillo_armadillo_set_number_of_omp_threads`, n))
69+
}
70+
5671
fastLm_impl <- function(X, y) {
5772
.Call(`_RcppArmadillo_fastLm_impl`, X, y)
5873
}

R/init.R

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## init.R: Startup
2+
##
3+
## Copyright (C) 2023 Dirk Eddelbuettel
4+
##
5+
## This file is part of RcppArmadillo.
6+
##
7+
## RcppArmadillo 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+
## RcppArmadillo 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 RcppArmadillo. If not, see <http://www.gnu.org/licenses/>.
19+
20+
.pkgenv <- new.env(parent=emptyenv())
21+
22+
.onLoad <- function(libname, pkgname) {
23+
.pkgenv[["omp_threads"]] <- armadillo_get_number_of_omp_threads()
24+
}
25+
26+
##' Throttle (or Reset) (Rcpp)Armadillo to Two Cores
27+
##'
28+
##' Helper functions to throttle use of cores by RcppArmadillo-internal
29+
##' code on systems with OpenMP. On package load, the initial value is
30+
##' saved and used to reset the value.
31+
##' @param n Integer value of desired cores, default is two
32+
armadillo_throttle_cores <- function(n = 2) {
33+
armadillo_set_number_of_omp_threads(n)
34+
}
35+
36+
##' @rdname armadillo_throttle_cores
37+
armadillo_reset_cores <- function() {
38+
n <- .pkgenv[["omp_threads"]]
39+
armadillo_set_number_of_omp_threads(n)
40+
}

man/armadillo_get_number_of_omp_threads.Rd

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/armadillo_throttle_cores.Rd

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/fastLm.Rd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ fastLm(X, \dots)
6969
Romain Francois, Dirk Eddelbuettel, Douglas Bates and Binxiang Ni.
7070
}
7171
\examples{
72+
\dontshow{
73+
## as an illustration, the example is computationally inexpensive
74+
## and does not require this per se
75+
armadillo_throttle_cores(2)
76+
}
7277
data(trees, package="datasets")
7378

7479
## bare-bones direct interface
@@ -89,5 +94,7 @@ fastLm(X, \dots)
8994
dd$y <- mm \%*\% seq_len(ncol(mm)) + rnorm(nrow(mm), sd = 0.1)
9095
summary(lm(y ~ f1 * f2, dd)) # detects rank deficiency
9196
summary(fastLm(y ~ f1 * f2, dd)) # some huge coefficients
97+
98+
\dontshow{armadillo_reset_cores()}
9299
}
93100
\keyword{regression}

src/RcppArmadillo.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
// RcppArmadillo.cpp: Rcpp/Armadillo glue
33
//
4-
// Copyright (C) 2010 - 2020 Dirk Eddelbuettel, Romain Francois and Douglas Bates
4+
// Copyright (C) 2010 - 2023 Dirk Eddelbuettel, Romain Francois and Douglas Bates
55
//
66
// This file is part of RcppArmadillo.
77
//
@@ -90,3 +90,26 @@ void armadillo_set_seed(unsigned int val) {
9090
//Rcpp::Rcout << "Setting value " << val << std::endl;
9191
arma::arma_rng::set_seed(val); // set the seed to given value
9292
}
93+
94+
//' Report (or Set) Maximum Number of OpenMP Threads
95+
//'
96+
//' @param n Number of threads to be set
97+
//' @return For the getter, and on a system with OpenMP, the maximum
98+
//' number of threads that OpenMP may be using and on systems without it,
99+
//' one. The setter does not return a value.
100+
// [[Rcpp::export]]
101+
int armadillo_get_number_of_omp_threads() {
102+
#ifdef _OPENMP
103+
return omp_get_max_threads();
104+
#else
105+
return 1;
106+
#endif
107+
}
108+
109+
//' @rdname armadillo_get_number_of_omp_threads
110+
// [[Rcpp::export]]
111+
void armadillo_set_number_of_omp_threads(int n) {
112+
#ifdef _OPENMP
113+
omp_set_num_threads(n);
114+
#endif
115+
}

src/RcppExports.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ BEGIN_RCPP
4141
return R_NilValue;
4242
END_RCPP
4343
}
44+
// armadillo_get_number_of_omp_threads
45+
int armadillo_get_number_of_omp_threads();
46+
RcppExport SEXP _RcppArmadillo_armadillo_get_number_of_omp_threads() {
47+
BEGIN_RCPP
48+
Rcpp::RObject rcpp_result_gen;
49+
Rcpp::RNGScope rcpp_rngScope_gen;
50+
rcpp_result_gen = Rcpp::wrap(armadillo_get_number_of_omp_threads());
51+
return rcpp_result_gen;
52+
END_RCPP
53+
}
54+
// armadillo_set_number_of_omp_threads
55+
void armadillo_set_number_of_omp_threads(int n);
56+
RcppExport SEXP _RcppArmadillo_armadillo_set_number_of_omp_threads(SEXP nSEXP) {
57+
BEGIN_RCPP
58+
Rcpp::RNGScope rcpp_rngScope_gen;
59+
Rcpp::traits::input_parameter< int >::type n(nSEXP);
60+
armadillo_set_number_of_omp_threads(n);
61+
return R_NilValue;
62+
END_RCPP
63+
}
4464
// fastLm_impl
4565
Rcpp::List fastLm_impl(const arma::mat& X, const arma::colvec& y);
4666
RcppExport SEXP _RcppArmadillo_fastLm_impl(SEXP XSEXP, SEXP ySEXP) {
@@ -58,6 +78,8 @@ static const R_CallMethodDef CallEntries[] = {
5878
{"_RcppArmadillo_armadillo_version", (DL_FUNC) &_RcppArmadillo_armadillo_version, 1},
5979
{"_RcppArmadillo_armadillo_set_seed_random", (DL_FUNC) &_RcppArmadillo_armadillo_set_seed_random, 0},
6080
{"_RcppArmadillo_armadillo_set_seed", (DL_FUNC) &_RcppArmadillo_armadillo_set_seed, 1},
81+
{"_RcppArmadillo_armadillo_get_number_of_omp_threads", (DL_FUNC) &_RcppArmadillo_armadillo_get_number_of_omp_threads, 0},
82+
{"_RcppArmadillo_armadillo_set_number_of_omp_threads", (DL_FUNC) &_RcppArmadillo_armadillo_set_number_of_omp_threads, 1},
6183
{"_RcppArmadillo_fastLm_impl", (DL_FUNC) &_RcppArmadillo_fastLm_impl, 2},
6284
{NULL, NULL, 0}
6385
};

0 commit comments

Comments
 (0)