parallel patterns reduce scan
play

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


  1. 1 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ PARALLEL ¡PATTERNS ¡ REDUCE ¡& ¡SCAN ¡

  2. 2 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ 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 ¡

  3. 3 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ We ¡Have ¡Already ¡Seen ¡Some ¡Pa7erns ¡

  4. 4 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ 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 ¡

  5. 5 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ Map ¡ • Given ¡a ¡funcKon ¡f ¡: ¡(A) ¡=> ¡B ¡ • A ¡collecKon ¡a: ¡A[] ¡ • Generates ¡a ¡collecKon ¡b: ¡B[], ¡where ¡B[i] ¡= ¡f( ¡A[i] ¡) ¡ A ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ B ¡ • Parallel.For, ¡Paralle.ForEach ¡ • Where ¡each ¡loop ¡iteraKon ¡is ¡independent ¡

  6. 6 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ 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 ¡

  7. 7 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ 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 ¡

  8. 8 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ Reduce ¡ • Given ¡a ¡funcKon ¡f: ¡(A, ¡B) ¡=> ¡B ¡ • A ¡collecKon ¡a: ¡A[] ¡ • An ¡iniKal ¡value ¡b 0 : ¡B ¡ • Generate ¡a ¡final ¡value ¡b: ¡B ¡ • Where ¡b ¡= ¡f(A[n-­‑1], ¡… ¡f(A[1], ¡f(A[0], ¡b 0 )) ¡ ¡) ¡ A ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ b ¡ b 0 ¡

  9. 9 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ Reduce ¡ • Given ¡a ¡funcKon ¡f: ¡(A, ¡B) ¡=> ¡B ¡ • A ¡collecKon ¡a: ¡A[] ¡ • An ¡iniKal ¡value ¡b 0 : ¡B ¡ • Generate ¡a ¡final ¡value ¡b: ¡B ¡ • Where ¡b ¡= ¡f(A[n-­‑1], ¡… ¡f(A[1], ¡f(A[0], ¡b 0 )) ¡ ¡) ¡ A ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ b ¡ b 0 ¡ • Only ¡consider ¡where ¡A ¡and ¡B ¡are ¡the ¡same ¡type ¡

  10. 10 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ Reduce ¡ B acc = b_0; for( i = 0; i < n; i++ ) { acc = f( a[i], acc ); } b = acc; A ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ b ¡ b 0 ¡

  11. 11 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ 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 ¡

  12. 12 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ 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, ¡ ¡

  13. 13 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ 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 ¡ A ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ b 0 ¡ I ¡ f ¡ b ¡

  14. 14 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ 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 ¡

  15. 15 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ 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) ¡

  16. 16 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ 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 ¡

  17. 17 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ Scan ¡ • Given ¡a ¡funcKon ¡f: ¡(A, ¡B) ¡=> ¡B ¡ • A ¡collecKon ¡a: ¡A[] ¡ • An ¡iniKal ¡value ¡b 0 : ¡B ¡ • Generate ¡a ¡collecKon ¡b: ¡B[] ¡ • Where ¡b[i] ¡= ¡f(A[i-­‑1], ¡… ¡f(A[1], ¡f(A[0], ¡b 0 )) ¡ ¡) ¡ A ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ b 0 ¡

  18. 18 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ Scan ¡ B acc = b_0; for( i = 0; i < n; i++ ) { acc = f( a[i], acc ); } A ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ b 0 ¡

  19. 19 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ Scan ¡is ¡Efficiently ¡Parallelizable ¡ • When ¡f ¡is ¡associaKve ¡

  20. 20 ¡ 6/16/2010 ¡ Parallel ¡Pa7erns ¡-­‑ ¡Reduce ¡& ¡Scan ¡ 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], ¡____) ¡ A ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ f ¡ b 0 ¡ ? ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend