多线程(pthreads)MATLAB mex函数导致MATLAB退出后崩溃
Multithreaded (pthreads) MATLAB mex function causes MATLAB to crash after exiting
我有一个任务需要在 MATLAB 中多次执行,我想通过使用多线程 MEX 函数来减少执行任务所花费的时间。但是,我 运行 遇到了一个小问题,MEX 函数导致 MATLAB 在退出后由于双重释放或损坏错误而崩溃。
任务本身相当大,但我已经能够通过以下最小(非)工作示例重现错误:
#include <pthread.h>
#include "mex.h"
#include "matrix.h"
/* hard coded number of matrices to create per thread */
int num_mat = 10;
/* struct for passing information to the thread */
typedef struct {
int pnum;
int num_mat;
mxArray *outMatrix;
} pt_info_t;
/* thread function */
void *doThread(void *tinfo){
/* extract info from the struct */
pt_info_t pinfo = *((pt_info_t *)tinfo);
mxArray *outMatrix = pinfo.outMatrix;
/* create a cell matrix */
mxArray *one_cell = mxCreateCellMatrix(pinfo.num_mat, 1);
int i = 0;
for(i = 0; i < pinfo.num_mat; i++){
/* fill the cell matrix with 1x1 double matrices */
mxArray *one_mat = mxCreateDoubleMatrix(1,1,mxREAL);
mxSetCell(one_cell, i, one_mat);
}
/* add the cell matrix to the return cell matrix */
mxSetCell(outMatrix, pinfo.pnum, one_cell);
/* exit the thread */
pthread_exit(NULL);
}
/* thread entry function */
void t_comp_routine(int num_threads, mxArray *outMatrix){
/* thread setup */
pthread_t threads[num_threads];
pt_info_t pinfo[num_threads];
/* add information to the thread info structs and start threads */
int pnum = 0;
for(pnum = 0; pnum < num_threads; pnum++){
pinfo[pnum].pnum = pnum;
pinfo[pnum].num_mat = num_mat;
pinfo[pnum].outMatrix = outMatrix;
pthread_create(&threads[pnum], NULL, doThread, (void *)(pinfo + pnum));
}
/* join the threads */
for(pnum = 0; pnum < num_threads; pnum++){
pthread_join(threads[pnum], NULL);
}
}
/* same routine but without threads */
void comp_routine(int num_pretend_threads, mxArray *outMatrix){
int pnum = 0;
for(pnum = 0; pnum < num_pretend_threads; pnum++){
mxArray *one_cell = mxCreateCellMatrix(num_mat, 1);
int i = 0;
for(i = 0; i < num_mat; i++){
mxArray *one_mat = mxCreateDoubleMatrix(1,1,mxREAL);
mxSetCell(one_cell, i, one_mat);
}
mxSetCell(outMatrix, pnum, one_cell);
}
}
/* The gateway function
* nlhs = NUM left hand side args (OUTPUT)
* nrhs = NUM rhs args (INPUT)
* plhs = array of output args (OUTPUT)
* prhs = array of input args (INPUT)
*/
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
int num_threads = mxGetScalar(prhs[0]);
mxArray *outMatrix = mxCreateCellMatrix(num_threads, 1);
/* t_comp_routine causes matlab to crash after exiting */
t_comp_routine(num_threads, outMatrix);
/* comp_routine does not */
/* comp_routine(num_threads, outMatrix); */
plhs[0] = outMatrix;
printf("Finished!\n");
}
该程序接受一个 num_threads
变量并输出一个元胞数组,该元胞数组的元素数与线程数相同。输出数组的每个单元格都是一个长度为 10 的单元格矩阵,其中每个元素都包含一个 1x1 双精度矩阵。
使用 10 个线程调用函数会导致以下情况:
>> thread_test(10)
Finished!
ans =
{10x1 cell}
{10x1 cell}
{10x1 cell}
{10x1 cell}
{10x1 cell}
{10x1 cell}
{10x1 cell}
{10x1 cell}
{10x1 cell}
{10x1 cell}
*** glibc detected *** /usr/local/MATLAB/R2013a/bin/glnxa64/MATLAB: double free or corruption (fasttop): 0x00002b89a000cf10 ***
为简洁起见,我排除了 Backtrace,但如果需要,我非常乐意提供它。
偶尔,调用少于 10 个线程的函数将完成而不显示错误,尽管我怀疑可能仍然存在问题。非线程函数 comp_routine
执行相同的任务并且 运行 多次没有错误。
我认为错误可能是在线程函数内创建 mxArrays 的结果,但我不知道如何解决这个问题,因为对于完整的程序我必须即时创建这些数组(我不知道例如,总是提前知道创建它们的大小)。
MEX API(包括 mx*
函数)是 NOT thread safe (MathWorks Support Team)。您只能从主 MATLAB 线程调用 mx*/mex* 函数,但有少数例外(见底部)。在用 pthread_create
启动的 doThread
中,您正在调用几个不能在那里使用的函数:
mxCreateCellMatrix
mxCreateDoubleMatrix
mxSetCell
您必须将普通旧数据传入和传出 doThread
,并在使用 pthread_create
调用的线程之外管理元胞数组。除了 example from MathWorks, which demonstrates using the Windows API for multithreading, another nice example of multithreading a MEX function using both Pthreads and the Windows API is provided by Dirk-Jan Kroon in his tutorial on the File Exchange and his non-rigid registration submission, among others. It may also be worth checking out the C++11 approach using std::thread
in the MexThread submission.
异常 (YMMV):mexErrMsgIdAndTxt
.
我有一个任务需要在 MATLAB 中多次执行,我想通过使用多线程 MEX 函数来减少执行任务所花费的时间。但是,我 运行 遇到了一个小问题,MEX 函数导致 MATLAB 在退出后由于双重释放或损坏错误而崩溃。
任务本身相当大,但我已经能够通过以下最小(非)工作示例重现错误:
#include <pthread.h>
#include "mex.h"
#include "matrix.h"
/* hard coded number of matrices to create per thread */
int num_mat = 10;
/* struct for passing information to the thread */
typedef struct {
int pnum;
int num_mat;
mxArray *outMatrix;
} pt_info_t;
/* thread function */
void *doThread(void *tinfo){
/* extract info from the struct */
pt_info_t pinfo = *((pt_info_t *)tinfo);
mxArray *outMatrix = pinfo.outMatrix;
/* create a cell matrix */
mxArray *one_cell = mxCreateCellMatrix(pinfo.num_mat, 1);
int i = 0;
for(i = 0; i < pinfo.num_mat; i++){
/* fill the cell matrix with 1x1 double matrices */
mxArray *one_mat = mxCreateDoubleMatrix(1,1,mxREAL);
mxSetCell(one_cell, i, one_mat);
}
/* add the cell matrix to the return cell matrix */
mxSetCell(outMatrix, pinfo.pnum, one_cell);
/* exit the thread */
pthread_exit(NULL);
}
/* thread entry function */
void t_comp_routine(int num_threads, mxArray *outMatrix){
/* thread setup */
pthread_t threads[num_threads];
pt_info_t pinfo[num_threads];
/* add information to the thread info structs and start threads */
int pnum = 0;
for(pnum = 0; pnum < num_threads; pnum++){
pinfo[pnum].pnum = pnum;
pinfo[pnum].num_mat = num_mat;
pinfo[pnum].outMatrix = outMatrix;
pthread_create(&threads[pnum], NULL, doThread, (void *)(pinfo + pnum));
}
/* join the threads */
for(pnum = 0; pnum < num_threads; pnum++){
pthread_join(threads[pnum], NULL);
}
}
/* same routine but without threads */
void comp_routine(int num_pretend_threads, mxArray *outMatrix){
int pnum = 0;
for(pnum = 0; pnum < num_pretend_threads; pnum++){
mxArray *one_cell = mxCreateCellMatrix(num_mat, 1);
int i = 0;
for(i = 0; i < num_mat; i++){
mxArray *one_mat = mxCreateDoubleMatrix(1,1,mxREAL);
mxSetCell(one_cell, i, one_mat);
}
mxSetCell(outMatrix, pnum, one_cell);
}
}
/* The gateway function
* nlhs = NUM left hand side args (OUTPUT)
* nrhs = NUM rhs args (INPUT)
* plhs = array of output args (OUTPUT)
* prhs = array of input args (INPUT)
*/
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
int num_threads = mxGetScalar(prhs[0]);
mxArray *outMatrix = mxCreateCellMatrix(num_threads, 1);
/* t_comp_routine causes matlab to crash after exiting */
t_comp_routine(num_threads, outMatrix);
/* comp_routine does not */
/* comp_routine(num_threads, outMatrix); */
plhs[0] = outMatrix;
printf("Finished!\n");
}
该程序接受一个 num_threads
变量并输出一个元胞数组,该元胞数组的元素数与线程数相同。输出数组的每个单元格都是一个长度为 10 的单元格矩阵,其中每个元素都包含一个 1x1 双精度矩阵。
使用 10 个线程调用函数会导致以下情况:
>> thread_test(10)
Finished!
ans =
{10x1 cell}
{10x1 cell}
{10x1 cell}
{10x1 cell}
{10x1 cell}
{10x1 cell}
{10x1 cell}
{10x1 cell}
{10x1 cell}
{10x1 cell}
*** glibc detected *** /usr/local/MATLAB/R2013a/bin/glnxa64/MATLAB: double free or corruption (fasttop): 0x00002b89a000cf10 ***
为简洁起见,我排除了 Backtrace,但如果需要,我非常乐意提供它。
偶尔,调用少于 10 个线程的函数将完成而不显示错误,尽管我怀疑可能仍然存在问题。非线程函数 comp_routine
执行相同的任务并且 运行 多次没有错误。
我认为错误可能是在线程函数内创建 mxArrays 的结果,但我不知道如何解决这个问题,因为对于完整的程序我必须即时创建这些数组(我不知道例如,总是提前知道创建它们的大小)。
MEX API(包括 mx*
函数)是 NOT thread safe (MathWorks Support Team)。您只能从主 MATLAB 线程调用 mx*/mex* 函数,但有少数例外(见底部)。在用 pthread_create
启动的 doThread
中,您正在调用几个不能在那里使用的函数:
mxCreateCellMatrix
mxCreateDoubleMatrix
mxSetCell
您必须将普通旧数据传入和传出 doThread
,并在使用 pthread_create
调用的线程之外管理元胞数组。除了 example from MathWorks, which demonstrates using the Windows API for multithreading, another nice example of multithreading a MEX function using both Pthreads and the Windows API is provided by Dirk-Jan Kroon in his tutorial on the File Exchange and his non-rigid registration submission, among others. It may also be worth checking out the C++11 approach using std::thread
in the MexThread submission.
异常 (YMMV):mexErrMsgIdAndTxt
.