Skip to content

Commit 52e072f

Browse files
authored
OpenMP startup and CI refinements (#500)
* On macOS also install and show cores * Trying alternate macOS config for openmp * Shorten and reduce to two * Refine openmp thread count setting at startup * Agree on name for thread count environment-stored variable * And a #nocov end tag cleanup
1 parent f03b596 commit 52e072f

File tree

4 files changed

+70
-59
lines changed

4 files changed

+70
-59
lines changed

.github/workflows/ci.yaml

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
include:
1717
- { name: container, os: ubuntu-latest, container: rocker/r2u4ci }
1818
- { name: macos, os: macos-latest, openmp: yes }
19-
- { name: macos, os: macos-latest, openmp: no }
19+
#- { name: macos, os: macos-latest, openmp: no }
2020
#- { name: ubuntu, os: ubuntu-latest }
2121

2222

@@ -30,25 +30,32 @@ jobs:
3030
- name: Setup
3131
uses: eddelbuettel/github-actions/r-ci@master
3232

33-
- name: OpenMP for macOS
34-
if: ${{ matrix.os == 'macos-latest' && matrix.openmp == 'yes' }}
35-
run: |
36-
curl -fsSL https://raw.githubusercontent.com/coatless-shell/openmp/main/install-openmp.sh | bash -s -- --yes
37-
mkdir -p ~/.R
38-
cat <<EOF > ~/.R/Makevars
39-
CPPFLAGS += -Xclang -fopenmp
40-
LDFLAGS += -lomp
41-
EOF
33+
# - name: OpenMP for macOS
34+
# if: ${{ matrix.os == 'macos-latest' && matrix.openmp == 'yes' }}
35+
# run: |
36+
# curl -fsSL https://raw.githubusercontent.com/coatless-shell/openmp/main/install-openmp.sh | bash -s -- --yes
37+
# mkdir -p ~/.R
38+
# cat <<EOF > ~/.R/Makevars
39+
# CPPFLAGS += -Xclang -fopenmp
40+
# LDFLAGS += -lomp
41+
# EOF
42+
# cat ~/.R/Makevars
43+
# echo -n "R CMD config CXX: "; R CMD config CXX
44+
# echo -n "R CMD config CPPFLAGS: "; R CMD config CPPFLAGS
45+
# echo -n "R CMD config LDFLAGS: "; R CMD config LDFLAGS
46+
# echo -n "R CMD config SHLIB_CXXLD: "; R CMD config SHLIB_CXXLD
47+
# echo -n "R CMD config SHLIB_CXXLDFLAGS: "; R CMD config SHLIB_CXXLDFLAGS
4248

43-
- name: Show config on macOS
49+
- name: OpenMP for macOS
4450
if: ${{ matrix.os == 'macos-latest' && matrix.openmp == 'yes' }}
4551
run: |
46-
cat ~/.R/Makevars
47-
echo -n "R CMD config CXX: "; R CMD config CXX
48-
echo -n "R CMD config CPPFLAGS: "; R CMD config CPPFLAGS
49-
echo -n "R CMD config LDFLAGS: "; R CMD config LDFLAGS
50-
echo -n "R CMD config SHLIB_CXXLD: "; R CMD config SHLIB_CXXLD
51-
echo -n "R CMD config SHLIB_CXXLDFLAGS: "; R CMD config SHLIB_CXXLDFLAGS
52+
curl -O https://mac.r-project.org/openmp/openmp-17.0.6-darwin20-Release.tar.gz
53+
sudo tar fvxz openmp-17.0.6-darwin20-Release.tar.gz -C /
54+
rm openmp-17.0.6-darwin20-Release.tar.gz
55+
sudo xcode-select -s /Applications/Xcode_16.2.app
56+
echo "MACOSX_DEPLOYMENT_TARGET=11.0" >> $GITHUB_ENV
57+
echo "xcode is set: $(xcode-select --print-path)"
58+
echo "using SDK: $(xcrun --show-sdk-version)"
5259
5360
- name: Configure
5461
run: ./configure; cat src/Makevars
@@ -59,6 +66,12 @@ jobs:
5966
- name: Test
6067
run: ./run.sh run_tests
6168

69+
- name: Install and Verify on macOS
70+
if: ${{ matrix.os == 'macos-latest' }}
71+
run: |
72+
R CMD INSTALL .
73+
Rscript -e 'RcppArmadillo::armadillo_get_number_of_omp_threads()'
74+
6275
- name: Coverage
6376
if: ${{ matrix.os == 'ubuntu-latest' }}
6477
env:

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: 19 additions & 4 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[["omp_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[["omp_threads"]], " cores. See ",
37+
"'help(\"RcppArmadillo-package\")' for details.")
38+
}
2439
}
2540

2641
##' Throttle (or Reset) (Rcpp)Armadillo to Two Cores
@@ -37,4 +52,4 @@ armadillo_throttle_cores <- function(n = 2) {
3752
armadillo_reset_cores <- function() {
3853
n <- .pkgenv[["omp_threads"]]
3954
armadillo_set_number_of_omp_threads(n)
40-
}
55+
} # nocov end

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)