RcppCore / RcppEigen

Rcpp integration for the Eigen templated linear algebra library
Other
110 stars 40 forks source link

make fastLm use mutliple threads #129

Closed waynelapierre closed 1 year ago

waynelapierre commented 1 year ago

Eigen uses multiple threads when the OpenMP compiler option is enabled, does that mean adding //[[Rcpp::plugins(openmp)]] to the file fastLm.cpp makes the function fastLm use multiple threads?

eddelbuettel commented 1 year ago

No -- a plugin for Rcpp is generally for use by sourceCpp() and alike.

But I just had a look at the upstream docs and played around a little. We can enable it during package build (which I would to make conditional on OpenMP being available, just as in RcppArmadillo):

modified   src/Makevars
@@ -1,10 +1,10 @@
 ## -*- mode: makefile; -*-

-PKG_CXXFLAGS = -I../inst/include
+PKG_CXXFLAGS = -fopenmp -I../inst/include

 ## With Rcpp 0.11.0 and later, we no longer need to set PKG_LIBS for
-## Rcpp as there is no user-facing library. 
-PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
+## Rcpp as there is no user-facing library.
+PKG_LIBS = -fopenmp $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)

 ## With R 3.1.0 or later, you can uncomment the following line to tell R to 

The adding a simple accessor:

@@ -41,3 +41,8 @@ Rcpp::IntegerVector eigen_version(bool single) {
 bool Eigen_SSE() {
     return Rcpp::wrap(Eigen::SimdInstructionSetsInUse());
 }
+
+// [[Rcpp::export]]
+int EigenNbThreads() {
+    return Eigen::nbThreads();
+}

lets us access the threads (here from my six-core hyperthreaded box):

> RcppEigen:::EigenNbThreads()
[1] 12
> 

But that is likely not a change I can make easily as every package using RcppEigen would all of a sudden get into trouble at CRAN for using more than two threads.

So something to be pondered and maybe added locally. I could just document the option of adding -fopenmp to the src/Makevars.

waynelapierre commented 1 year ago

Thanks for the clarification!

eddelbuettel commented 1 year ago

I am adding the new function to report threads, and add comments to src/Makevars.{cpp,R}. I think that's the best we can do for now.

One way to enable to multithreaded use would be via a more official 'opt-in', maybe an options() value or env.var. To be discussed.

I am closing this for now as this issue seems addressed.