Thread Subject: Problem creating sparse matrix using mex-file

Subject: Problem creating sparse matrix using mex-file

From: Arne Vagren

Date: 12 Oct, 2009 13:32:03

Message: 1 of 2

Hello!

I have a problem trying to implement a mex function (attached the below). The purpose of the function is to assemble a sparse matrix given a sparsity pattern in the form of a sparse matrix created in matlab. However, at least for some values of the input data, the function crashes with the error message "Assertion failed: Forced Assertion at line 714 of file ".\memmgr\mem32aligned.cpp. Corrupted memory block found", or sometimes simply a segmentation fault. The problem seem to occur after the function is finished rather than inside the function.

Any help greatly appreciated.

Best Regards
Arne

"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    double *def = mxGetPr(prhs[1]);
    double *Bt, *dof;
    double *J;
    mwIndex i, j, k, e, *ir, *jc, row, col;
    mwSize m, n;
    mwSize nzmax = mxGetNzmax(prhs[0]);
    mwSize noElements = (mwSize) mxGetScalar(prhs[4]);
    mwSize noDOFs = (mwSize) mxGetScalar(prhs[5]);
       
    plhs[0] = mxCreateSparse(noDOFs,noDOFs,nzmax,mxREAL);
    
    J = mxGetPr(plhs[0]);
    ir = mxGetIr(plhs[0]);
    jc = mxGetJc(plhs[0]);
    
    // Copy ir and jc from sparsity pattern matrix
    memcpy(ir, mxGetIr(prhs[0]), nzmax*sizeof(mwIndex));
    memcpy(jc, mxGetJc(prhs[0]), (noDOFs+1)*sizeof(mwIndex));
    
    for(e=0; e<noElements; e++)
    {
         dof = mxGetPr(mxGetCell(prhs[2],e));
         Bt = mxGetPr(mxGetCell(prhs[3],e));
         m = mxGetM(mxGetCell(prhs[3],e));
         n = mxGetN(mxGetCell(prhs[3],e));
                    
         for(j = 0; j<n; j++)
         {
            for (i=0; i<m; i++)
            {
                 if (Bt[i+j*m]!=0.)
                 {
                       row = (mwIndex)dof[i]-1;
                       col = (mwIndex)dof[j]-1;
                       for (k = jc[col]; k<jc[col+1]; k++)
                       {
                           if (row == ir[k])
                               break;
                       }
                       J[k] += def[e]*Bt[i+j*m];
                 }
            }
         }
    }
}
"

Subject: Problem creating sparse matrix using mex-file

From: Arne Vagren

Date: 26 Oct, 2009 12:22:05

Message: 2 of 2

If anyones interested it turned out that the problem was not in the code that I posted,
but elsewhere when creating the dof array. The code should work fine, although it is not as efficient as it could be.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
mex Arne Vagren 12 Oct, 2009 09:34:11
sparse matrix Arne Vagren 12 Oct, 2009 09:34:11
mxcreatesparse Arne Vagren 12 Oct, 2009 09:34:11
rssFeed for this Thread

Contact us at files@mathworks.com