With Release R2007b, you now have the ability to set (and get) the maximum number of allowed threads. What does this mean for you?
Here's some pertinent information about maxNumCompThreads.
- maxNumCompThreads allows you to programmatically control the maximum number of computational threads that MATLAB uses.
- Unlike the setting from the preferences panel, this setting does not persist between MATLAB sessions.
- maxNumCompThreads controls both the JIT and BLAS, meaning both element-wise and matrix operations. Since the JIT doesn't operate at the command line, maxNumCompThreads only affects element-wise computations performed from M-files.
- The 'automatic' setting is designed to take best advantage of the hardware the program is currently running on.
- Not all operations run with the maximum number of computational threads. When MATLAB uses fewer than the maximum, it's avoiding the overhead of threading which can dominate certain operations, for example, matrix multiplication for 2x2 matrices.
- Currently, only a subset of functions that could benefit from multithreading are taking advantage of it. We will expand this list with each upcoming release.
The main message here is that you should not generally need to use this function at all! Why? Because we'd like to make MATLAB do the best job possible for you. However, there are some situations where the use of maxNumCompThreads is merited.
- Benchmarking - This needs to be done with care. For example, as I mentioned earlier, matrix multiplication doesn't use multiple threads for 2x2 matrices since the thread overhead dominates the numerical work that's being done. Be sure to test a variety of sizes to characterize your results.
- If you notice some slowdown in code that was previously running faster, you can programmatically reduce maxNumCompThreads and then restore the maxNumCompThreads value when the calculation is finished.
Please let us know if you encounter a situation in which setting maxNumCompThreads manually is necessary for better performance, by contacting Technical Support. It is our intention that you should not need to use maxNumCompThreads and it will help us to improve the code if you notify us of such situations.
Get the MATLAB code
Published with MATLAB® 7.5