Docsity

Vectorisation - Compiler Optimisation - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Vectorisation - Compiler Optimisation - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Vectorisation

Michael O’BoyleFebruary, 2011

M. O’Boyle

Vectorisation

1

Course Structure

•^

Course work deadline today. New coursework today - see website

-^

4/5 lectures on high level restructuring for parallelism and memory

-^

Dependence Analysis

-^

Program Transformations

-^

Automatic vectorisation

ch2 and 5 of Allen and Kennedy

•^

Automatic parallelisation

-^

Speculative Parallelisation M. O’Boyle

Vectorisation

3

Vector code

•^

Use Fortran 90 vector notation to express vectorised loops.

-^

Triple notation used x(start:finish:step) to represent a vector in x

-^

Vectorisation depends on loop dependence Do i = 1,N

x(i) = x(i) +c Enddo

Do i = 1,N

x(i+1) = x(i) +c Enddo

No loop carried dependence [0]

Loop carried dependence [1]

Vectorisable

Not vectorisable

x(1:N) = x(1:N) +c M. O’Boyle

Vectorisation

4

Vector code: varying vector length

Vector registers are a fixed size. Need to fit code to registers

Do i = 1,N

x(i) = x(i) +c Enddo

Do i = 1,N,s Do ii = i, i+s- x(ii) = x(ii) +c Enddo Enddo

Original

Strip-mine

Do i = 1,N,s x(i:i+s-1) = x(i:i+s-1) +c Enddo Vectorise M. O’Boyle

Vectorisation

6

Inner loop vectorisation

Do i = 1,N Do j = 1,M

a(i+1,j) = a(i,j) +c Enddo Enddo

.^..

..^

..^

..^

j i

Cannot vectorise as dependence (1,0).

If outer loop run sequential then can

vectorise inner loop with dep (0). Generalises to nested loops. Do i = 1,N

a(i+1,1:M) = a(i,1:M) +c

Enddo M. O’Boyle

Vectorisation

7

Vectorisation algorithm

•^

Simple description of Ch2 algorithm. Look at Ch2 for more details

-^

Form dependence graph

-^

Strongly Connected Component (SCC) identification (cycles)

-^

Separate out weakly connected and vectorise using loop distribution andstatement reordering

-^

Strip off outer dependence level (loop will be sequentialised) and repeat M. O’Boyle

Vectorisation

9

Running Example with S1 dependences

Do i = 1, s

x(i) = y(i) +10Do j = 1,

s

b(j) = a(j,n)Do k = 1,

s

a(j+1,k) = b(j) +c(j,k) Enddo

s

y(i+j) = a(j+1,n) Enddo

d^1 s

s

s

s

1

2 4

3

Loop carried flow dependence from s4 to s1 on y.

d

1

No other dependences reach s1 M. O’Boyle

Vectorisation

10

Running Example S2 dependences

,d

1

d

−1d 1

d

1

s

s

2

3

d^1 o

(^1) −

d^2

d^1

b(j) in s2 has two flow dependences with s3. Loop carried and loop independent d

.d 1

inf

Corresponding loop carried anti dep from s3 to s

d

−

1 1

. Finally loop carried output

dependence in s2a(j+1,k) in s3 has a level one and two flow dep with s2.

Corresponding loop

carried antidep from s2 to s3. M. O’Boyle

Vectorisation

12

Example S4 dependences

d

s

s

s

s

1

2

3

4 o

•^

Trivial loop carried output dependence in s4 at level 1 on write to y(i+j).

-^

Other dependence with s1 already shown M. O’Boyle

Vectorisation

13

Putting it all together

,d^1

(^1) − d^1

d^1 d

d

−1d 1 d^1

d

d

1 d^1

s

s

s

s

1

2

3

4 d^1

o

o

o

(^1) −

d^2

d^1

•^

Analysing connected components using Tarjan’s algorithm.

Two separate

groups (s1),(s2,s3,s4).

-^

Separate out s1 by loop distribution. Statement reordering required here. M. O’Boyle

Vectorisation

15

Vectorise(

s2,s3,s

,2) level 1 dependences stripped off

d

d

s

s

(^2) s

3

4

d^2

•^

Analysing connected components using Tarjan’s algorithm.

Two separate

groups (s4),(s2,s3).

-^

Separate out s4 by loop distribution. No Statement reordering required here. M. O’Boyle

Vectorisation

16

Vectorisation algorithm

Apply on level2 vectorise (s1,s2,s3,2).s4 not part of SCC. Isolated, distributed and vectorised giving

Do i = 1,100Do j = 1,

vectorise({s2,s3},3) Enddo

s

y(i+1:i+100) = a(2:101,N)Enddo

s1 x(1:100) = y(1:100) +10^ •

Apply vectorise again striping off next level M. O’Boyle

Vectorisation

18

Vectorisation algorithm : Final code

Do i = 1, Do j = 1,

s

b(j) = a(j,n)

s

a(j+1,1:100) = b(j) +c(j,1:100) Enddo

s

y(i+1:i+100) = a(2:101,N) Enddo

s

x(1:100) = y(1:100) +

As s2 has no loop of depth 3,distribution leaves a single statement.^ •

What happened if no vectorisable regions found? • Try transformations M. O’Boyle

Vectorisation

19

Loop Interchange: move loop carried dependences outermost

Do j = 1,M

Do i = 1,Na(i+1,j) = a(i,j) +c Enddo Enddo [0,1] even if j run sequentially, loop carried dep - i not vectorisable.

Do i = 1,N

Do j = 1,M

a(i+1,j) = a(i,j) +c Enddo Enddo

Do i = 1,N

a(i+1,1:N) = a(i,1:N) +c

Enddo

Now [1,0] - inner loop vectorisable M. O’Boyle

Vectorisation