Main Content

coder.loop.Control Class

Namespace: coder.loop

Customize loop optimizations in generated code

Since R2023a

Description

Use instances of the coder.loop.Control class to optimize MATLAB® for-loops in the generated code.

Creation

Description

loopSchedule = coder.loop.Control creates a loop control object without the transformSchedule property set.

Use the object functions to add loop transforms to the loop control object. Provide the loop index name as inputs to the object functions.

example

Properties

expand all

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

expand all

Examples

collapse all

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