简体   繁体   English

如何分析使用 matlab 编辑器编写并使用 gcc 编译的 MEX function

[英]How can I profile a MEX function written using the matlab editor and compiled using gcc

I'd like to profile a mex function I've written in the matlab (2021a) editor.我想介绍我在 matlab (2021a) 编辑器中编写的 mex function。 The best I can do right now is to using matlab's tic, toc functions to measure total execution time, but I dont know how to use more detailed diagnostic tools to evaluate the code performance.我现在能做的最好的就是使用matlab的tic、toc函数来测量总执行时间,但我不知道如何使用更详细的诊断工具来评估代码性能。 I've found other questions and responses discussing this issue by using visual studio, but the responses appear to use older versions of visual studio as opposed to my current one of 2019. I'm also not super familiar with visual studio, so I'm not sure where to find some of the tools they mention which appear to have been moved from where they were in previous versions.我发现其他问题回复通过使用 Visual Studio 讨论此问题,但回复似乎使用旧版本的 Visual Studio,而不是我目前的 2019 版本。我对 Visual Studio 也不是很熟悉,所以我我不确定在哪里可以找到他们提到的一些工具,这些工具似乎已从以前版本中的位置移走。

There were two solutions I decided to pursue for this.我决定为此采取两种解决方案。 Number one was adding timers to my code as suggested in the comments above (stackoverflow.com/a/47888078/7328782).第一个是在我的代码中添加计时器,如上面评论中的建议(stackoverflow.com/a/47888078/7328782)。 You can then program your script to output the values to the matlab console using the following code snippet:然后,您可以使用以下代码段将您的脚本编程到 output 到 matlab 控制台的值:

       std::ostringstream stream;
           stream << "Here is the name/value pair that you entered." << std::endl;
           displayOnMATLAB(stream);
           stream << SIG.rows() << " " << SIG.cols() << std::endl;
           displayOnMATLAB(stream);

And make sure to include the following function definition outside the main body of the mexFunction but still inside the class definition of the mexFunction:并确保在 mexFunction 的主体之外包含以下 function 定义,但仍在 mexFunction 的 class 定义内:

// outputting to matlab console for debugging
    void displayOnMATLAB(std::ostringstream& stream) {
        // Pass stream content to MATLAB fprintf function
        matlabPtr->feval(u"fprintf", 0,
            std::vector<matlab::data::Array>({ factory.createScalar(stream.str()) }));
        // Clear stream buffer
        stream.str("");
    }

However, simply timing a piece of c++ code doesn't necessarily give a full picture of what you can optimize with your code.但是,仅对一段 c++ 代码进行计时并不一定能全面了解您可以使用代码优化什么。 For example, Microsoft visual studio's profiling tools (Detailed introduction/overview/tutorials included here ) can tell you how much memory is being consumed at a given line, your processor usage throughout the script and much more.例如,Microsoft Visual Studio 的分析工具( 此处包含详细介绍/概述/教程)可以告诉您在给定的行中消耗了多少 memory、您在整个脚本中的处理器使用情况等等。 You might be explicitly looking to get a good idea of how neat and efficient a certain line is, or you might catch that an innocuous line could be running fast relative to your bottleneck but is actually consuming far more resources than it should.您可能明确地希望了解某条线路的整洁和高效程度,或者您可能会发现一条无害的线路相对于您的瓶颈可能运行得很快,但实际上消耗的资源比它应该消耗的多得多。 I decided to simply port my mex function code over to visual studio.我决定简单地将我的 mex function 代码移植到 Visual Studio。 Luckily, Matlab support has published a response on their forums explaining how to do this.幸运的是,Matlab 支持已经在他们的论坛上发布了回复,解释了如何做到这一点。 So far, it seems to be working with 2021a and Visual Studio 2019 even though the response was written for older versions of both Matlab and Visual Studio.到目前为止,即使响应是针对 Matlab 和 Visual Studio 的旧版本编写的,它似乎也适用于 2021a 和 Visual Studio 2019。

(Note, the module definition file only needs you to substitute the MYFILE statement with your project name I think. The other two lines should be left as written) (注意,模块定义文件只需要您将 MYFILE 语句替换为您的项目名称即可。其他两行应保持原样)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM