I am working on a project on Matlab where we have to optimize performance, and I was thinking of parallelizing several function calls that were made from a .m file.
The idea was simple: a C file compiled as MEX is called from a Matlab file (.m), and from this C file, create a pair of threads and call the matlab functions from each thread.
The theory works, I can create threads, and I can also call the matlab function, the problem is that I cannot call the matlab function from the stream:
//Global variables mxArray **g_plhs; mxArray **g_prhs; int g_nlhs; int g_nrhs; //Thread function DWORD WINAPI my_function( LPVOID lpParam ) { mexCallMATLAB(g_nlhs,g_plhs,g_nrhs,g_prhs,"matlab_function"); return 0; } //Main function void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { DWORD dwThreadIdArray[MAX_THREADS]; HANDLE hThreadArray[MAX_THREADS]; g_plhs = plhs; g_prhs = prhs; g_nlhs = nlhs; g_nrhs = nrhs; hThreadArray[0] = CreateThread( NULL, 0, my_function, NULL, 0, &dwThreadIdArray[0]); WaitForMultipleObjects(MAX_THREADS, hThreadArray, TRUE, INFINITE); for(i=0; i<MAX_THREADS; i++) { CloseHandle(hThreadArray[i]); } }
Do we have restrictions on this option when working with Matlab? Has anyone tried something like this?
Edit: is there an option that does not require the Parallel Toolbox?
source share