Docsity

Program Transformations: A Comprehensive Overview, Slides of Computer Science

Partial preview of the text

Download Program Transformations: A Comprehensive Overview and more Slides Computer Science in PDF only on Docsity!

Program Transformations

Michael O’Boyle

February, 2011

M. O’Boyle

Program Transformations

1

Course Structure

L1 Introduction and Recap, L2 Course Work

4 lectures on high level restructuring for parallelism and memory

Dependence Analysis

Program Transformations - loop and arrays

Automatic vectorisation

Automatic parallelisation

M. O’Boyle

Program Transformations

3

References

Loop

Distribution

with

arbitrary

control-flow

McKinley

and

Kennedy

Supercomputing 1990

D.F. Bacon, S.L. Graham, and O.J. Sharp.

Compiler Transformations for

High-Performance Computing. ACM Computing Surveys, 26(4), 1994.

A Framework for Unifying Reordering Transformations (1993) TR

On the Complexity of Loop Fusion Alain Darte, PACT 1999

L. Lamport. The parallel execution of do loops. Communications of the ACM,pages 83–93, February 1974.

M. O’Boyle

Program Transformations

February, 2011

4

What is a program transformation

A program transformation is a rewriting of the program such that it has thesame semantics

More conservatively, all data dependences must be preserved

Previous lectures looked at IR

IR transformations or assembler

assembler

transformations

Focus on transformations in the high level source prog.

language: source to

source transformations

Why:

Only place where memory reference explicit.

Key to restructuring for

memory behaviour and large scale parallelism.

M. O’Boyle

Program Transformations

February, 2011

6

Loop Restructuring Index Splitting

Always a legal transformation. No test needed

Do i = 1, 100

a(101 -i) =a(i) Enddo

Do i = 1, 50

a(101 -i) =a(i) EnddoDo i = 51, 100

a(101 -i) =a(i) Enddo

A sequential loop with dependence [*] is transformed into two independent parallelloops. Careful selection of split point.Neither access in each loop refers to same memory location.All of first loop must execute before second though - why? M. O’Boyle

Program Transformations

7

Loop Restructuring: Loop Unrolling

Used for exploiting Instruction Level ParallelismAlways legal - take care of epilogue using index splitting

Do i = 1, 100

a(i) = i Enddo

Do i = 1, 100, 3

a(i) = ia(i+1) = i+1a(i+2) = i+ EnddoDo i = 100,

a(i) = i Enddo

Non-convex iteration space after transformation - steps.

Causes difficulties for

dependence analysis. Can normalise loop though M. O’Boyle

Program Transformations

9

Loop Distribution + Statement Reordering

Do i = 1,

a(i) =

=a(i+1)

Enddo

s1 s

Do i = 1,

=a(i+1)

EnddoDo i = 1,

a(i) =

Enddo

s2 s

Anti-dependences honoured. M. O’Boyle

Program Transformations

10

Loop Restructuring: Loop Fusion

Inverse of loop distribution - needs conformant loops

Do i = 1,

a(i) =

EnddoDo j = 1,

b(j) =

Enddo

Do i = 1,

a(i) =b(i) =

Enddo

More difficult than distribution. Dependence constrains application.Used for increasing ILP and improving register use.

Also for fork/join based

parallelisation.Loops can be partly fused after pre-distribution M. O’Boyle

Program Transformations

12

Iteration reordering: Loop interchange

j

i

.^..

.

.. .. .. .. .. ..

j

i

.^..

.

.. .. .. .. .. ..

Illegal to interchange [1,-1]: New vector [-1,1]:Impossible dependence.Linear models check

T D >

M. O’Boyle

Program Transformations

13

Loop skewing

Always legal used in wavefront parallelisation

Do i =1, N

Do j = 1,N

a(i,j) = a(i,j-1)

+b(i)

Enddo

Enddo

Do i =1, N

Do j = i+1,i+N

a(i,j-i) = a(i,j-i-1)

+b(i)

Enddo

Enddo

[

i, j

]^

[i, j

i

]

Equivalent to a change of basis.

Shifting by a constant referred to as loop bumping

M. O’Boyle

Program Transformations

15

Tiling = strip-mining plus interchange

Do i =1, N

Do j = 1,N

a(i,j) = a(i,j) +b(i)

Enddo

Enddo

Do i =1, N,s

Do j = 1,N,s

Do ii = i+1, i+s-

Do jj = j+1,j+s-

a(ii,jj) = a(ii,jj) +b(ii) Enddo

Enddo Enddo

Enddo

Do i =1, N

Do j = 1,N,s

Do jj = jj+1,jj+s-

a(i,jj) = a(i,jj)+b(i)

Enddo Enddo

Enddo

Strip-mine by factor s Non-convex spaceInterchange placing smaller strip-mineinside

M. O’Boyle

Program Transformations

16

Array layout transformations

Less extensive literature though perhaps have a more significant impact

Loop transformations affect all memory references within the loop but notelsewhere. Local in nature

Array and more generally data transformations have global impact but do notaffect other references to other arrays.

Array layout transformations are used to improve memory access performance

Also form the basis for data distribution based parallelisation schemes fordistributed memory machines.

M. O’Boyle

Program Transformations

18

Linearisation/delinearisation

Dual of loop strip-mining/linearisation

REAL a[10,20]Do i =1, 10

Do j = 1,

a(i,j) = a(i+1,j-1) +b(i) Enddo Enddoa(1,2) =

REAL a[200]Do i =1, 10

Do j = 1,

a(20(i-1)+j) = a(20(i)+j-1)

+b(i)

Enddo Enddoa(2) =

M. O’Boyle

Program Transformations

19

Padding

REAL A[10,20]Do i =1, 10

Do j = 1,

a(i,j) = a(i+1,j-1) +b(i)

Enddo Enddoa(1,2) =

REAL A[17,20]Do i =1, 10

Do j = 1,

a(i,j) = a(i+1,j-1) +b(i)

Enddo Enddoa(1,2) =

Frequently used to overcome cache conflicts. Very simple

Pad factor 7 in first index. Normally prime.

M. O’Boyle

Program Transformations