coder.unroll
Unrollfor
-loop by making a copy of the loop body for each loop iteration
Syntax
Description
coder.unroll()
unrolls afor
循环。Thecoder.unroll
call must be on a line by itself immediately preceding thefor
-loop that it unrolls.
Instead of producing afor
-loop in the generated code, loop unrolling produces a copy of thefor
-loop body for each loop iteration. In each iteration, the loop index becomes constant. To unroll a loop, the code generator must be able to determine the bounds of thefor-
loop.
For small, tight loops, unrolling can improve performance. However, for large loops, unrolling can increase code generation time significantly and generate inefficient code.
coder.unroll
is ignored outside of code generation.
Examples
Unroll afor
-loop
To produce copies of afor
-loop body in the generated code, usecoder.unroll
.
In one file, write the entry-point functioncall_getrand
and a local functiongetrand
.getrand
unrolls afor
-loop that assigns random numbers to an n-by-1 array.call_getrand
callsgetrand
with the value 3.
functionz = call_getrand%#codegenz = getrand(3);endfunctiony = getrand(n) coder.inline('never'); y = zeros(n, 1); coder.unroll();fori = 1:n y(i) = rand();endend
Generate a static library.
codegen-config:libcall_getrand-report
In the generated code, the code generator produces a copy of thefor
-loop body for each of the three loop iterations.
static void getrand(double y[3]) { y[0] = b_rand(); y[1] = b_rand(); y[2] = b_rand(); }
Controlfor
-loop Unrolling with Flag
Control loop unrolling by usingcoder.unroll
with theflag
argument.
In one file, write the entry-point functioncall_getrand_unrollflag
and a local functiongetrand_unrollflag
. When the number of loop iterations is less than 10,getrand_unrollflag
unrolls thefor
循环。call_getrand
callsgetrand
with the value 50.
functionz = call_getrand_unrollflag%#codegenz = getrand_unrollflag(50);endfunctiony = getrand_unrollflag(n) coder.inline('never'); unrollflag = n < 10; y = zeros(n, 1); coder.unroll(unrollflag)fori = 1:n y(i) = rand();endend
Generate a static library.
codegen-config:libcall_getrand_unrollflag-report
The number of iterations is not less than 10. Therefore, the code generator does not unroll thefor
循环。It produces afor
-loop in the generated code.
static void getrand_unrollflag(double y[50]) { int i; for (i = 0; i < 50; i++) { y[i] = b_rand(); } }
Use Legacy Syntax to Unrollfor
-Loop
functionz = call_getrand%#codegenz = getrand(3);endfunctiony = getrand(n) coder.inline('never'); y = zeros(n, 1);fori = coder.unroll(1:n) y(i) = rand();endend
Use Legacy Syntax to Controlfor
-Loop Unrolling
functionz = call_getrand_unrollflag%#codegenz = getrand_unrollflag(50);endfunctiony = getrand_unrollflag(n) coder.inline('never'); unrollflag = n < 10; y = zeros(n, 1);fori = coder.unroll(1:n, unrollflag) y(i) = rand();endend
Input Arguments
Tips
Sometimes, the code generator unrolls a
for
-loop even though you do not usecoder.unroll
. For example, if afor
-loop indexes into a heterogeneous cell array or intovarargin
orvarargout
, the code generator unrolls the loop. By unrolling the loop, the code generator can determine the value of the index for each loop iteration. The code generator uses heuristics to determine when to unroll afor
循环。如果老的启发式无法识别olling is warranted, or if the number of loop iterations exceeds a limit, code generation fails. In these cases, you can force loop unrolling by usingcoder.unroll
. SeeNonconstant Index into varargin or varargout in a for-Loop.If a
for
-loop is not preceded bycoder.unroll
, the code generator uses a loop unrolling threshold to determine whether to automatically unroll the loop. If the number of loop iterations is less than the threshold, the code generator unrolls the loop. If the number of iterations is greater than or equal to the threshold, the code generator produces afor
循环。The default value of the threshold is5
. By modifying this threshold, you can fine-tune loop unrolling. For more details, seeUnroll for-Loops.