Main Content

coder.loop.tile

R2026b

Tile for-loops in generated code

Since R2023a

    Description

    coder.loop.tile(loopID,tileSize,tiledLoopId) directs the code generator to apply loop tiling to the loop with index loopID in the generated code and partitions the iteration space into smaller blocks of size tileSize. This directive introduces an outer tiled loop and an inner loop that retains the original loop index loopID. Use the optional tiledLoopId to add additional loop-control directives to the tiled loop.

    Use this directive with nested loops that operate on large data sets that have regular array access patterns and independent iterations. To improve data reuse and memory access, choose a tile size that fits in the cache. This directive provides less benefit if the data already fits in the cache or if the added loop overhead outweighs its benefits. For more information about loop optimizations, see Optimize Loops in Generated Code.

    example

    Examples

    collapse all

    Examine the function tileForLoop, which performs addition on a matrix. The function uses the coder.loop.tile directive to tile the for-loop in the generated code.

    function out = tileForLoop %#codegen
    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 a C static library for this function.

    codegen -config:lib -report tileForLoop

    Open the code generation report and inspect the generated function. The generated code contains an additional tiled loop with the loop identifier ii.

    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

    Index name of the for-loop to tile, specified as a string scalar or character vector.

    Size of each loop tile, specified as an integer.

    Index name of the new tiled loop, specified as a string scalar or character vector.

    Tips

    • To view potential issues that the code generator encounters when applying the coder.loop.tile directive, review the Code Insights section of the code generation report. See Code Generation Reports.

    Extended Capabilities

    expand all

    C/C++ Code Generation
    Generate C and C++ code using MATLAB® Coder™.

    GPU Code Generation
    Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

    Version History

    Introduced in R2023a