Loren on the Art of MATLAB

Turn ideas into MATLAB


Loren on the Art of MATLAB has been archived and will not be updated.

Controlling Multithreading

With Release R2007b, you now have the ability to set (and get) the maximum number of allowed threads. What does this mean for you?


The Skinny on maxNumCompThreads

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.

When to Use maxNumCompThreads

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.

If You Find a Performance Issue...

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.

Published with MATLAB® 7.5

  • print


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.