PARALLEL PATTERNS REDUCE & SCAN 2 6/16/2010 Parallel - - PowerPoint PPT Presentation

parallel patterns reduce scan
SMART_READER_LITE
LIVE PREVIEW

PARALLEL PATTERNS REDUCE & SCAN 2 6/16/2010 Parallel - - PowerPoint PPT Presentation

1 6/16/2010 Parallel Pa7erns - Reduce & Scan PARALLEL PATTERNS REDUCE & SCAN 2 6/16/2010 Parallel Pa7erns - Reduce & Scan


slide-1
SLIDE 1

PARALLEL ¡PATTERNS ¡ REDUCE ¡& ¡SCAN ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

1 ¡

slide-2
SLIDE 2

Programming ¡Pa7erns ¡For ¡Parallelism ¡

  • Some ¡pa7erns ¡repeat ¡in ¡many ¡different ¡contexts ¡
  • e.g. ¡Search ¡an ¡element ¡in ¡an ¡array ¡
  • IdenKfying ¡such ¡pa7erns ¡important ¡ ¡
  • Solve ¡a ¡problem ¡once ¡and ¡reuse ¡the ¡soluKon ¡
  • Split ¡a ¡hard ¡problem ¡into ¡individual ¡problems ¡
  • Helps ¡define ¡interfaces ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

2 ¡

slide-3
SLIDE 3

We ¡Have ¡Already ¡Seen ¡Some ¡Pa7erns ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

3 ¡

slide-4
SLIDE 4

We ¡Have ¡Already ¡Seen ¡Some ¡Pa7erns ¡

  • Divide ¡and ¡Conquer ¡
  • Split ¡a ¡problem ¡into ¡n ¡sub ¡problems ¡
  • Recursively ¡solve ¡the ¡sub ¡problems ¡
  • And ¡merge ¡the ¡soluKon ¡
  • Data ¡Parallelism ¡
  • Apply ¡the ¡same ¡funcKon ¡to ¡all ¡elements ¡in ¡a ¡collecKon, ¡array ¡
  • Parallel.For, ¡Parallel.ForEach ¡
  • Also ¡called ¡as ¡“map” ¡in ¡funcKonal ¡programming ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

4 ¡

slide-5
SLIDE 5

Map ¡

  • Given ¡a ¡funcKon ¡f ¡: ¡(A) ¡=> ¡B ¡
  • A ¡collecKon ¡a: ¡A[] ¡
  • Generates ¡a ¡collecKon ¡b: ¡B[], ¡where ¡B[i] ¡= ¡f( ¡A[i] ¡) ¡
  • Parallel.For, ¡Paralle.ForEach ¡
  • Where ¡each ¡loop ¡iteraKon ¡is ¡independent ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

5 ¡

f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ A ¡ B ¡

slide-6
SLIDE 6

Reduce ¡And ¡Scan ¡

  • In ¡pracKce, ¡parallel ¡loops ¡have ¡to ¡work ¡together ¡to ¡

generate ¡an ¡answer ¡

  • Reduce ¡and ¡Scan ¡pa7erns ¡capture ¡common ¡cases ¡of ¡

processing ¡results ¡of ¡Map ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

6 ¡

slide-7
SLIDE 7

Reduce ¡And ¡Scan ¡

  • In ¡pracKce, ¡parallel ¡loops ¡have ¡to ¡work ¡together ¡to ¡

generate ¡an ¡answer ¡

  • Reduce ¡and ¡Scan ¡pa7erns ¡capture ¡common ¡cases ¡of ¡

processing ¡results ¡of ¡Map ¡

  • Note: ¡Map ¡and ¡Reduce ¡are ¡similar ¡to ¡but ¡not ¡the ¡same ¡

as ¡MapReduce ¡

  • MapReduce ¡is ¡a ¡framework ¡for ¡distributed ¡compuKng ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

7 ¡

slide-8
SLIDE 8

Reduce ¡

  • Given ¡a ¡funcKon ¡f: ¡(A, ¡B) ¡=> ¡B ¡
  • A ¡collecKon ¡a: ¡A[] ¡
  • An ¡iniKal ¡value ¡b0: ¡B ¡
  • Generate ¡a ¡final ¡value ¡b: ¡B ¡
  • Where ¡b ¡= ¡f(A[n-­‑1], ¡… ¡f(A[1], ¡f(A[0], ¡b0)) ¡ ¡) ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

8 ¡

f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ b0 ¡ b ¡ A ¡

slide-9
SLIDE 9

Reduce ¡

  • Given ¡a ¡funcKon ¡f: ¡(A, ¡B) ¡=> ¡B ¡
  • A ¡collecKon ¡a: ¡A[] ¡
  • An ¡iniKal ¡value ¡b0: ¡B ¡
  • Generate ¡a ¡final ¡value ¡b: ¡B ¡
  • Where ¡b ¡= ¡f(A[n-­‑1], ¡… ¡f(A[1], ¡f(A[0], ¡b0)) ¡ ¡) ¡
  • Only ¡consider ¡where ¡A ¡and ¡B ¡are ¡the ¡same ¡type ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

9 ¡

f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ b0 ¡ b ¡ A ¡

slide-10
SLIDE 10

Reduce ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

10 ¡

f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ b0 ¡ b ¡ A ¡ B acc = b_0; for( i = 0; i < n; i++ ) { acc = f( a[i], acc ); } b = acc;

slide-11
SLIDE 11

AssociaKvity ¡of ¡the ¡Reduce ¡funcKon ¡

  • Reduce ¡is ¡parallelizable ¡if ¡f ¡is ¡associaKve ¡

¡f(a, ¡f(b, ¡c)) ¡= ¡f(f(a,b), ¡c) ¡

  • E.g. ¡AddiKon ¡: ¡(a ¡+ ¡b) ¡+ ¡c ¡= ¡a ¡+ ¡(b ¡+ ¡c) ¡
  • Where ¡+ ¡is ¡integer ¡addiKon ¡(with ¡modulo ¡arithmeKc) ¡
  • But ¡not ¡when ¡+ ¡is ¡floaKng ¡point ¡addiKon ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

11 ¡

slide-12
SLIDE 12

AssociaKvity ¡of ¡the ¡Reduce ¡funcKon ¡

  • Reduce ¡is ¡parallelizable ¡if ¡f ¡is ¡associaKve ¡

¡f(a, ¡f(b, ¡c)) ¡= ¡f(f(a,b), ¡c) ¡

  • E.g. ¡AddiKon ¡: ¡(a ¡+ ¡b) ¡+ ¡c ¡= ¡a ¡+ ¡(b ¡+ ¡c) ¡
  • Where ¡+ ¡is ¡integer ¡addiKon ¡(with ¡modulo ¡arithmeKc) ¡
  • But ¡not ¡when ¡+ ¡is ¡floaKng ¡point ¡addiKon ¡
  • Max, ¡min, ¡mulKply, ¡… ¡
  • Set ¡union, ¡intersecKon, ¡ ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

12 ¡

slide-13
SLIDE 13

We ¡can ¡use ¡Divide ¡and ¡Conquer ¡

  • Reduce(f, ¡A[1…n], ¡b_0) ¡

¡= ¡f ¡( ¡ ¡Reduce(f, ¡A[1..n/2], ¡b_0), ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Reduce(f, ¡A[n/2+1…n], ¡I) ¡) ¡ ¡ ¡where ¡I ¡is ¡the ¡idenKty ¡element ¡of ¡f ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

13 ¡

f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ b0 ¡ b ¡ A ¡ I ¡ f ¡

slide-14
SLIDE 14

ImplementaKon ¡OpKmizaKons ¡

  • Switch ¡to ¡sequenKal ¡Reduce ¡for ¡the ¡base ¡k ¡elements ¡
  • Do ¡k ¡way ¡splits ¡instead ¡of ¡two ¡way ¡splits ¡
  • Maintain ¡a ¡thread-­‑local ¡accumulated ¡value ¡
  • A ¡task ¡updates ¡the ¡value ¡of ¡the ¡thread ¡it ¡executes ¡in ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

14 ¡

slide-15
SLIDE 15

ImplementaKon ¡OpKmizaKons ¡

  • Switch ¡to ¡sequenKal ¡Reduce ¡for ¡the ¡base ¡k ¡elements ¡
  • Do ¡k ¡way ¡splits ¡instead ¡of ¡two ¡way ¡splits ¡
  • Maintain ¡a ¡thread-­‑local ¡accumulated ¡value ¡
  • A ¡task ¡updates ¡the ¡value ¡of ¡the ¡thread ¡it ¡executes ¡in ¡
  • Requires ¡that ¡the ¡reduce ¡funcKon ¡is ¡also ¡commutaKve ¡

¡ ¡f(a, ¡b) ¡= ¡f(b, ¡a) ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

15 ¡

slide-16
SLIDE 16

ImplementaKon ¡OpKmizaKons ¡

  • Switch ¡to ¡sequenKal ¡Reduce ¡for ¡the ¡base ¡k ¡elements ¡
  • Do ¡k ¡way ¡splits ¡instead ¡of ¡two ¡way ¡splits ¡
  • Maintain ¡a ¡thread-­‑local ¡accumulated ¡value ¡
  • A ¡task ¡updates ¡the ¡value ¡of ¡the ¡thread ¡it ¡executes ¡in ¡
  • Requires ¡that ¡the ¡reduce ¡funcKon ¡is ¡also ¡commutaKve ¡

¡ ¡f(a, ¡b) ¡= ¡f(b, ¡a) ¡

  • Thread ¡local ¡values ¡are ¡then ¡merged ¡in ¡a ¡separate ¡pass ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

16 ¡

slide-17
SLIDE 17

Scan ¡

  • Given ¡a ¡funcKon ¡f: ¡(A, ¡B) ¡=> ¡B ¡
  • A ¡collecKon ¡a: ¡A[] ¡
  • An ¡iniKal ¡value ¡b0: ¡B ¡
  • Generate ¡a ¡collecKon ¡b: ¡B[] ¡
  • Where ¡b[i] ¡= ¡f(A[i-­‑1], ¡… ¡f(A[1], ¡f(A[0], ¡b0)) ¡ ¡) ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

17 ¡

f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ b0 ¡ A ¡

slide-18
SLIDE 18

Scan ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

18 ¡

f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ b0 ¡ A ¡ B acc = b_0; for( i = 0; i < n; i++ ) { acc = f( a[i], acc ); }

slide-19
SLIDE 19

Scan ¡is ¡Efficiently ¡Parallelizable ¡

  • When ¡f ¡is ¡associaKve ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

19 ¡

slide-20
SLIDE 20

Scan ¡is ¡Efficiently ¡Parallelizable ¡

  • When ¡f ¡is ¡associaKve ¡
  • Scan(f, ¡A[1..n], ¡b_0) ¡= ¡Scan(f, ¡A[1..n/2], ¡b_0), ¡ ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Scan(f, ¡A[n/2+1…n], ¡____) ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

20 ¡

f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ b0 ¡ A ¡ ? ¡

slide-21
SLIDE 21

Scan ¡is ¡Efficiently ¡Parallelizable ¡

  • When ¡f ¡is ¡associaKve ¡
  • Scan(f, ¡A[1..n], ¡b_0) ¡= ¡Scan(f, ¡A[1..n/2], ¡b_0), ¡ ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Scan(f, ¡A[n/2+1…n], ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Reduce(f, ¡A[1..n/2], ¡b_0)) ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

21 ¡

f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ b0 ¡ A ¡ ? ¡

slide-22
SLIDE 22

Scan ¡is ¡useful ¡in ¡many ¡places ¡

  • Radix ¡Sort ¡ ¡
  • Ray ¡Tracing ¡
  • … ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

22 ¡

slide-23
SLIDE 23

Scan ¡is ¡useful ¡in ¡many ¡places ¡

  • Radix ¡Sort ¡( ¡ ¡) ¡
  • Ray ¡Tracing ¡
  • … ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

23 ¡

slide-24
SLIDE 24

CompuKng ¡Line ¡of ¡Sight ¡

  • Given ¡x1, ¡… ¡xn ¡with ¡alKtudes ¡a[1],…a[n] ¡
  • Which ¡of ¡the ¡points ¡are ¡visible ¡from ¡x0 ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

24 ¡

slide-25
SLIDE 25

CompuKng ¡Line ¡of ¡Sight ¡

  • Given ¡x0, ¡… ¡xn ¡with ¡alKtudes ¡alt[0],…alt[n] ¡
  • Which ¡of ¡the ¡points ¡are ¡visible ¡from ¡x0 ¡
  • angle[i] ¡= ¡arctan( ¡(alt[i] ¡– ¡alt[0]) ¡/ ¡i ¡) ¡
  • xi ¡is ¡visible ¡from ¡x0 ¡if ¡all ¡points ¡between ¡them ¡have ¡

lesser ¡angle ¡than ¡angle[i] ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

25 ¡

slide-26
SLIDE 26

SoluKon ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

26 ¡

slide-27
SLIDE 27

Radix ¡Sort ¡

5 ¡= ¡101 ¡ 7 ¡= ¡111 ¡ 2 ¡= ¡010 ¡ 4 ¡= ¡100 ¡ 5 ¡= ¡101 ¡ 3 ¡= ¡011 ¡ 1 ¡= ¡001 ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

27 ¡

slide-28
SLIDE 28

Radix ¡Sort ¡

5 ¡= ¡101 ¡ 7 ¡= ¡111 ¡ 2 ¡= ¡010 ¡ 4 ¡= ¡100 ¡ 5 ¡= ¡101 ¡ 3 ¡= ¡011 ¡ 1 ¡= ¡001 ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

28 ¡

2 ¡= ¡010 ¡ 4 ¡= ¡100 ¡ 5 ¡= ¡101 ¡ 7 ¡= ¡111 ¡ 5 ¡= ¡101 ¡ 3 ¡= ¡011 ¡ 1 ¡= ¡001 ¡

slide-29
SLIDE 29

Radix ¡Sort ¡

5 ¡= ¡101 ¡ 7 ¡= ¡111 ¡ 2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= ¡[1 ¡1 ¡0 ¡0 ¡1 ¡1 ¡1] ¡
  • Pack ¡all ¡elements ¡with ¡flag ¡= ¡0 ¡before ¡elements ¡with ¡

flag ¡ ¡= ¡1 ¡

  • A’ ¡= ¡[2 ¡4 ¡5 ¡7 ¡5 ¡3 ¡1] ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

31 ¡

slide-32
SLIDE 32

SoluKon ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

32 ¡

slide-33
SLIDE 33

Other ¡ApplicaKons ¡of ¡Scan ¡

  • Radix ¡Sort ¡
  • CompuKng ¡Line ¡of ¡Sight ¡
  • Adding ¡mulK-­‑precision ¡numbers ¡
  • Quick ¡Sort ¡
  • To ¡search ¡for ¡regular ¡expressions ¡
  • Parallel ¡grep ¡
  • … ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

33 ¡

slide-34
SLIDE 34

High ¡Level ¡Points ¡

  • Minimize ¡dependence ¡between ¡parallel ¡loops ¡
  • Unintended ¡dependences ¡= ¡data ¡races ¡
  • Next ¡lecture ¡
  • Carefully ¡analyze ¡remaining ¡dependences ¡
  • Use ¡Reduce ¡and ¡Scan ¡pa7erns ¡where ¡applicable ¡ ¡

6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡

34 ¡