Normalized loop

Normalized loop

In computer science, a normalized loop (sometimes called well-behaved loop), is a loop which the loop variable starts at 0 (or any constant) and get incremented by one at every iteration until the exit condition is met. Normalized loops are very important for compiler theory, loop dependence analysis as they simplify the data dependence analysis.

Contents

Well-behaved loops

A well behaved loop is normally of the form:

for ( i = 0; i < MAX; i++ )
  a[i] = b[i] + 5;

Because the increment is unitary and constant, it's very easy to see that, if both a and b are bigger than MAX, this loop will never access memory outside the allocated range.

Non-normalized loops

A non-normalized loop may begin at different indexes, increment by not-unitary amounts and have exit conditions complicated to define. Such loops are hard to optimize, vectorize and even traverse, especially if functions are executed on any part of the loop conditions.

A simple example, where it doesn't start at the beginning and increments by more than one:

// Example 1
for ( i = 7; i < MAX; i+=3 )
  a[i] = b[i] + 5;

A more complicated example, with an additional exit condition:

// Example 2
for ( i = 7; i < MAX || i > MIN; i+=3 )
  a[i] = b[i] + 5;

Loops can also have non-predictable behaviour during compilation time, where the exit condition depends on the contents of the data being modified:

// Example 3
for ( i = 7; i < MAX && a[i]; i+=3 )
  a[i] = b[i] + 5;

Or even dynamic calculations by means of function calls:

// Example 4
for ( i = start(); i < max(); i+=increment() )
  a[i] = b[i] + 5;

Reverse loops are also very simple, and can be easily normalized:

// Example 5
for ( i = MAX; i > 0; i-- )
  a[i] = b[i] + 5;

Converting to a normalized loop

If the non-normalized doesn't have dynamic behaviour, it's normally very easy to transform it to a normalized one. For instance, the fist example (Example 1) above can easily be converted to:

// Example 1 -> normalized
for ( i = 0; i < MAX/3-7; i++ )
  a[i*3+7] = b[i*3+7] + 5;

While the third example can be partially normalized to allow some parallelization, but still lack the ability to know the loop span (how many iterations there will be), making it harder to vectorize by using multi-media hardware.

Starting at 7 is not so much of a problem, as long as the increment is regular, preferably one. When multiple statements inside the loop use the index, some private temporary variables may be created to cope with the different iteration paces.

The reverse loop (Example 5) is also easy to normalize:

// Example 5 -> normalized
for ( i = 0; i < MAX; i++ )
  a[MAX-i] = b[MAX-i] + 5;

Note that the access is still backwards. In this case, it makes no sense to leave it backwards (as there is no data dependence), but where dependences exist, caution must be taken to revert the access as well, as it could disrupt the order of assignments.

Impossible conversions

The last example above makes it impossible to predict anything from that loop. Unless the functions themselves are trivial (constant), there is no way to know where the loop will start, stop and how much it'll increment each iteration. Those loops are not only hard to parallelize, but they also perform horribly.

Each iteration, the loop will evaluate two functions (max and increment). Even if the functions are inlined, the condition becomes too complex to be worth optimizing. The programmer should take extra care not to create those loops unless strictly necessary (if ever).

Another danger of such loops appear if the evaluation depends on the data being modified. For instance, a normal error when using iterators is to remove items from a list while modifying it, or relying on sizes (for exit condition) that are not true any more.

See also


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Loop dependence analysis — In compiler theory, loop dependence analysis is the task of determining whether statements within a loop body form a dependence, almost always with respect to array access and modification. For a normalized loop: for i1 from l1 to u1 do for i2… …   Wikipedia

  • H-infinity loop-shaping — is a design methodology in modern control theory. It combines the traditional intuition of classical control methods with H infinity optimization techniques to achieve controllers whose stability and performance properties hold good in spite of… …   Wikipedia

  • Parallélisation interprocédurale de programmes scientifiques — Pour les articles homonymes, voir PIPS. PIPS Développeur Centre de Recherc …   Wikipédia en Français

  • Bode plot — ; the straight line approximations are labeled Bode pole ; phase varies from 90° at low frequencies (due to the contribution of the numerator, which is 90° at all frequencies) to 0° at high frequencies (where the phase contribution of the… …   Wikipedia

  • Chern–Simons theory — The Chern–Simons theory is a 3 dimensional topological quantum field theory of Schwarz type, introduced by Edward Witten. It is so named because its action is proportional to the integral of the Chern–Simons 3 form. In condensed matter physics,… …   Wikipedia

  • Chern-Simons theory — The Chern Simons theory is a 3 dimensional topological quantum field theory of Schwarz type, developed by Shiing Shen Chern and James Harris Simons. In condensed matter physics, Chern Simons theory describes the topological orderin fractional… …   Wikipedia

  • Feynman diagram — The Wick s expansion of the integrand gives (among others) the following termNarpsi(x)gamma^mupsi(x)arpsi(x )gamma^ upsi(x )underline{A mu(x)A u(x )};,whereunderline{A mu(x)A u(x )}=int{d^4pover(2pi)^4}{ig {mu u}over k^2+i0}e^{ k(x x )}is the… …   Wikipedia

  • Tropical cyclone — Hurricane redirects here. For other uses, see Hurricane (disambiguation). Hurricane Isabel (2003) as seen from orbit during Expedition 7 of the International Space Station. The eye, eyewall and surrounding rainbands that are characteristics of… …   Wikipedia

  • Euler spiral — A double end Euler spiral. An Euler spiral is a curve whose curvature changes linearly with its curve length (the curvature of a circular curve is equal to the reciprocal of the radius). Euler spirals are also commonly referred to as spiros,… …   Wikipedia

  • Viscoplasticity — Figure 1. Elements used in one dimensional models of viscoplastic materials. Viscoplasticity is a theory in continuum mechanics that describes the rate dependent inelastic behavior of solids. Rate dependence in this context means that the… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”