|
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];
}
}
}
}
}
"
|