Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed up RcppArmadillo: How to link to OpenBlas in an R package

Tags:

c++

r

rcpp

I am working on an R package which uses RcppArmadillo. I am trying to take advantage of faster matrix multiplication found in OpenBLAS. In the documentation of the C++ armadillo library, it says if we have OpenBLAS on our machine then Armadillo would use OpenBLAS instead of BLAS. However, when I compile my R package, I get something like this:

g++ -m64 -std=c++11 -shared -L/usr/lib64/R/lib -Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -o PackageTest.so class1.o class2.o class3.o class4.o class5.o class6.o class7.o RcppExports.o class8.o class9.o class10.o -L/usr/lib64/R/lib -lRlapack -L/usr/lib64/R/lib -lRblas -lgfortran -lm -lquadmath -L/usr/lib64/R/lib -lR

So it is compiling with the -lRlapack and -lRblas options. How can I properly modify the Makevars and Makevars.win files to have RcppArmadillo compile the package with the option -lopenblas? My attempt to solve this problem was to modify the Makevars file in the following way:

PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
PKG_CXXFLAGS =-fopenmp -std=c++11 -lopenblas
PKG_CXX1XFLAGS = $(PKG_CXXFLAGS)

The package did compile with -lopenblas, but is this the best way to do it?

like image 984
kolonel Avatar asked Oct 18 '22 09:10

kolonel


1 Answers

That is a problem with your RedHat installation which chose to rely on the internal LAPACK sources for R when installing R --- plus the fact that RcppArmadillo uses whatever R uses.

On my Debian/Ubuntu based machine, it happens differently. Ie for

R> library(Rcpp)
R> cppFunction("arma::mat foo(arma::mat x) { return x + x;} ", depends="RcppArmadillo", verbose=TRUE)

I get (inter alia)

g++ -Wl,-S -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions \
    -Wl,-z,relro -o sourceCpp_4.so file677111d81351.o \
    -fopenmp -llapack -lblas -lgfortran -lm -lquadmath \
    -L/usr/lib/R/lib -lR

and we see -llapack -lblas -lgfortran as expected.

like image 100
Dirk Eddelbuettel Avatar answered Nov 15 '22 06:11

Dirk Eddelbuettel