Multithreading with Matlab

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?

+6
source share
4 answers

You can only call the mx * and mex * functions from the main MATLAB thread. You can write multi-threaded MEX files to ensure they work at a level below the mx interface. If you need several MATLAB interpreters, you need several MATLAB processes. One way is through the Parallel Computing Toolbox, as pointed out by @You. This gives you PARFOR and SPMD loops for simultaneous operation.

+6
source

You are probably better off using MATLAB's built-in multi-threaded functions such as parfor . In fact, many MATLAB functions are already multithreaded (including operations with matrices), so you will not need to parallelize things yourself, except for replacing for with parfor . (In the general case, while loops cannot be parallelized.)

+3
source

Your best bet is parfor . if you are a student, you can get a parallel set of tools for fairly cheap. even the full price is not so much if you are serious about performance. Your code above will be error prone and hard to verify. using parfor intuitive and clean.

+1
source

I am surprised that everyone is eager for parfor . I would advise you, at least, to think about whether you can design your algorithm for calling from Matlab and run low critical sections from multi-threaded C / C ++ without resorting to mex. This is usually possible. Especially if you use the Matlab profiler or a similar tool to find out which steps of your analysis are the bottleneck, you can write only 1 or 2 steps in multi-threaded C.

Another way is to write your parallelism in Java, which is easier to work with Matlab.

Other options you can test include a multicore view in the Matlab Central or MatlabMPI library . Both of them are a bit cloned and designed for interprocess communication of parallelism (you need to run several instances of Matlab), therefore it is not suitable for very fine-grained, complex parallelism. But to simply break down the work into 4 or 8 or 16 parts, they must do their job, and at least multicore has reasonable community support. I have not tried MatlabMPI , but it looks promising. As a bonus, they should work on multiple computers, although they probably need a shared network file system.

+1
source

Source: https://habr.com/ru/post/893139/


All Articles