Techniquecore
Loop tiling
Also called loop blocking, cache blocking.
Splits the iteration space of a nest into blocks that fit in cache and runs each block fully, for data reuse and parallelism.
Loop tiling replaces a nest of loops by a nest that runs the iteration space block by block. Each block, a tile, is a small rectangle of iterations, and the program finishes one tile before it starts the next. The construction is strip mining of each loop of a band, followed by interchange that moves the strip loops outward. Tiling is legal on a fully permutable band: a set of adjacent loops in which every dependence that the outer loops do not carry has a nonnegative distance on every loop of the band. The gain is reuse. In a tile, the data touched is small enough to stay in cache while every iteration of the tile uses it, so a program like matrix multiplication loads each element a few times instead of once per use. Tiles are also units of parallel work, on CPUs and as the shared-memory tiles of GPUs.
In plain words
Picture a big field that you water row after row. When you reach the end of a row, the hose you used at its start has to be pulled all the way back. Tiling means you water the field in small squares instead: finish one square, then move to the next. Everything you need for one square stays within reach. A program does the same with its data. In a small square of work, the values it uses are still in the fast memory when they are needed again. The order of the work changes, so it is safe only when no square needs a result from a square that now runs later. Choosing the square size is the second part: too large, and the data no longer fits.
When it applies
The nest has a band of adjacent loops with step 1. The band is fully permutable: every dependence distance vector with has for . Dependences carried by the loops outside the band, which have a positive entry before , are free. A band of one loop is always fully permutable. For direction vectors, the condition is that no such vector has or on the band.
If the band has a dependence with a negative entry, such as , the band is not fully permutable. Loop skewing by a large enough factor makes it so, and then the skewed band can be tiled. The tile sizes are positive and do not change while the nest runs.
The template
For a band of two loops with rectangular bounds:
TILE(for i = L1 to U1: for j = L2 to U2: BODY(i, j), sizes B1, B2)- 1
if some dependence d has d_1 < 0 or d_2 < 0 then - 2
return "not fully permutable: skew first" - 3
for it = L1 to U1 step B1 do ▷ tile loops - 4
for jt = L2 to U2 step B2 do - 5
for i = it to min(it + B1 - 1, U1) do ▷ element loops - 6
for j = jt to min(jt + B2 - 1, U2) do - 7
BODY(i, j)
The holes are the band, the tile sizes, and the order of the element loops inside a tile, which is free on a fully permutable band. For a band of loops there are tile loops followed by element loops. When the bounds are not rectangular, for example after skewing, the bounds of the tile loops and element loops come from Fourier-Motzkin elimination of the original bounds together with .
Legality. Let be the tile number of the iteration on loop . The tiled nest runs the iterations in the lexicographic order of the key
Claim. If the band is fully permutable, then every dependence from to has .
Proof. If the first nonzero entry of is before , then and differ first in that entry, and . Otherwise , and full permutability gives , so for every band loop. The floor function does not decrease, so for each . If some tile number differs, the first one that differs is larger in , and . If all tile numbers are equal, the rest of the two keys is and without their common prefix, and is lexicographically positive with zeros before , so again .
With Theorem 1 of the legality page, the tiled nest computes the same result. The condition is also needed in general: if and , the source at the first column of a tile and the sink in the previous column of tiles get , and when both rows are in one row of tiles, the sink runs first.
Profit. The cost model is the number of cache misses. If the data that one tile touches fits in the cache, each element is loaded once per tile that uses it, instead of once per row or column of the whole space. For matrix multiplication with tiles in a cache too small for a whole matrix, this brings the misses from about for the order , which reads B down a column, and about for the order , down to a small constant times , where is the number of elements in a cache line. A rule of thumb for three operands is the cache size in elements.
Classic examples
Matrix multiplication. The nest for i: for k: for j: C[i][j] = C[i][j] + A[i][k] * B[k][j] has the dependence distance on C[i][j] in the order . All entries are nonnegative, so the three loops form a fully permutable band, and tiling all three is legal. With , lines of 8 elements, the three arrays in separate memory and a fully associative LRU cache, the counted misses are:
| loop order | 32 lines (256 elements) | 64 lines (512 elements) |
|---|---|---|
| , no tiling | 33025 | 4353 |
| , no tiling | 4353 | 4353 |
| , tiles | 1853 | 1161 |
| , tiles | 4735 | 895 |
The lowest possible count is , one miss per line of each matrix. Tiles of 8 cut the misses by a factor of about 2.3 and 3.7 against the best untiled order. Tiles of 16 are better when the cache has 512 elements and worse than no tiling at all when it has 256: one tile of B fills the whole small cache. The tiled code gives the same matrix as the original, with the same order of the additions into each C[i][j], because the loop still runs in increasing order inside and across tiles. Blocked matrix multiplication and cache-oblivious matrix multiplication treat this problem in depth.
A stencil after skewing. A Gauss-Seidel style sweep A[i][j] = (A[i-1][j] + A[i][j-1] + A[i-1][j+1]) / 3, with the time step and the position, has the distances , and . The last one has a negative entry, so the band is not fully permutable, and tiles in are illegal. Skewing maps the distances to , and , and the skewed nest can be tiled. In the original space, each tile is a parallelogram. The loop skewing page gives the factor.
Parallel tiles. On a fully permutable band of depth 2, the tiles on one anti-diagonal do not depend on each other, because every dependence goes to a tile with an equal or larger number in each coordinate, and so to a larger sum, or to the same tile. The tiles of one anti-diagonal can run at the same time, as a wavefront.
Variations
- Tiling for registers. Tiles of or with the element loops fully unrolled keep a small block of
Cin registers. This is unroll and jam seen as tiling. - Multilevel tiling. Tile the tile loops again, with one size per level of the memory hierarchy: registers, L1, L2.
- Non-rectangular tiles. Skewing then tiling gives parallelogram tiles in the original space. The Pluto algorithm chooses the hyperplanes of the tiles by an integer program.
- Cache-oblivious recursion. Splitting the space recursively in halves gives tiles of every size at once and needs no tile size. See the cache-oblivious model.
- Copying. Copy a tile of an operand into a contiguous buffer before use. This removes the conflict misses described below, at the cost of the copy.
Pitfalls
- Tiling a band that is not fully permutable. One distance is enough to make tiling wrong. Check the band, and skew first.
- Tiles too large. A tile whose data does not fit runs slower than the untiled loop, as the 256-element column of the table shows.
- Conflict misses. In a direct-mapped or low-associativity cache, the rows of a tile can map to the same sets, for example when the row length is a power of two. Lam, Rothberg and Wolf measured that the misses then depend strongly on the tile size and the matrix size. Choose a tile size from the cache geometry, or copy the tile.
- Partial tiles. Missing
minbounds run iterations outside the space when does not divide the trip count. - Reductions. If the band is fully permutable only after a reduction loop is treated as free of dependences, the floating-point result can change.
Exercises
- A nest has the distances , and . Can both loops be tiled? Answer. Yes, all entries are nonnegative, so the band is fully permutable.
- A nest has the distances and . Which skewing factor in makes the band fully permutable? Answer. The new distances are and , so is the smallest one.
- For
for i = 1 to 10: for j = 1 to 10and tiles of , how many tiles are there and how many iterations does the last tile have? Answer. tiles. The tile with has iterations. - A cache holds 4096 elements. What is the largest square tile for matrix multiplication by the rule cache size? Answer. , since and .
References
- François Irigoin and Rémi Triolet, Supernode partitioning, Proceedings of the 15th ACM Symposium on Principles of Programming Languages (POPL), 1988, 319-329, the partition of the iteration space into tiles (supernodes) by hyperplanes, and the condition that no dependence goes from a later tile back to an earlier one
- Michael Wolfe, More iteration space tiling, Proceedings of Supercomputing '89, ACM, 1989, 655-664, tiling as strip mining plus interchange, its legality, and skewing before tiling
- Monica S. Lam, Edward E. Rothberg and Michael E. Wolf, The cache performance and optimizations of blocked algorithms, Proceedings of ASPLOS IV, 1991, 63-74, the measured cache behavior of blocked matrix multiplication, self-interference misses in caches of low associativity, and the choice of the block size
- Michael E. Wolf and Monica S. Lam, A data locality optimizing algorithm, Proceedings of the ACM SIGPLAN '91 Conference on Programming Language Design and Implementation (PLDI), 1991, 30-44, fully permutable loop nests as the loops that can be tiled, and the choice of the loops to tile
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag FMPTMB Edit this page History Report an error Markdown