Automatically Parallelize for Loops in Generated Code - MATLAB & Simulink (original) (raw)
Iterations of parallel for
-loops can run simultaneously on multiple cores on the target hardware. Parallelization of a section of code might significantly improve the execution speed of the generated code. See How parfor-Loops Improve Execution Speed.
While generating C/C++ code from your MATLAB® code, you can generate parallel for
-loops automatically. Automatic parallelization is a compiler transformation that converts sequential code to multithreaded code without manual intervention.
Automatic parallelization of for
-loop supports these build types for C/C++ targets:
- MEX
- Static library
- Dynamically linked library
- Executable
Parallelize for
Loops by Using MATLAB Coder App
To enable automatic parallelization of for
-loops, in theMATLAB Coderâ„¢ app, in the Generate Code step, select .
Parallelize for
Loops at Command Line
You can enable parallelization of the for
-loops by using the command-line interface. Consider the functionautoparExample
:
function x = autoparExample(x) %#codegen for i = 10:numel(x) x(i) = sqrt(x(i)); end end
To automatically generate parallel for
-loops, run these commands:
cfg = coder.config('lib'); cfg.EnableAutoParallelization = 1; x = rand(1,2000); codegen -config cfg autoparExample -args {x} -report
Code generation successful: View report
Inspect Generated Code and Code Insights
Open and inspect the code generation report.
Generated Code
Observe the Open Multiprocessing (OpenMP) pragmas generated above thefor
-loops.
void autoparExample(double x[2000]) { int i; if (!isInitialized_autoparExample) { autoparExample_initialize(); } #pragma omp parallel for num_threads(omp_get_max_threads()) private(i)
for (i = 0; i < 1991; i++) { x[i + 9] = sqrt(x[i + 9]); } }
The gutter highlighted in green next to the loops shows the part of the code that is parallelized.
Code Insights
In the Code Insights tab, under Automatic parallelization issues, you can see detailed information about the for
-loops that were not parallelized in the generated code.
For example, to view a particular code insight, regenerate code for theautoparExample
function that you defined in the previous section. Specify a smaller size for the input arguments.
cfg = coder.config('lib'); cfg.EnableAutoParallelization = 1; x = rand(1,1000); codegen -config cfg autoparExample -args {x} -report
The generated code does not contain parallel for
-loops because the size of the input argument is smaller than the threshold value for automatic parallelization. To view detailed information about the nonparallelized part of the code, open the report and click Code Insights > Automatic parallelization issues.
Disable Automatic Parallelization of a for
Loop
You might want to disable automatic parallelization of a particular loop if that loop performs better in serial execution. To prevent parallelization of a specificfor
-loop, place thecoder.loop.parallelize('never')
pragma immediately before the loop in your MATLAB code. This pragma overrides theEnableAutoParallelization
setting. Also, this pragma supports only those for
loops that are explicitly defined in your MATLAB code. For more information on explicit and implicit loops, see the next section.
For example, the code generator does not parallelize this loop:
% Pragma to disable automatic parallelization of for-loops coder.loop.parallelize('never'); for i = 1:n y(i) = y(i)*sin(i); end
Parallelize Implicit for
Loops
The example function autoparExample
used in the previous sections contains an explicit for
-loop. But your MATLAB code can also contain implicit for
-loops that do not appear explicitly in the code that you author. For example, the MATLAB function mtimes multiplies two matrices and must perform loop iterations implicitly over the matrix elements.
Automatic parallelization supports loops that are implicit in your MATLAB code. For example, consider this functionautoparExample_implicit
.
function y = autoparExample_implicit(y) %#codegen y = y * 17; % Generates implicit for loop end
Generate code by running these commands:
cfg = coder.config('lib'); cfg.EnableAutoParallelization = 1; y = rand(1,2000); codegen -config cfg autoparExample_implicit -args {y} -report
Open the report and inspect the generated code. The generated code contains a parallel for
-loop for the multiplication operation.
void autoparExample_implicit(double y[2000]) { int i; if (!isInitialized_autoparExample_implicit) { autoparExample_implicit_initialize(); } #pragma omp parallel for num_threads(omp_get_max_threads()) private(i)
for (i = 0; i < 2000; i++) { y[i] *= 17.0; }
Usage Notes and Limitations
Only outermost loops are parallelized
Automatic parallelization applies to the outermost loops in your MATLAB code only. This makes inner loops available for vectorization by the system compiler.parfor loops remain as parallel loops
- Automatic parallelization honors parfor loops that you define and does not convert them into sequential
for
-loops. for
loops that containparfor
-loops are not parallelized.
- Automatic parallelization honors parfor loops that you define and does not convert them into sequential
Loops containing persistent variables are not parallelized automatically
Automatic parallelization does not supportfor
-loops whose bodies contain either persistent variables or functions that access persistent variables.Loops containing external calls are not parallelized automatically
Automatic parallelization does not supportfor
-loops in your code that contain external calls.Function not inlined after automatic parallelization
If afor
-loop inside a function is automatically parallelized, the code generator does not inline that function.- Empty loops and
while
loops are not parallelized automatically
See Also
parfor | coder.loop.parallelize | coder.config | coder.MexCodeConfig | coder.CodeConfig | coder.EmbeddedCodeConfig