Difference between revisions of "Pack"

From CSE231 Wiki
Jump to navigation Jump to search
Line 10: Line 10:
 
For example, if you have a String array input:
 
For example, if you have a String array input:
 
{| class="wikitable"
 
{| class="wikitable"
|Africa||Asia||South America||North America||Europe||Australia||Antarctica
+
|Africa||Asia||SouthAmerica||NorthAmerica||Europe||Australia||Antarctica
 
|}
 
|}
  
Line 24: Line 24:
  
 
{| class="wikitable"
 
{| class="wikitable"
|Africa||South America||North America||Antarctica
+
|Africa||SouthAmerica||NorthAmerica||Antarctica
 
|}
 
|}
  

Revision as of 02:10, 3 April 2019

Motivation

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.

Think about quick sort. In the partition step, we are given a pivot and need to separate the array by the predicate of whether an element is larger than the pivot. This is the perfect place to use the pack operation. You are going to build a more general pack filter in this studio, but you can still attempt the parallel partitioner challenge here: Quicksort Parallel Partitioner.

Background

For example, if you have a String array input:

Africa Asia SouthAmerica NorthAmerica Europe Australia Antarctica

You want to filter out all Strings which do not contain "c". You can first create a flag array in which all the indices where arr[index] contains an "c" 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.

Africa SouthAmerica NorthAmerica Antarctica

Code To Investigate

class: ParallelPack.java DEMO: Java.png
methods: createArray
isChangedFromNeighborOnTheLeft
package: pack.studio
source folder: src//java

createArray

You will definitely want to invoke createArray to... well... create the necessary array of a specified type and length.

	private static <T> T[] createArray(Class<T[]> arrayType, int length) {
		return arrayType.cast(Array.newInstance(arrayType.getComponentType(), length));
	}

isChangedFromNeighborOnTheLeft

Invoking isChangedFromNeighborOnTheLeft is optional but still worth investigating. Since our implementation of Hillis and Steele scan does not mutate the input data, it is not required. If you were to use a scan which mutated the input, you could still divine whether or not to copy the value into the packed array by using this method on the sum scan.

	private static boolean isChangedFromNeighborOnTheLeft(int[] prefixSum, int index) {
		if (index > 0) {
			return prefixSum[index - 1] < prefixSum[index];
		} else {
			return prefixSum[0] == 1;
		}
	}

Code To Implement

Parallel Pack

class: ParallelPack.java Java.png
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.svg (parallel implementation required)

Applications which use scan tend to have step after step each with CPL. Pack is no different. Each step can be parallelized.

Be sure to invoke your parallel scan from the Scan studio.

Testing Your Solution

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

Output

class: ParallelPackOutput.java EXAMPLE Java.png
methods: main
package: pack.output
source folder: src/main/java
String[] names = { "Yogi", "BooBoo", "Paddington", "Winnie", "Baloo", "Smokey", "Fozzie" };
launchApp(() -> {
	String[] packedNamesWhichContainAnI = ParallelPack.pack(String[].class, names, (name) -> {
		return name.contains("i");
	});
	System.out.println(Arrays.toString(names));
	System.out.println(Arrays.toString(packedNamesWhichContainAnI));
});

produces the output:

[Yogi, BooBoo, Paddington, Winnie, Baloo, Smokey, Fozzie]
[Yogi, Paddington, Winnie, Fozzie]