Techniquecore
Strip mining
Also called loop sectioning.
Splits one loop into an outer loop over strips and an inner loop over a fixed number of iterations, as for vector registers.
Strip mining splits one loop into two: an outer loop that steps over strips of consecutive iterations, and an inner loop that runs the iterations of one strip. The last strip is shorter when does not divide the trip count. The iterations run in the same order as before, so strip mining is always legal and changes no data dependence. It gives no speedup by itself. Its value is the new loop level it creates: the inner strip loop can become one vector instruction of length (vectorization), a chunk of work for one thread, or, after loop interchange moves the strip loop outward, a tile of a tiled nest.
In plain words
Think of reading a long list of numbers to a person who can only keep four of them in mind at a time. You read them four by four, in the same order as before, and the last group may be smaller. Nothing about the list or the order changes, so the result is the same. What changes is that the work now comes in groups of a fixed size, and other tools can use that. A processor with registers that hold four numbers can do one group in one step, and a cache can keep one group while it is in use. Splitting into groups is safe. Doing a group all at once is a separate question.
When it applies
Take a counted loop for i = L to U: BODY(i) with step 1, and a strip size . The conditions are the same as for loop unrolling: and are evaluated once, BODY does not assign , and the bound is computed without overflow. must be a positive constant or a value that does not change during the loop. No dependence test is needed for the split itself.
A loop with step is first normalized to step 1, so that the strips count iterations and not index values.
The template
STRIP-MINE(for i = L to U: BODY(i), strip size B)- 1
for is = L to U step B do ▷ strip loop: is = L, L + B, L + 2B, ... - 2
for i = is to min(is + B - 1, U) do ▷ element loop: at most B iterations - 3
BODY(i)
The holes are the body, the bounds and , and the strip size . When divides the trip count , the min is not needed and every strip has exactly iterations. Otherwise a compiler often runs the full strips with the simple bound and handles the last, short strip in a separate copy of the loop, the same way as the remainder loop of unrolling.
Legality. The strips are the sets for . Every with is in exactly one strip, the one with , because holds for this value and for no other multiple. The strips run in increasing order of , and inside a strip increases. So implies that runs first: either both are in one strip and the inner loop runs first, or the strip of has a smaller and the outer loop runs it first. The new code therefore runs in the original order, and from the same state it reaches the same final state. Every dependence keeps its source before its sink.
In the distance notation. The new nest has iteration vectors . A dependence of the original loop with distance from to has, in the new nest, the distance , where is the strip of . The first entry is or positive, so the new distance vector is lexicographically positive, as the proof says. The first entry depends on where is in its strip, so one old distance gives several new ones. For and the new distances are inside a strip and across the boundary of two strips.
Profit. None by itself: the loop runs the same work with a little more control code. The profit comes from the step that follows.
Classic examples
Strips of 4 over 10 iterations. For for i = 1 to 10, with :
| strip | inner bound | iterations | |
|---|---|---|---|
| 1 | 1 | 4 | 1, 2, 3, 4 |
| 2 | 5 | 8 | 5, 6, 7, 8 |
| 3 | 9 | 10 | 9, 10 |
The order of the iterations is , as before.
Vector code. On a machine with vector registers of length 4, the loop for i = 1 to n: A[i] = B[i] + C[i] is strip-mined with , and the inner loop becomes one vector load of each operand, one vector add and one vector store. Here a second condition enters, because the vector statement does not run the four iterations one by one: it reads all four operands before it writes any result. That is legal for this loop, which has no loop-carried dependence. For A[i] = A[i-3] + 1 it is not legal with : in the strip , iteration 4 reads , which iteration 1 of the same strip writes, and the vector load reads the old value. With every read of a strip refers to an element that an earlier strip wrote, and the vector code is correct. The strip mining is legal in both cases. The vector execution of a strip is a separate step, with its own condition, given on the vectorization page.
Chunks for threads. A DOALL loop of iterations is strip-mined with for threads, and the strip loop becomes a parallel for: thread runs strip . Each thread then works on a contiguous range of the arrays, which keeps cache lines from being shared between threads.
The first half of tiling. In blocked matrix multiplication, each of the loops , and is strip-mined, and the three strip loops are moved outward with interchange. The interchange is the step that needs a dependence test, given on the loop tiling page.
Variations
- Strip size equal to the vector length. For vector machines and SIMD units, is the number of elements a vector register holds, and the short last strip is done with a mask or with scalar code.
- Strip mining with alignment. The first strip is made shorter, so that the following strips start at an address that is a multiple of the vector size. This is strip mining with a shifted origin.
- Strip mining a nest. Strip-mine each loop of a nest and then move the strip loops outward. That is loop tiling.
- Unknown strip size. On vector machines with a vector length register, is read at run time, and the bound is computed once per strip.
Pitfalls
- Missing
minin the inner bound. Withfor i = is to is + B - 1, the last strip runs past when does not divide the trip count. - Running the strip as a vector without a check. The split keeps the order, but a vector instruction does not. A flow dependence with a distance less than makes the vector form wrong, as in the example
A[i] = A[i-3] + 1with . - Expecting a cache gain from the split alone. The order of the memory accesses does not change, so the cache misses do not change. Locality improves only when another loop is moved inside the strip loop, as in tiling.
- Running the strip loop in parallel. Strip mining does not remove a loop-carried dependence. The strips of a loop with a carried dependence must still run one after the other.
Exercises
- Strip-mine
for i = 3 to 17with . List the strips. Answer. ; the strips are , and , all full, because the trip count is . - In which strip is for and ? Answer. , so the strip , the third one.
- A loop has a flow dependence with distance 5. What is the largest strip size for which each strip can run as one vector statement? Answer. . Every read in a strip of length at most 5 refers to an element written at least 5 iterations earlier, so in an earlier strip.
- Strip-mine a loop with full strips and then interchange the two new loops. When is the result legal? Answer. The new outer loop runs over the position from to , and the inner loop over the strips, so the order is . A dependence with distance that is a multiple of keeps and goes to a later strip, so it keeps its order. If is not a multiple of , a source with has its sink at a smaller position, and the sink runs first. So, for a loop with enough strips, the result is legal exactly when every loop-carried distance is a multiple of .
References
- Michael Wolfe, More iteration space tiling, Proceedings of Supercomputing '89, ACM, 1989, 655-664, strip mining as the one-loop case, and tiling as strip mining followed by loop interchange
- Michael Wolfe, High Performance Compilers for Parallel Computing, Addison-Wesley, 1996, the chapter on loop restructuring, strip mining and its use for vector registers and for tiling
- Randy Allen and Ken Kennedy, Optimizing Compilers for Modern Architectures, a Dependence-Based Approach, Morgan Kaufmann, 2002 (copyright 2001), Chapter 9 (Managing Cache), strip-mine-and-interchange as the basic step of blocking
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 QXEEEZ Edit this page History Report an error Markdown