MATLAB: Using RAM by mex-function

cmexmex compilerram

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

I haven't looked over your code in any detail, but this line definitely raises a red flag:
FullVec U[NR+2][NZ+2];
U is a local array variable, so its memory will be allocated from the stack, not the heap. While 50MB is not a particularly large number when compared to the heap, it is a potentially huge number when compared to the stack memory available. I can easily see where this could work without issue for small sizes, but blow the stack and crash MATLAB for a 50MB array. The solution is to never allocate variables of anywhere near this size off of the stack ... always allocate off of the heap instead. That means you need to allocate the memory for U using mxMalloc or mxCalloc since that memory comes from the heap. And since you are using the double bracket syntax [ ][ ] for accessing the elements, you will either need to give that syntax up and do your own manual indexing with single bracket [ ] notation, or you will need to allocate U in two stages (one for the first [ ] stuff and one for the second [ ] stuff). Let me know if you need help doing that. E.g., see this link for some example code on using double bracket [ ][ ] syntax with heap allocated memory:
https://www.mathworks.com/matlabcentral/answers/309434-matlab-crashing-when-evaluating-mex