coder.loop.unrollAndJam - Unroll and jam for-loops in the generated code - MATLAB (original) (raw)

Main Content

Unroll and jam for-loops in the generated code

Since R2023a

Syntax

Description

coder.loop.unrollAndJam("[loopID](#mw%5Fa0353a11-5287-433d-a824-68f67801a556)",[unrollFactor](#mw%5F424a6d15-5769-4836-845c-09ff66f9c097)) prompts the code generator to unroll and jam the loop with index nameloopID 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](#mw%5Fec8f9913-3a10-4952-802f-d33f72ed4e76) = coder.loop.unrollAndJam(___) creates a loop control object with transformSchedule property set tocoder.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): View report

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"))

/*

/* Include Files */ #include "unrollAndJamTransform.h" #include <string.h>

/* Function Declarations */ static double local(double x, double z);

/* Function Definitions / /

/*

/*

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 tocoder.loop.unrollAndJam.

Version History

Introduced in R2023a