Difference between revisions of "Scan"

From CSE231 Wiki
Jump to navigation Jump to search
Line 39: Line 39:
  
 
<youtube>RmekgaW8X8A</youtube>
 
<youtube>RmekgaW8X8A</youtube>
 +
 +
=Warmup=
 +
==[[Sequential_Scan_Assignment|Sequential Sum Scan]]==
 +
  
 
=Code To Use=
 
=Code To Use=
 +
==[[PowersOf2Iterable]]==
 +
This [[PowersOf2Iterable|exercise]] should come in handy here. Recall that we implemented a <code>PowersOfTwoLessThan</code> class in this exercise.
 +
 +
<syntaxhighlight lang="java">
 +
for(int v : new PowersOfTwoLessThan(71)) {
 +
    System.out.println(v);
 +
}
 +
</syntaxhighlight>
 +
 
class [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/PhasableIntArrays.html PhasableIntArrays] extends [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html AbstractPhasable]
 
class [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/PhasableIntArrays.html PhasableIntArrays] extends [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html AbstractPhasable]
 
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html#getSrcForPhase(int) getSrcForPhase(phaseIndex)]
 
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html#getSrcForPhase(int) getSrcForPhase(phaseIndex)]
 
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html#getSrcForPhase(int) getSrcForPhase(phaseIndex)]
 
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html#getSrcForPhase(int) getSrcForPhase(phaseIndex)]
  
[[PowersOf2Iterable]]
+
==PhasableIntArrays==
 +
One of the downsides to parallel scan requires memory.  For our scans we add the additional requirement that we will not mutate the incoming array parameter.  We could create log(n) arrays, one for each level but that would be wasteful.  If we create two array buffers and switch which is the source to read from and which is the destination to write to at each power-of-two level of the scan, we should be good to go. Re-assigning two buffers back and forth as they switch between source and destination roles raises finality issues when attempting to access them inside of lambdas.
  
==PhasableIntArrays==
+
This is what <code>PhasableIntArrays</code> helps us with. By using its <code>getDstForPhase(int phaseIndex)</code> and <code>getSrcForPhase(int phaseIndex)</code>, we can get the source and destination array corresponding to the phase.
One of the downsides to parallel scan requires memory.  For our scans we add the additional requirement that we will not mutate the incoming array parameter.  We could create log(n) arrays, one for each level but that would be wasteful.  If we create two array buffers and switch which is the source to read from and which is the destination to write to at each power-of-two level of the scan, we should be good to go. This is what <code>PhasableIntArrays</code> helps us with. By using its <code>getDstForPhase(int phaseIndex)</code> and <code>getSrcForPhase(int phaseIndex)</code>, we can get the source and destination array corresponding to the phase.
 
  
 
The table below shows which of the two buffer arrays, a or b, will be passed back for each phase index as the source or the destination array.
 
The table below shows which of the two buffer arrays, a or b, will be passed back for each phase index as the source or the destination array.
Line 64: Line 77:
  
 
'''NOTE:''' think about which array is the correct one to return from your scan method given how you write your code.
 
'''NOTE:''' think about which array is the correct one to return from your scan method given how you write your code.
 
==[[PowersOf2Iterable]]==
 
This [[PowersOf2Iterable|studio]] should come in handy here. Recall that we implemented a <code>PowersOfTwoLessThan</code> class in this studio.
 
 
Example usage:
 
<nowiki>for(int v : new PowersOfTwoLessThan(71)) {
 
    System.out.println(v);
 
}</nowiki>
 
  
 
=Code To Implement=
 
=Code To Implement=

Revision as of 19:43, 13 March 2023

Motivation

Scan, also known as parallel prefix, is a fundamental and useful operation in parallel programming. We will gain experience in building Hillis & Steele scan with an optional work efficient Blellock scan.

Further, the dependencies in scan:

make it seem to have little hope for parallelism. However, simple yet clever approaches can achieve critical path lengths.

While we will simply implement prefix sum, scan can be used for other associative operations.

Background

Prefix Sum

Hillis-Steele Prefix Sum

Hillis and Steele Algorithm

Data parallel algorithms

Hillis-Steele Prefix Sum.svg

(Extra Credit) Work-efficient

Exclusive (Blelloch)

Blelloch Algorithm

Scan Primitives for Vector Computers

Prefix Sums and Their Applications

Inclusive

Prefix sum 16.svg

Lecture

slides

Warmup

Sequential Sum Scan

Code To Use

PowersOf2Iterable

This exercise should come in handy here. Recall that we implemented a PowersOfTwoLessThan class in this exercise.

for(int v : new PowersOfTwoLessThan(71)) {
    System.out.println(v);
}

class PhasableIntArrays extends AbstractPhasable

getSrcForPhase(phaseIndex)
getSrcForPhase(phaseIndex)

PhasableIntArrays

One of the downsides to parallel scan requires memory. For our scans we add the additional requirement that we will not mutate the incoming array parameter. We could create log(n) arrays, one for each level but that would be wasteful. If we create two array buffers and switch which is the source to read from and which is the destination to write to at each power-of-two level of the scan, we should be good to go. Re-assigning two buffers back and forth as they switch between source and destination roles raises finality issues when attempting to access them inside of lambdas.

This is what PhasableIntArrays helps us with. By using its getDstForPhase(int phaseIndex) and getSrcForPhase(int phaseIndex), we can get the source and destination array corresponding to the phase.

The table below shows which of the two buffer arrays, a or b, will be passed back for each phase index as the source or the destination array.

phase 0 1 2 3 4
src data a b a b
dst a b a b a
bonus info: power of 2 1 2 4 8 16

NOTE: think about which array is the correct one to return from your scan method given how you write your code.

Code To Implement

class: StepEfficientParallelSumScanner.java Java.png
methods: sumScan
package: scan.exercise
source folder: student/src/main/java

method: int[] sumScan(int[] data) Parallel.svg (parallel implementation required)

Visualization

class: ScanApp.java VIZ
package: scan.viz
source folder: student/src/main/java

Step Efficient Scan Viz.png

Testing Your Solution

Correctness

Required

class: __ScanTestSuite.java Junit.png
package: scan.exercise
source folder: testing/src/test/java

Optional Work Efficient

class: _WorkEfficientScanTestSuite.java Junit.png
package: scan.challenge
source folder: testing/src/test/java