MATLAB Coder: C Version Much Slower Than Mex Version
25 次查看(过去 30 天)
显示 更早的评论
I used MATLAB Coder to turn my MATLAB code into C code and expected a great decrease in run time, but instead found that my C code runs much slower than both the MATLAB code and the mex code. Over 5 tests, I get the following:
- Tthe MATLAB code took 77.48 seconds on average.
- The mex version running in MATLAB took 46.27 seconds on average.
- The generated C code took 262.35 seconds on average.
All three versions of the code produces the exact same output, so I am confident that each version is working properly. I am running the MATLAB and mex versions in R2023a and the C version in Debian 11 (bullseye) in a docker container. The cmake command I am running is
cmake -DCMAKE_BUILD_TYPE=Release ..
and my understanding is that this enables the C compiler optimisations, but I'm unsure if there are further optimisations I can enable. Before specifying the build type as release, my C code ran about 90 seconds slower still (~350s), which is horrendous compared to the MATLAB/mex version.
These are the settings I am generating my code with:
The full settings I used are attached in config.mat but they are almost all just the default settings.
My understanding is that MATLAB does some computations in parallel automatically, while the generated C code doesn't, so I tried turning on automatic parallelisation in the generated code. This only made the code run slower, however. I'm not sure how the automatic paralellisation works, but when I manually tried to parallelise parts of my code, it also did not run faster, so I did not really expect this to improve anything.
I'm just really unsure why the C version runs so much slower than even the mex version, which I expected to be similar or worse than the C version. Are there settings I can tweak in Coder? Are there further optimisations I can enable on the Linux side of things? If this is normal behaviour, what is the explanation for the discrepency between C and mex?
采纳的回答
Harsh Sharma
2024-10-9
Hi Nyx,
There can be several reasons for the C code to be slower than MEX version or the MATLAB code depending on your exact code. MATLAB tends to outperform the generated C code when an application predominantly relies on pre-compiled binaries in MATLAB or on tasks that the JIT (Just-In-Time) compiler optimizes effectively.
When generating MEX code, Coder has access to the high-performance libraries used by MATLAB. You mentioned in the comments that you are using matrix decompositions and computing products with large matrices; it explains the discrepancy between C and MEX. When using MEX files, MATLAB can directly leverage highly optimized libraries like BLAS, LAPACK, or Intel's MKL for matrix operations. Also, MATLAB handles memory allocation and deallocation efficiently, whereas C requires manual management, which can introduce overhead and inefficiencies.
You mentioned that you tried to parallelize parts of your code but that only worsened the run time. This could be because the number of calculations within each parallel region are too small. Parallelization introduces overheads that can cancel the benefits of a parallel execution in such cases.
Finally, to improve the generated C code run time you can link the C code to optimized BLAS and LAPACK libraries. This can significantly speed up matrix operations and decompositions. Also minimize dynamic memory allocation and deallocation within performance-critical sections. Use stack allocation where feasible and ensure data locality to improve cache usage. You may check the following page for more information - https://www.mathworks.com/help/coder/ug/optimize-generated-code.html#:~:text=MATLAB%20Coder.-,Strategies%20to%20Optimize%20Speed,-Use%20this%20table
I hope this answers your question. Happy coding !
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Execution Speed 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!