Azzera filtri
Azzera filtri

Compiled mex file raises memory issue

2 visualizzazioni (ultimi 30 giorni)
Jeff Boker
Jeff Boker il 28 Nov 2022
Modificato: Jeff Boker il 28 Nov 2022
I am trying to run the code that is implemented on this github post. I was able to compile the inter_layer.c code into inter_layer.mexmaci64. There is a function that calls inter_layer and it raises the following error:
Error using inter_layer
Requested array exceeds the maximum possible variable size.
Error in comp_res (line 75)
[tmp_pair]=inter_layer(dflt_ht(l),dflt_ht(l+1),tmp7, ...
The inter_layer(...) function above takes the following parameters: dflt_ht(l) is 1x8 double, tmp7 is 188x150 double, d_inter_mn is 7x150 double, d_inter_std is 7x150 double, thk is 1x8 double, w2b is 1x7 double, d_inter_max is 7x150 double.
I have been trying to figure out what inside of inter_layer.c is causing it to produce an array to exceeds memory:
#include <math.h>
#include <matrix.h>
#include <mex.h>
#define pair(i,j,n) pair[(i)+((j)+(n)*thk2)*thk1]
#define tmp(k,n) tmp[(k)+(n)*sz1]
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
/* nlhs:no of outputs=1(pair); nrhs=6(base_ht1, base_ht2, tmp, d_mn, d_std,thk) */
/* plhs[0]=> output "pair" */
/* prhs[0]=base_ht1, prhs[1]=base_ht2, prhs[2]=tmp, prhs[3]=d_mn, prhs[4]=d_std prhs[5]=thk */
/* declare variables for input and output */
mxArray *pair_out, *base_ht1_in, *base_ht2_in, *tmp_in, *d_mn_in, *d_std_in, *thk_in1,*thk_in2,*b_in, *d_min_in, *d_max_in;
double *pair, *base_ht1_ptr,*base_ht2_ptr,*tmp,*d_mn,*d_std,*thk_ptr1,*thk_ptr2, *bptr,*d_min,*d_max;
const mwSize *dims1;
int dims[3],sz1,sz2;
double base_ht1, base_ht2,avg,dst, b;
int n,i,j,ri,rj,k,thk1,thk2;
/* associate inputs */
base_ht1_in = mxDuplicateArray(prhs[0]);
base_ht2_in = mxDuplicateArray(prhs[1]);
tmp_in = mxDuplicateArray(prhs[2]);
d_mn_in=mxDuplicateArray(prhs[3]);
d_std_in=mxDuplicateArray(prhs[4]);
thk_in1=mxDuplicateArray(prhs[5]);
thk_in2=mxDuplicateArray(prhs[6]);
b_in=mxDuplicateArray(prhs[7]);
d_min_in=mxDuplicateArray(prhs[8]);
d_max_in=mxDuplicateArray(prhs[9]);
/* figure out dimensions */
dims1 = mxGetDimensions(prhs[2]);
sz1=(int)dims1[0];
sz2=(int)dims1[1];
/* get the arrays */
base_ht1_ptr=mxGetPr(base_ht1_in);
base_ht2_ptr=mxGetPr(base_ht2_in);
tmp=mxGetPr(tmp_in);
d_mn=mxGetPr(d_mn_in);
d_std=mxGetPr(d_std_in);
thk_ptr1=mxGetPr(thk_in1);
thk_ptr2=mxGetPr(thk_in2);
base_ht1=base_ht1_ptr[0];
base_ht2=base_ht2_ptr[0];
thk1=(int)thk_ptr1[0];
thk2=(int)thk_ptr2[0];
bptr=mxGetPr(b_in);
b=bptr[0];
d_min=mxGetPr(d_min_in);
d_max=mxGetPr(d_max_in);
/*associate outputs */
dims[0]=thk1;
dims[1]=thk2;
dims[2]=sz2;
pair_out=plhs[0]=mxCreateNumericArray(3, dims, mxDOUBLE_CLASS,mxREAL);
pair = mxGetPr(pair_out);
for(n=0;n<sz2;n++)
{
/*printf("%lf %lf \n",d_min[n],d_max[n]);*/
for(i=0;i<thk1;i++)
{
for(j=0;j<thk2;j++)
{
ri=i+base_ht1;
rj=j+base_ht2;
if((rj-ri)<d_min[n] || (rj-ri)>d_max[n])
{
pair(i,j,n)=-999999;
}
else
{
avg=0;
for(k=ri; k<=rj;k++)
{
avg=avg+tmp(k,n);
}
/* dst=sqrt((((rj-ri)-d_mn[n])*((rj-ri)-d_mn[n]))/d_std[n]); */
/* lth1=(exp(- 0.5 * (lth1' - mn(n(idx)) ./ sigma(n(idx))) .^ 2) ./ (sigma(n(idx)) *2* sqrt(2 * pi)))'; */
dst=exp(-0.5*pow(((((double)(rj-ri))-d_mn[n])/d_std[n]),2));
avg=avg/(rj-ri+1);
pair(i,j,n)=avg+b*dst;
/* printf("\n %lf \t %lf ",avg, dst); */
}
}
}
}
}
  2 Commenti
Steven Lord
Steven Lord il 28 Nov 2022
What are the values with which you call this MEX-file on line 75 of comp_res when the error occurs? If you're not sure set a breakpoint on that line or set an error breakpoint.
Jeff Boker
Jeff Boker il 28 Nov 2022
dflt_ht(l) is 1x8 double,
tmp7 is 188x150 double,
d_inter_mn is 7x150 double,
d_inter_std is 7x150 double,
thk is 1x8 double,
w2b is 1x7 double,
d_inter_max is 7x150 double.

Accedi per commentare.

Risposte (1)

Jan
Jan il 28 Nov 2022
Modificato: Jan il 28 Nov 2022
Avoid using int as dimensions. Call mxCreateNumericArray as explained in the documentation:
mxArray *mxCreateNumericArray(mwSize ndim, const mwSize *dims,
mxClassID classid, mxComplexity ComplexFlag);
So change
int dims[3],sz1,sz2;
and the corresponding (int) casts to (mwSize) and try it again.
By the way:
thk_in1 = mxDuplicateArray(prhs[5]);
thk_ptr1 = mxGetPr(thk_in1);
thk1 = (int) thk_ptr1[0];
is a lot of clutter. Leaner:
thk1 = (mwSize) mxGetScalar(prhs[5]);

Categorie

Scopri di più su Write C Functions Callable from MATLAB (MEX Files) in Help Center e File Exchange

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by