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.
Consider the following example written in C:
The essential problem with this code is that each iteration of the inner loop on <code>a[i][j]</code> requires that the previous iteration's result, <code>a[i][j - 1]</code>, 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 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 <code>a(i, j) = a[i-j][j]</code> then <code>a(i, j)</code> only depends on <code>a(i - 1, x)</code>, with . (However, each iteration of the outer loop does depend on previous iterations.)
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 <code>src</code> contains <code>h</code> rows of <code>w</code> pixels, each pixel having a grayscale value between 0 and 255 inclusive. After the routine has finished, the output array <code>dst</code> 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 <code>src</code> array. (Notice that <code>src</code> and <code>dst</code> are both read and written during the computation; <code>src</code> is not read-only, and <code>dst</code> is not write-only.)
Each iteration of the inner loop modifies the values in <code>src[i][j]</code> based on the values of <code>src[i-1][j]</code>, <code>src[i][j-1]</code>, and <code>src[i+1][j-1]</code>. (The same dependencies apply to <code>dst[i][j]</code>. For the purposes of loop skewing, we can think of <code>src[i][j]</code> and <code>dst[i][j]</code> as the same element.) We can illustrate the dependencies of <code>src[i][j]</code> graphically, as in the diagram on the right.
Performing the affine transformation 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 <code>p</code> and <code>t</code> instead of <code>i</code> and <code>j</code>, obtaining the following "skewed" routine.