Aussie AI

Loop Collapsing

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

Loop Collapsing

Loop collapsing is closely related to loop coalescing, since both aim to flatten nested loops, but loop collapsing is a special situation where the array is also flattened to one dimension.

Consider a matrix initialization via nested loops over a 2-dimensional array:

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            arr[i][j] = 0.0f;
        }
    }

The loop collapsed version has one big loop over a different one-dimensional array:

    int maxx = n * m;
    for (int x = 0; x < maxx; x++) {
        arr2[x] = 0.0f;
    }

This loop transformation to a single loop is obviously more amenable to vectorization.

 

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