Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setFromTriplets function gets segfault error #136

Open
yanzhaobiomath opened this issue Mar 15, 2024 · 5 comments
Open

setFromTriplets function gets segfault error #136

yanzhaobiomath opened this issue Mar 15, 2024 · 5 comments

Comments

@yanzhaobiomath
Copy link

yanzhaobiomath commented Mar 15, 2024

Hello,
I'm using setFromTriplets function to cast a triplet into a sparse matrix which is large. I initialized the sparse matrix with certain dimensions, and reserved the memory for it. But I still get segfault error, saying 'memory not mapped'.

The matrix is 80000*80000 with 50% non-zero elements. Do you have any hint about the reason?

Best,
Yan

@eddelbuettel
Copy link
Member

We do not have a hint, and cannot do anything with a reproducible example.

Also keep in mind that R has limits on vector length. That does affect the Matrix package and its sparse matrix representation.

@yanzhaobiomath
Copy link
Author

yanzhaobiomath commented Mar 17, 2024

Sorry, the file of triplets is too large to be uploaded here, therefore here I give an example which generates a triplet list and also undergoes the same problem:

#include <RcppEigen.h>
#include <string>
#include <Eigen/Dense>
using namespace Rcpp;


// [[Rcpp::depends(RcppEigen)]]

// ' @export
// [[Rcpp::export]]
Eigen::SparseMatrix<double> ComputeSNNasym(int n) {
    

    typedef Eigen::Triplet<double> Trip;
    std::vector<Trip> trp;

    int idx = 0;
    Eigen::VectorXi xcol(n);

    double overlapping = 0.35;

    int a = n/3*2;
    
     for (int i = 0; i < n; ++i){  //number of columns ?
        
        int id = 0;
        for (int j = 0; j < n; ++j){  // Iterate over rows

            int k = i-j;
            
            if(abs(k) < a){
                trp.push_back(Trip(j,
                               i,
                               overlapping));;
                idx++;
                if(idx == 2147483647){
                    Rcpp::Rcout << "overflowing..." << std::endl;
                }
                id++;
            }
        }
        xcol[i] = id;
    }
     Rcpp::Rcout << "overlapping is done..." << std::endl;


     double sp = (idx+0.0)/n/n;
     Rcpp::Rcout << "number of non-zeros is " << idx << std::endl;
     Rcpp::Rcout << "sparsity is " << sp << std::endl;


    Eigen::SparseMatrix<double> res(n, n);
    Rcpp::Rcout << "initialization is done..." << std::endl;
    res.reserve(xcol);
    Rcpp::Rcout << "reservation is done..." << std::endl;
    
    res.setFromTriplets(trp.begin(), trp.end());
     Rcpp::Rcout << "sparse is done..." << std::endl;

    return res;
}

You may get the same error when you call this function from R with setting n=80500.

I checked the number of nonzero elements in the matrix, it is more than 2^31, so it's overflowing.

But I got this segfault error in the step of setfromtriplets, which is within the cpp script. I'm trying to figure out the reason. Is RcppEigen library also limited by the vector length of 2^31?

@eddelbuettel
Copy link
Member

I am not sure if Eigen is limited but I can assure that R is. We are having the exact same issue in another projects -- the problem is as best as I can tell due to <i,j,x> vectors in standard COO notation. When forming a sparseMatrix object using the Matrix package, then the integer indexing for the vectors i, j and x is the constraint: the 2^31-1 you are aware of. A first guess would be that Eigen has the same problem.

So I am afraid I have no real fix to offer here.

@eddelbuettel
Copy link
Member

I know the spam64 package on CRAN explicitly choses a different (64-bit) index type to have larger vectors, but that of course is not integrated with Eigen.

@yanzhaobiomath
Copy link
Author

I see, thanks for sharing your experience. I will try to find a way to walk around this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants