coder.loop.Control Class
Namespace: coder.loop
Description
Use instances of the coder.loop.Control
class to optimize MATLAB®
for
-loops in the generated code.
Creation
Description
creates a
loop control object without the transformSchedule property set. loopSchedule
= coder.loop.Control
Use the object functions to add loop transforms to the loop control object. Provide the loop index name as inputs to the object functions.
Properties
transformSchedule
— Loop transform
coder.loop.Control
object
Loop transform specified as a coder.loop.Control
object.
This property further contains its own transformSchedule
property which itself is a coder.loop.Control
object. You can use the
object functions mentioned below to add more transforms to your top-level object. The
subsequent transforms are stored in the transformSchedule
property
in a recursive manner.
Methods
Public Methods
apply |
|
interchange |
Use this transform when accessing array elements stored in contiguous memory blocks. If so, the data is loaded into cache and can be reused without a cache refresh. For example, see Interchange for-Loops in Generated Code. |
parallelize |
This prompts the generated code for that
loop to execute the iterations in parallel with the threads available for your
target. This transforms requires For example, see Selectively Parallelize for Loops in Generated Code. |
reverse |
This prompts the generated code for that loop to execute the iterations in reverse order. Use this transform when you know the upper bound of the loop iterator. For example, see Reverse for-Loop Iteration Order in Generated Code. |
tile |
This prompts the generated code for that
loop to create an outer loop with loop index Use this transform to reduce iteration space of a loop into smaller blocks. This involves partitioning a large array from memory into smaller blocks that fit into your cache size. Use this transform when you have limited cache availability. For example, see Apply Tile Transform to for-Loop in Generated Code. |
unrollAndJam |
Unroll and jam transforms are usually applied to perfectly
nested loops, which are loops where all the
data elements are accessed within the inner loop. This transform unrolls the body
of the inner loop according to the loop index of the outer loop. The default value
of the For example, see Apply unrollAndJam on a for-Loop in the Generated Code. |
vectorize |
This prompts the code generator to use
the SIMD instruction set for your target hardware in the generated code. Set the
For example, see Vectorize for Loop in the Generated Code. |
Examples
Apply Parallelize and Interchange Transforms to for
Loops
Use the coder.loop.Control
object to apply
parallelize and interchange transforms to for
loops in the generated
code. Use the parallelize
and interchange
object
functions provided by coder.loop.Control
to optimize the generated
code.
Define a function forLoopParallelize
. Within the function, create a
coder.loop.Control
object and add a parallel transform to the
for
loop with index name
i
.
function [out] = forLoopParallelize(u,v) %#codegen row = size(u,1); col = size(u,2); out = zeros(row, col); schedule = coder.loop.Control; schedule = schedule.parallelize('i'); schedule.apply; for i = 1:col for j = 1:row out(i,j) = out(i,j) + u(i, j) * v(i, j); end end end
Generate code for this function by running the following command:
codegen -config:lib forLoopParallelize -args ... {reshape(1:100,[10,10]), 2.*reshape(1:100,[10,10])} -launchreport
Inspect the generated code in the report to see the parallelized loop. Notice that the code generator uses OpenMP when applicable.
void forLoopParallelize(const double u[100], const double v[100], double out[100]) { int i; int j; int out_tmp; if (!isInitialized_forLoopParallelize) { forLoopParallelize_initialize(); } memset(&out[0], 0, 100U * sizeof(double)); #pragma omp parallel for num_threads(omp_get_max_threads()) private(j, out_tmp) for (i = 0; i < 10; i++) { for (j = 0; j < 10; j++) { out_tmp = i + 10 * j; out[out_tmp] += u[out_tmp] * v[out_tmp]; } } }
Define a function forLoopInterchange
. Create a
coder.loop.Control
object and add an interchange transform to the
for-loops with indices i and j.
function out = forLoopInterchange() out = zeros(100,70); schedule = coder.loop.Control; schedule = schedule.interchange('i','j'); schedule.apply; for i = 1:100 for j = 1:70 out(i,j) = out(i,j) + i*j; end end
Generate code for this function by running the following command:
codegen -config:lib forLoopInterchange -launchreport
Inspect the generated code in the report to see the interchanged loop. Notice that the code generator interchanges the loop indices for both for-loops.
void forLoopInterchange(double out[7000]) { int i; int j; memset(&out[0], 0, 7000U * sizeof(double)); for (j = 0; j < 70; j++) { for (i = 0; i < 100; i++) { int out_tmp; out_tmp = i + 100 * j; out[out_tmp] += (double)((i + 1) * (j + 1)); } } }
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Version History
Introduced in R2023a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)