Profile MEX Functions by UsingMATLABProfiler
You can profile execution times for MEX functions generated by MATLAB®Coder™ by using the MATLAB Profiler. The profile for the generated code shows the number of calls and the time spent for each line of the corresponding MATLAB function. Use the Profiler to identify the lines of MATLAB code that produce generated code that take the most time. This information can help you identify and correct performance issues early in the development cycle. For more information on the MATLAB Profiler, seeprofile
andProfile Your Code to Improve Performance.
The graphical interface to the Profiler is not supported inMATLAB Online™.
MEX Profile Generation
You can use the MATLAB Profiler with a generated MEX function. Alternatively, if you have a test file that calls your MATLAB function, you can generate the MEX function and profile it in one step. You can perform these operations at the command line or in the MATLAB Coder app.
To use the Profiler with a generated MEX function:
Enable MEX profiling by setting the configuration object property
EnableMexProfiling
totrue
.Alternatively, you can use
codegen
with the-profile
option.The equivalent setting in the MATLAB Coder app isEnable execution profilingin theGeneratestep.
Generate the MEX file
MyFunction_mex
.Run the MATLAB Profiler and view the Profile Summary Report, which opens in a separate window.
profileon; MyFunction_mex; profileviewer;
Make sure that you have not changed or moved the original MATLAB file
MyFunction.m
. Otherwise, the Profiler does not considerMyFunction_mex
for profiling.
If you have a test fileMyFunctionTest.m
that calls your MATLAB function, you can:
Generate the MEX function and profile it in one step by using
codegen
with the-test
and the-profile
选项。如果你打开了MATLAB分析器更加积极re, turn it off before you use these two options together.codegenMyFunction-testMyFunctionTest-profile
Profile the MEX function by selectingEnable execution profilingin theVerifystep of the app. If you turned on the MATLAB Profiler before, turn it off before you perform this action.
Example
You use the Profiler to identify the functions or the lines of the MATLAB code that produce generated code that take the most time. Following is an example of a MATLAB function that converts the representation of its input matricesA
andB
from row-major to column-major layout in one of its lines. Such a conversion has a long execution time for large matrices. Avoiding the conversion by modifying that particular line makes the function more efficient.
Consider the MATLAB function:
function[y] = MyFunction(A,B)%#codegen% Generated code uses row-major representation of matrices A and Bcoder.rowMajor; length = size(A,1);% Summing absolute values of all elements of A and B by traversing over the% matrices row by rowsum_abs = 0;forrow = 1:lengthforcol = 1:length sum_abs = sum_abs + abs(A(row,col)) + abs(B(row,col));endend% Calling external C function 'foo.c' that returns the sum of all elements% of A and Bsum = 0; sum = coder.ceval('foo',coder.ref(A),coder.ref(B),length);% Returning the difference of sum_abs and sumy = sum_abs - sum;end
The generated code for this function uses a row-major representation of the square matricesA
andB
. The code first computessum_abs
(the sum of absolute values of all elements ofA
andB
) by traversing over the matrices row by row. This algorithm is optimized for matrices that are represented in a row-major layout. The code then usescoder.ceval
to call the external C functionfoo.c
:
#include#include #include "foo.h" double foo(double *A, double *B, double length) { int i,j,s; double sum = 0; s = (int)length; /*Summing all the elements of A and B*/ for(i=0;i
The corresponding C header filefoo.h
is:
#include "rtwtypes.h" double foo(double *A, double *B, double length);
foo.c
returns the variablesum
, which is the sum of all elements ofA
andB
. The performance of the functionfoo.c
is independent of whether the matricesA
andB
are represented in row-major or column-major layouts.MyFunction
returns the difference ofsum_abs
andsum
.
You can measure the performance ofMyFunction
for large input matricesA
andB
, and then optimize it further:
使墨西哥人分析并生成墨西哥人code for
MyFunction
. RunMyFunction_mex
for two large random matricesA
andB
. View the Profile Summary Report.A = rand(20000); B = rand(20000); codegenMyFunction-args{A,B}foo.cfoo.h-profileprofileon; MyFunction_mex(A,B); profileviewer;
A separate window opens showing the Profile Summary Report.
The Profile Summary Report shows the total time and the self time for the MEX file and its child, which is the generated code for the original MATLAB function.
Under Function Name, click the first link to view the Profile Detail Report for the generated code for
MyFunction
. You can see the lines where the most time was spent:The line calling
coder.ceval
takes a lot of time (16.914 s). This line has considerable execution time becausecoder.ceval
converts the representation of the matricesA
andB
from row-major layout to column-major layout before passing them to the external C function. You can avoid this conversion by using an additional argument-layout:rowMajor
incoder.ceval
:sum = coder.ceval('-layout:rowMajor','foo',coder.ref(A),coder.ref(B),length);
Generate the MEX function and profile again using the modified
MyFunction
.A = rand(20000); B = rand(20000); codegenMyFunction-args{A,B}foo.cfoo.h-profileprofileon; MyFunction_mex(A,B); profileviewer;
MyFunction
shows that the line callingcoder.ceval
now takes only 0.653 s:
Effect of Folding Expressions on MEX Code Coverage
When you usecoder.const
to fold expressions into constants, it causes a difference in the code coverage between the MATLAB function and the MEX function. For example, consider the function:
functiony = MyFoldFunction%#codegena = 1; b = 2; c = a + b; y = 5 + coder.const(c);end
Profiling the MATLAB functionMyFoldFunction
shows this code coverage in the Profile Detail Report:
However, profiling the MEX functionMyFoldFunction_mex
shows a different code coverage:
Lines 2, 3, and 4 are not executed in the generated code because you have folded the expressionc = a + b
into a constant for code generation.
This example uses user-defined expression folding. The code generator sometimes automatically folds certain expressions to optimize the performance of the generated code. Such optimizations also cause the coverage of the MEX function to be different from the MATLAB function.
See Also
profile
|codegen
|coder.MexCodeConfig
|coder.rowMajor
|coder.ceval
|coder.const