加速 RcppArmadillo:如何在 R 包中链接到 OpenBlas [英] Speed up RcppArmadillo: How to link to OpenBlas in an R package

查看:65
本文介绍了加速 RcppArmadillo:如何在 R 包中链接到 OpenBlas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用 RcppArmadillo 的 R 包.我正在尝试利用 OpenBLAS 中更快的矩阵乘法.在 C++ 犰狳库的 文档 中,它说如果我们的机器上有 OpenBLAS 那么Armadillo 将使用 OpenBLAS 而不是 BLAS.然而,当我编译我的 R 包时,我得到了这样的东西:

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

因此它使用 -lRlapack-lRblas 选项进行编译.如何正确修改 MakevarsMakevars.win 文件以让 RcppArmadillo 使用选项 -lopenblas 编译包?我试图解决这个问题是通过以下方式修改 Makevars 文件:

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)

该包确实使用 -lopenblas 编译,但这是最好的方法吗?

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

推荐答案

这是你的 RedHat 安装的一个问题,它在安装 R 时选择依赖 R 的内部 LAPACK 源 --- 加上 RcppArmadillo 使用任何 R 的事实使用.

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.

在我基于 Debian/Ubuntu 的机器上,情况有所不同.即为

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)

我明白(除其他外)

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

我们看到 -llapack -lblas -lgfortran 符合预期.

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

这篇关于加速 RcppArmadillo:如何在 R 包中链接到 OpenBlas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆