10

我刚刚用 c++ 重写了一个 Matlab 程序作为一个 mex 函数来加快速度,结果非常好。这个优化决定是一个非常非常好的主意,在没有线程的情况下可以将速度提高 20 倍。它仍然让我对 mex 函数花费时间并想要确定可能的瓶颈感到好奇。

我正在寻找一种分析 mex 函数的方法。matlab 探查器用处不大,我下载的其他探查器(免费和试用版)都希望运行可执行文件。我不是 mex-guru,但据我所知,运行 mex 的唯一方法是在 Matlab 中。mex 函数编译为 dll,但称为 .mex64。所以这个问题应该类似于分析一个dll。为了编写 c++ mex 函数,我使用了单用户 VS2005(即,不是团队版本),并且在 x64 平台上运行。

有谁知道分析 mex 函数的好方法?当我从 Matlab 开始时,我应该使用什么工具以及如何使用它?或者有没有其他方法来分析 c++ 代码?

4

3 回答 3

8

我设法做到这一点的唯一方法是分离出执行工作的函数并编写一个单独的包装器(而不是 mexFunction),它加载带有测试数据的 .mat 文件并作为独立的可执行文件运行。然后可以使用例如 gprof 对其进行分析

于 2009-08-14T09:51:53.227 回答
3

Is there a way to run the whole thing under a VC IDE. Like you could say "debug DLL Foo.dll, using Matlab as the startup app".

Then, if the function Bar you're using in Foo.dll runs in < 1 sec, make sure Matlab calls it a lot of times, or add a wrapper function in the DLL to call it a lot of times.

If you can get to that point, you can use the manual call stack sampling technique, that really works in spite of not being popular or requiring installation of a tool.

于 2009-08-15T20:19:06.843 回答
1

除了已经建议的好的解决方案之外,还有两种可能的解决方案。

  1. 可以编写一个脚本来加载数据,运行你的 mex 函数,然后退出。然后,您可以让分析器使用该脚本调用 Matlab 可执行文件。当我分析我的 CUDA mex 函数时,这对我有用 nVidia 的视觉分析器。

  2. 如果您在 Visual Studio 中构建了 mex 函数,您应该能够附加到 Matlab 进程,然后运行该函数以进行调试或分析。

于 2011-05-24T14:26:52.413 回答