Compiled mex file raises memory issue

조회 수: 2 (최근 30일)
Jeff Boker
Jeff Boker 2022년 11월 28일
편집: Jeff Boker 2022년 11월 28일
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
Steven Lord
Steven Lord 2022년 11월 28일
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 2022년 11월 28일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2022년 11월 28일
편집: Jan 2022년 11월 28일
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]);

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by