coder.loop.parallelize
R2026bParallelize for-loops in generated code or disable automatic
parallelization
Description
coder.loop.parallelize( parallelizes
the loopID)for-loop with index name loopID. The code
generator produces code that executes the iterations of the specified loop in parallel using
available target threads. To use this directive, your compiler must support the OpenMP
library, and you must enable the Enable dynamic memory
allocation and Enable OpenMP library if
possible parameters. See Automatic Parallelization of for-Loops in the Generated Code.
Use this directive for for-loops that have many iterations and
perform independent, non-trivial work in each iteration. To effectively parallelize across
multiple CPU cores, the code generator must be able to divide loop iterations evenly across
threads. Because parallelization introduces overhead, this directive works best for loops
with efficient memory access. For more information about loop optimizations, see Optimize Loops in Generated Code.
coder.loop.parallelize(
disables automatic parallelization of the loopID,"never")for-loop that immediately
follows this function call in the generated code. This directive overrides the
EnableAutoParallelization configuration setting. This directive
supports explicit for-loops only. For more information on explicit and
implicit loops, see Parallelization of Explicit and Implicit for-loops.
Automatic parallelization of for-loops can improve
performance by distributing work across threads. Disable
parallelization for loops with low work per iteration, irregular memory access patterns, or
dependencies among iterations, as the added overhead can reduce or negate
performance gains.
Examples
Input Arguments
Limitations
To use the coder.loop.parallelize directive, your compiler must support the OpenMP library, and you must enable the Enable dynamic memory allocation and Enable OpenMP library if possible parameters. See Automatic Parallelization of for-Loops in the Generated Code.
Tips
To view potential issues that the code generator encounters when applying the
coder.loop.parallelizedirective, review the Code Insights section of the code generation report. See Code Generation Reports.Use the
coder.loop.parallelizefunction and thecoder.loop.Control.parallelizemethod to parallelize generated code. To parallelize both the MATLAB code and the generated code, use aparfor-loop instead of afor-loop. See Algorithm Acceleration Using Parallel for-Loops (parfor).
Extended Capabilities
Version History
Introduced in R2021a