Polytope model

The polyhedral model (also called the polytope method) is a mathematical framework for programs that perform large numbers of operations -- too large to be explicitly enumerated -- thereby requiring a compact representation. Nested loop programs are the typical, but not the only example, and the most common use of the model is for loop nest optimization in program optimization. The polyhedral method treats each loop iteration within nested loops as lattice points inside mathematical objects called polyhedra, performs affine transformations or more general non-affine transformations such as tiling on the polytopes, and then converts the transformed polytopes into equivalent, but optimized (depending on targeted optimization goal), loop nests through polyhedra scanning.

Simple example
Consider the following example written in C:

The essential problem with this code is that each iteration of the inner loop on  requires that the previous iteration's result, , be available already. Therefore, this code cannot be parallelized or pipelined as it is currently written.

An application of the polytope model, with the affine transformation $$(i',\, j') = (i+j,\, j)$$ and the appropriate change in the boundaries, will transform the nested loops above into:

In this case, no iteration of the inner loop depends on the previous iteration's results; the entire inner loop can be executed in parallel. Indeed, given  then   only depends on , with $$x \in \{j - 1,\, j\}$$. (However, each iteration of the outer loop does depend on previous iterations.)

Detailed example
The following C code implements a form of error-distribution dithering similar to Floyd–Steinberg dithering, but modified for pedagogical reasons. The two-dimensional array  contains   rows of   pixels, each pixel having a grayscale value between 0 and 255 inclusive. After the routine has finished, the output array  will contain only pixels with value 0 or value 255. During the computation, each pixel's dithering error is collected by adding it back into the  array. (Notice that  and   are both read and written during the computation;   is not read-only, and   is not write-only.)

Each iteration of the inner loop modifies the values in  based on the values of ,  , and. (The same dependencies apply to . For the purposes of loop skewing, we can think of   and   as the same element.) We can illustrate the dependencies of   graphically, as in the diagram on the right.



Performing the affine transformation $$(p,\, t) = (i,\, 2j+i)$$ on the original dependency diagram gives us a new diagram, which is shown in the next image. We can then rewrite the code to loop on  and   instead of   and , obtaining the following "skewed" routine.

External links and references

 * "The basic polytope method", tutorial by Martin Griebl containing diagrams of the pseudocode example above
 * "Code Generation in the Polytope Model" (1998). Martin Griebl, Christian Lengauer, and Sabine Wetzel
 * "The CLooG Polyhedral Code Generator"
 * "CodeGen+: Z-polyhedra scanning"
 * PoCC: the Polyhedral Compiler Collection
 * PLUTO - An automatic parallelizer and locality optimizer for affine loop nests
 * polyhedral.info - A website that collect information about polyhedral compilation
 * Polly - LLVM Framework for High-Level Loop and Data-Locality Optimizations
 * The MIT Tiramisu Polyhedral Framework.
 * The MIT Tiramisu Polyhedral Framework.