Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RcppEigen svd is very slow [closed]

Did RcppEigen's JacobiSVD become slower with upgrading to 3.0 ? My library using RcppEigen is now working fast anymore.

> n<-1000
> m<-matrix(rnorm(n*n),n,n)

> unix.time(s1<-svd(m))       # R
   user  system elapsed 
 10.376   0.028  10.407 

> unix.time(s2<-svdArma(m))   # RcppArmadillo
   user  system elapsed 
 22.997   0.000  23.001 

> unix.time(s3<-svdEigen(m))  # RcppEigen
   user  system elapsed 
180.708   0.000 180.712 

here is a test code on R :

library(inline)

codeArma='
    arma::mat    m = Rcpp::as<arma::mat>(m_);

    arma::mat u;
    arma::vec s;
    arma::mat v;

    arma::svd(u,s,v,m); 
    return List::create( Rcpp::Named("u")=u,
                         Rcpp::Named("d")=s,
                         Rcpp::Named("v")=v );
'
svdArma <- cxxfunction(signature(m_="matrix"),codeArma, plugin="RcppArmadillo")

#-----------------------------------------------------------------------

codeEigen='
  const Eigen::Map<Eigen::MatrixXd> m (as<Eigen::Map<Eigen::MatrixXd> >(m_ ));

  Eigen::JacobiSVD <Eigen::MatrixXd>svd(m,
                   Eigen::ComputeThinU|Eigen::ComputeThinV);
  return List::create( Rcpp::Named("u")=svd.matrixU(),
                       Rcpp::Named("d")=svd.singularValues(),
                       Rcpp::Named("v")=svd.matrixV() );
'
svdEigen <- cxxfunction(signature(m_="matrix"), codeEigen, plugin="RcppEigen")

#------------------------------------------------------------------------
n<-1000
m<-matrix(rnorm(n*n),n,n)

system.time(s1<-svd(m))       # R
m1<-s1$u %*% diag(s1$d) %*% t(s1$v)
all.equal(m,m1)

system.time(s2<-svdArma(m))   # Armadillo
m2<-s2$u %*% diag(array(s2$d)) %*% t(s2$v)
all.equal(m,m2)

system.time(s3<-svdEigen(m))  # Eigen
m3<-s3$u %*% diag(s3$d) %*% t(s3$v)
all.equal(m,m3)

----------------------------------------------------------

like image 641
Jona Avatar asked Jan 27 '26 19:01

Jona


1 Answers

Switching to R 3.0.0 should not per see have an impact on how a package such as RcppEigen performs. If you saw a regression in performance, something else may be going on.

You can also try to ballpark things by compiling an SVD directly in C++ using Armadillo and / or Eigen (if you have them installed outside of R, and/or you may get the headers from the R packages used with some tinkering).

like image 68
Dirk Eddelbuettel Avatar answered Jan 30 '26 12:01

Dirk Eddelbuettel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!