There are two levels of parallelism present in MATLAB:
- Implicit Multi-threaded parallelism for certain built-in MATLAB commands, such as Matrix-Matrix Multiplication or Matrix Factorization.
- Explicit parallelism present in Parallel Computing Toolbox
Let's focus on the implicit multi-threaded parallelism first.
If you have a multi-core or multi-processor machine then the implicit multi-threaded parallelism is on by default in the client MATLAB. When I use the term client MATLAB, I mean the interactive MATLAB that you are running on your Windows/Linux/Mac desktop.
The number of threads used is set automatically by MATLAB at run time. You can type
to find out how many threads MATLAB is using for computation.
Keep in mind that MATLAB ignores hyperthreading. So for example if you have a hyperthreaded processor, your operating system might report 8 cores, but MATLAB will only see the 4 physical cores and report 4 as result of maxNumCompThreads.
If you need to you can disable implicit MATLAB multi-threading using one of the following:
- Start Start MATLAB with -singleCompThread startup option
- Type maxNumCompThreads(1) in your program
Note that maxNumCompThreads is currently deprecated and could be discontinued in a future release of MATLAB.
In relation to the earlier answer, if you start MATLAB on a machine that has 192 cores, MATLAB will report maxNumCompThreads of 192. On such a large machine the client MATLAB will have implicit parallelism of 192 threads. However, you as a user will not be able to control on which cores the threads are run. That will be handled by the operating system.
Now let's discuss the explicit parallelism provided by the Parallel Computing Toolbox and MDCS.
When you type MATLABPOOL open, MATLAB starts instances of headless MATLAB workers. These workers run either on your local machine (local scheduler) or on a MDCS cluster. These workers are by default single-threaded.
You can test that using the following code snippet:
matlabpool open
spmd
maxNumCompThreads
end
matlabpool close
However, if you believe that your application would benefit from using a hybrid of explicit and implicit parallelism, for example your tasks are performing many matrix multiplications or matrix factorizations, you can re-enable the implicit parallelism by placing
at the top of the function you are trying to run on workers, or inside spmd block.
spmd
maxNumCompThreads(N);
end
In my example N is the number of threads that you want to use and should be a reasonable value, for example 2, 4, 8.
You should only re-enable implicit parallelism on workers in situations where it is really warranted, for example, if you have a single MATLAB worker on a multi-core node.
At the moment it is still possible to enable multi-threading on the workers using the maxNumCompThreads command. However, this functionality is deprecated and could be removed in a future release of MATLAB.