Docsity

High Performance Compilation: Improving Reuse and Reducing Cache Misses, Study notes of Computer Science

Toggle sidebar
bg1
Compiling For High Performance
Issues
•parallelism
•locality
Classical compilation
•focus on individual operations
•scalar variables
•flow of values
•unstructured code
High-performance compilation
•focus on aggregate operations (loops)
•array variables
•memory access patterns
•structured code
Issues
•dependence analysis
•loop transformations
CMSC 430 Lecture 24, Page 1
D ata L o cality
W hy lo cality?
•memory accesses are ex pensive
•ex ploit h igh er levels of memory h ierarch y by reusing registers, c ache lines, T L B , etc.
•locality of reference ⇔reuse
L ocality
•temporal locality reuse of a specific location
•spatial locality reuse of adjacent locations
(cache lines, pages)
R euse
•self-reuse caused by same reference
•group-reuse caused by multiple references
W hat reuse occurs in this loop nest?
do i = 1, N
do j = 1, N
A(i) += B(j) + B(j+2)
Refs R euse on loop i R euse on loop j
Aspatial temporal
Btemporal, group spatial spatial, group temporal
CMSC 430 Lecture 24, Page 2
L oop T rans formations to Improv e R eus e
T o calculate temporal and spatial reuse
F or each loop lin a nest, consider linnermost
1. partition references with grou p-reuse
⇒reference groups
2. compute th e cost in cach e lines accessed
⇒loop cost
3. rank the loops based on th eir loop cost
⇒memory order
K ey insight
If loop lpromotes more reuse than loop kat the innermost position, then it
probably promotes more reuse at any outer position
S electing a loop permutation
•select memory order if legal
•if not, find a nearby legal permutation
•avoids evaluating many permutations
CMSC 430 Lecture 24, Page 3
S electing a L oop Permu tation
Cost of reference group for loop k
1. select representative from reference group
2. find cost (in cach e lines) with kinnermost
3. multiply by trip counts of outer loops
invariant 1
unit-stride (Uk−Lk+ 1)/c ls
oth erwise Uk−Lk+ 1
L oop cost = sum of costs for reference groups
M atrix multiplication ex ample
do j = 1, N
do k = 1, N
do i = 1, N
C(i,j) = C(i,j) + A(i,k) * B(k,j)
RefG ro u p s J K I
C(i,j) n∗n21∗n21
4n∗n2
A(i,k) 1∗n2n∗n21
4n∗n2
B(k ,j) n∗n21
4n∗n21∗n2
total 2n3+n25
4n3+n21
2n3+n2
L oopCost (w ith cls = 4)
CMSC 430 Lecture 24, Page 4
M atrix M u ltiply (ex ec time in second s)
CMSC 430 Lecture 24, Page 5
300 x 300
JKI KJI JIK IJK KIJ IKJ
10
20
30
40
50
150 x 150
JKI KJI JIK IJK KIJ IKJ
0.5
1.5
2.5
3.5
512 x 512
JKI KJI JIK IJK KIJ IKJ
100
200
300
400
S un S parc2
Intel i8 6 0
IB M R S6 k
CMSC 430 Lecture 24, Page 6
pf3
pf4
pf5

Partial preview of the text

Download High Performance Compilation: Improving Reuse and Reducing Cache Misses and more Study notes Computer Science in PDF only on Docsity!

Compiling For High Performance

Issues

  • parallelism
  • locality

Classical compilation

  • focus on individual operations
  • scalar variables
  • flow of values
  • unstructured code

High-performance compilation

  • focus on aggregate operations (loops)
  • array variables
  • memory access patterns
  • structured code

Issues

  • dependence analysis
  • loop transformations

CMSC 430 Lecture 24, Page 1

D ata L ocality

W hy locality?

  • memory accesses are ex pensive
  • ex ploit h igh er levels of memory h ierarch y by reusing registers, cach e lines, T L B , etc.
  • locality of reference ⇔ reuse

L ocality

  • temporal locality reuse of a specific location
  • spatial locality reuse of adjacent locations (cache lines, pages) R euse
  • self-reuse caused by same reference
  • group-reuse caused by multiple references

W hat reuse occurs in this loop nest? do i = 1, N do j = 1, N A(i) += B(j) + B(j+2)

Refs R euse on loop i R euse on loop j A spatial temporal

B temporal, group spatial spatial, group temporal

CMSC 430 Lecture 24, Page 2

L oop T ransformations to Improv e R eu se

T o calculate temporal and spatial reuse

F or each loop l in a nest, consider l innermost

  1. partition references with group-reuse ⇒ reference groups
  2. compute th e cost in cach e lines accessed ⇒ loop cost
  3. rank th e loops based on th eir loop cost ⇒ memory order

K ey insight

If loop l promotes more reuse than loop k at the innermost position, then it probably promotes more reuse at any outer position

S electing a loop permutation

  • select memory order if legal
  • if not, fi nd a nearby legal permutation
  • avoids evaluating many permutations

CMSC 430 Lecture 24, Page 3

S electing a L oop Permu tation

Cost of reference group for loop k

  1. select representative from reference group
  2. fi nd cost (in cach e lines) with k innermost
  3. multiply by trip counts of outer loops

invariant 1 unit-stride (Uk − Lk + 1)/ cls oth erwise Uk − Lk + 1

L oop cost = sum of costs for reference groups M atrix multiplication ex ample

do j = 1, N do k = 1, N do i = 1, N C(i,j) = C(i,j) + A(i,k) * B(k,j)

RefG ro u p s J K I C(i,j) n ∗ n^2 1 ∗ n^2 14 n ∗ n^2 A(i,k ) 1 ∗ n^2 n ∗ n^2 14 n ∗ n^2 B(k ,j) n ∗ n^2 14 n ∗ n^2 1 ∗ n^2 total 2 n^3 + n^2 54 n^3 + n^2 12 n^3 + n^2

L oopCost (w ith cls = 4 )

CMSC 430 Lecture 24, Page 4

M atrix M u ltiply (ex ec time in second s) 3 00 x 3 00

JKI KJI JIK IJK KIJ IKJ

15 0 x 15 0

JKI KJI JIK IJK KIJ IKJ

5 12 x 5 12

JKI KJI JIK IJK KIJ IKJ

S un S parc Intel i8 6 0 IB M R S 6 k

M atrix M u ltiply

E x ample

do i = 1, n do j = 1, n do k = 1, n A(i,j) = A(i,j) + B(i,k) * C(k,j)

Q uestion

  • suppose arrays do not fi t in cach e
  • can we ex ploit more reuse?

CMSC 430 Lecture 24, Page 7

R eu sing Cache L ines

M atrix multiple ex ample

T iled v ersion do ii = 1, 500, T do jj = 1, 500, T do i = ii, ii+T- do j = jj, jj+T- do k = 1, 500 A(i,j) = A(i,j) + B(i,k) * C(k,j)

CMSC 430 Lecture 24, Page 8

T iling

T ransformation

  • strip-mine loops, th en interch ange

Benefi ts

  • increases siz e of localiz ed iteration set
  • ex ploits reuse among multiple loops
  • uses larger portion of cach e

P rob lems

  • less reuse per loop
  • h igh er loop overh ead
  • needs information about cach e siz e
  • need to avoid conflict misses

Q uestion

  • h ow to ch oose tile dimensions, siz e?
  • is it simply q uestion of cach e siz e?

CMSC 430 Lecture 24, Page 9

Performance of T iling

IBM R S 6 0 0 0 D E C 3 1 0 0

R esults

  • potentially large improvement, but sensitive to cach e / array siz e

Cache Confl icts

  • set associativity → conflict misses
  • too many addresses mapped to same cach e line
  • common for power-of-two array siz es

M. Lam , E. R o th b erg, M. W o lf, “ T h e C ach e Perfo rm an ce an d O p tim iz atio n s o f B lo ck ed A lgo rith m s,” A S PLO S ’9 1

CMSC 430 Lecture 24, Page 10

A v oid ing Cache Confl ict

U se less cache

  • ch oose smaller tile siz es
  • reduces portion of cach e used
  • lowers ch ance of cach e conflict
  • empirically, 20– 3 0% seems to work
  • h igh er loop overh ead

Copy optimization

  • copy each tile to contiguous locations
  • modify code to access new location
  • amortiz e overh ead for h igh reuse
  • ex plicit copy code, overh ead

R ectangular tiles

  • calculate rectangular tile siz e
  • ex plicitly ch osen to avoid cach e conflict
  • may result in long th in tiles (eq uivalent to no tiling)

S calar R eplacment

Array references

  • diffi cult to allocate to registers
  • need to identify potential aliases
  • dependences point out reuse

do i = 1, A(0) = A(0)+A(i) enddo

S calar replacement transformation

  • replace array reference with scalar
  • eliminates aliases th rough renaming
  • relies on dependence analysis
  • simplifi es scalar compiler back end

t = A(0) do i = 1, t = t+A(i) enddo A(0) = t

D. C allah an , S. C arr, K. K en n ed y , “ Im p ro v in g R egister A llo catio n fo r S ub scrip ted V ariab les,” PLD I’9 0

D epend ence T esting

G iven

do i 1 = L 1 , U 1

... do in = Ln, Un S 1 A(f 1 (i 1 ,... , in),... , fm(i 1 ,... , in)) =... S 2... = A(g 1 (i 1 ,... , in),... , gm(i 1 ,... , in))

A dependence between statement S 1 and S 2 , denoted S 1 δ S 2 , indicates th at S 1 , th e source, must be ex ecuted before S 2 , th e sink on some iteration of th e nest.

L et α & β be a vector of n integers with in th e ranges of th e lower and upper bounds of th e n loops.

D oes ∃ α ≤ β, s.t. fk(α) = gk(β) ∀k, 1 ≤ k ≤ m?

CMSC 430 Lecture 24, Page 19

Iteration S pace

do I = 1, 5 do J = I, 6

... enddo enddo

1 ≤ I ≤ 5

I ≤ J ≤ 6 J

I

  • lex icograph ical (seq uential) order for th e above iteration space is (1,1), (1,2),.. ., (1,6 ) (2,2), (2,3 ),... (2,6 ) ... (5 ,5 ), (5 ,6 )
  • given I = (i 1 ,... in) and I′^ = (i′ 1 ,... , i′ n), I < I′^ iff (i 1 , i 2 ,... ik) = (i′ 1 , i′ 2 ,... i′ k) & ik+ 1 < i′ k+ 1

CMSC 430 Lecture 24, Page 20

D istance V ectors

do I = 1, N do J = 1, N S 1 A(I,J) = A(I,J-1) enddo enddo

do I = 1, N do J = 1, N S 2 A(I,J) = A(I-1,J-1) S 3 B(I,J) = B(I-1,J+1) enddo enddo

J

I

J

I

D istance V ector = number of iterations between accesses to th e same location

distance vector S 1 δ S 1 (0,1) S 2 δ S 2 (1,1) S 3 δ S 3 (1,-1)

CMSC 430 Lecture 24, Page 21

L oop Interchange

do I = 1, N do J = 1, N S 1 A(I,J) = A(I,J-1) S 2 B(I,J) = B(I-1,J-1) enddo enddo

=⇒ loop interchange =⇒

do J = 1, N do I = 1, N S 1 A(I,J) = A(I,J-1) S 2 B(I,J) = B(I-1,J-1) enddo enddo

J

I

J

I

L oop interchange is safe iff

  • it does not create a lex icograph ically negative direction vector (1,– 1) → (– 1,1) ⇒ B enefi ts ◦ may ex pose parallel loops, increase granularity ◦ reordering iterations may improve reuse

CMSC 430 Lecture 24, Page 22

Forms of Parallelism

Instruction-lev el parallelism

  • for superscalar and V L IW arch itectures
  • ex amine dependences between statements
  • very fi ne grain parallelism

A = 1 B = C

T ask -lev el parallelism

  • for multiprocessors
  • ex amine dependences between task s
  • parallelism is not scalable

do i = 1,10 do i = 1, A(i) = A(i+1) B(i) = B(i+1)

L oop-lev el parallelism

  • for vector mach ines and multiprocessors
  • ex amine dependences between loop iterations
  • parallelism is scalable doall i = 1, A(i) = B(i+1)

L oop-lev el Parallelism

Basic approach

  • ex ecute loop iterations in parallel
  • safe if no loop-carried data dependences (i.e., no accesses to same memory location)

do i = 1,10 doall i = 1, A(i) = A(i+1) A(i) = A(i+10)

S ev eral parallel architectures

  • vector processors

A[1:10] = B[2:11]

  • multiprocessors

doall i = 1, A(i) = B(i+1)

  • message-passing mach ines

if (...) send B(1) if (...) recv B(11) do i = L,B A(i) = B(i+1)

W hich L oops are Parallel?

do I = 1, N do J = 1, N S 1 A(I,J) = A(I,J-1)

do I = 1, N do J = 1, N S 2 A(I,J) = A(I-1,J-1)

do I = 1, N do J = 1, N S 3 B(I,J) = B(I-1,J+1)

J

I

  • a dependence D = (d 1 ,... , dk) is carried at level i, if di is th e fi rst nonz ero element of th e distance vector
  • a loop li is parallel, if +∃ a dependence Dj carried at level i

distance vector ∀Dj d 1 ,... , di− 1 > 0 O R d 1 ,... , di = 0

CMSC 430 Lecture 24, Page 25

E x posing Parallelism

S calar analy sis

  • improve precision of dependence tests
  • eliminate unnecessary scalar statements
  • based on data-flow analysis

S olution techniq ues

  • forward propagation (ex pose value of scalar variables)

do i = 1,10 do i =1, k = i+1 k = i+ A(k) = A(i+1) =

  • constant propagation k = 1 k = 1 do i = 1,10 do i =1, A(i+k) = A(i+1) =
  • induction variable recognition k = 1 k = 1 do i = 1,10 do i =1, k = k+1 A(i+1) = A(k) = k = 11

CMSC 430 Lecture 24, Page 26

E x posing Parallelism

S torage-related d epend ences

  • anti and output dependences
  • caused by reusing storage
  • no flow of values (not inh erently seq uential)

S olution techniq ues

  • renaming do i = 1,10 do i = 1, A(i) = A(i+1) T(i) = A(i+1) do i = 1, A(i) = T(i)
  • scalar/ array ex pansion do i = 1,10 do i =1, t = t(i) = = t = t(i)
  • scalar/ array privatiz ation do j = 1,10 do j = 1, do i = 1,10 private A(10) A(i) = do i = 1, B(i,j) = A(i) A(i) = t B(i,j) = A(i)

CMSC 430 Lecture 24, Page 27

E x posing Parallelism

R ed uctions

  • loop-carried true (flow) dependences
  • operations are associative (can commute) do i = 1,10 do i = 1, t = t + A(i) if (t < A(i)) t = A(i)
  • roundoff error for floating point arith metic

S olution techniq ues

  • vector reduction operation do i = 1,10 t = VADD(A[1:10]) t = t + A(i)
  • paralleliz e reduction

do i = 1,10 private tt t = t + A(i) tt = 0 do i = L,U tt = tt + A(i) lock t = t + tt unlock

CMSC 430 Lecture 24, Page 28

V ectoriz ation

V ector processors

  • operations on vectors of data
  • overlap iterations of inner loop

doall i = 1,10 A[1:10] = 1. A(i) = 1.0 B[1:10] = A[1:10] B(i) = A(i)

  • ex ploits fi ne-grain parallelism
  • ex pressed in vector languages (AP L , F ortran 9 0)

E x ecution mod el

  • single th read of control single instruction, multiple data (S IM D )
  • load data into vector registers
  • effi ciently ex ecute pipelined operations

Issues

  • vector length - coalesce loops to reduce overh ead
  • control flow - convert conditions into ex plicit data

Paralleliz ation

M ultiprocessors

  • multiple independent processors (M IM D )
  • assign iterations to diff erent processors

doall j = 1,10 do j = L,U do i = 1,10 do i = 1, A(i,j) = 1.0 A(i,j) = 1.

  • ex ploits coarse-grain parallelism

E x ecution mod el

  • fork -join parallelism
  • master ex ecutes seq uential code
  • work ers (and master) ex ecute parallel code
  • master continues after work ers fi nish

Issues

  • granularity - larger computation partitions to reduce overh ead
  • sch eduling - policy for assigning iterations to processors