Main Content

coder.loop.unrollAndJam

Unroll and jam for-loops in the generated code

Since R2023a

    Description

    coder.loop.unrollAndJam("loopID",unrollFactor) prompts the code generator to unroll and jam the loop with index name loopID by a factor specified by unrollFactor in the generated code.

    Unroll and jam transforms are usually applied to perfectly nested loops, which are loops where all 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.

    For more information about loop optimizations, see Optimize Loops in Generated Code.

    example

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

    Examples

    collapse all

    You can use the coder.loop.unrollAndJam function to apply an unroll and jam transform on a for-loop in the generated code.

    Define a MATLAB® function unrollAndJamTransform that performs operations on a matrix.

    type unrollAndJamTransform
    function out = unrollAndJamTransform
    out = zeros(10);
    
    coder.loop.unrollAndJam("i",5);
    for i = 1:10
        for j = 1:10
            out(i,j) = out(i,j) + local(i,j);
        end
    end
    end
    
    function y = local(x,z)
    coder.inline('never');
    y = x^2 + z^2;
    end
    

    Generate code for this function at the command line.

    codegen unrollAndJamTransform -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/unrollAndJamTransform/html/report.mldatx')
    

    Inspect the generated code for the function. The first loop is unrolled and subsequent operations are performed in the inner loop. This helps keep the array in cache to be reused.

    type(fullfile("codegen","lib","unrollAndJamTransform","unrollAndJamTransform.c"))
    /*
     * File: unrollAndJamTransform.c
     *
     * MATLAB Coder version            : 24.2
     * C/C++ source code generated on  : 05-Sep-2024 16:33:34
     */
    
    /* Include Files */
    #include "unrollAndJamTransform.h"
    #include <string.h>
    
    /* Function Declarations */
    static double local(double x, double z);
    
    /* Function Definitions */
    /*
     * Arguments    : double x
     *                double z
     * Return Type  : double
     */
    static double local(double x, double z)
    {
      return x * x + z * z;
    }
    
    /*
     * Arguments    : double out[100]
     * Return Type  : void
     */
    void unrollAndJamTransform(double out[100])
    {
      int i;
      int j;
      memset(&out[0], 0, 100U * sizeof(double));
      for (i = 0; i <= 5; i += 5) {
        for (j = 0; j < 10; j++) {
          int out_tmp;
          out_tmp = i + 10 * j;
          out[out_tmp] += local((double)i + 1.0, (double)j + 1.0);
          out[out_tmp + 1] += local(((double)i + 1.0) + 1.0, (double)j + 1.0);
          out[out_tmp + 2] += local(((double)i + 2.0) + 1.0, (double)j + 1.0);
          out[out_tmp + 3] += local(((double)i + 3.0) + 1.0, (double)j + 1.0);
          out[out_tmp + 4] += local(((double)i + 4.0) + 1.0, (double)j + 1.0);
        }
      }
    }
    
    /*
     * File trailer for unrollAndJamTransform.c
     *
     * [EOF]
     */
    

    Input Arguments

    collapse all

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

    Data Types: char | string

    Factor by which loop is unrolled in the generated code, specified as a numeric value.

    Output Arguments

    collapse all

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

    Version History

    Introduced in R2023a