Difference between revisions of "Slices"

From CSE231 Wiki
Jump to navigation Jump to search
 
(16 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
This assignment has been replaced by [[Ranges]].
 +
<!--
 +
 
credit for this assignment: Finn Voichick and Dennis Cosgrove
 
credit for this assignment: Finn Voichick and Dennis Cosgrove
  
Line 11: Line 14:
 
This studio is about creating an easier way to split data so that it can be worked on in parallel. The splitting of the data is still done sequentially. That means at no point in this studio should you be using async() or finish().
 
This studio is about creating an easier way to split data so that it can be worked on in parallel. The splitting of the data is still done sequentially. That means at no point in this studio should you be using async() or finish().
  
{{warning | Do NOT Copy The Data Into SubArrays}}
+
=Code To Investigate=
 +
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/slice/core/IndexedRange.html class IndexedRange]
 +
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/slice/core/IndexedRange.html#%3Cinit%3E(int,int,int) constructor].
 +
 
 +
<pre>public IndexedRange(int sliceIndexId, int minInclusive, int maxExclusive)</pre>
 +
 
 +
This class has everything you need for [[Nucleobase_Counting#Coarsening_N-Way_Split|n-way split]] problems, specifically: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/slice/core/IndexedRange.html#getSliceIndexId() getSliceIndexId()], [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/slice/core/IndexedRange.html#getMinInclusive() getMinInclusive()], and [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/slice/core/IndexedRange.html#getMaxExclusive() getMaxExclusive()].
  
Our "Slices" will actually be a list of objects called "Slice". A Slice contains a couple things, one of them being the full, original data that is passed through at the start. It is important that the entire array is given to the Slice. Not a copy of the array, nor a shortened version of the array, but the ''entire'' thing.
+
=Code to Implement=
 +
{{CodeToImplement|Slices|createNSlices|slices.studio}}
  
=class Slice<T>=
+
==createNSlices==
use: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/slice/core/Slice.html class Slice]
 
  
This class has everything you need for [[Nucleobase_Counting#Coarsening_N-Way_Split|n-way split]] problems, specifically: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/slice/core/Slice.html#getSliceIndexId-- getSliceIndexId()], [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/slice/core/Slice.html#getMinInclusive-- getMinInclusive()], and [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/slice/core/Slice.html#getMaxExclusive-- getMaxExclusive()].
+
{{Sequential|public static List<IndexedRange> createNSlices(int minInclusive, int maxExclusive, int numSlices)}}
  
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/slice/core/Slice.html#getOriginalUnslicedData-- getOriginalUnslicedData()] is not necessary until the [[K-MerCounting_Assignment|K-Mer Counting Lab]]
+
Given a range [minInclusive, maxExclusive) and a number of slices to make, return a List of IndexedRanges. Understanding what a IndexedRange is and how it is used is important when writing this solution.  
  
 +
The goal is to have each slice's range be the entire array when put together, with no overlap. Some examples are giving below. Working through more examples can be a helpful way of figuring out what code to write if you get stuck.
  
=Code to Implement=
+
{{ tip | In a slice, the minimum is inclusive and the maximum is exclusive.  This means for two slices next to each other, slice1.getMaxExclusive() is equal to slice2.getMinInclusive(). }}
{{CodeToImplement|Slices|createNSlicesForArrayObject|slices.studio}}
 
  
==createNSlicesForArrayObject==
+
==Strict Specification==
 +
We are overly strict about the specification of how the data '''must''' be sliced up.  This is to allow to accurately compare results intermediate results throughout the semester.
  
{{Sequential|private static <T> List<Slice<T>> createNSlicesForArrayObject(A data, int numSlices)}}
+
'''Example: array.length=8; numSlices=4'''
  
Given an Array (A) and a number of slices to make, return a List of object Slice.
+
{| class="wikitable"
 +
|A
 +
|A
 +
|B
 +
|B
 +
|C
 +
|C
 +
|D
 +
|D
 +
|}
  
 +
{| class="wikitable"
 +
|-
 +
!Slice ID !! Min Inclusive !! Max Exclusive
 +
|-
 +
|0 || 0 || 2
 +
|-
 +
|1 || 2 || 4
 +
|-
 +
|2 || 4 || 6
 +
|-
 +
|3 || 6 || 8
 +
|}
  
In order to support primitive arrays (e.g. byte[], int[], et cetera) and non-primitive arrays (e.g. Object[]) we provide all of the public methods, each which simply call the single method requiring implementation:
 
  
<nowiki> public static <T> List<Slice<T[]>> createNSlices(T[] data, int numSlices) {
 
return createNSlicesForArrayObject(data, numSlices);
 
}
 
public static List<Slice<byte[]>> createNSlices(byte[] data, int numSlices) {
 
return createNSlicesForArrayObject(data, numSlices);
 
}
 
public static List<Slice<char[]>> createNSlices(char[] data, int numSlices) {
 
return createNSlicesForArrayObject(data, numSlices);
 
}
 
public static List<Slice<short[]>> createNSlices(short[] data, int numSlices) {
 
return createNSlicesForArrayObject(data, numSlices);
 
}
 
public static List<Slice<int[]>> createNSlices(int[] data, int numSlices) {
 
return createNSlicesForArrayObject(data, numSlices);
 
}
 
public static List<Slice<long[]>> createNSlices(long[] data, int numSlices) {
 
return createNSlicesForArrayObject(data, numSlices);
 
}
 
public static List<Slice<float[]>> createNSlices(float[] data, int numSlices) {
 
return createNSlicesForArrayObject(data, numSlices);
 
}
 
public static List<Slice<double[]>> createNSlices(double[] data, int numSlices) {
 
return createNSlicesForArrayObject(data, numSlices);
 
}</nowiki>
 
  
{{ tip | To pull this off we will need to use the [https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Array.html#getLength-java.lang.Object- Array.getLength(Object)] method. }}
+
'''Example: array.length=7; numSlices=4'''
  
=Strict Specification=
 
We are overly strict about the specification of how the data '''must''' be sliced up.  This is to allow to accurately compare results intermediate results throughout the semester.
 
==Example: array.length=7; numSlices=4==
 
 
Distribute the remainder 1 each to the lower end slices (the first few slices or the lower index slices).
 
Distribute the remainder 1 each to the lower end slices (the first few slices or the lower index slices).
  
Line 75: Line 78:
 
|C
 
|C
 
|D
 
|D
 +
|}
 +
 +
{| class="wikitable"
 +
|-
 +
!Slice ID !! Min Inclusive !! Max Exclusive
 +
|-
 +
|0 || 0 || 2
 +
|-
 +
|1 || 2 || 4
 +
|-
 +
|2 || 4 || 6
 +
|-
 +
|3 || 6 || 7
 
|}
 
|}
  
Line 87: Line 103:
 
|D
 
|D
 
</s>
 
</s>
 +
 +
Giving all the remainder to one slice defeats the purpose of balancing the workload!
 +
 +
==Convenience Methods==
 +
 +
In order to support primitive arrays (e.g. byte[], int[], et cetera) and non-primitive arrays (e.g. Object[]) we provide all of the public methods, each which simply call the single method requiring implementation:
 +
 +
<nowiki> public static <C> List<IndexedRange> createNSlices(C[] data, int numSlices) {
 +
return createNSlices(0, data.length, numSlices);
 +
}
 +
 +
public static List<IndexedRange> createNSlices(byte[] data, int numSlices) {
 +
return createNSlices(0, data.length, numSlices);
 +
}
 +
 +
public static List<IndexedRange> createNSlices(char[] data, int numSlices) {
 +
return createNSlices(0, data.length, numSlices);
 +
}
 +
 +
public static List<IndexedRange> createNSlices(short[] data, int numSlices) {
 +
return createNSlices(0, data.length, numSlices);
 +
}
 +
 +
public static List<IndexedRange> createNSlices(int[] data, int numSlices) {
 +
return createNSlices(0, data.length, numSlices);
 +
}
 +
 +
public static List<IndexedRange> createNSlices(long[] data, int numSlices) {
 +
return createNSlices(0, data.length, numSlices);
 +
}
 +
 +
public static List<IndexedRange> createNSlices(float[] data, int numSlices) {
 +
return createNSlices(0, data.length, numSlices);
 +
}
 +
 +
public static List<IndexedRange> createNSlices(double[] data, int numSlices) {
 +
return createNSlices(0, data.length, numSlices);
 +
}</nowiki>
  
 
=Testing Your Solution=
 
=Testing Your Solution=
 
==Correctness==
 
==Correctness==
 
{{TestSuite|SlicesTestSuite|slice.studio}}
 
{{TestSuite|SlicesTestSuite|slice.studio}}
 +
 +
=Pledge, Acknowledgments, Citations=
 +
{{Pledge|studio-slices}}
 +
-->

Latest revision as of 21:56, 30 November 2022

This assignment has been replaced by Ranges.