Aussie AI

Loop Iterator Strength Reduction

  • Book Excerpt from "Generative AI in C++"
  • by David Spuler, Ph.D.

Loop Iterator Strength Reduction

Loop strength reduction is the arithmetic optimization of “strength reduction” applied to loop iteration variables. For example, strength reduction aims to replace multiplication with addition. Consider this loop:

    for (int i = 0; i < n; i++) {
        a[i] = 10 * i;
    }

This can be optimized to change the multiplication into an incremental addition:

    for (int i = 0, x = 0; i < n; i++) {
        a[i] = x;
        x += 10;
    }

Note that the loop strength reduction optimization isn't a good choice for loop parallelization. Although it would be desirable to change a vectorized multiplication to addition, this optimization has changed to an incremental algorithm. This makes each loop iteration dependent on the prior one, with the results dependent on the previous computation, so they cannot be done in parallel.

 

Next:

Up: Table of Contents

Buy: Generative AI in C++: Coding Transformers and LLMs

Generative AI in C++ The new AI programming book by Aussie AI co-founders:
  • AI coding in C++
  • Transformer engine speedups
  • LLM models
  • Phone and desktop AI
  • Code examples
  • Research citations

Get your copy from Amazon: Generative AI in C++