Timo Schmid
2013-Oct-26 07:55 UTC
[R] No speed effect by using RcppArmadillo compared to R in matrix operations
Hello, I am looking for a way to do fast matrix operations (multiplication, Inversion) for large matrices (n=8000) in R. I know R is not that fast in linear algebra than other software. So I wanted to write some code in C++ and incorporate this code in R. I have used the package RcppArmadillo, because a lot of people write that it is really fast in doing matrix algebra. So I have run a short example. See the code below. I was wondering that I got almost the same CPU time for the matrix algebra in my example. I expect that using C++ Code in R is faster than using the standard matrix operations in R. Is there a way to do matrix algebra in R faster as the standard command (e.g. %*%) using the Rcpp or RcppArmadillo packages? I would be happy about any idea or advice. Thanks in advance > library(Rcpp)> library(RcppArmadillo) > library(inline) > library(RcppEigen) > library(devtools) > > # Generation of the matrix > n=2000 > A<-matrix(rnorm(n^2,0,1), n,n) > > # Code in R > system.time(+ D<-A%*%A%*%A+A) user system elapsed 12.29 0.01 12.33> > # Code using RcppArmadillo > src <-+ ' + arma::mat X = Rcpp::as<arma::mat>(X_); + arma::mat ans = X * X * X + X; + return(wrap(ans)); + '> mprod6_inline_RcppArma <- cxxfunction(signature(X_="numeric"),+ body = src, plugin="RcppArmadillo")> > system.time(+ C<-mprod6_inline_RcppArma(X=A)) user system elapsed 12.30 0.08 12.40 [[alternative HTML version deleted]]
Jeff Newmiller
2013-Oct-26 08:44 UTC
[R] No speed effect by using RcppArmadillo compared to R in matrix operations
I think you don't have accurate information about the speed of R in performing linear algebra computations. It relies on standard numerical libraries for that work, so it is as fast as those libraries are (you are unlikely to beat even an unoptimized version of those libraries with your ad hoc code). You can investigate installing custom versions of those libraries (e.g. [1]), but most performance issues arise due to inefficient handling of data during preparation or post processing. [1] http://www.avrahamadler.com/2013/10/22/an-openblas-based-rblas-for-windows-64/ --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Timo Schmid <timo_schmid at hotmail.com> wrote:>Hello, > >I am looking for a way to do fast matrix operations (multiplication, >Inversion) for >large matrices (n=8000) in R. I know R is not that fast in linear >algebra than >other software. >So I wanted to write some code in C++ and incorporate this code in R. I >have used the >package RcppArmadillo, because a lot of people write that it is really >fast in >doing matrix algebra. So I have run a short example. See the code >below. >I was wondering that I got almost the same CPU time for the matrix >algebra in my >example. I expect that using C++ Code in R is faster than using the >standard >matrix operations in R. > >Is there a way to do matrix algebra in R faster as the standard command >(e.g. %*%) using >the Rcpp or RcppArmadillo packages? I would be happy about any idea or >advice. >Thanks in advance > > > > library(Rcpp) >> library(RcppArmadillo) >> library(inline) >> library(RcppEigen) >> library(devtools) >> >> # Generation of the matrix >> n=2000 >> A<-matrix(rnorm(n^2,0,1), n,n) >> >> # Code in R >> system.time( >+ D<-A%*%A%*%A+A) > user system elapsed > 12.29 0.01 12.33 >> >> # Code using RcppArmadillo >> src <- >+ ' >+ arma::mat X = Rcpp::as<arma::mat>(X_); >+ arma::mat ans = X * X * X + X; >+ return(wrap(ans)); >+ ' >> mprod6_inline_RcppArma <- cxxfunction(signature(X_="numeric"), >+ body = src, >plugin="RcppArmadillo") >> >> system.time( >+ C<-mprod6_inline_RcppArma(X=A)) > user system elapsed > 12.30 0.08 12.40 > > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
Andreas Recktenwald
2013-Oct-26 10:59 UTC
[R] No speed effect by using RcppArmadillo compared to R in matrix operations
Hi, another option if you're using Linux AND an Intel processor would be linking R against Intel MKL (Math Kernel Library). Under Linux you can get a (free) non-commercial licence for it. Here I'm using an Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz laptop processor with R 3.0.2 build with intel compilers and linked against Intel MKL 11 and get the following times:> set.seed(123) > n <- 2000 > A<-matrix(rnorm(n^2,0,1), n,n) > system.time(D<-A%*%A%*%A+A)User System verstrichen 1.480 0.004 1.482 PS: I'm using the sequential version of Intel MKL. Zitat von Timo Schmid <timo_schmid at hotmail.com>:> Hello, > > I am looking for a way to do fast matrix operations (multiplication, > Inversion) for > large matrices (n=8000) in R. I know R is not that fast in linear > algebra than > other software. > So I wanted to write some code in C++ and incorporate this code in > R. I have used the > package RcppArmadillo, because a lot of people write that it is > really fast in > doing matrix algebra. So I have run a short example. See the code below. > I was wondering that I got almost the same CPU time for the matrix > algebra in my > example. I expect that using C++ Code in R is faster than using the standard > matrix operations in R. > > Is there a way to do matrix algebra in R faster as the standard > command (e.g. %*%) using > the Rcpp or RcppArmadillo packages? I would be happy about any idea > or advice. > Thanks in advance > > > > library(Rcpp) >> library(RcppArmadillo) >> library(inline) >> library(RcppEigen) >> library(devtools) >> >> # Generation of the matrix >> n=2000 >> A<-matrix(rnorm(n^2,0,1), n,n) >> >> # Code in R >> system.time( > + D<-A%*%A%*%A+A) > user system elapsed > 12.29 0.01 12.33 >> >> # Code using RcppArmadillo >> src <- > + ' > + arma::mat X = Rcpp::as<arma::mat>(X_); > + arma::mat ans = X * X * X + X; > + return(wrap(ans)); > + ' >> mprod6_inline_RcppArma <- cxxfunction(signature(X_="numeric"), > + body = src, plugin="RcppArmadillo") >> >> system.time( > + C<-mprod6_inline_RcppArma(X=A)) > user system elapsed > 12.30 0.08 12.40 > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.