Algorithm Search⌘K

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 BB consecutive iterations, and an inner loop that runs the iterations of one strip. The last strip is shorter when BB 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 BB (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 B≥1B \ge 1. The conditions are the same as for loop unrolling: LL and UU are evaluated once, BODY does not assign ii, and the bound is+B−1is + B - 1 is computed without overflow. BB 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 s>1s > 1 is first normalized to step 1, so that the strips count iterations and not index values.

The template

  1. STRIP-MINE(for i = L to U: BODY(i), strip size B)
  2. 1for is = L to U step B do ▷ strip loop: is = L, L + B, L + 2B, ...
  3. 2for i = is to min(is + B - 1, U) do ▷ element loop: at most B iterations
  4. 3BODY(i)

The holes are the body, the bounds LL and UU, and the strip size BB. When BB divides the trip count U−L+1U - L + 1, the min is not needed and every strip has exactly BB iterations. Otherwise a compiler often runs the full strips with the simple bound is+B−1is + B - 1 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 {is,…,min⁡(is+B−1,U)}\{is, \dots, \min(is + B - 1, U)\} for is=L,L+B,L+2B,⋯≤Uis = L, L + B, L + 2B, \dots \le U. Every ii with L≤i≤UL \le i \le U is in exactly one strip, the one with is=L+B⌊(i−L)/B⌋is = L + B \lfloor (i - L)/B \rfloor, because is≤i≤is+B−1is \le i \le is + B - 1 holds for this value and for no other multiple. The strips run in increasing order of isis, and inside a strip ii increases. So i<i′i < i' implies that ii runs first: either both are in one strip and the inner loop runs ii first, or the strip of ii has a smaller isis and the outer loop runs it first. The new code therefore runs BODY(L),…,BODY(U)\mathrm{BODY}(L), \dots, \mathrm{BODY}(U) 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 (is,i)(is, i). A dependence of the original loop with distance d>0d > 0 from ii to i+di + d has, in the new nest, the distance (is′−is,d)(is' - is, d), where is′is' is the strip of i+di + d. The first entry is 00 or positive, so the new distance vector is lexicographically positive, as the proof says. The first entry depends on where ii is in its strip, so one old distance gives several new ones. For d=1d = 1 and B=4B = 4 the new distances are (0,1)(0, 1) inside a strip and (4,1)(4, 1) 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 B=4B = 4:

strip isis inner bound min⁡(is+3,10)\min(is + 3, 10) 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 1,2,…,101, 2, \dots, 10, 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 B=4B = 4, 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 B=4B = 4: in the strip 1,…,41, \dots, 4, iteration 4 reads A[1]A[1], which iteration 1 of the same strip writes, and the vector load reads the old value. With B=3B = 3 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 nn iterations is strip-mined with B=⌈n/p⌉B = \lceil n/p \rceil for pp threads, and the strip loop becomes a parallel for: thread tt runs strip tt. 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 ii, jj and kk 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, BB 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, BB is read at run time, and the bound min⁡(is+B−1,U)\min(is + B - 1, U) is computed once per strip.

Pitfalls

  • Missing min in the inner bound. With for i = is to is + B - 1, the last strip runs past UU when BB 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 BB makes the vector form wrong, as in the example A[i] = A[i-3] + 1 with B=4B = 4.
  • 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

  1. Strip-mine for i = 3 to 17 with B=5B = 5. List the strips. Answer. is=3,8,13is = 3, 8, 13; the strips are 3..73..7, 8..128..12 and 13..1713..17, all full, because the trip count is 1515.
  2. In which strip is i=22i = 22 for L=1L = 1 and B=8B = 8? Answer. is=1+8⌊21/8⌋=17is = 1 + 8 \lfloor 21/8 \rfloor = 17, so the strip 17..2417..24, the third one.
  3. 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. 55. 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.
  4. 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 c=i−isc = i - is from 00 to B−1B - 1, and the inner loop over the strips, so the order is L,L+B,L+2B,…,L+1,L+1+B,…L, L + B, L + 2B, \dots, L + 1, L + 1 + B, \dots. A dependence with distance dd that is a multiple of BB keeps cc and goes to a later strip, so it keeps its order. If dd is not a multiple of BB, a source with c≥B−(d mod B)c \ge B - (d \bmod B) 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 BB.

References

  1. 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
  2. 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
  3. 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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • three classic examples