Chapter 5
Generated Code Architecture
5-30
ni.com
In Example 5-9, a hard-subscript,
i
, is used to access both inputs and
outputs. The reason
i
is a hard-subscript is that the range of
i
—that is,
the values of 1 through to the number of inputs (
u.size
)—is known when
the code is generated. AutoCode then can unroll the loop so as to translate
y(i)
into a single scalar variable for each value of
i
. The same is true
for
u(i)
.
Example 5-9
Hard-Subscript Used to Access an Array
Inputs: u;
Outputs: y;
float u(:), y(u.size);
integer i;
for i = 1:u.size do
y(i) = u(i) * 2*i;
endfor;
In Example 5-10, a soft-subscript,
j
, is being used to access both inputs and
outputs. That script will not generate code. The reason
j
is a soft-subscript
is that the range of
j
, that is, the values of 1 through to the value of the first
input
u(1)
, is not known when code is generated because the upper-bound
of
j
’s range is the value of the first input. Because the range of
j
is not
known, AutoCode cannot unroll the loop so as to translate the input/output
arrays into their scalar representation.
Example 5-10
Soft-Subscript Example
Inputs: u;
Outputs: y;
integer u(:), y(u.size);
integer j;
for j = 1:u(1)do
y(j) = u(j) + j;
endfor;
Rolling Loops with Scalar Code Generation
Although inputs, outputs, and states are translated into scalars, parameters
and local variables of the BlockScript can be either scalar or array variables
in the generated code. Thus, a common trick has been to copy the inputs
and outputs into local variables that are arrays and use those variables for
the algorithm. Refer to Example 5-11.