gaofangshu
gaofangshu

Reputation: 41

Matrix-multiplication with RcppArmadillo: Why is it not faster?

I am trying to do some matrix-multiplication with RcppArmadillo. However, my code shows that it doesn't become faster with RcppArmadillo.

I'm using Windows_10_Pro with R 3.2.4, and RcppArmadillo 0.6.600.4.0

For example:

library(RcppArmadillo)
library(inline)

MCplus <- cxxfunction(signature(X_="numeric", Y_="numeric"),body ='
arma::mat X = Rcpp::as<arma::mat>(X_);
arma::mat Y = Rcpp::as<arma::mat>(Y_);
arma::mat ans = X * Y * X;
return(wrap(ans));
', plugin="RcppArmadillo")

A <- matrix(1:16000000,4000,4000)
C <- matrix(2:16000001,4000,4000)

R_M <- proc.time()
ans_R <- A%*%C%*%A # test with R
proc.time() - R_M

C_M <- proc.time()
ans_C <- MCplus(A,C) # test with RcppArmadillo
proc.time() - C_M

The R outputs:

user  system elapsed
106.75   0.24 106.98

And the RcppArmadillo outputs:

user  system elapsed
108.28   0.23 108.56 

Is there something can be improved?

Thanks in advance!

Upvotes: 1

Views: 2834

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368629

R itself farms this out to LAPACK/BLAS -- and so does code linked to R that calls via the LAPACK/BLAS. So yes, both approaches will run the same code and differences are only due to the small overhead.

There are many tutorials out there that tell you how to change your LAPACK libraries. This depends of course on the operating system. Start maybe with the R Installation and Administration manual and its appendices.

Upvotes: 6

Related Questions