Main Content

coder.unroll

Unrollfor-loop by making a copy of the loop body for each loop iteration

Description

example

coder.unroll()unrolls afor循环。Thecoder.unrollcall must be on a line by itself immediately preceding thefor-loop that it unrolls.

而不是生产for-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.unrollis ignored outside of code generation.

example

coder.unroll(flag)unrolls afor-loop ifflagistrue.flagis evaluated at code generation time. Thecoder.unrollcall must be on a line by itself immediately preceding thefor-loop that it unrolls.

Examples

collapse all

To produce copies of afor-loop body in the generated code, usecoder.unroll.

In one file, write the entry-point functioncall_getrandand a local functiongetrand.getrandunrolls afor-loop that assigns random numbers to an n-by-1 array.call_getrandcallsgetrandwith 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(); }

Control loop unrolling by usingcoder.unrollwith theflagargument.

In one file, write the entry-point functioncall_getrand_unrollflagand a local functiongetrand_unrollflag. When the number of loop iterations is less than 10,getrand_unrollflagunrolls thefor循环。call_getrandcallsgetrandwith 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
static void getrand_unrollflag(double y[50]) { int i; for (i = 0; i < 50; i++) { y[i] = b_rand(); } }

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.

  • 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
  • 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

collapse all

Whenflagistrue, the code generator unrolls thefor循环。Whenflagisfalse, the code generator produces afor-loop in the generated code.flagis evaluated at code generation time.

Tips

  • Sometimes, the code generator unrolls afor-loop even though you do not usecoder.unroll. For example, if afor-loop indexes into a heterogeneous cell array or intovararginorvarargout, 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循环。如果启发式足总il to identify that unrolling 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 afor-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 and parfor-Loops.

Extended Capabilities

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

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

版本历史

Introduced in R2011a