分析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中完成),并调用mexFunction.
#include
#include
#include
#include
#include
#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 MA