Mex and CUSP for sparse matrices Using mex to operate on sparse matrices in CUSP

Hi,
I am trying to use mex for operations on sparse matrices.
I get the matrices from MATLAB and then send it CUSP to operate on them.
However, whenever I try sending two matrices of equal dimension to this code,
MATLAB crashes with fatal segmentation violation.

Since MATLAB, sends the matrices in CSC form, I creating the view of its transpose so that
it becomes CSR format for CUSP.
I have attached the code with this post.

Kindly help with me with any hints, as to where am I going wrong.

Cheers.

Ok, so I have this code, which uses sparse matrices in CSC format coming from matlab. I send it to CUSP library and I want to do some operations on it.

However when it reaches to the stage where it tries to copy the matrix to device, matlab just crashes with seg fault. Here is the shorter version of code that I am using.

#include "mex.h"

#include<cusp/print.h>

#include<cusp/csr_matrix.h>

#include<cusp/coo_matrix.h>

#include<cusp/array2d.h>

#include<cusp/transpose.h>

#include<cusp/convert.h>

#include<cusp/copy.h>

#include<cusp/memory.h>

#include<cusp/multiply.h>

__host__ void cleanup()

{

        cudaDeviceReset();

}

void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray

*prhs[])

{

        /***********Matrix 1***********/

        double *A;

        mwIndex *jc,*ir;

        int m,n;

        A = mxGetPr(prhs[0]);

        m = mxGetM(prhs[0]);

        n = mxGetN(prhs[0]);

        ir = mxGetIr(prhs[0]);

        jc = mxGetJc(prhs[0]);

        int NNZa = jc[n];

/***********Matrix 2***********/

        double *B;

        mwIndex *jcb,*irb;

        int p,q;

        B = mxGetPr(prhs[1]);

        p = mxGetM(prhs[1]);

        q = mxGetN(prhs[1]);

        irb = mxGetIr(prhs[1]);

        jcb = mxGetJc(prhs[1]);

        int NNZb = jcb[q];

        /*************Copy to device CSR Matrix*******/

        cusp::csr_matrix<mwIndex,double,cusp::device_memory>At(n,m,NNZa);

        cusp::csr_matrix<mwIndex,double,cusp::device_memory>Bt(q,p,NNZb);

thrust::copy(jc,jc+n+1,At.row_offsets.begin());

        thrust::copy(ir,ir+NNZa,At.column_indices.begin());

        thrust::copy(A,A+NNZa,At.values.begin());

thrust::copy(jcb,jcb+q+1,Bt.row_offsets.begin());

        thrust::copy(irb,irb+NNZb,Bt.column_indices.begin());

        thrust::copy(B,B+NNZb,Bt.values.begin());

cusp::print(At);

        cusp::print(Bt);

        mexPrintf("Fine before going to device\n");

        cusp::csr_matrix<size_t,double,cusp::device_memory>Ct;

        cusp::multiply(Bt,At,Ct);

        cusp::print(Ct);

mexPrintf("Fine now!!\n");

        mexAtExit(cleanup);

}

Just after I print the statement “Fine before going to device\n”, matlab crashes with segmentation violation.

I am passing the CSC matrix such that it will be viewed as trasposed CSR matrix inside CUSP.

Can anybody help me to get rid of this problem ?

Any hints or gimmicks might help.

Thanks in advance.

Cheers.