From ccb3bd376adf3d3d265eaa1402d1dec03cf420e3 Mon Sep 17 00:00:00 2001 From: Dirk Eddelbuettel Date: Sat, 3 Jan 2026 13:04:01 -0600 Subject: [PATCH 1/6] On macOS also install and show cores --- .github/workflows/ci.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 68471fdb..e9d702eb 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -59,6 +59,12 @@ jobs: - name: Test run: ./run.sh run_tests + - name: Install + if: ${{ matrix.os == 'macos-latest' }} + run: | + R CMD INSTALL . + Rscript -e 'RcppArmadillo::armadillo_get_number_of_omp_threads()' + - name: Coverage if: ${{ matrix.os == 'ubuntu-latest' }} env: From d38bf035e3151acf4075696b07e1a2fdba6c13c2 Mon Sep 17 00:00:00 2001 From: Dirk Eddelbuettel Date: Sat, 3 Jan 2026 13:34:01 -0600 Subject: [PATCH 2/6] Trying alternate macOS config for openmp --- .github/workflows/ci.yaml | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index e9d702eb..253fc431 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -17,6 +17,7 @@ jobs: - { name: container, os: ubuntu-latest, container: rocker/r2u4ci } - { name: macos, os: macos-latest, openmp: yes } - { name: macos, os: macos-latest, openmp: no } + - { name: macos, os: macos-latest, openmp: alt } #- { name: ubuntu, os: ubuntu-latest } @@ -39,10 +40,6 @@ jobs: CPPFLAGS += -Xclang -fopenmp LDFLAGS += -lomp EOF - - - name: Show config on macOS - if: ${{ matrix.os == 'macos-latest' && matrix.openmp == 'yes' }} - run: | cat ~/.R/Makevars echo -n "R CMD config CXX: "; R CMD config CXX echo -n "R CMD config CPPFLAGS: "; R CMD config CPPFLAGS @@ -50,6 +47,17 @@ jobs: echo -n "R CMD config SHLIB_CXXLD: "; R CMD config SHLIB_CXXLD echo -n "R CMD config SHLIB_CXXLDFLAGS: "; R CMD config SHLIB_CXXLDFLAGS + - name: Alternate OpenMP for macOS + if: ${{ matrix.os == 'macos-latest' && matrix.openmp == 'alt' }} + run: | + curl -O https://mac.r-project.org/openmp/openmp-17.0.6-darwin20-Release.tar.gz + sudo tar fvxz openmp-17.0.6-darwin20-Release.tar.gz -C / + rm openmp-17.0.6-darwin20-Release.tar.gz + sudo xcode-select -s /Applications/Xcode_16.2.app + echo "MACOSX_DEPLOYMENT_TARGET=11.0" >> $GITHUB_ENV + echo "xcode is set: $(xcode-select --print-path)" + echo "using SDK: $(xcrun --show-sdk-version)" + - name: Configure run: ./configure; cat src/Makevars @@ -59,7 +67,7 @@ jobs: - name: Test run: ./run.sh run_tests - - name: Install + - name: Install and Verify on macOS if: ${{ matrix.os == 'macos-latest' }} run: | R CMD INSTALL . From acfecb940cde2941c51b3c5f4d1322cea1e92b69 Mon Sep 17 00:00:00 2001 From: Dirk Eddelbuettel Date: Sat, 3 Jan 2026 13:51:35 -0600 Subject: [PATCH 3/6] Shorten and reduce to two --- .github/workflows/ci.yaml | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 253fc431..124651f8 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -16,8 +16,7 @@ jobs: include: - { name: container, os: ubuntu-latest, container: rocker/r2u4ci } - { name: macos, os: macos-latest, openmp: yes } - - { name: macos, os: macos-latest, openmp: no } - - { name: macos, os: macos-latest, openmp: alt } + #- { name: macos, os: macos-latest, openmp: no } #- { name: ubuntu, os: ubuntu-latest } @@ -31,24 +30,24 @@ jobs: - name: Setup uses: eddelbuettel/github-actions/r-ci@master + # - name: OpenMP for macOS + # if: ${{ matrix.os == 'macos-latest' && matrix.openmp == 'yes' }} + # run: | + # curl -fsSL https://raw.githubusercontent.com/coatless-shell/openmp/main/install-openmp.sh | bash -s -- --yes + # mkdir -p ~/.R + # cat < ~/.R/Makevars + # CPPFLAGS += -Xclang -fopenmp + # LDFLAGS += -lomp + # EOF + # cat ~/.R/Makevars + # echo -n "R CMD config CXX: "; R CMD config CXX + # echo -n "R CMD config CPPFLAGS: "; R CMD config CPPFLAGS + # echo -n "R CMD config LDFLAGS: "; R CMD config LDFLAGS + # echo -n "R CMD config SHLIB_CXXLD: "; R CMD config SHLIB_CXXLD + # echo -n "R CMD config SHLIB_CXXLDFLAGS: "; R CMD config SHLIB_CXXLDFLAGS + - name: OpenMP for macOS if: ${{ matrix.os == 'macos-latest' && matrix.openmp == 'yes' }} - run: | - curl -fsSL https://raw.githubusercontent.com/coatless-shell/openmp/main/install-openmp.sh | bash -s -- --yes - mkdir -p ~/.R - cat < ~/.R/Makevars - CPPFLAGS += -Xclang -fopenmp - LDFLAGS += -lomp - EOF - cat ~/.R/Makevars - echo -n "R CMD config CXX: "; R CMD config CXX - echo -n "R CMD config CPPFLAGS: "; R CMD config CPPFLAGS - echo -n "R CMD config LDFLAGS: "; R CMD config LDFLAGS - echo -n "R CMD config SHLIB_CXXLD: "; R CMD config SHLIB_CXXLD - echo -n "R CMD config SHLIB_CXXLDFLAGS: "; R CMD config SHLIB_CXXLDFLAGS - - - name: Alternate OpenMP for macOS - if: ${{ matrix.os == 'macos-latest' && matrix.openmp == 'alt' }} run: | curl -O https://mac.r-project.org/openmp/openmp-17.0.6-darwin20-Release.tar.gz sudo tar fvxz openmp-17.0.6-darwin20-Release.tar.gz -C / From 786f60bc4c0f9656247de72593430c82c4b3a674 Mon Sep 17 00:00:00 2001 From: Dirk Eddelbuettel Date: Sat, 3 Jan 2026 14:30:38 -0600 Subject: [PATCH 4/6] Refine openmp thread count setting at startup --- ChangeLog | 6 ++++ R/init.R | 21 ++++++++++++-- man/RcppArmadillo-package.Rd | 53 ++++++++++-------------------------- 3 files changed, 39 insertions(+), 41 deletions(-) diff --git a/ChangeLog b/ChangeLog index 506df3fc..3f949e5a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2026-01-03 Dirk Eddelbuettel + + * R/init.R: Expand openmp core count logic respecting Ncpus and + OMP_THREAD_LIMIT else setting all cores; add startup message + * man/RcppArmadillo-package.Rd: Document thread count setting options + 2025-12-16 Dirk Eddelbuettel * DESCRIPTION (Version, Date): RcppArmadillo 15.2.3-1 diff --git a/R/init.R b/R/init.R index 2c13619a..6dcb2fab 100644 --- a/R/init.R +++ b/R/init.R @@ -1,6 +1,6 @@ ## init.R: Startup ## -## Copyright (C) 2023 Dirk Eddelbuettel +## Copyright (C) 2023-2026 Dirk Eddelbuettel ## ## This file is part of RcppArmadillo. ## @@ -19,8 +19,23 @@ .pkgenv <- new.env(parent=emptyenv()) -.onLoad <- function(libname, pkgname) { - .pkgenv[["omp_threads"]] <- armadillo_get_number_of_omp_threads() # #nocov +.onLoad <- function(libname, pkgname) { # nocov start + ## simple fallback: 'Ncpus' (if set) or else all cpus seen by OpenMP + ncores <- getOption("Ncpus", armadillo_get_number_of_omp_threads()) + ## consider OMP_THREAD_LIMIT (cf Writing R Extensions), gets NA if envvar unset + ompcores <- as.integer(Sys.getenv("OMP_THREAD_LIMIT")) + ## keep the smaller value, omitting NA + ncores <- min(na.omit(c(ncores, ompcores))) + .pkgenv[["nb_threads"]] <- ncores + armadillo_throttle_cores(ncores) +} + +.onAttach <- function(libname, pkgname) { + if (interactive()) { + packageStartupMessage("RcppArmadillo ", packageVersion("RcppArmadillo"), + " using ", .pkgenv[["nb_threads"]], " cores. See ", + "'help(\"RcppArmadillo-package\")' for details.") + } } ##' Throttle (or Reset) (Rcpp)Armadillo to Two Cores diff --git a/man/RcppArmadillo-package.Rd b/man/RcppArmadillo-package.Rd index 75730361..e57d7ffa 100644 --- a/man/RcppArmadillo-package.Rd +++ b/man/RcppArmadillo-package.Rd @@ -3,12 +3,8 @@ \alias{RcppArmadillo} \alias{RcppArmadilloExample} \docType{package} -\title{ - R and Armadillo Integration -} -\description{ - The package brings the power of Armadillo to R. -} +\title{R and Armadillo Integration} +\description{The package brings the power of Armadillo to R.} \section{Armadillo}{ \code{Armadillo} is a C++ linear algebra library, aiming towards a good balance between speed and ease of use. @@ -28,45 +24,28 @@ Armadillo has been primarily developed at NICTA (Australia) by Conrad Sanderson, with contributions from around the world. } - \section{RcppArmadillo}{ \code{RcppArmadillo} acts as a bridge between \code{Rcpp} and \code{Armadillo}, allowing the programmer to write code using Armadillo classes that integrate seemlessly with \code{R} via \code{Rcpp}. } - \section{Using RcppArmadillo}{ The simplest way to get started is to create a skeleton of a package using \code{RcppArmadillo}. This can be done conveniently by the \code{\link{RcppArmadillo.package.skeleton}} function. - - The important steps are - \itemize{ - \item Include the \code{RcppArmadillo.h} header file, which also includes - \code{armadillo.h}. - \item Import Rcpp, and LinkingTo Rcpp and RcppArmadillo by adding these lines to - the DESCRIPTION file: - - \preformatted{ - Imports: Rcpp (>= 0.11.0) - LinkingTo: Rcpp, RcppArmadillo - } - - \item Link against the BLAS and LAPACK libraries, by adding this line - in the \code{Makevars} and \code{Makevars.win} files: - \preformatted{PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) } - } } - -\section{Support}{ - Please use the Rcpp-devel mailing list on r-forge - for questions about RcppArmadillo (subscribe first). - \url{https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel} - +\section{Threading}{ + The Armadillo library can take advantage of OpenMP to execute computations in + parallel via multi-threaded code. The number of cores uses can be set (or + retrieved) explicitly via helper functions \code{armadillo_get_number_of_omp+threads()} and + \code{armadillo_set_number_of_omp_threads()}. A default value is stored at package startup; it + recognises R option value \code{Ncpus} and environment variable + \code{OMP_THREAD_LIMIT}. Additional helper functions + \code{armadillo_throttle_cores()} and \code{armadillo_reset_cores()} are + available to (temporarily) lower the number of cores uses and to reset to + the package default value set at startup. } - - \author{ For RcppArmadillo: Dirk Eddelbuettel, Romain Francois, Doug Bates and Binxiang Ni @@ -87,9 +66,7 @@ "RcppArmadillo: Accelerating R with high-performance C++ linear algebra", Computational Statistics and Data Analysis, 2014, 71, March, pages 1054-1063, \doi{10.1016/j.csda.2013.02.005}. -) - } -\keyword{ package } -\keyword{ programming } -\keyword{ interface } +\keyword{package} +\keyword{programming} +\keyword{interface} From 60a56ee1603e21bc37484161ed74a8b21cadfce8 Mon Sep 17 00:00:00 2001 From: Dirk Eddelbuettel Date: Sat, 3 Jan 2026 14:37:46 -0600 Subject: [PATCH 5/6] Agree on name for thread count environment-stored variable --- R/init.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/init.R b/R/init.R index 6dcb2fab..b18c3085 100644 --- a/R/init.R +++ b/R/init.R @@ -26,14 +26,14 @@ ompcores <- as.integer(Sys.getenv("OMP_THREAD_LIMIT")) ## keep the smaller value, omitting NA ncores <- min(na.omit(c(ncores, ompcores))) - .pkgenv[["nb_threads"]] <- ncores + .pkgenv[["omp_threads"]] <- ncores armadillo_throttle_cores(ncores) } .onAttach <- function(libname, pkgname) { if (interactive()) { packageStartupMessage("RcppArmadillo ", packageVersion("RcppArmadillo"), - " using ", .pkgenv[["nb_threads"]], " cores. See ", + " using ", .pkgenv[["omp_threads"]], " cores. See ", "'help(\"RcppArmadillo-package\")' for details.") } } From aca4443efcbb01cc9baafebeb72472f3b464c613 Mon Sep 17 00:00:00 2001 From: Dirk Eddelbuettel Date: Sat, 3 Jan 2026 14:43:10 -0600 Subject: [PATCH 6/6] And a #nocov end tag cleanup --- R/init.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/init.R b/R/init.R index b18c3085..58d952c7 100644 --- a/R/init.R +++ b/R/init.R @@ -52,4 +52,4 @@ armadillo_throttle_cores <- function(n = 2) { armadillo_reset_cores <- function() { n <- .pkgenv[["omp_threads"]] armadillo_set_number_of_omp_threads(n) -} +} # nocov end