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

Sparse Matrix Multiplication

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

Sparse Matrix Multiplication

Fri Apr 25, 2014 8:49 pm
Dear Eigen community,

I am a new guy using Eigen and so far I am really satisfied using it, because I have had a good performance using the Full-Matrix module compared to Matlab. However, this is not the same if I use The Sparse Module. I have tried using MKL (9.38 s)and other libraries in order to improve my performance and I have got a worse performance than Matlab (0.82 s). I would like to know what I am doing wrong, please find bellow the code I am employing.

If somebody can help me, I would really appreciate it!

Thanks for taking time in answering,

Code: Select all
#include <cstdlib>
#include <sstream>
#include <fstream>
#include <iostream>

#include <ctime>

#include <eigen3/Eigen/Core>
#include <eigen3/Eigen/Sparse>

int main() {

    clock_t start, finish;
    const int dim = 10000;

  //First Test:
    Eigen::SparseMatrix<double> ccc(dim,dim);
    Eigen::SparseMatrix<double> ddd(dim,dim);
    ccc.reserve(2*dim);
    ddd.reserve(dim*dim);

    for(int i = 0; i < dim; i++)
        ccc.insert(i,i) = 1;

    for(int i = 1; i < dim; i++)
        ccc.insert(1,i) = 1;

    start = clock();

    ddd = Eigen::SparseMatrix<double>(ccc.transpose())*ccc;

    finish = clock();
    std::cout << "Time for Multiplication (a'*a) (seconds): " << ((double)(finish - start))/CLOCKS_PER_SEC << std::endl;
   

    return 0;

}
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Sparse Matrix Multiplication

Sat Apr 26, 2014 12:24 pm
Your example is quite unusual because the result is completely dense. With Eigen, you can use a different product algorithm by calling .pruned():

ddd = (ccc.transpose()*ccc).pruned();

It should be faster for your use case.
dkusanovic
Registered Member
Posts
2
Karma
0

Re: Sparse Matrix Multiplication

Sat Apr 26, 2014 12:58 pm
Dear ggael,

Thanks for your answer. Actually ".pruned()" IS FASTER than my method. I have got 2.47 (s) instead of 9.38 (s). However, I still don't understand why Matlab gets 0.82 (s); what does make Matlab so fast? is it possible to get that performance in Eigen?

P.S: That sort of multiplication, you will find it very often in Finite Element Analysis to assemble stiffness matrices and force vectors, in fact, they are not as the example I gave above, the idea is to check if at least I get the same performance like Matlab, because I want to change Matlab to C++, and Eigen is the most suitable library so far.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
It does not make sense to bench sparse algebra on non representative sparse structures.


Bookmarks



Who is online

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