This forum has been archived. All content is frozen. Please use KDE Discuss instead.

sparseLU slower than matlab

Tags: None
(comma "," separated)
AN_001
Registered Member
Posts
2
Karma
0

sparseLU slower than matlab

Sun Jan 26, 2014 11:37 pm
I ran the following code for sparseLU:

Code: Select all
#include <iostream>
#include <Eigen/Sparse>
#include <vector>
#include "boost/date_time/posix_time/posix_time.hpp"

int main(int argc,char* argv[]) {

   Eigen::VectorXd b(5), x(5);
   Eigen::SparseMatrix<double,Eigen::ColMajor> A(5,5);
   A.coeffRef(0,0) = 0.8;
   A.coeffRef(0,1) = 0.2;
   A.coeffRef(4,3) = 0.15;
   A.coeffRef(4,4) = 0.85;
   for(int i = 1; i < 4; ++i) {
      A.coeffRef(i,i-1) = 0.1;
      A.coeffRef(i,i) = 0.8;
      A.coeffRef(i,i+1) = 0.1;
   }
        A.makeCompressed();
   Eigen::SparseLU<Eigen::SparseMatrix<double,Eigen::ColMajor >> solver;
   solver.analyzePattern(A);
   solver.factorize(A);
   for(int i = 0; i < 5 ;i++) {
      b(i) = i;
   }
   boost::posix_time::ptime now1  = boost::posix_time::microsec_clock::local_time();
   x = solver.solve(b);
   std::cout << x << std::endl;
   boost::posix_time::ptime now2  = boost::posix_time::microsec_clock::local_time();
   boost::posix_time::time_duration diff2 = now2 - now1;
   std::cout << diff2.total_microseconds() << " micro seconds";
}

The code in matlab inv(A)*b took about 50 microseconds

The code in Eigen took 185 micro seconds! Is there a way I can speed up sparseLU ? Note I only timed the part in which the LU system is actually solved and displayed, the same as I did in matlab.
jitseniesen
Registered Member
Posts
204
Karma
2

Re: sparseLU slower than matlab  Topic is solved

Mon Jan 27, 2014 9:12 am
If you want to speed up the solver in this example, you could replace the sparse LU by a normal LU. Sparse algorithms will not perform well on 5x5 matrices where about half the entries are nonzero. If you are interested in larger matrices, I suggest you time them first and take it from there.
AN_001
Registered Member
Posts
2
Karma
0

Re: sparseLU slower than matlab

Mon Jan 27, 2014 11:43 am
Correct on a 1000 x 1000 matrix Eigen is 10 times faster than Matlab.


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot], Yahoo [Bot]