MATLAB Multithreading

What Is MATLAB Multithreading?

Multithreading in technical computing involves executing multiple threads simultaneously within a single process to improve computational efficiency and resource utilization. With multithreading, tasks can be divided and run concurrently on multiple CPU cores, enhancing performance for compute-intensive applications. This approach can lead to faster execution times, especially for operations that can be parallelized, such as matrix computations, Fourier transforms, and image processing. However, it also requires careful management of shared resources to avoid race conditions, deadlocks, and other issues.

MATLAB multithreading is easy to use. MATLAB® and Simulink® enable you to take advantage of multicore and multiprocessor computers in two main ways: built-in MATLAB multithreading and explicit parallelism using functions in Parallel Computing Toolbox™. You can run your MATLAB applications and Simulink models faster and more efficiently by using the full computational power of your machine.

MATLAB multithreading diagram showing serial and parallel processing options using MATLAB worker processes. MATLAB multithreading approaches can increase throughput.

Comparison of implicit and explicit MATLAB multithreading and serial processing.

Built-In MATLAB Multithreading

The built-in multithreading feature in MATLAB automatically parallelizes computations using underlying libraries at run time, resulting in significant performance improvements for many functions, including linear algebra and numerical functions such as fft, eig, mldivide, svd, and sort. Since MATLAB does this implicitly, you do not need to enable it manually. If you would like to control the number of computational threads, you can use the maxNumCompThreads function.

Similarly, Simulink provides built-in features for multithreading for subsystems, cosimulation, and data flow. As appropriate, Simulink prepares all eligible models and blocks for execution on multiple threads. When Simulink detects that performance can be improved, it automatically executes the eligible models using multiple threads, resulting in faster execution times.

Explicit Parallelism Using MATLAB Workers

Parallel Computing Toolbox provides functions for creating and using parallel pools, enabling you to use additional hardware resources through threads and processes to enhance your application’s performance and reduce time to results. You can leverage parallel pools in MATLAB and Simulink by using parallel for-loops (parfor), parallel queues (parfeval), parallel execution of a single program with multiple data (spmd), parallel Simulink simulations (parsim), and other parallel programming features.

A growing number of functions and features for MATLAB and Simulink help you take advantage of parallel computing resources without requiring any extra coding. For example, in Simulink, you can easily enable parallel support in some interfaces by setting a flag or preference.

Thread-Based Parallel Pool

Thread-based parallel pools enable you to achieve parallelism and distribute computations across multiple threads within a single MATLAB process. These pools have low communication overhead and provide shared memory access, but their use is limited to computations on a single machine. With MATLAB, you can enable a single additional thread worker with backgroundPool, while Parallel Computing Toolbox lets you use multiple thread workers through parpool("Threads").

Process-Based Parallel Pool

Parallel Computing Toolbox also enables you to execute MATLAB applications in parallel using multiple worker processes (computational engines) on a local machine or a remote cluster. By default, these process workers are computationally single-threaded.

Unlike multithreading, the explicit parallelism provided by a parallel process-based pool can help you scale parallel applications that use MATLAB workers beyond a single machine by leveraging compute clusters enabled with MATLAB Parallel Server™.


See also: parallel computing, GPU computing