Reduction in Strength
180 likes | 275 Vues
Explore our sample program calculation and learn about optimizing code through reduction in strength for more efficient execution. See how to identify and remove redundant operations in loops.
Reduction in Strength
E N D
Presentation Transcript
Reduction in Strength CS 480
Our sample calculation for i := 1 to n for j := 1 to m c [i, j] := 0 for k := 1 to p c[i, j] := c[i, j] + a[i, k] * b[k, j]
Graph rep of our program Node 1 t1 <- n i <- 1 (implicit goto node 2) Node 2 If i > t1 goto node 10 Node 3 t2 <- m j <- 1 Node 4 if j > t2 goto node 9 Node 5 (c + ((i*4-4) * m + (j*4-4))) <- 0 t3 <- p k <- 1 Node 6 If k > t3 goto node 8 Node 7 t4 <- i*4-4 t5 <- j*4-4 t7 <- t4 * m + t5 t8 <- k*4-4 (c + t7) <- @(c+t7) + @(a + (t4 * p + t8)) * @(b + (t8 * m + t5)) k <- k + 1 goto node 6 Node 8 j <- j + 1 goto Node 4 Node 9 i <- i + 1 goto Node 2 Node 10 (procedure exit)
Innermost loop 7 *, 12 + Node 5 (c + ((i*4-4) * m + (j*4-4))) <- 0 t3 <- p k <- 1 Node 6 If k > t3 goto node 8 Node 7 t4 <- i*4-4 t5 <- j*4-4 t7 <- t4 * m + t5 t8 <- k*4-4 (c + t7) <- @(c+t7) + @(a + (t4 * p + t8)) * @(b + (t8 * m + t5)) k <- k + 1 goto node 6
Loop Invariant Code Removal • Last time we looked for expressions that did not change within a loop, and moved them out • Called Loop Invariant Code removal • We were able to reduce the operations to 8 additions and 3 multiplications
After loop invariant code removal Node 5 t4 <- i * 4 – 4 t5 <- j * 4 – 4 t7 <- t4 * m + t5 t6 <- t4 * p (c + t7) <- 0 t3 <- p k <- 1 Node 6 If k > t3 goto node 8 Node 7 t8 <- k*4-4 (c + t7) <- @(c+t7) + @(a + (t6 + t8)) * @(b + (8 * m + t5)) k <- k + 1 goto node 6
So, are we done? • Originally had +/-: 12 and */%: 7 in innermost loop • Now have +/-: 8 and */%: 3 • Are we done? Can we do better? • Sure we can.. • Next optimization
Reduction in Strength • Reduce a costly (“strong”) operation to a less costly (“weak”) operation • Typically replace multiplications by additions, which can be executed much faster
Reduction in Strength algorithm • Step 1 within a loop, look for variables that are changing by the addition or subtraction of a constant • Called Induction Variables • Step 2 look for expressions of the form (iv + c) * c + c (these come up a lot in subscript calculations) Step 3. Replace these with a temporary variable
Justification • Think about the variable k in the innermost loop • How is it changing? • 1,2,3,……. • Then how does the expression k * 4 – 4 change?
Finding induction variables • Same type of analysis as before, only now we need to know not only is a variable being changed, but NOW it is being changed • Look for iv <- iv + c • Most commonly from for statements, but can actually come from anyplace (or even multiple assignments)
Finding expressions • Look for expressions of the form (iv + c) * c + c (again, these are common in subscripts) Create a temporary that tracks these expressions Every time our induction variable changes, change the temporary (by adding a constant)
See what the constant is • Suppose we have iv = iv + c1 • Suppose we have (iv + c2) * c3 + c4 • New expression is (iv + c1 + c2) * c3 + c4 • Which is (iv + c2) * c3 + c4 + c1 * c2 • Which is old expression + c1 * c2 • So new expression is just a constant change from old expression
Lets try it Node 5 t4 <- i * 4 – 4 t5 <- j * 4 – 4 t7 <- t4 * m + t5 t6 <- t4 * p (c + t7) <- 0 t3 <- p k <- 1 t8 <- 0 (this is k * 4 – 4 at this point) Node 6 If k > t3 goto node 8 Node 7 t8 <- k*4-4 (NO LONGER NEEDED) (c + t7) <- @(c+t7) + @(a + (t6 + t8)) * @(b + (t8 * m + t5)) k <- k + 1 t8 <- t8 + 4 goto node 6
Are we done yet? • Nope. • As aways, after we have done one optimization, we have a different program • Need to look once more since there might now be further optimizations that were not possible previously
New induction variable • T8 is now an induction variable • It changes only by a constant amount (namely, m) each time through the loop • Can replace with another temporary
Lets try it Node 5 t4 <- i * 4 – 4 t5 <- j * 4 – 4 t7 <- t4 * m + t5 t6 <- t4 * p (c + t7) <- 0 t3 <- p k <- 1 t8 <- 0 t11 <- 0 (this is t8* m at this point) Node 6 If k > t3 goto node 8 Node 7 (c + t7) <- @(c+t7) + @(a + t6 + t8) * @(b + t11+ t5) k <- k + 1 t8 <- t8 + 4 t11 <- t11 + 4m goto node 6
Operation counts • Original 7 * 12 + • After loop invariant code removal 3 * 8 + • After reduction in strength 1 * 9 + • Still other types of optizations that could be done, but this is pretty good