Code acceleration by mex-file

조회 수: 10 (최근 30일)
Marc Laub
Marc Laub 2020년 12월 2일
답변: Marc Laub 2020년 12월 3일
Hey,
I'am new to the mex-section, but I wanted to accelerate my code, more a very specific time consuming function.
To start with I wrote a little funtction to see how much faster this function as mex is. The same is done in a tutorial, bot somehow my mex code isn't as fast as the one in the tutorial. So my question is on what exactly does the speed up with mex functions depends on?
function N=addodd(L)
counter=1;
N=0;
while ne(counter,L)
if ne(mod(counter,2),0)
N=N+counter;
else
N=N;
end
counter=counter+1;
end
end
Above is my function, that I run in Matlab as you see it above, and as mex-function.
Here on my Laptop Matlab needs fpr N=10^8 about 9 seconds (same as in the tutorial). My mex function needs about 7 seconds, whereas it needs only 1 second in the tutorial. So what am I doing different?
Its not a function where memory should be the bootleneck. And on my much faster desktop pc I can run the MatLab code for same N in only 4 seconds, mex also needs 4sec here.
Why would my Laptop take the same time for the matlab code, but 7 times longer for the mex code??
Where is the secret?
Maybe somebody coukld help me with this.
Many thanks in advance
Best regards
  댓글 수: 2
James Tursa
James Tursa 2020년 12월 2일
편집: James Tursa 2020년 12월 2일
I don't know for sure what the MATLAB Coder will generate for your example. The results may be dependent on MATLAB version. Just for curiosity, how fast does this equivalent hand-written mex function run:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
long long L, counter = 1, N = 0;
if( nrhs != 1 || !mxIsNumeric(prhs[0]) || mxIsComplex(prhs[0]) ||
mxGetNumberOfElements(prhs[0]) != 1 ) {
mexErrMsgTxt("Need one numeric real scalar input");
}
L = (long long) mxGetScalar(prhs[0]);
while( counter != L ) {
if( counter & 1LL ) N += counter;
counter++;
}
plhs[0] = mxCreateDoubleScalar(N);
}
Om my machine:
>> tic;s=mextest(1e8);toc;disp(s)
Elapsed time is 0.071268 seconds.
2.5000e+15
>> tic;s=mextest(1e9);toc;disp(s)
Elapsed time is 0.687518 seconds.
2.5000e+17
>> tic;s=mextest(1e10);toc;disp(s)
Elapsed time is 6.807796 seconds.
6.5533e+18
Raghu Boggavarapu
Raghu Boggavarapu 2020년 12월 3일
mod(counter,2) is an expensive function when dealing with floating point inputs. Please modify the code as follows to see speed up in MEX execution time :
function N=loopTest(L)
counter=int32(1);
N=int32(0);
while ne(counter,L)
if ne(mod(counter,2),0)
N=N+counter;
else
N=N;
end
counter=counter+int32(1);
end
end
  • Avoid floating point arithmetic where unnecessary to speed up the execution.
  • Additionally turn off "Integrity Checks" and "Responsiveness Checks" in MEX configuration to speed up the execution time.

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

채택된 답변

Marc Laub
Marc Laub 2020년 12월 3일
Thanks, it was the double instead of integer problem.
I simply typed the code as in that tutorial but somehow in his tutorial his input und variables where directly interpreted as integers, whereas mine where doubles, even though none of us had declared them as int.

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by