Here is an example of a typical calculation that may get one in trouble. Suppose you have a function that takes two matrices from R, converts them into Armadillo matrices and passes these functions to other functions to do some repetitive calculations. For example, imagine passing transition and emission probability matrices to execute the forward-backward algorithm on multiple realizations of the same hidden Markov model.
Below you see my C++ code, saved in the file "matrix_code.cpp" that has two sets of functions: a) doStuff1(), twoMatrices1(); b) doStuff2(), twoMatrices2(). The functions twoMatrices1() and twoMatrices2() are exported to R via Rcpp attributes. The twoMatrices1() calls doStuff1() 100 times, while twoMatrices2() calls doStuff2() 100 times. The only difference between the two sets of functions is in that doStuff1() passes two Armadillo matrices by value, which requires copying these matrices. In contrast, doStuff2() passes Armadillo matrices by reference without calling copy-constructor under the hood.
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
#include <Rcpp.h>
using namespace Rcpp;
// First passing Armadillo matrices by value
void doStuff1(arma::mat c, arma::mat d){
double x = c(1,1) + d(1,1);
}
// [[Rcpp::export]]
void twoMatrices1(NumericMatrix A, NumericMatrix B) {
arma::mat M1(A.begin(), A.nrow(), A.ncol(), false);
arma::mat M2(B.begin(), B.nrow(), B.ncol(), false);
for (int i = 0; i < 100; i++){
doStuff1(M1, M2);
}
}
// Now passign Armadillo matrices by reference
void doStuff2(const arma::mat& c, const arma::mat& d){
double x = c(1,1) + d(1,1);
}
// [[Rcpp::export]]
void twoMatrices2(NumericMatrix A, NumericMatrix B) {
arma::mat M1(A.begin(), A.nrow(), A.ncol(), false);
arma::mat M2(B.begin(), B.nrow(), B.ncol(), false);
for (int i = 0; i < 100; i++){
doStuff2(M1, M2);
}
}
Now I am going to call twoMatrices1() and twoMatrices2() from R, using rbenchmark package to compare their speed. I am going to use fairly large matrices (100x100), so the cost of moving them around is significant.
library(RcppArmadillo) library(rbenchmark) sourceCpp("matrix_code.cpp") x = matrix(1,100,100) y = matrix(1,100,100) res <- benchmark(twoMatrices1(x, y), twoMatrices2(x, y), columns = c("test", "replications", "elapsed", "relative"), order="relative", replications=1000) print(res) test replications elapsed relative 2 twoMatrices2(x, y) 1000 0.006 1.000 1 twoMatrices1(x, y) 1000 0.829 138.167
As you can see, passing two 100x100 Armadillo matrices by value is more than 100 times slower than passing these matrices by reference. Perhaps not a very surprising outcome, but an important one to keep in mind when porting R code to C++.
No comments:
Post a Comment