我有一個Mex函數(可以從Matlab調用的函數,可以從Matlab調用),我想用valgrind/kcachegrind剖析它。如果你直接運行一個C++程序,我知道如何使用valgrind/kcachegrind,但是如果我從Matlab調用C++程序,是否有辦法執行此分析?如何在Matlab中剖析MEX函數
回答
由於MEX文件是共享庫,因此對MEX文件進行性能分析非常棘手。它不能在Linux上使用標準的'gprof'方法來完成--gprof根本就沒有這樣做。我試圖用sprof,但我得到「PLTREL not found error」 - sprof也不能使用。以前有一篇文章here,但沒有人給出最終答案。
幸運的是,有一種方法可以在valgrind上使用Linux。首先,我們需要編寫加載mex文件的「運行」代碼,提供mexFunction符號供我們調用,並設置MEX文件的參數。我選擇了使用推薦的方法來使用MATLAB來完成此操作 - 使用MATLAB engine。下面的代碼(保存爲test.c)加載一個MEX文件並找到mexFunction符號,從之前保存爲'input.mat'的文件加載輸入數據(可以使用保存命令在MATLAB中完成),並調用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文件需要運行在命令行「測試」程序。在兩個測試和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中)。所以如果你沒有它,你必須安裝它才能工作。
你可以用-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崩潰而不是報告內存錯誤。
- 1. 在visual studio中剖析mex函數
- 2. 使用NVIDIA可視化剖析器剖析MATLAB mex CUDA應用程序
- 3. 如何在Matlab調用的mex函數中使用Matlab引擎
- 4. MEX函數填補mxCreateDoubleMatrix MATLAB
- 5. 如何在mex函數中在Matlab中創建Enum類型
- 6. 在python函數裏剖析
- 7. 如何從Matlab C Mex函數中獲取兩個輸出?
- 8. 如何在C++和OpenGL中剖析我自己的函數?
- 9. 如何在Swift中剖析方法/函數所使用的CPU
- 10. 如何在php中剖析concreate函數或代碼段?
- 11. 如何Matlab的MEX編譯
- 12. 如何欺騙這個Matlab MEX再次讀取一個函數?
- 13. matlab在多次調用mex函數後沒有任何反應
- 14. 在matlab中編寫一個非常基本的mex函數
- 15. 在MATLAB MEX函數中鏈接Cassandra/C++驅動程序庫
- 16. 如何在Scala中剖析方法?
- 17. 如何在1.9.2中剖析Ruby代碼?
- 18. 如何在Zend Framework中剖析主義
- 19. 如何在Magento中剖析cron任務
- 20. 如何在jar中剖析一個類?
- 21. matlab結構mex
- 22. C++/MATLAB Mex binding
- 23. 剖析R中
- 24. 如何將matlab中的整數數組傳遞給mex?
- 25. 如何剖析門廊的數量
- 26. 如何剖析opensge數據庫?
- 27. 如何剖析代碼段?
- 28. 如何剖析OpenMP瓶頸
- 29. 我該如何剖析這個JavaScript回調函數的例子?
- 30. matlab中mex函數的安全,快速的CFLAGS
偉大的問題,我經常想知道這件事。可悲的是我不知道答案,但我知道可以使用Visual Studio來剖析Mex代碼...... –
@BillCheatham您可以在Linux上使用valgrind使用包裝代碼來加載MEX文件,該文件本質上是一個動態庫。看看我的答案。 – angainor
亞歷克斯,你有沒有找到其他方式來分析mex文件?我好奇。 – angainor