Skip to content

Commit 786f60b

Browse files
committed
Refine openmp thread count setting at startup
1 parent acfecb9 commit 786f60b

File tree

3 files changed

+39
-41
lines changed

3 files changed

+39
-41
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2026-01-03 Dirk Eddelbuettel <edd@debian.org>
2+
3+
* R/init.R: Expand openmp core count logic respecting Ncpus and
4+
OMP_THREAD_LIMIT else setting all cores; add startup message
5+
* man/RcppArmadillo-package.Rd: Document thread count setting options
6+
17
2025-12-16 Dirk Eddelbuettel <edd@debian.org>
28

39
* DESCRIPTION (Version, Date): RcppArmadillo 15.2.3-1

R/init.R

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## init.R: Startup
22
##
3-
## Copyright (C) 2023 Dirk Eddelbuettel
3+
## Copyright (C) 2023-2026 Dirk Eddelbuettel
44
##
55
## This file is part of RcppArmadillo.
66
##
@@ -19,8 +19,23 @@
1919

2020
.pkgenv <- new.env(parent=emptyenv())
2121

22-
.onLoad <- function(libname, pkgname) {
23-
.pkgenv[["omp_threads"]] <- armadillo_get_number_of_omp_threads() # #nocov
22+
.onLoad <- function(libname, pkgname) { # nocov start
23+
## simple fallback: 'Ncpus' (if set) or else all cpus seen by OpenMP
24+
ncores <- getOption("Ncpus", armadillo_get_number_of_omp_threads())
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
30+
armadillo_throttle_cores(ncores)
31+
}
32+
33+
.onAttach <- function(libname, pkgname) {
34+
if (interactive()) {
35+
packageStartupMessage("RcppArmadillo ", packageVersion("RcppArmadillo"),
36+
" using ", .pkgenv[["nb_threads"]], " cores. See ",
37+
"'help(\"RcppArmadillo-package\")' for details.")
38+
}
2439
}
2540

2641
##' Throttle (or Reset) (Rcpp)Armadillo to Two Cores

man/RcppArmadillo-package.Rd

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
\alias{RcppArmadillo}
44
\alias{RcppArmadilloExample}
55
\docType{package}
6-
\title{
7-
R and Armadillo Integration
8-
}
9-
\description{
10-
The package brings the power of Armadillo to R.
11-
}
6+
\title{R and Armadillo Integration}
7+
\description{The package brings the power of Armadillo to R.}
128
\section{Armadillo}{
139
\code{Armadillo} is a C++ linear algebra library, aiming towards a good
1410
balance between speed and ease of use.
@@ -28,45 +24,28 @@
2824
Armadillo has been primarily developed at NICTA (Australia) by Conrad
2925
Sanderson, with contributions from around the world.
3026
}
31-
3227
\section{RcppArmadillo}{
3328
\code{RcppArmadillo} acts as a bridge between \code{Rcpp} and \code{Armadillo},
3429
allowing the programmer to write code using Armadillo classes that integrate
3530
seemlessly with \code{R} via \code{Rcpp}.
3631
}
37-
3832
\section{Using RcppArmadillo}{
3933
The simplest way to get started is to create a skeleton of a package
4034
using \code{RcppArmadillo}. This can be done conveniently by the
4135
\code{\link{RcppArmadillo.package.skeleton}}
4236
function.
43-
44-
The important steps are
45-
\itemize{
46-
\item Include the \code{RcppArmadillo.h} header file, which also includes
47-
\code{armadillo.h}.
48-
\item Import Rcpp, and LinkingTo Rcpp and RcppArmadillo by adding these lines to
49-
the DESCRIPTION file:
50-
51-
\preformatted{
52-
Imports: Rcpp (>= 0.11.0)
53-
LinkingTo: Rcpp, RcppArmadillo
54-
}
55-
56-
\item Link against the BLAS and LAPACK libraries, by adding this line
57-
in the \code{Makevars} and \code{Makevars.win} files:
58-
\preformatted{PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) }
59-
}
6037
}
61-
62-
\section{Support}{
63-
Please use the Rcpp-devel mailing list on r-forge
64-
for questions about RcppArmadillo (subscribe first).
65-
\url{https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel}
66-
38+
\section{Threading}{
39+
The Armadillo library can take advantage of OpenMP to execute computations in
40+
parallel via multi-threaded code. The number of cores uses can be set (or
41+
retrieved) explicitly via helper functions \code{armadillo_get_number_of_omp+threads()} and
42+
\code{armadillo_set_number_of_omp_threads()}. A default value is stored at package startup; it
43+
recognises R option value \code{Ncpus} and environment variable
44+
\code{OMP_THREAD_LIMIT}. Additional helper functions
45+
\code{armadillo_throttle_cores()} and \code{armadillo_reset_cores()} are
46+
available to (temporarily) lower the number of cores uses and to reset to
47+
the package default value set at startup.
6748
}
68-
69-
7049
\author{
7150
For RcppArmadillo: Dirk Eddelbuettel, Romain Francois, Doug Bates and
7251
Binxiang Ni
@@ -87,9 +66,7 @@
8766
"RcppArmadillo: Accelerating R with high-performance C++ linear algebra",
8867
Computational Statistics and Data Analysis, 2014, 71, March, pages
8968
1054-1063, \doi{10.1016/j.csda.2013.02.005}.
90-
)
91-
9269
}
93-
\keyword{ package }
94-
\keyword{ programming }
95-
\keyword{ interface }
70+
\keyword{package}
71+
\keyword{programming}
72+
\keyword{interface}

0 commit comments

Comments
 (0)