Registered Member
|
Hi, I am a new Eigen user. I use SparseLU solver to solve a linear system of size about 800x800. Most of the entries are zero. The factorization takes about 2.5s. In matlab, for this size of dense matrix only take less than 1s. There must be something wrong with my code. Anyone can help??
Eigen::SparseMatrix<double> mat(dq+du,dq+du); mat.setFromTriplets(tripletLocalMList->begin(), tripletLocalMList->end()); mat. makeCompressed(); // mat.reserve(Eigen::VectorXi::Constant(dq+du,3*(dp-1)+1)); // cout<<tripletLocalMList->size()<<endl; // for(j=0; j<tripletLocalMList->size(); j++){ // TRI T=(*tripletLocalMList)[j]; // mat.insert(T.row(), T.col()) = T.value(); // alternative: mat.coeffRef(i,j) += v_ij; // } // mat.makeCompressed(); cout<<"mat is sparse matrix of size: "<<mat.rows()<<"x"<<mat.cols()<<endl; tic(); Eigen::SparseLU<Eigen::SparseMatrix<double>, Eigen::AMDOrdering<int> > solver; solver.analyzePattern(mat); toc(); tic(); solver.factorize(mat); toc(); |
Registered Member
|
The sparse matrix mat is of size 864x864 with 21600 nonzero entries. The factorization take 2.07s.
|
Moderator
|
Make sure you compiled with compiler optimizations ON, for instance with -O2 -DNDEBUG with gcc or clang, or in "Release" mode with visual.
|
Registered Member
|
Thanks ggael. I add new option -O2 in the g++ . The program performs much better. However, it still take almost 0.4s which is much slower than matlab.
|
Moderator
|
0.4s seems to be too high, indeed. Could you export your matrix as explained here: http://eigen.tuxfamily.org/dox-devel/gr ... tml#title3 so that we can reproduce.
Anyway, for such a small matrix 800x800, better use a dense representation and PartialPivLU which takes 0.02s on my laptop with the devel branch and AVX/FMA. (0.04s with Eigen 3.2) |
Registered Member
|
Thank you very much for the help. I trying to factorize a sparse matrix of size 5324x5324 with 252890 nonzero entries. It take 0.06s to analyze pattern and 46s to factorize. I think this speed is unacceptable. I have output the matrix into a .dat file. How can I send you the data file for reproduction the computation?
|
Moderator
|
Your matrix has numerous explicit zeros. Removing them leads to 6s for the factorization with either SparseLU or SuperLU. Your matrix as a typical structure for which multifrontal algorithms are much faster than supernodal ones. So you might use Umfpack through Eigen's wrapper: http://eigen.tuxfamily.org/dox-devel/gr ... odule.html. It takes about 0.7s here.
|
Registered Member
|
If I trying to use umfpack through eigen, do I need to install the SuiteSparse package for UMFPACK? In fact, I have already tried UMFPACK in SuiteSparse. For my matrix it also take very long time to get the solution.
|
Registered Member
|
Another thing is what you mean I got numerous explicit zeros in the matrix. I share two files for you. One is in triplet format. I don't think there are numerous explicit zeros in the triplet. And the nonzero entries is about 0.8%. Another file is in dense matrix and I don't use this dense matrix to solve. I actually use the sparse matrix constructed from the triplet and SparseLU solver. So I expect it can be very fast. In matlab it taks about 12s to get the inverse of a random matrix of size 5324x5324. I tried Eigen::MatrixXd x =dmat.fullPivLu().solve(b); to solve this system where the matrix is used as a dense matrix. It takes about 240s which are much longer than matlab. How could this happen.
|
Moderator
|
Umfpack is what is used within MatLab. And yes, to use it within Eigen, you need to install Suitesparse and properly link to it and a fast blas implementation (Eigen provides one).
The triplet file contains numerous explicit zeros, maybe they are due to truncation when exporting the numerical values. Do not use fullPivLU which is excessively slow and overkill. x = dmat.lu().Solve(b) will do just fine (partial pivoting LU). It takes 5s on my laptop for your matrix. |
Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]