1 / 18

Reduction in Strength

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)

marty
Download Presentation

Reduction in Strength

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Reduction in Strength CS 480

  2. 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]

  3. 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)

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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?

  11. 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)

  12. 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)

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

More Related