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

SparseMatrix::insert() is slow

Tags: sparse sparse sparse
(comma "," separated)
jdumas
Registered Member
Posts
1
Karma
0

SparseMatrix::insert() is slow

Mon Feb 23, 2015 3:42 pm
Hi there,

I have a big sparse matrix that I want to initialize directly from its list of triplets. I have the list in the correct order (column-major by default), and I know the exact number of nonzeros. According to the documentation I can make use of the reserve+insert methods to do that efficiently. However this does not seem to work. Would it be possible to provide a public insertBack() method that is guaranteed to be O(1) and fails if given indices are not increasing as expected?

Sample code and matrix file provided below:

Code: Select all
#include <Eigen/Sparse>
#include <iostream>

using namespace std;
using namespace Eigen;

int main(void) {
        int r, c, nnz;
        cin >> r >> c >> nnz;
        SparseMatrix<double> m(r, c);
#if 1
        // Takes forever
        m.reserve(nnz);
        for (int k = 0; k < nnz; ++k) {
                int i, j, x;
                cin >> i >> j >> x;
                m.insert(i, j) = x;
        }
#else
        // Performances are okay
        vector<Triplet<double> > t;
        t.reserve(nnz);
        for (int k = 0; k < nnz; ++k) {
                int i, j, x;
                cin >> i >> j >> x;
        }
        m.setFromTriplets(t.begin(), t.end());
#endif
        return 0;
}


Matrix file:
http://s000.tinyupload.com/?file_id=56174212691750863358

Compiled with gcc 4.9.2 and eigen 3.2.4:
Code: Select all
g++ -O3 -I/usr/include/eigen3 main.cpp
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: SparseMatrix::insert() is slow

Mon Mar 02, 2015 1:25 pm
I filled a bug-report with a patch: http://eigen.tuxfamily.org/bz/show_bug.cgi?id=974


Bookmarks



Who is online

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