我正在尝试通过并行化加速Matlab程序中的某些处理。我无法访问Parallel Computing Toolbox,所以我一直试图通过mex文件来实现。我所拥有的(可能)相关部分是:
#include "mex.h"
#include "math.h"
#include <windows.h>
#include <process.h>
#define voidthread unsigned __stdcall
#define ThreadHANDLE HANDLE
...
voidthread computeloop (double **Args) {
...
}
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) {
...
/* double pointer array to store all needed function variables) */
double ***ThreadArgs, **ThreadArgs1;
/* Handles to the worker threads */
ThreadHANDLE *ThreadList;
...
/* Reserve room for handles of threads in ThreadList */
ThreadList = (ThreadHANDLE*)malloc(Nthreads* sizeof( ThreadHANDLE ));
ThreadID = (double **)malloc( Nthreads* sizeof(double *) );
ThreadArgs = (double ***)malloc( Nthreads* sizeof(double **) );
/* Assign pointer to output. */
O_grad =(double *)mxGetData(plhs[0]);
...
for (i=0; i<Nthreads; i++) {
/* Make Thread ID */
ThreadID1= (double *)malloc( 1* sizeof(double) );
ThreadID1[0]=(double)i;
ThreadID[i]=ThreadID1;
O_gradThread[i]=mxCreateNumericArray(3, Osize, mxDOUBLE_CLASS, mxREAL);
/* Make Thread Structure */
ThreadArgs1 = (double **)malloc( 10* sizeof( double * ) );
ThreadArgs1[0]=O_grid_size;
ThreadArgs1[1]=Nthreadsd;
ThreadArgs1[2]=d_p;
ThreadArgs1[3]=dxa;
ThreadArgs1[4]=dya;
ThreadArgs1[5]=dza;
ThreadArgs1[6]=d_p_size;
ThreadArgs1[7]=O_grid_index;
ThreadArgs1[8]=ThreadID[i];
ThreadArgs1[9]=(double *)mxGetData(O_gradThread[i]);
ThreadArgs[i]=ThreadArgs1;
ThreadList[i]=(HANDLE)_beginthreadex(NULL, 0, &computeloop, ThreadArgs[i], 0, NULL);
}
...
}
但是,当我尝试编译它时,我收到错误消息
error C2664: '_beginthreadex' : cannot convert parameter 3 from 'unsigned int (__cdecl *)(double **)' to 'unsigned int (__cdecl *)(void *)'
引用mexFunction中的行ThreadList[i]=(HANDLE)_beginthreadex(NULL, 0, &computeloop, ThreadArgs[i], 0, NULL);
。
当我尝试更改computeloop
以获取void *
参数而不是double **
(类似于this question的解决方案)并在其中投射参数时,我刚刚结束更多的错误。此外,这种通用模式适用于我在Matlab文件交换中看到的几个文件。这是否是其他地方出现错误的情况,直到此时编译器才会捕获,或者我错过了什么?我正在使用Microsoft Visual Studio 2010 Professional作为底层编译器通过Matlab编译它。