How does a mex function work ?

조회 수: 3 (최근 30일)
Mayssa
Mayssa 2017년 9월 26일
편집: Mayssa 2017년 9월 28일
I am not that familiar with mex functions (nor C coding) and I need to understand a code in which the author is using one. It is about simplex projection (a method of resolution in optimization). Anyways, in the script code, the function is called this way:
ad = projsplx_c(-ad')';
it is clear that it needs one variable where ad is a matrix of size NxN-1 but when I go to the function script, I got a little confused, I see a lot of input variables and I can't see the relation between the calling and the definition of the function:
#include<math.h>
#include"mex.h"
Void mexFunction(int nlhs, mxArray*plhs[], int nrhs, const mxArray*prhs[])
{
int nulDims,m,n,k,d,npos,ft;
const mwSize*dims;
double *y,*s,*x,*vs;
double sumResult = -1, tmpValue, tmax,f,lamda_m;
dims= mxGetDimension(prhs[0]);
m= dims[0];
n = dims[1] ;
y = mxGetPr(prhs[0])
/*set the output pointer to the output matrix*/
Plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL);
x=mxGetPr(plhs[0])
s =(double*)calloc(m,sizeof(double));
vs=(double*)calloc(m,sizeof(double));
for(k=0;k<n;k++){
/*s=sort(y,asecnd);*/
double means =0;
double mins = 100000;
for(j=0;j<m;j++){
s[j]=y[j+k*m];
means+=s[j];
mins=(mins>s[j])? s[j]:mins;
}
for(j=0;j<m;j++){
s[j]-=(means-1)/m;
}
ft=1;
if(mins<0){
f=1;
lamda_m=0;
while(fabs(f)>1e-10){
npos=0;
f=0;
for(j=0;j<m;j++){
vs[j]=s[j]-lamda_m;
if (vs[j]>0){
npos+=1;
f=+vs[j];
}
}
lamda_m+=(f-1)/npos;
if (ft>100){
for(j=0;j<=m-1;j++){
x[j=k*m]=(vs[j]>0)?vs[j]:0;
}
break;
}
ft+=1;
}
for(j=0;j<=m-1;j++){
x[j+k*m]=(vs[j]>0)?vs[j]:0;
}
}
else{
for(j=0;j<=m-1;j++){
x[j+k*m]=s[j];
}
}
}
}

답변 (2개)

Jan
Jan 2017년 9월 26일
The C code will not work. Therer is no "Const" or "Void" qualifier, no "For" loop, no "Int" type and no "Plhs" variable: C is case-sensitive and only lower case charaters are required here.
The arrays allocated by calloc are neither replied, nor freed after the calculations. Therefore the code will leak memory: With each call more RAM is blocked until you restart the computer.
The indentation of the code looks very confusing. Opening it in the editor and hitting "Ctrl-I" will fix this to improve the readability.
Considering the missing documentation and the bunch of typos, I consider this code as junk. Ask the author for a working version before you try to understand, what it should do.
  댓글 수: 5
James Tursa
James Tursa 2017년 9월 26일
편집: James Tursa 2017년 9월 26일
Could this be what problem? You haven't stated that you were having a problem with the code, or compiling the code. Thus far you have only stated that you were trying to understand the code. Certainly missing headers in a C/C++ file is a real problem if you are compiling the code. (e.g., the stdlib.h header is missing for the calloc function). But what are you currently trying to do?
Jan's comments are all still valid. This code will not compile unless there are macros up front defining Int, Const, Void, Plhs. And the calloc memory is leaked until MATLAB is restarted. And the indenting style and use (or non-use) of spaces is horrible. Also, the only helpful comment in the code is this:
/*s=sort(y,asecnd);*/
So we would have to presume that that is what the code is attempting to do ... an equivalent of a column-based ascending sort. But I'm guessing nobody on this forum will be taking the time to go through that code line-by-line to see if everything is OK and matches the comment.
You say the pre-compiled mex routine works for you, so can you run some tests yourself to see if the results match that comment?
Mayssa
Mayssa 2017년 9월 27일
편집: Mayssa 2017년 9월 27일
It should work for you now, I updated the function code. it was my mistake, I did modify it unpurposely.

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


dpb
dpb 2017년 9월 26일
편집: dpb 2017년 9월 26일
The author should have written the help information into the associated m-file that calls the mex function or at least commented the source file more fully.
But, the input array in the function above is y locally in the function --
Void mexFunction(int nlhs, mxArray*plhs[], int nrhs, const mxArray*prhs[])
Int nulDims,m,n,k,d,npos,ft;
Const mwSize*dims;
double *y,*s,*x,*vs;
double sumResult = -1, tmpValue, tmax,f,lamda_m;
dims= mxGetDimension(prhs[0]);
m= dims[0];
n = dims[1] ;
y = mxGetPr(prhs[0])
The above preamble code associates the pointer to the RHS to the variable y which is passed by reference.
Then just past that the output variable is allocated and set to the LH return variable pointer--the author did comment this part
/*set the output pointer to the output matrix*/
Plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL);
x=mxGetPr(plhs[0])
You really don't need to know anything about the internals of the mex function to use it; just call it as any other function and go on with your business (assuming, of course, it is coded correctly as far as algorithm or you don't need to make it do something in addition to or different from what it does currently.
The other variables are just things like the dimensions of the passed array that are hidden variables passed in the background transparently to the user in m-files but the details that the function needs to operate on whatever data is passed.

카테고리

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