Clear Filters
Clear Filters

MEX function is slower than matlab script. Profiling possiblilites?

21 views (last 30 days)
Hey guys. I am working on a toolbox for realtime classification of driving situations. For this purpose I train a least squares SVM, see LS SVM. This one does rely heavily on solving linear systems in realtively highdimensional space (matrices about 200x40). I need to make a crossvalidation to ensure there is no overfitting, so I need to calculate the whole model a lot of times.
Now there is the problem, that the normal script version is about 3 times faster than the generated mex code (it was quite hard to make the whole thing capable for code generation...). Especially, if the dimension grow. The code should be vectorized well, so I think this is the "problem" for the c code?
I've disabled memory integrity, responsiveness to ctrl+c. May i tune the borders for dynamical memory a bit?
Or is there a way to profile the mex file, so I can see where the speeddown comes from?
Thank you very much! Jan

Answers (1)

Subhajyoti
Subhajyoti on 9 Aug 2024 at 9:25
Hi Jan,
You can use the Profiler with the ‘-profile’ flag to track execution time of the MATLAB function in its corresponding MEX file.
Consider the following MATLAB Function for profiling:
function y = sampleMex(x)
y = x * 2;
for i = 1:1000
y = y + 1;
end
for i = 1:1000000
y = y - 1;
end
end
1) Use ‘codegen’ to compile the MATLAB Function into MEX File and perform profiling.
  • Generate MEX File for profiling:
>> x = 10;
>> codegen sampleMex -args {x} -profile
  • Profile the MEX Function for performance analysis:
>> profile on
>> sampleMex_mex(100)
>> profile viewer
Note: Set the ‘codegen’ arguments appropriately according to your needs.
2) A separate window opens showing the Profile Summary Report. View the Profile Detail Report for the generated code for the function (here, ‘sampleMex’).
You may go through the following MathWorks documentation link to learn more about profiling MEX functions generated by MATLAB Coder by using the MATLAB Profiler. The explanation and examples here provide clear step-by-step guidance.
Also, you may go through the following MathWorks documentation links to learn about ‘Performance Profiling’ using MATLAB Code Generation:
I hope this helps.

Categories

Find more on Generating Code in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!