为什么 Rcpp 在使用矩阵乘法时比 R 慢?
why Rcpp is slower than R when using matrix multiply?
为了加速我的包,其中包括大量矩阵计算,我使用 Rcpp 来
重写所有代码。但是,某些功能甚至比以前更慢。我用microbenchmark分析,发现Rcpp中的矩阵乘法比较慢。
为什么会这样?
以及如何加速我的包裹?非常感谢。
Rcpp代码如下:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericMatrix mmult(const NumericMatrix& a, const NumericMatrix& b){
if (a.ncol() != b.nrow()) stop ("Incompatible matrix dimensions");
NumericMatrix out(a.nrow(),b.ncol());
NumericVector rm1, cm2;
for (int i = 0; i < a.nrow(); ++i) {
rm1 = a(i,_);
for (int j = 0; j < b.ncol(); ++j) {
cm2 = b(_,j);
out(i,j) = std::inner_product(rm1.begin(), rm1.end(), cm2.begin(), 0.);
}
}
return out;}
R代码如下:
X = matrix(rnorm(10*10,1),10,10)
Y = matrix(rnorm(10*10,1),10,10)
microbenchmark(
mmult(X,Y),
X%*%Y)
结果是:
Unit: microseconds
expr min lq mean median uq max neval
mmult(X, Y) 45.720 48.9860 126.79228 50.385 51.785 6368.512 100
X %*% Y 5.599 8.8645 12.85787 9.798 10.730 153.486 100
这是相反的,但 预期 结果来自所见 。这里 R 使用 BLAS 来完成所有繁重的工作,这些工作甚至可以并行工作。您正在使用朴素的矩阵乘法丢弃在 BLAS 库中完成的所有优化内存管理。
与其尝试重新发明矩阵乘法之类的低级内容,不如尝试使用类似 RcppArmadillo 的代码来实现更大的部分,它使用与 R 相同的 BLAS 库,而且(不仅!)提供一个方便的语法。
为了加速我的包,其中包括大量矩阵计算,我使用 Rcpp 来 重写所有代码。但是,某些功能甚至比以前更慢。我用microbenchmark分析,发现Rcpp中的矩阵乘法比较慢。 为什么会这样? 以及如何加速我的包裹?非常感谢。 Rcpp代码如下:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericMatrix mmult(const NumericMatrix& a, const NumericMatrix& b){
if (a.ncol() != b.nrow()) stop ("Incompatible matrix dimensions");
NumericMatrix out(a.nrow(),b.ncol());
NumericVector rm1, cm2;
for (int i = 0; i < a.nrow(); ++i) {
rm1 = a(i,_);
for (int j = 0; j < b.ncol(); ++j) {
cm2 = b(_,j);
out(i,j) = std::inner_product(rm1.begin(), rm1.end(), cm2.begin(), 0.);
}
}
return out;}
R代码如下:
X = matrix(rnorm(10*10,1),10,10)
Y = matrix(rnorm(10*10,1),10,10)
microbenchmark(
mmult(X,Y),
X%*%Y)
结果是:
Unit: microseconds
expr min lq mean median uq max neval
mmult(X, Y) 45.720 48.9860 126.79228 50.385 51.785 6368.512 100
X %*% Y 5.599 8.8645 12.85787 9.798 10.730 153.486 100
这是相反的,但 预期 结果来自所见
与其尝试重新发明矩阵乘法之类的低级内容,不如尝试使用类似 RcppArmadillo 的代码来实现更大的部分,它使用与 R 相同的 BLAS 库,而且(不仅!)提供一个方便的语法。