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.
[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
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"))
/*
- Prerelease License - for engineering feedback and testing purposes
- only. Not for sale.
- File: unrollAndJamTransform.c
- MATLAB Coder version : 25.1
- C/C++ source code generated on : 01-Feb-2025 08:01:19 */
/* 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
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
Loop control object with transformSchedule
property set tocoder.loop.unrollAndJam
.
Version History
Introduced in R2023a