Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreading with Matlab

I'm working on a project on Matlab where we have to optimize the performance, and I was thinking about parallelizing a couple of function calls that were made from a .m file.

The idea was simple, from a Matlab file (.m) call a C file compiled as MEX, and from that C file, create a couple of threads and call back the matlab functions from each thread.

The theory works, I can create the threads, and I can also call the matlab function, the problem is that I cannot call the matlab function from the thread:

//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 any restriction on that option when working with matlab? Has somebody tried something like this?

Edit: Is there any option that doesn't require Parallel Toolbox?

like image 262
Roberto Luis Bisbé Avatar asked Jul 20 '11 08:07

Roberto Luis Bisbé


People also ask

Does MATLAB use multiple CPU cores?

Only certain functions are optimized to take advantage of multiple core processor. More generally matlab remains a single threaded application and you have to use parallel computing toolbox to take full advantage of multi core processors.

Is Simulink multithreaded?

By default, Simulink configures all models to run on multiple threads with the MultiThreadCoSim parameter.

How do you do parallel processing in MATLAB?

Depending on your preferences, MATLAB can start a parallel pool automatically. To enable this feature, select Parallel > Parallel Preferences in the Environment group on the Home tab, and then select Automatically create a parallel pool. Set your solver to use parallel processing.

How many cores MATLAB uses?

Direct link to this question MATLAB was assigned: 6 logical cores by the OS. MATLAB is using: 3 logical cores. MATLAB is not using all logical cores because hyper-threading is enabled.


2 Answers

You can only call the mx* and mex* functions from the MATLAB main thread. You can write multithreaded MEX files providing these do their work at a level below the mx interface. If you want multiple MATLAB interpreters, you need multiple MATLAB processes. One way is through the Parallel Computing Toolbox as pointed out by @You. This gives you PARFOR loops and SPMD blocks for running things simultaneously.

like image 112
Edric Avatar answered Sep 20 '22 04:09

Edric


You'd probably be better off using MATLABs built-in multithreading features such as parfor. In fact, many MATLAB functions are already multithreaded (including matrix operations), so there should be no need for you to parallelize things yourself apart from replacing for with parfor. (In general, while loops cannot be paralellized.)

like image 42
You Avatar answered Sep 18 '22 04:09

You