Conversion of R matrices to armadillo is really slow
Asked Answered
T

1

6

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

#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:

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...

Questions arising

  1. What's going on? Why is mat so much slower than NumericMatrix?
  2. 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?

(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!)

Tipperary answered 29/7, 2015 at 4:16 Comment(0)
C
10

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

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

// [[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
Curnin answered 29/7, 2015 at 5:14 Comment(1)
can you please take a short look at this similar question? I appreciate it.Pasqualepasqueflower

© 2022 - 2024 — McMap. All rights reserved.