Profile MEX Functions by UsingMATLAB剖面
您可以介绍MATLAB生成的MEX函数的执行时间®使用MATLAB PROFILER,CODER™。生成的代码的配置文件显示了相应MATLAB函数的每一行的呼叫数量和时间。使用Profiler识别产生最多时间的生成代码的MATLAB代码线。此信息可以帮助您在开发周期的早期识别和纠正绩效问题。有关MATLAB Profiler的更多信息,请参阅轮廓
andProfile Your Code to Improve Performance。
The graphical interface to the Profiler is not supported inMatlab Online™。
MEX Profile Generation
您可以使用具有生成的MEX功能的MATLAB Profiler。另外,如果您有一个调用MATLAB函数的测试文件,则可以生成MEX功能并在一步中进行配置。您可以在命令行或MATLAB编码器应用程序中执行这些操作。
To use the Profiler with a generated MEX function:
通过设置配置对象属性启用MEX分析
EnableMexProfiling
到true
。一种lternatively, you can use
代码根
with the-轮廓
option.The equivalent setting in the MATLAB Coder app isEnable execution profilingin the产生步。
产生the MEX file
myFunction_mex
。运行MATLAB PROFILER并查看配置文件摘要报告,该报告在单独的窗口中打开。
轮廓on;myFunction_mex;轮廓viewer;
Make sure that you have not changed or moved the original MATLAB file
MyFunction.m
。否则,探查者不考虑myFunction_mex
为了profiling.
If you have a test filemyFunctionTest.m
that calls your MATLAB function, you can:
产生the MEX function and profile it in one step by using
代码根
with the-测试
和-轮廓
选项。如果您之前打开MATLAB Profiler,请在一起使用这两个选项之前将其关闭。代码根MyFunction-测试myFunctionTest-轮廓
通过选择MEX功能来介绍MEX功能Enable execution profilingin the核实step of the app. If you turned on the MATLAB Profiler before, turn it off before you perform this action.
Example
您可以使用Profiler来识别产生最多时间的生成代码的MATLAB代码的功能或行。以下是MATLAB函数的示例,该函数转换了其输入矩阵的表示一种
andb
从一条线上的一条线中,从行 - 马约尔到列布局。这样的转换对于大型矩阵的执行时间很长。通过修改特定行来避免转换,使功能更有效。
Consider the MATLAB function:
function[y] = myfunction(a,b)%#codegen%生成的代码使用矩阵A和B的行分子表示Coder.Rowmajor;长度=尺寸(a,1);% Summing absolute values of all elements of A and B by traversing over the% matrices row by row和_abs = 0;为了行= 1:长度为了col = 1:长度sum_abs = sum_abs + abs(a(row,col)) + abs(b(row,col));结尾结尾% Calling external C function 'foo.c' that returns the sum of all elementsA和B的%和= 0; sum = coder.ceval('foo',coder.ref(a),coder.ref(b),长度);% Returning the difference of sum_abs and sumy = sum_abs - sum;结尾
The generated code for this function uses a row-major representation of the square matrices一种
andb
。代码首先计算和_abs
(the sum of absolute values of all elements of一种
andb
)通过行遍历矩阵。该算法是针对在行分段布局中表示的矩阵进行了优化的。然后代码使用coder.ceval
调用外部C函数foo.c
:
#include#include #include“ foo.h” double foo(double *a,double *b,double length){int i,j,s;double sum = 0;s =(int)长度;/*总结A和b*/的所有元素(i = 0; i
相应的C标头文件foo.h
是:
#include“ rtwtypes.h” double foo(double *a,double *b,double Length);
foo.c
returns the variable和
,这是所有要素的总和一种
andb
。功能的性能foo.c
独立于矩阵是否一种
andb
以行 - 尺寸或列尺寸布局为代表。MyFunction
返回差异和_abs
and和
。
You can measure the performance ofMyFunction
为了large input matrices一种
andb
,然后进一步优化:
启用MEX分析并生成MEX代码
MyFunction
。RunmyFunction_mex
对于两个大型随机矩阵一种
andb
。View the Profile Summary Report.一种= rand(20000); B = rand(20000); codegenMyFunction-args{a,b}foo.cfoo.h-轮廓轮廓on;myFunction_mex(A,B); profileviewer;
单独的窗口打开显示配置文件摘要报告。
个人资料摘要报告显示了MEX文件及其孩子的总时间和自我时间,这是原始MATLAB函数的生成代码。
Under Function Name, click the first link to view the Profile Detail Report for the generated code for
MyFunction
。您可以看到花费最多的时间的线:线呼叫
coder.ceval
takes a lot of time (16.914 s). This line has considerable execution time becausecoder.ceval
converts the representation of the matrices一种
andb
从行-major布局到列 - 马约尔布局,然后将其传递到外部C函数。您可以使用其他参数避免此转换-layout:rowMajor
incoder.ceval
:和= coder.ceval('-layout:rowMajor',,,,'foo',coder.ref(a),coder.ref(b),长度);
产生the MEX function and profile again using the modified
MyFunction
。一种= rand(20000); B = rand(20000); codegenMyFunction-args{a,b}foo.cfoo.h-轮廓轮廓on;myFunction_mex(A,B); profileviewer;
MyFunction
shows that the line callingcoder.ceval
现在仅采用0.653 s:
折叠表达式对MEX代码覆盖的影响
当您使用时coder.const
到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);结尾
Profiling the MATLAB functionMyFoldFunction
shows this code coverage in the Profile Detail Report:
However, profiling the MEX functionMyFoldFunction_mex
显示不同的代码覆盖范围:
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.
也可以看看
轮廓
|代码根
|coder.MexCodeConfig
|Coder.Rowmajor
|coder.ceval
|coder.const