Main Content

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:

  1. 通过设置配置对象属性启用MEX分析EnableMexProfilingtrue

    一种lternatively, you can use代码根with the-轮廓option.

    The equivalent setting in the MATLAB Coder app isEnable execution profilingin the产生步。

  2. 产生the MEX filemyFunction_mex

  3. 运行MATLAB PROFILER并查看配置文件摘要报告,该报告在单独的窗口中打开。

    轮廓on;myFunction_mex;轮廓viewer;

    Make sure that you have not changed or moved the original MATLAB fileMyFunction.m。否则,探查者不考虑myFunction_mex为了profiling.

If you have a test filemyFunctionTest.mthat 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.creturns the variable,这是所有要素的总和一种andb。功能的性能foo.c独立于矩阵是否一种andb以行 - 尺寸或列尺寸布局为代表。MyFunction返回差异和_absand

You can measure the performance ofMyFunction为了large input matrices一种andb,然后进一步优化:

  1. 启用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;

    单独的窗口打开显示配置文件摘要报告。

    Profile summary exhibiting a table with field Function Name Calls, Total Time in seconds, Self Time in seconds and total time plot. A flame graph is present, representing the table in a bar graph.

    个人资料摘要报告显示了MEX文件及其孩子的总时间和自我时间,这是原始MATLAB函数的生成代码。

  2. Under Function Name, click the first link to view the Profile Detail Report for the generated code forMyFunction。您可以看到花费最多的时间的线:

    Table with fields Line Number, Code, Cells, Total time in seconds, Percentage of time and time plot with relevant data entries from example code. Important to point out that the total time for coder.ceval is relatively high.

  3. 线呼叫coder.cevaltakes a lot of time (16.914 s). This line has considerable execution time becausecoder.cevalconverts the representation of the matrices一种andb从行-major布局到列 - 马约尔布局,然后将其传递到外部C函数。您可以使用其他参数避免此转换-layout:rowMajorincoder.ceval

    和= coder.ceval('-layout:rowMajor',,,,'foo',coder.ref(a),coder.ref(b),长度);
  4. 产生the MEX function and profile again using the modifiedMyFunction

    一种= rand(20000); B = rand(20000); codegenMyFunction-args{a,b}foo.cfoo.h-轮廓轮廓on;myFunction_mex(A,B); profileviewer;
    The Profile Detail Report forMyFunctionshows that the line callingcoder.ceval现在仅采用0.653 s:

    与上面提到的相同图像,这里的编码器的总时间减少了0.653。

折叠表达式对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 functionMyFoldFunctionshows 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 + Binto 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.

也可以看看

|||||

Related Topics