<div dir="ltr">Thank you Dirk for the response.<div><br></div><div>I called RcppArmadillo::armadillo_get_number_of_omp_threads() on both machines and correctly see that machine A and B have 20 and 40 cores, respectively. I also see that calling the setter changes this value.</div><div><br></div><div>However, calling the setter does not seem to change the number of cores used on either machine A or B. I have updated my code example as below: the execution uses 20 cores on machine A and 1 core on machine B as before, despite my setting the number of omp threads to 5. Do you have any further hints?</div><div><br></div><div>library(RcppArmadillo)<br>library(Rcpp)<br><br>RcppArmadillo::armadillo_set_number_of_omp_threads(5)<br>print(sprintf("There are %d threads",<br>      RcppArmadillo::armadillo_get_number_of_omp_threads()))<br><br>src <-<br>r"(#include <RcppArmadillo.h><br><br>// [[Rcpp::depends(RcppArmadillo)]]<br><br>// [[Rcpp::export]]<br>arma::vec getEigenValues(arma::mat M) {<br>  return arma::eig_sym(M);<br>})"<br><br>size <- 10000<br>m <- matrix(rnorm(size^2), size, size)<br>m <- m * t(m)<br><br># This line compiles the above code with the -fopenmp flag.<br>sourceCpp(code = src, verbose = TRUE, rebuild = TRUE)<br>result <- getEigenValues(m)<br>print(result[1:10])<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Feb 23, 2024 at 12:53 PM Dirk Eddelbuettel <<a href="mailto:edd@debian.org">edd@debian.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
On 23 February 2024 at 09:35, Robin Liu wrote:<br>
| Hi all,<br>
| <br>
| Here is an R script that uses Armadillo to decompose a large matrix and print<br>
| the first 10 eigenvalues.<br>
| <br>
| library(RcppArmadillo)<br>
| library(Rcpp)<br>
| <br>
| src <-<br>
| r"(#include <RcppArmadillo.h><br>
| <br>
| // [[Rcpp::depends(RcppArmadillo)]]<br>
| <br>
| // [[Rcpp::export]]<br>
| arma::vec getEigenValues(arma::mat M) {<br>
|   return arma::eig_sym(M);<br>
| })"<br>
| <br>
| size <- 10000<br>
| m <- matrix(rnorm(size^2), size, size)<br>
| m <- m * t(m)<br>
| <br>
| # This line compiles the above code with the -fopenmp flag.<br>
| sourceCpp(code = src, verbose = TRUE, rebuild = TRUE)<br>
| result <- getEigenValues(m)<br>
| print(result[1:10])<br>
| <br>
| When I run this code on server A, I see that arma can implicitly leverage all<br>
| available cores by running top -H. However, on server B it can only use one<br>
| core despite multiple being available: there is just one process entry in top<br>
| -H. Both processes successfully exit and return an answer. The process on<br>
| server B is of course much slower.<br>
<br>
It is documented in the package how this is applied and the policy is to NOT<br>
blindly enforce one use case (say all cores, or half, or a magically chosen<br>
value of N for whatever value of N) but to follow the local admin setting and<br>
respecting standard environment variables.<br>
<br>
So I suspect that your machine 'B' differs from machine 'A' in this regards.<br>
<br>
Not that this is a _run-time_ and not _compile-time_ behavior. As it is for<br>
multicore-enabled LAPACK and BLAS libraries, the OpenMP library and basically<br>
most software of this type.<br>
<br>
You can override it, see<br>
  RcppArmadillo::armadillo_set_number_of_omp_threads<br>
  RcppArmadillo::armadillo_get_number_of_omp_threads<br>
<br>
Can you try and see if these help you?<br>
<br>
Dirk<br>
<br>
| Here is the compilation on server A:<br>
| /usr/local/lib/R/bin/R CMD SHLIB --preclean -o 'sourceCpp_2.so'<br>
| 'file197c21cbec564.cpp'<br>
| g++ -std=gnu++11 -I"/usr/local/lib/R/include" -DNDEBUG -I../inst/include<br>
| -fopenmp  -I"/usr/local/lib/R/site-library/Rcpp/include" -I"/usr/local/lib/R/<br>
| site-library/RcppArmadillo/include" -I"/tmp/RtmpwhGRi3/<br>
| sourceCpp-x86_64-pc-linux-gnu-1.0.9" -I/usr/local/include   -fpic  -g -O2<br>
| -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time<br>
| -D_FORTIFY_SOURCE=2 -g  -c file197c21cbec564.cpp -o file197c21cbec564.o<br>
| g++ -std=gnu++11 -shared -L/usr/local/lib/R/lib -L/usr/local/lib -o<br>
| sourceCpp_2.so file197c21cbec564.o -fopenmp -llapack -lblas -lgfortran -lm<br>
| -lquadmath -L/usr/local/lib/R/lib -lR<br>
| <br>
| and here it is for server B:<br>
| /sw/R/R-4.2.3/lib64/R/bin/R CMD SHLIB --preclean -o 'sourceCpp_2.so'<br>
| 'file158165b9c4ae1.cpp'<br>
| g++ -std=gnu++11 -I"/sw/R/R-4.2.3/lib64/R/include" -DNDEBUG -I../inst/include<br>
| -fopenmp  -I"/home/my_username/.R/library/Rcpp/include" -I"/home/ my_username<br>
| /.R/library/RcppArmadillo/include" -I"/tmp/RtmpvfPt4l/<br>
| sourceCpp-x86_64-pc-linux-gnu-1.0.10" -I/usr/local/include   -fpic  -g -O2  -c<br>
| file158165b9c4ae1.cpp -o file158165b9c4ae1.o<br>
| g++ -std=gnu++11 -shared -L/sw/R/R-4.2.3/lib64/R/lib -L/usr/local/lib64 -o<br>
| sourceCpp_2.so file158165b9c4ae1.o -fopenmp -llapack -lblas -lgfortran -lm<br>
| -lquadmath -L/sw/R/R-4.2.3/lib64/R/lib -lR<br>
| <br>
| I thought that the -fopenmp flag should let arma implicitly parallelize matrix<br>
| computations. Any hints as to why this may not work on server B?<br>
| <br>
| The actual code I'm running is an R package that includes RcppArmadillo and<br>
| RcppEnsmallen. Server B is the login node to an hpc cluster, but the code does<br>
| not use all cores on the compute nodes either.<br>
| <br>
| Best,<br>
| Robin<br>
| _______________________________________________<br>
| Rcpp-devel mailing list<br>
| <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
| <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
<br>
-- <br>
<a href="http://dirk.eddelbuettel.com" rel="noreferrer" target="_blank">dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a><br>
</blockquote></div>