【问题标题】:How do I profile a MEX-function in Matlab如何在 Matlab 中分析 MEX 函数
【发布时间】:2012-06-28 12:48:03
【问题描述】:

我有一个我编写的 Mex 函数(一个可以从 Matlab 调用的 c++ 函数),我想使用 valgrind/kcachegrind 对其进行分析。如果您直接运行 c++ 程序,我知道如何使用 valgrind/kcachegrind,但是如果我从 Matlab 调用 c++ 程序,有没有办法进行此分析?

【问题讨论】:

  • 很好的问题,我经常想知道这个问题。遗憾的是我不知道答案,但我知道可以使用 Visual Studio 分析 Mex 代码...
  • @BillCheatham 您可以在 Linux 上使用 valgrind 使用包装代码来加载 MEX 文件,该文件本质上是一个动态库。看看我的回答。
  • Alex,你有没有找到其他方法来分析 mex 文件?我很好奇。

标签: c++ matlab profiling mex


【解决方案1】:

分析 MEX 文件很棘手,因为 MEX 文件是共享库。它不能在 Linux 上使用标准的“gprof”方法来完成——gprof 根本不这样做。我尝试使用 sprof,但我得到 “PLTREL not found error” - sprof 也不能使用。之前有一个帖子here,但是没有人给出最终答案。

幸运的是,有一种方法可以在 Linux 上使用 valgrind 来实现。首先,我们需要编写“运行”代码来加载 mex 文件,提供 mexFunction 符号供我们调用,并设置 MEX 文件的参数。我选择使用推荐的方法来使用 MATLAB - 使用 MATLAB engine。以下代码(另存为 test.c)加载 MEX 文件并找到 mexFunction 符号,从之前保存为“input.mat”的文件中加载输入数据(可以在 MATLAB 中使用 save 命令完成),并调用 mexFunction。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <dlfcn.h>
#include "engine.h"

typedef void (*mexFunction_t)(int nargout, mxArray *pargout [ ], int nargin, const mxArray *pargin[]);

int main(int argc, const char *argv[])

{
  Engine *ep;
  char buff[1024];
  int i;

  /* matlab must be in the PATH! */
  if (!(ep = engOpen("matlab -nodisplay"))) {
    fprintf(stderr, "Can't start MATLAB engine\n");
    return -1;
  }
  engOutputBuffer(ep, buff, 1023);

  /* load the mex file */
  if(argc<2){
    fprintf(stderr, "Error. Give full path to the MEX file as input parameter.\n");
    return -1;
  }
  void *handle = dlopen(argv[1], RTLD_NOW);
  if(!handle){
    fprintf(stderr, "Error loading MEX file: %s\n", strerror(errno));
    return -1;
  }

  /* grab mexFunction handle */
  mexFunction_t mexfunction = (mexFunction_t)dlsym(handle, "mexFunction");
  if(!mexfunction){
    fprintf(stderr, "MEX file does not contain mexFunction\n");
    return -1;
  }

  /* load input data - for convenience do that using MATLAB engine */
  /* NOTE: parameters are MEX-file specific, so one has to modify this*/
  /* to fit particular needs */
  engEvalString(ep, "load input.mat");
  mxArray *arg1 = engGetVariable(ep, "Ain");
  mxArray *arg2 = engGetVariable(ep, "opts");
  mxArray *pargout[1] = {0};
  const mxArray *pargin[2] = {arg1, arg2};

  /* execute the mex function */
  mexfunction(1, pargout, 2, pargin);

  /* print the results using MATLAB engine */
  engPutVariable(ep, "result", pargout[0]);
  engEvalString(ep, "result");
  printf("%s\n", buff);

  /* cleanup */
  mxDestroyArray(pargout[0]);
  engEvalString(ep, "clear all;");
  dlclose(handle);
  engClose(ep);

  return 0;
}

MEX 文件本身也应该使用mex -g 开关进行编译。上面的代码必须使用mex -g 编译并使用 engopts.sh 作为编译参数。从 MATLAB 命令行类型

mex('-v', '-f', fullfile(matlabroot,...
    'bin','engopts.sh'),...
    'test.c');

或在标准 Linux 终端中运行

/path/to/matlab/bin/mex -g -f /path/to/matlab/bin/engopts.sh test.c

使用 valgrind 分析 MEX 文件需要从命令行运行“测试”程序。在 test 和 MEX 文件所在的目录中键入命令:

PATH=$PATH:/path/to/matlab/bin/ LD_LIBRARY_PATH=/path/to/matlab/bin/glnxa64/:/path/to/matlab/sys/os/glnxa64/ valgrind --tool=callgrind ./test ./mex_file.mexa64

请注意,需要设置 MATLAB 的路径和正确的架构相关库路径! matlab 可执行文件必须存在于 PATH 中,否则 'test' 将失败。

还有一个问题。 MATLAB 引擎需要在系统上安装 csh(您可以使用任何 shell,csh 只需存在于 /bin 中)。所以如果你没有它,你必须安装它才能工作。

【讨论】:

    【解决方案2】:

    您可以使用 -D 选项启动 MATLAB,如 MatlabCentral thread 中所述:

    matlab -nojvm -nodesktop -nosplash -D"valgrind --error-limit=no --leak-check=yes --tool=memcheck -v --log-file=valgrind.log"

    我会添加以确保您拥有最新版本的 valgrind。当我尝试使用 valgrind 3.6 版调试 MEX 文件时,valgrind 崩溃而不是报告内存错误。

    【讨论】:

      猜你喜欢
      • 2010-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多