Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS314 – Section 5 Recitation 13

Similar presentations


Presentation on theme: "CS314 – Section 5 Recitation 13"— Presentation transcript:

1 CS314 – Section 5 Recitation 13
Long Zhao SIV, ZIV Vectorization Slides available at

2 Dependence Distance Vector
Definition Suppose that there is a dependence from S1 on the i-th level iteration x to S2 on iteration y; then the i-th element of the dependence distance vector d is defined as d = y - x Example: DO I = 1, DO J = 1, I S1 A(I+1,J) = A(I,J) ENDDO ENDDO True dependence between S1 and itself on i = (1,1) and j = (2,1): d(i, j) = (1,0) i = (2,1) and j = (3,1): d(i, j) = (1,0) i = (2,2) and j = (3,2): d(i, j) = (1,0)

3 Dependence Direction Vector
Definition Suppose that there is a dependence from S1 on iteration i and S2 on iteration j; then the dependence direction vector D(i, j) is defined as “<” if d(i, j)k > 0 “=” if d(i, j)k = 0 “>” if d(i, j)k < 0 D(i, j)k =

4 Example 4 DO I = 1, 4 DO J = 1, 4 S1 A(I,J+1) = A(I-1,J) ENDDO ENDDO 3
2 1 Distance vector is (1,1) S1 (<,<) S1 I 1 2 3 4

5 ZIV Test e1, e2 are constants or loop invariant symbols
DO j = 1, 100 S A(e1) = A(e2) + B(j) ENDDO e1, e2 are constants or loop invariant symbols If (e1-e2)!=0 No Dependence exists

6 Strong SIV Test Strong SIV subscripts are of the form
For example the following are strong SIV subscripts

7 Strong SIV Test Strong SIV test when
DO i1 = L1, U1 S1 A(f(i1,...,in)) = ... S = A(g(i1,...,in)) ENDDO Strong SIV test when f(...) = ai+c1 and g(…) = ai’+c2 Plug in α,β and solve for dependence: β-α = (c1 – c2)/a A dependence exists from S1 to S2 if: β-α is an integer |β-α| ≤ U1- L1

8 Strong SIV Test Example
DO k = 1, 100 DO j = 1, 100 S1 A(j+1,k) = ... S = A(j,k) + 32 ENDDO

9 Weak SIV Tests Weak SIV subscripts are of the form
For example the following are weak SIV subscripts

10 Weak-zero SIV Test Weak-Zero SIV test when
DO i1 = L1, U1 S1 A(f(i1,...,in)) = ... S = A(g(i1,...,in)) ENDDO Weak-Zero SIV test when f(...) = ai+c1 and g(…) = c2 Plug in α and solve for dependence: α = (c2 – c1)/ a A dependence exists from S1 to S2 if: α is an integer L1 ≤ α ≤ U1

11 Weak-crossing SIV Test
DO i1 = L1, U1 S1 A(f(i1,...,in)) = ... S = A(g(i1,...,in)) ENDDO Weak-Crossing SIV test when f(...) = ai+c1 and g(…) = -ai+c2 To find crossing point, set α = β and solve: α = (c2 – c1) / 2a A dependence exists from S1 to S2 if: 2α is an integer L1 ≤ α ≤ U1

12 Vectorization Simple vectorizer assumptions: singly-nested loops
constant upper and lower bounds, step is always 1 body is sequence of assignment statements to array variables simple array index expressions of induction variable (i +/- c or c); can use ZIV or SIV test no function calls

13 Vectorization (1) A single-statement loop that carries no dependence can be vectorized DO I = 1, 4 S1 X(I) = X(I) + C ENDDO vectorize S1 X(1:4) = X(1:4) + C X(1) X(2) X(3) X(4) + Fortran 90 array statement C (anti), d = 0 S1 X(1) X(2) X(3) X(4) Vector operation

14 Vectorization (2) This example has a loop- carried dependence (true dependence), and is therefore invalid S1 (<) S1 DO I = 1, N S1 X(I+1) = X(I) + C ENDDO (true), d = 1 S1 X(2:N+1) = X(1:N) + C S1 Invalid Fortran 90 statement

15 Vectorization (2) This example has a loop- carried dependence (anti- dependence), and is valiad S1 (<) S1 DO I = 2, N S1 X(I-1) = X(I) + C ENDDO (anti), d = 1 S1 X(1:N-1) = X(2:N) + C S1

16 S1 Vectorization (3) (true), d = 1 S2 DO I = 1, N S1 A(I+1) = B(I) + C S2 D(I) = A(I) + E ENDDO Because only single loop statements can be vectorized, loops with multiple statements must be transformed using the loop distribution transformation Loop has no loop-carried dependence or has forward flow dependences loop distribution DO I = 1, N S1 A(I+1) = B(I) + C ENDDO DO I = 1, N S2 D(I) = A(I) + E ENDDO vectorize S1 A(2:N+1) = B(1:N) + C S2 D(1:N) = A(1:N) + E

17 S1 Vectorization (4) (true), d = 1 S2 DO I = 1, N S1 D(I) = A(I) + E S2 A(I+1) = B(I) + C ENDDO When a loop has backward flow dependences and no loop-independent dependences, interchange the statements to enable loop distribution loop distribution DO I = 1, N S2 A(I+1) = B(I) + C ENDDO DO I = 1, N S1 D(I) = A(I) + E ENDDO vectorize S2 A(2:N+1) = B(1:N) + C S1 D(1:N) = A(1:N) + E

18 Vectorization (5) S1 S2 DO I = 2, 100 S1 A(I) = D(I-1) + B(I-1) S2 B(I) = A(I-1) * 2 S3 C(I) = C(I-1) + 3 S4 D(I) = C(I) ENDDO S3 S4

19 Vectorization (5) S1 S2 S3 S4 B: (true), d = 1 A: (true), d = 1
DO I = 2, 100 S1 A(I) = D(I-1) + B(I-1) S2 B(I) = A(I-1) * 2 S3 C(I) = C(I-1) + 3 S4 D(I) = C(I) ENDDO D: (true), d = 1 (true), d = 1 S3 (true), d = 0 S4

20 Vectorization (5) S3 S1 S4 S2 S1 S3 S2 S4 (true), d = 1
B: (true), d = 1 A: (true), d = 1 S4 S2 D: (true), d = 1 D: (true), d = 1 (true), d = 1 S1 S3 A: (true), d = 1 B: (true), d = 1 (true), d = 0 S2 S4

21 Vectorization (5) S3 S4 S1 S2 (true), d = 1
DO I = 2, 100 S1 A(I) = D(I-1) + B(I-1) S2 B(I) = A(I-1) * 2 S3 C(I) = C(I-1) + 3 S4 D(I) = C(I) ENDDO S3 (true), d = 0 S4 D: (true), d = 1 DO I = 2, 100 S3 C(I) = C(I-1) + 3 S4 D(I) = C(I) S1 A(I) = D(I-1) + B(I-1) S2 B(I) = A(I-1) * 2 ENDDO S1 A: (true), d = 1 B: (true), d = 1 S2

22 Vectorization (5) S3 S4 S1 S2 (true), d = 1
DO I = 2, 100 S3 C(I) = C(I-1) + 3 S4 D(I) = C(I) S1 A(I) = D(I-1) + B(I-1) S2 B(I) = A(I-1) * 2 ENDDO S3 (true), d = 0 S4 D: (true), d = 1 DO I = 2, 100 S3 C(I) = C(I-1) ENDDO DO I = 2, 100 S4 D(I) = C(I) S1 A(I) = D(I-1) + B(I-1) S2 B(I) = A(I-1) * 2 ENDDO S1 A: (true), d = 1 B: (true), d = 1 S2

23 Vectorization (5) S3 S4 S1 S2 DO I = 2, 100 S3 C(I) = C(I-1) + 3 ENDDO
DO I = 2, 100 S4 D(I) = C(I) S1 A(I) = D(I-1) + B(I-1) S2 B(I) = A(I-1) * 2 ENDDO (true), d = 1 S3 (true), d = 0 S4 D: (true), d = 1 DO I = 2, 100 S3 C(I) = C(I-1) ENDDO S4 D(2:100) = C(2:100) DO I = 2, 100 S1 A(I) = D(I-1) + B(I-1) S2 B(I) = A(I-1) * 2 ENDDO S1 A: (true), d = 1 B: (true), d = 1 S2

24 Vectorization (5) S3 S4 S1 S2 DO I = 2, 100 S3 C(I) = C(I-1) + 3 ENDDO
S4 D(2:100) = C(2:100) DO I = 2, 100 S1 A(I) = D(I-1) + B(I-1) S2 B(I) = A(I-1) * 2 ENDDO (true), d = 1 S3 (true), d = 0 S4 D: (true), d = 1 S1 A: (true), d = 1 B: (true), d = 1 S2


Download ppt "CS314 – Section 5 Recitation 13"

Similar presentations


Ads by Google