To effectively analyze and transform the loop nests common in tensor computations, the polyhedral model employs a precise mathematical framework. This framework relies on three fundamental components: iteration domains to define when computations happen, access functions to define what data is touched, and dependence analysis to understand the constraints on reordering computations. Let's examine each of these.
Each execution of a statement inside a potentially nested loop structure occurs at a specific point defined by the values of the surrounding loop indices. We call such a specific execution a statement instance. The polyhedral model captures the set of all possible integer index vectors for which a statement executes. This set is known as the Iteration Domain.
Formally, for a statement S nested within d loops with index variables i1,i2,…,id, the iteration domain DS is a set of integer vectors iter=(i1,i2,…,id)∈Zd. This set is defined by the loop bounds, which must be expressible as affine inequalities involving the loop indices and potentially symbolic constants (parameters) representing unknown problem sizes (like matrix dimensions).
An iteration domain DS can be represented as:
DS={iter∈Zd∣A⋅iter+b≥0}Here, A is an integer matrix and b is an integer vector representing the constraints derived from the loop bounds. The inequality is element-wise. Any set of integers definable by such linear inequalities forms an integer polyhedron.
Example: Matrix Multiplication
Consider the core computation in matrix multiplication C=A×B:
// Assuming C is initialized to zero
for (int i = 0; i < M; ++i) { // Loop index i
for (int j = 0; j < N; ++j) { // Loop index j
for (int k = 0; k < P; ++k) { // Loop index k
// Statement S1:
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
}
}
The statement S1 is nested within three loops. An instance of S1 is identified by the vector iter=(i,j,k). The iteration domain DS1 is the set of all valid (i,j,k) tuples defined by the loop bounds:
DS1={(i,j,k)∈Z3∣0≤i<M,0≤j<N,0≤k<P}This can be written using affine inequalities: i≥0 −i+(M−1)≥0 (equivalent to i≤M−1, or i<M) j≥0 −j+(N−1)≥0 k≥0 −k+(P−1)≥0
This forms a bounded integer polyhedron (a rectangular prism) in the 3D iteration space (i,j,k). M,N,P are symbolic parameters.
Within each iteration iter∈DS, the statement S typically reads from or writes to memory locations, often elements of arrays (tensors). An Access Function describes which memory location is accessed by a particular statement instance.
The polyhedral model requires these access functions to be affine functions of the iteration vector iter. For an array access like Array[index_expr_1]...[index_expr_n]
, the function maps the iteration vector iter to the array index vector (index_expr_1,…,index_expr_n). This mapping must take the form:
where M is an integer matrix and c is an integer vector.
Example: Matrix Multiplication Accesses
Continuing with the matrix multiplication example, statement S1 involves four accesses: a read and write to C
, a read from A
, and a read from B
. Let the iteration vector be iter=(i,j,k).
Write to C[i][j]
: The accessed index is (i,j). The access function FC,write maps (i,j,k) to (i,j).
This is affine.
Read from C[i][j]
: The accessed index is (i,j). The access function FC,read is identical to FC,write.
Read from A[i][k]
: The accessed index is (i,k). The access function FA,read maps (i,j,k) to (i,k).
This is affine.
Read from B[k][j]
: The accessed index is (k,j). The access function FB,read maps (i,j,k) to (k,j).
This is also affine.
Since all iteration domains are defined by affine inequalities and all access functions are affine, the loop nest is suitable for analysis within the polyhedral model.
Optimizations like changing loop order, tiling, or parallelization are only valid if they preserve the program's semantics. This requires understanding the data dependencies between different statement instances. A data dependence exists between two statement instances if they access the same memory location and at least one of the accesses is a write.
The standard types of data dependencies are:
To formally identify dependencies using the polyhedral framework, we look for pairs of iterations (iter1,iter2) such that:
The set of all pairs (iter1,iter2) satisfying these conditions for a specific type of dependence (e.g., flow) defines the dependence relation. This relation itself can often be represented as another polyhedron.
Example: Dependence in a Simple Loop
Consider this loop:
for (int i = 1; i < N; ++i) {
A[i] = A[i-1] + B[i]; // Statement S
}
A[i]
: Fwrite(i)=iA[i-1]
: Fread(i)=i−1Let's find flow dependencies (Write then Read). We need iter1=i1 and iter2=i2 such that:
Combining these conditions: 1≤i1<N, 1≤i2<N, i1<i2, and i1=i2−1. This simplifies to: for any i2 such that 2≤i2<N, there is a flow dependence from iteration i1=i2−1. The dependence exists from iteration i to iteration i+1 for all i from 1 to N−2. The dependence vector is d=iter2−iter1=i2−i1=1. This indicates that iteration i+1 depends on the result computed in iteration i.
Flow dependencies in the example loop. Each iteration i writes to
A[i]
, which is read by the next iteration i+1. The dependence vector is constant, d=(1).
Dependence Analysis in Matrix Multiplication
Applying this to the matrix multiplication example for the accesses to C[i][j]
:
These dependencies tell us that, for a given (i,j), the iterations of the k loop cannot be arbitrarily reordered or fully parallelized without mechanisms like reductions, because each iteration depends on the previous one's partial sum stored in C[i][j]. However, iterations with different (i,j) values are independent concerning the C array accesses.
By precisely defining iteration domains, access functions, and the resulting data dependencies, the polyhedral model provides the foundation needed to mathematically reason about loop nests and derive complex, correctness-preserving transformations for performance optimization. The next step involves using this dependence information to construct valid schedules that map iterations to execution time steps, enabling optimizations like tiling and parallelization.
© 2025 ApX Machine Learning