Scan

From CSE231 Wiki
Revision as of 16:46, 23 May 2018 by WillZhao (talk | contribs) (Added pack)
Jump to: navigation, search

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:

y_i = y_{i-1} + x_i

make it seem to have little hope for parallelism. However, simple yet clever approaches can achieve \log n 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

Hillis-Steele Prefix Sum.svg

Pack

One of the applications for scan is pack operation. Given an input array, the operation produces an output array containing only the elements that satisfy some specified predicate.

The problem with parallelizing pack is that although it is easy to determine whether an element should be filtered out into the output, we can't know where to put the element in the output array. It seems that placing an element into the output requires knowledge of the placement of the previous elements.

This is where prefix sum becomes very useful. For example, if you have an integer array input:

2 6 1 3 7 9 4

You want to filter out all elements that are less than five. You can first create a flag array in which all the positions i where input[i] is less than 5 is flagged as "1" and all other positions are marked as "0".

1 0 1 1 0 0 1

The prefix sum of this flag array is:

1 1 2 3 3 3 4

Notice how each position that that was flagged now has a distinct number assigned to it in the prefix sum array. We can use this to help us index the output array.

(Optional) Work-efficient Blelloch Scan

Blelloch Algorithm

Prefix sum 16.svg

Code To Investigate

class ArraysHolder

getSrc()
getDst()
nextSrcAndDst()

class PowersOfTwoLessThan implements Iterable<Integer>

Code To Implement

Sequential Scan

class: SequentialScan.java Java.png
methods: sumScan
package: scan.studio
source folder: src/main/java

method: public int[] sumScan(int[] data) Sequential.svg (sequential implementation only)

Default Arrays Holder

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 from at each power of two level of the scan, we should be good to go.

The table below shows which array will be passed back for each offset if nextSrcAndDst used appropriately from a parallel scan.

1 2 4 8 16
src: data a b a b
dst: a b a b a

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

class: DefaultArraysHolder.java Java.png
methods: getSrc
getDst
nextSrcAndDst
size
package: scan.studio
source folder: src/main/java

Hillis and Steele Parallel Scan

class: ParallelScan.java Java.png
methods: sumScan
package: scan.studio
source folder: src/main/java

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

Parallel Pack

class: ParallelPack.java Java.png
methods: pack
package: pack.studio
source folder: src/main/java

method: public static <T> T[] pack(Class<T[]> arrayType, T[] arr, Predicate<T> predicate) Parallel.svg (parallel implementation required)

(Optional) Blelloch Work Efficient Scan

class: WorkEfficientScan.java Java.png
methods: sumScan
package: scan.challenge
source folder: src/main/java

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

Testing Your Solution

Correctness

Required

class: ScanTestSuite.java Junit.png
package: scan.studio
source folder: src/test/java
class: PackTestSuite.java Junit.png
package: pack.studio
source folder: src/test/java

Optional Work Efficient

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