beautypg.com

Vectorized code, Types of loops, Vectorized code -31 types of loops -31 – National Instruments AutoCode NI MATRIX User Manual

Page 139

background image

Chapter 5

Generated Code Architecture

© National Instruments Corporation

5-31

AutoCode Reference

Example 5-11

Local Variables Used to Allow Loops in Scalar Code Generation

Inputs: u;

Outputs: y;

float u(:), y(u.size), local_u(u.size), local_y(y.size);

integer i,j;

for i = 1:u.size do

local_u(i) = u(i);

endfor;

for i = 1:u.size do

for j = i:u.size do

local_y(i) = local_u(i) + local_u(j);

endfor;

endfor;

for i = 1:y.size do

y(i) = local_y(i);

endfor;

Although the generated code is not very efficient, the amount of code that
is generated for Example 5-11 is far less than if the local variables were not
used for the same algorithm.

Vectorized Code

AutoCode can generate vectorized code (refer to Chapter 6,

Vectorized

Code Generation

) and at the most basic level, it means that arrays will be

used instead of scalar variables. Given that, AutoCode does not have to
translate the BlockScript inputs, outputs, and states into scalars, rather it
will generate arrays. As a result, when AutoCode is generating vectorized
code, the soft-script limitation does not apply. Therefore, if generating
vectorized code, Example 5-10 generates code, and the trick to use local
variable as shown in Example 5-11 is not needed.

Types of Loops

BlockScript provides two different types of loops. Each loop type has a
specific usage that effects what you are allowed to use within the loop
body.

WHILE

Loop—Always generate a rolled loop for both scalar and

vectorized code. A soft-subscript is never allowed to access inputs,
outputs, or states.