Something strange occurs when I try to transfer matrices to mex-function. When matrices size is not big the mex-function perform well, but if size of my matrices about 500*500 and more matlab will crush without any message. The terminal reports about a segmentation fault.
#include "mex.h"#include <iostream>#include "CFullVec.h"double GAMMA, Mnuclon;int NR, NZ; void mexFunction(int nlhs, mxArray *plhs[], // output to ML int nrhs, const mxArray *prhs[]) // input to C++ {{ double *parGas, *parGrid, *parTime; size_t mrows, ncols; std::cout << "zdfgbDSFB" << std::endl; // check nb of gas parameters mrows = mxGetM(prhs[0]); ncols = mxGetN(prhs[0]); if(mrows!=2 || ncols!=1){ mexErrMsgIdAndTxt("MATLAB:mexcpp:nargin", "Incorrect vector of the gas parameters"); } // check nb of grid parameters mrows = mxGetM(prhs[1]); ncols = mxGetN(prhs[1]); if(mrows!=6 || ncols!=1){ mexErrMsgIdAndTxt("MATLAB:mexcpp:nargin", "Incorrect vector of the grid parameters"); } // check nb of time parameters mrows = mxGetM(prhs[2]); ncols = mxGetN(prhs[2]); if(mrows!=3 || ncols!=1){ mexErrMsgIdAndTxt("MATLAB:mexcpp:nargin", "Incorrect vector of the time parameters"); } parGas=mxGetPr(prhs[0]); double GAMMA = parGas[0]; double Mnuclon = parGas[1]; parGrid=mxGetPr(prhs[1]); NR = static_cast<int>( (parGrid[1]-parGrid[0])/parGrid[2] ) + 1; NZ = static_cast<int>( (parGrid[4]-parGrid[3])/parGrid[5] ) + 1; parTime=mxGetPr(prhs[2]); const int ST = static_cast<int>( parTime[0]/parTime[1] ); const int CVAR = static_cast<int>( parTime[0]/parTime[2] ); // check data dimensions for(int i=3; i<=3+7; i++){ mrows = mxGetM(prhs[i]); ncols = mxGetN(prhs[i]); //std::cout << i << ' ' << mrows << ' ' << ncols << std::endl; if(mrows!=NR+2 || ncols!=NZ+2){ // mexErrMsgIdAndTxt("MATLAB:mexcpp:nargin", "Incorrect array of initial data"); } } double *rho, *Vr, *Vp, *Vz, *Br, *Bp, *Bz, *T; // these pointers are used both to input and to output // assign pointers to input rho=mxGetPr(prhs[3]); Vr=mxGetPr(prhs[4]); Vp=mxGetPr(prhs[5]); Vz=mxGetPr(prhs[6]); Br=mxGetPr(prhs[7]); Bp=mxGetPr(prhs[8]); Bz=mxGetPr(prhs[9]); T=mxGetPr(prhs[10]); FullVec U[NR+2][NZ+2]; // something else....
The error happens in the last three lines. Also if the declaration of U[NR+2][NZ+2] is deleted the error may not happen. Therefore I think this error is associated with RAM, but the matrices take not more than 40-50 MB RAM and U[NR+2][NZ+2] takes about the same.
I use linux mint 64-bit. I add -largeArrayDims during compilation. Could you tell me, what is going wrong?
Best Answer