将R矩阵转换为犰狳的速度非常慢

时间:2021-01-08 14:57:44

An Observation

For medium-size matrices, the overheads on passing matrices from R to C++ are massively slower for arma::mat types than for NumericMatrix types. Like taking around 250x as long. Here's a minimal example

对于中等大小的矩阵,从R到C ++传递矩阵的开销对于arma :: mat类型比对NumericMatrix类型要慢得多。喜欢长约250倍。这是一个最小的例子

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
using namespace arma;

// [[Rcpp::export]]
double test_nm( NumericMatrix X ) {
  return 0.0 ;
}

// [[Rcpp::export]]
double test_arma( mat X ) {
  return 0.0 ;
}

// [[Rcpp::export]]
double test_nm_conv( NumericMatrix X ) {
  mat X_arma = as<mat>( X ) ; 
  return 0.0 ;
}

Then, in R:

然后,在R:

XX <- matrix( runif( 10000 ), 2000, 50 )
microbenchmark( test_nm( XX ), test_arma( XX ), ( XX ) )

Unit: microseconds
               expr      min       lq      mean   median       uq      max neval
        test_nm(XX)    5.541   16.154   16.0781   17.577   18.876   48.024   100
      test_arma(XX) 1280.946 1337.706 1404.0824 1361.237 1389.476 3385.868   100
   test_nm_conv(XX) 1277.417 1338.835 1393.4888 1358.128 1386.101 4355.533   100

So just passing a matrix as an arma::mat type is around 250x slower than NumericMatrix. That's crazy! So...

因此,将矩阵作为arma :: mat类型传递的速度比NumericMatrix慢约250倍。太疯狂了!所以...

Questions arising

  1. What's going on? Why is mat so much slower than NumericMatrix?
  2. 这是怎么回事?为什么垫子比NumericMatrix慢得多?

  3. Is there a good way to deal with this? I've got a problem where I need to use an arma::mat for some fairly simple matrix algebra in a function that gets called a lot of times. I'm currently using arma types throughout, and my code is much slower than I expected (that's how I ended up cooking up the dumb examples above). A speed penalty of 250x is such a big deal that I'm about to rewrite large sections of code to use NumericMatrix types throughout. In fact, I might end up writing my own matrix multiplication function for NumericMatrix and abandon arma types altogether. But before I do, are there any better solutions?
  4. 有没有一个好方法来处理这个?我有一个问题,我需要在一个被调用很多次的函数中使用arma :: mat作为一些相当简单的矩阵代数。我目前正在使用arma类型,而且我的代码比我预期的要慢得多(这就是我最终编写上面的愚蠢示例)。 250x的速度惩罚是如此重要,我将重写大部分代码以使用NumericMatrix类型。事实上,我最终可能会为NumericMatrix编写自己的矩阵乘法函数,并完全抛弃arma类型。但在此之前,还有更好的解决方案吗?

(Although I guess another way to read this is not that arma::mat is slow to convert from R types, but that the NumericMatrix type is amazingly efficient!)

(虽然我想另一种方式来阅读这并不是说arma :: mat从R类型转换得很慢,但是NumericMatrix类型的效率非常高!)

1 个解决方案

#1


9  

I believe this creates a new Armadillo matrix then copies the contents of your numeric matrix.

我相信这会创建一个新的Armadillo矩阵,然后复制数字矩阵的内容。

To cast the NumericMatrix to type arma::mat, you should use the following:

要将NumericMatrix转换为类型为arma :: mat,您应该使用以下内容:

// [[Rcpp::export]]
double test_const_arma( const mat& X ) { 
  return 0.0 ;
}

Speed comparison on my machine:

我的机器上的速度比较:

microbenchmark( test_const_arma( XX ), test_nm( XX ), test_arma( XX ), test_nm_conv( XX ))
## Unit: microseconds
##                 expr    min     lq     mean  median      uq     max neval
##  test_const_arma(XX)  1.852  2.381  3.69014  2.7885  4.3490  11.994   100
##          test_nm(XX)  1.925  2.455  3.47679  2.8535  3.5195  21.222   100
##        test_arma(XX) 68.593 71.212 83.63055 73.4555 98.8070 278.981   100
##     test_nm_conv(XX) 68.700 70.983 80.55983 73.1705 82.2665 183.484   100

#1


9  

I believe this creates a new Armadillo matrix then copies the contents of your numeric matrix.

我相信这会创建一个新的Armadillo矩阵,然后复制数字矩阵的内容。

To cast the NumericMatrix to type arma::mat, you should use the following:

要将NumericMatrix转换为类型为arma :: mat,您应该使用以下内容:

// [[Rcpp::export]]
double test_const_arma( const mat& X ) { 
  return 0.0 ;
}

Speed comparison on my machine:

我的机器上的速度比较:

microbenchmark( test_const_arma( XX ), test_nm( XX ), test_arma( XX ), test_nm_conv( XX ))
## Unit: microseconds
##                 expr    min     lq     mean  median      uq     max neval
##  test_const_arma(XX)  1.852  2.381  3.69014  2.7885  4.3490  11.994   100
##          test_nm(XX)  1.925  2.455  3.47679  2.8535  3.5195  21.222   100
##        test_arma(XX) 68.593 71.212 83.63055 73.4555 98.8070 278.981   100
##     test_nm_conv(XX) 68.700 70.983 80.55983 73.1705 82.2665 183.484   100