Main Content

coder.loop.tile

Tile for-loops in the generated code

Since R2023a

    Description

    example

    coder.loop.tile("loopID",tileSize,"tiledLoopId") prompts the code generator to apply a tile transform the loop with loop index name loopID in the generated code. This creates an outer loop that is tiled according to the tileSize value and the inner loop index is set to the value of loopID.

    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 more information about loop optimizations, see Optimize Loops in Generated Code.

    loopObj = coder.loop.tile(___) creates a loop control object with transformSchedule property set to coder.loop.tile. Use the apply method to apply the transform to the specified loop.

    Examples

    collapse all

    Use the coder.loop.tile function to apply the tile transform to a for-loop. Provide the loopID, tileSize, and tileLoopId.

    Define a MATLAB® function tileForLoopTransform that performs addition on a matrix.

    function out = tileForLoopTransform
    out = zeros(100);
    
    coder.loop.tile('i',10,'ii');
    for i = 1:100
        for j = 1:100
            out(i,j) = out(i,j) + j;
        end
    end

    Generate code for this function by running the following command:

    codegen tileForLoopTransform.m -config:lib -launchreport

    Inspect the code generated for the function in the code generation report. Notice that an additional tile loop with loop identifier ii is included in the generated code.

    void tileForLoopTransform(double out[10000])
    {
      int i;
      int ii;
      int j;
      memset(&out[0], 0, 10000U * sizeof(double));
      for (ii = 0; ii <= 99; ii += 10) {
        for (i = ii; i <= ii + 9; i++) {
          for (j = 0; j < 100; j++) {
            int out_tmp;
            out_tmp = i + 100 * j;
            out[out_tmp] += (double)j + 1.0;
          }
        }
      }
    }

    Input Arguments

    collapse all

    for loop identifier or index name to tile, specified as a character vector a string scalar.

    Data Types: char | string

    Size of the new tile loop, specified as a numeric value.

    Data Types: single | double

    New tiled loop index name, specified as a string scalar or character vector

    Data Types: char | string

    Output Arguments

    collapse all

    Loop control object with transformSchedule property set to coder.loop.tile.

    Version History

    Introduced in R2023a