Difference between revisions of "Scan"
(Added default arrays holder, still need to change code to investigate) |
(Added pack) |
||
Line 13: | Line 13: | ||
[https://en.wikipedia.org/wiki/Prefix_sum Prefix Sum] | [https://en.wikipedia.org/wiki/Prefix_sum Prefix Sum] | ||
− | == | + | ==Hillis-Steele Prefix Sum== |
[https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_1:_Shorter_span,_more_parallel Hillis and Steele Algorithm] | [https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_1:_Shorter_span,_more_parallel Hillis and Steele Algorithm] | ||
Line 20: | Line 20: | ||
[[File:Hillis-Steele_Prefix_Sum.svg]] | [[File:Hillis-Steele_Prefix_Sum.svg]] | ||
− | ==Optional== | + | ==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: | ||
+ | {| class="wikitable" | ||
+ | |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". | ||
+ | {| class="wikitable" | ||
+ | |1||0||1||1||0||0||1 | ||
+ | |} | ||
+ | The prefix sum of this flag array is: | ||
+ | {| class="wikitable" | ||
+ | |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== | ||
[https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_2:_Work-efficient Blelloch Algorithm] | [https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_2:_Work-efficient Blelloch Algorithm] | ||
Line 62: | Line 81: | ||
{{Parallel|public int[] sumScan(int[] data)}} | {{Parallel|public int[] sumScan(int[] data)}} | ||
+ | |||
+ | ==Parallel Pack== | ||
+ | |||
+ | {{CodeToImplement|ParallelPack|pack|pack.studio}} | ||
+ | |||
+ | {{Parallel|public static <T> T[] pack(Class<T[]> arrayType, T[] arr, Predicate<T> predicate)}} | ||
==(Optional) Blelloch Work Efficient Scan== | ==(Optional) Blelloch Work Efficient Scan== | ||
Line 72: | Line 97: | ||
===Required=== | ===Required=== | ||
{{TestSuite|ScanTestSuite|scan.studio}} | {{TestSuite|ScanTestSuite|scan.studio}} | ||
+ | {{TestSuite|PackTestSuite|pack.studio}} | ||
===Optional Work Efficient=== | ===Optional Work Efficient=== | ||
{{TestSuite|WorkEfficientScanTestSuite|scan.challenge}} | {{TestSuite|WorkEfficientScanTestSuite|scan.challenge}} |
Revision as of 21:46, 23 May 2018
Contents
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
Hillis-Steele Prefix Sum
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
Code To Investigate
class ArraysHolder
class PowersOfTwoLessThan implements Iterable<Integer>
Code To Implement
Sequential Scan
class: | SequentialScan.java | |
methods: | sumScan | |
package: | scan.studio | |
source folder: | student/src/main/java |
method: public int[] sumScan(int[] data)
(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 | |
methods: | getSrc getDst nextSrcAndDst size |
|
package: | scan.studio | |
source folder: | student/src/main/java |
Hillis and Steele Parallel Scan
class: | ParallelScan.java | |
methods: | sumScan | |
package: | scan.studio | |
source folder: | student/src/main/java |
method: public int[] sumScan(int[] data)
(parallel implementation required)
Parallel Pack
class: | ParallelPack.java | |
methods: | pack | |
package: | pack.studio | |
source folder: | student/src/main/java |
method: public static <T> T[] pack(Class<T[]> arrayType, T[] arr, Predicate<T> predicate)
(parallel implementation required)
(Optional) Blelloch Work Efficient Scan
class: | WorkEfficientScan.java | |
methods: | sumScan | |
package: | scan.challenge | |
source folder: | student/src/main/java |
method: public int[] sumScan(int[] data)
(parallel implementation required)
Testing Your Solution
Correctness
Required
class: | ScanTestSuite.java | |
package: | scan.studio | |
source folder: | testing/src/test/java |
class: | PackTestSuite.java | |
package: | pack.studio | |
source folder: | testing/src/test/java |
Optional Work Efficient
class: | WorkEfficientScanTestSuite.java | |
package: | scan.challenge | |
source folder: | testing/src/test/java |