Why is my MATLAB Coder 2.1 (R2011b) generated MEX file slower than my MATLAB function?

11 次查看(过去 30 天)
My MATLAB code has a FOR loop that uses the FFT2 function. I generate a MEX file from this MATLAB code using MATLAB Coder. However the resulting MEX file is slower than the MATLAB code. Instead of a MEX file, I have also tried creating a standalone executable by compiling the generated C code in Visual Studio. This does not make any difference and the executable is still slower than the MATLAB function.
Considering that compiled code is faster than MATLAB on average, why am I observing a slowdown in this case? Are there any techniques to improve the performance of compiled code?

采纳的回答

MathWorks Support Team
编辑:MathWorks Support Team 2018-4-27
MATLAB Coder does not generally speed up MATLAB built-in functions like EIG, SVD, FFT, FFT2, FFTN, QR, LU, etc. In fact, the correct expectation is that MATLAB Coder generated MEX functions that are dominated by calculations with MATLAB built-in functions like these will be slightly slower than equivalent code running in MATLAB. This is because the MATLAB functions are compiled already, and sometimes they are even multi-threaded.
Specifically the FFT function in MATLAB (and thus the FFT2 function) is highly optimized for the PC. The generated C code on the other hand is more of a readable & portable C code and is not optimized for a particular platform. So the slowdown in this case is expected regardless of MEX or standalone compilation. As a general note, compiled code is not always faster than MATLAB and FFT is one of those cases.
However, starting in R2016a and R2017b, MATLAB Coder added the ability for the generated code to call LAPACK and FFTW respectively:
Using these will give you optimized linear algebra and FFT routines respectively that are near the performance of MATLAB.
For releases prior to R2016a, here are some options you can try for improving performance:
1) Turn off debugging for the compiler to enable optimizations
2) Turn off array bounds checking and Ctrl+C checking
Make sure you have turned off the Ensure Memory Integrity option, and probably also the Enable Responsiveness checks. This usually makes a huge difference. The integrity checks prevent the C compiler from generating efficient code for computationally-intensive loops.
The link below illustrates how to control the runtime checks in MATLAB coder:
3) Before R2017b, MATLAB Coder generated code for a simple radix-2 FFT algorithm. MATLAB uses FFTW. For large data (even moderately sized, actually), FFTW performs better than radix-2 FFT. To use FFTW before R2017b, you will need to make your FFT function extrinsic by adding the following line to your MATLAB code:
coder.extrinsic('fft2');
When running generated code in the MATLAB environment, calls to extrinsic functions transfer control from the generated code to MATLAB. MATLAB Coder does not compile or generate code for extrinsic functions. The overhead associated with data copying may be a bottleneck, but since you are running this code as a MEX file, 'coder.extrinsic' for the FFT2 function may help.
There is a section in our Help Documentation on accelerating MATLAB algorithms with MATLAB Coder. This link can be a useful reference for such situations:

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Algorithm Acceleration 的更多信息

产品


版本

R2011b

Community Treasure Hunt

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

Start Hunting!

Translated by