'Out of memory' mex file
1 view (last 30 days)
Show older comments
Two issues in the code snippet:
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]) {
const mwSize *dimsG = mxGetDimensions(prhs[1]);
unsigned int Nstates = dimsG[0];
unsigned int Nsamples = dimsG[1];
unsigned int Nmix = dimsG[2];
unsigned int ObsDim = mxGetM(prhs[0]);
if(Nmix == 0)Nmix=1;
const mwSize dims[]={ObsDim,Nmix,Nstates};
const mwSize dimsgam[]={Nstates,Nmix};
plhs[0] = mxCreateNumericArray(3,dims,mxDOUBLE_CLASS,mxREAL);
double* mu = mxGetPr(plhs[0]);
plhs[1] = mxCreateNumericArray(2,dimsgam,mxDOUBLE_CLASS,mxREAL);
double* gamsumRet = mxGetPr(plhs[1]);
double* X = mxGetPr(prhs[0]);
double* gamma = mxGetPr(prhs[1]);
for(unsigned int i=0;i<Nstates;++i)
for(unsigned int m=0;m<Nmix;++m){
double* sums= (double*)calloc(ObsDim,sizeof(double));
if(sums == NULL){
mexPrintf("\n No Memory Left");
break;
}
double gamsum = 0.0;
for(unsigned int j=0;j<Nsamples;++j){
for(unsigned int k=0;k<ObsDim;++k)
sums[k] = sums[k]+ X[ObsDim*j+k]*gamma[Nstates*j+Nstates*Nsamples*m+i];
gamsum = gamsum+gamma[Nstates*Nsamples*m+Nstates*j+i];
}
for(unsigned int k=0;k<ObsDim;++k)
mu[ObsDim*Nmix*i+ObsDim*m+k] = sums[k];
gamsumRet[Nstates*m+i]= gamsum;
free(sums);
}
}
Issue-1 :The code throws 'Out Of Memory' error every few runs. (Not every run). The code that calls it uses quite some memory. I wish to know if there's something I'm missing as far as memory management is concerned in this function.
Issue-2: The result of this code gets returned as a 3-D matrix to which I add another 3-D matrix with exact same dimensions and initialised to zero. i.e,
mma = zeros(ObsDim,Nmix,Nstates);
mma = mma+<value-returned from function>
Sometimes this value is correctly added and quite a few other times the error- 'Array dimensions must match for binary array op.' is returned.
None of these problems occur if I use a matlab script(that does the same job) instead of this mex function
Please Help.
2 Comments
James Tursa
on 16 Apr 2015
Is MATLAB throwing an "Out Of Memory" error, or are you simply getting your "No Memory Left" message printed out?
Answers (0)
See Also
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!