0
votes

I test SparseMatrix with eigen, it seem that when i new a 10*5billion SparseMatrix which include only a few nonzeros elements, it takes 50gb memory!

The demo code:

#include <Eigen/Core>
#include <Eigen/SparseCore>
#include <iostream>
using namespace std;

int main()
{
    typedef Eigen::SparseMatrix<double, 0, long int> SMatrixXd;
    cout << "tag1"<< endl;
    SMatrixXd sfeature(10, 5000000000);
    cout << "tag1 done" << endl;

    // load data
    typedef Eigen::Triplet<double, long int> T;
    std::vector<T> tripletList;
    tripletList.push_back(T(0, 1, 1.0));
    tripletList.push_back(T(0, 2, 2.0));
    tripletList.push_back(T(1, 3, 2.0));
    tripletList.push_back(T(2, 4, 2.0));
    tripletList.push_back(T(3, 5, 2.0));
    tripletList.push_back(T(4, 6, 2.0));
    tripletList.push_back(T(5, 7, 2.0));
    cout << "tag2 " << endl;
    sfeature.setFromTriplets(tripletList.begin(), tripletList.end());
    cout << "tag2 done" << endl;
    return 0;
}
2

2 Answers

0
votes

Look at how sparse matrices are stored to understand that in your case it needs to allocate an array of 5000000000 long int. In your case, simply use a RowMajor layout:

typedef Eigen::SparseMatrix<double, RowMajor, long int> SMatrixXd;

and the previous huge array will boil down to an array of 10 long int.

0
votes

Your matrix has 10 rows and 5000000000 columns. However, the maximum value that can be stored as "long int" (specified in typedef Eigen::SparseMatrix SmatrixXd ) is 2,147,483,647. As a result, the value of 5000000000 (the number of columns) is truncated. To which value exactly it is truncated depends on your computer, I guess (on mine it is truncated down to 705032704). This is the number of columns that EIGEN gets when SMatrixXd sfeature(10, 5000000000) is executed.

The range of values for Microsoft compilers (I use Visual C++) could be found here: https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx

Your compiler should have warned you about the truncation problem.

I tried running your example with __int64 or long long, but the column value value is still truncated to 705032704 (I run it through Debugger in Visual C++):

Eigen::SparseMatrix A(10, 5000000000); Eigen::SparseMatrix A(10, 5000000000);

It is is not the compiler problem, because if I set __int64 ival64 = 5000000000; long long ival_ll = 5000000000; the values are just fine.

Therefore, I doubt that your matrix could be process at all using EIGEN.