Main Content

coder.loop.tile

Tile for-loops in the generated code

Since R2023a

    Description

    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.

    example

    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.

    type tileForLoopTransform
    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 at the command line.

    codegen tileForLoopTransform.m -config:lib
    Warning: Code generation is using a coder.EmbeddedCodeConfig object. Because
    Embedded Coder is not installed, this might cause some Embedded Coder features
    to fail.
    
    Code generation successful (with warnings): To view the report, open('codegen/lib/tileForLoopTransform/html/report.mldatx')
    

    Inspect the code generated for the function. An additional tile loop with loop identifier ii is included in the generated code.

    type(fullfile("codegen","lib","tileForLoopTransform","tileForLoopTransform.c"))
    /*
     * File: tileForLoopTransform.c
     *
     * MATLAB Coder version            : 24.2
     * C/C++ source code generated on  : 05-Sep-2024 16:33:19
     */
    
    /* Include Files */
    #include "tileForLoopTransform.h"
    #include <string.h>
    
    /* Function Definitions */
    /*
     * Arguments    : double out[10000]
     * Return Type  : void
     */
    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;
          }
        }
      }
    }
    
    /*
     * File trailer for tileForLoopTransform.c
     *
     * [EOF]
     */
    

    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