Algorithm Acceleration Using Parallel for-Loops (parfor) - MATLAB & Simulink (original) (raw)

Parallel for-Loops (parfor) in Generated Code

To potentially accelerate execution, you can generate MEX functions or C/C++ code from MATLAB® code that contains parallel for-loops (parfor-loops).

A parfor-loop, like the standard MATLAB for-loop, executes a series of statements (the loop body) over a range of values. Unlike the for-loop, however, the iterations of the parfor-loop can run in parallel on multiple cores on the target hardware.

Running the iterations in parallel might significantly improve execution speed of the generated code. For more information, see How parfor-Loops Improve Execution Speed.

Note

The parallel execution occurs only in generated MEX functions or C/C++ code; not the original MATLAB code. To accelerate your MATLAB code, generate a MEX function from the parfor-loop. Then, call the MEX function from your code. For more information, see Workflow for Accelerating MATLAB Algorithms.

To use parfor in your MATLAB code, you require a Parallel Computing Toolbox™ license.

MATLAB Coder™ software uses the Open Multiprocessing (OpenMP) application interface to support shared-memory, multicore code generation. If you want distributed parallelism, use the Parallel Computing Toolbox product. By default, MATLAB Coder uses up to as many cores as it finds available. If you specify the number of threads to use, MATLAB Coder uses at most that number of cores for the threads, even if additional cores are available. For more information, see parfor.

Because the loop body can execute in parallel on multiple threads, it must conform to certain restrictions. If MATLAB Coder software detects loops that do not conform to parfor specifications, it produces an error. For more information, see parfor Restrictions.

How parfor-Loops Improve Execution Speed

A parfor-loop might provide better execution speed than its analogous for-loop because several threads can compute concurrently on the same loop.

Each execution of the body of a parfor-loop is called an iteration. The threads evaluate iterations in arbitrary order and independently of each other. Because each iteration is independent, they do not have to be synchronized. If the number of threads is equal to the number of loop iterations, each thread performs one iteration of the loop. If there are more iterations than threads, some threads perform more than one loop iteration.

For example, when a loop of 100 iterations runs on 20 threads, each thread executes five iterations of the loop simultaneously. If your loop takes a long time to run because of the large number of iterations or individual iterations being lengthy, you can reduce the run time significantly using multiple threads. In this example, you might not, however, get 20 times improvement in speed because of parallelization overheads, such as thread creation and deletion.

When to Use parfor-Loops

Use parfor when you have:

When Not to Use parfor-Loops

Do not use parfor when:

parfor-Loop Syntax

For more information, see parfor.

parfor Restrictions