<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://classes.engineering.wustl.edu/cse231/core/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Cosgroved</id>
	<title>CSE231 Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="http://classes.engineering.wustl.edu/cse231/core/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Cosgroved"/>
	<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Special:Contributions/Cosgroved"/>
	<updated>2026-11-27T12:05:19Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.34.4</generator>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=MergeSort&amp;diff=5454</id>
		<title>MergeSort</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=MergeSort&amp;diff=5454"/>
		<updated>2026-09-29T16:09:20Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* ParallelMergeSorter */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Sorting is a problem that is well solved by divide and conquer algorithms.  Merge sort is a elegant example which can be parallelized in a straight-forward manner.&lt;br /&gt;
&lt;br /&gt;
Finally, parallelization of the combine step, while not trivial, is possible (and left as an optional fun exercise for those so inclined).&lt;br /&gt;
=Background=&lt;br /&gt;
In computer science, merge sort refers to a sorting algorithm which splits an array of data into continuously smaller halves until the arrays reach a size of 1, after which the elements are continuously compared and merged into a sorted array. &lt;br /&gt;
&lt;br /&gt;
For more information on how this process works, visit [https://en.wikipedia.org/wiki/Merge_sort the wikipedia page on merge sort].&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|CS 50 Shorts: Merge Sort|&amp;lt;youtube&amp;gt;Pr2Jf83_kG0&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|HackerRank: Merge Sort|&amp;lt;youtube&amp;gt;KF2j-9iSf4Q&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|Merge Sort Combine Step|&amp;lt;youtube&amp;gt;YgXWDGZai4s&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
==Visualization==&lt;br /&gt;
If you are unclear on how merge sort works, take a look at the [https://visualgo.net/en/sorting?slide=10 visualgo explanation and visualization of merge sort].&lt;br /&gt;
&lt;br /&gt;
[[File:MergeSortViz.png|480px|link=https://visualgo.net/en/sorting?slide=10]]&lt;br /&gt;
&lt;br /&gt;
== Work and Span ==&lt;br /&gt;
The divide step doesn't do any real significant work- it just calculates the midpoint.  The combine step does all the work of merge sort.  The top-level combine (when its two recursive calls have completed) must do N work.  The next level does two ~N/2 combines.  The level below that performs four ~N/4 combines.  And so on.  The table is &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; wide and &amp;lt;math&amp;gt;\log N&amp;lt;/math&amp;gt; high.  Thus the work of merge sort is &amp;lt;math&amp;gt;O(N\log N)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For N=16:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center;&amp;quot;&lt;br /&gt;
! &lt;br /&gt;
! colspan=&amp;quot;16&amp;quot; | &amp;lt;- - - - - - - - - - - - - - N - - - - - - - - - - - - - -&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
!  &lt;br /&gt;
| colspan=&amp;quot;16&amp;quot; | mid&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; | mid&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; | mid&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | mid&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | mid&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | mid&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | mid&lt;br /&gt;
|-&lt;br /&gt;
! &lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | mid&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | mid&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | mid&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | mid&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | mid&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | mid&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | mid&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | mid&lt;br /&gt;
|-&lt;br /&gt;
! ^ &lt;br /&gt;
| style=&amp;quot;background-color:#CCCCFF;&amp;quot; | base&lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
|-&lt;br /&gt;
! &amp;amp;#124;&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | combine [0, 2)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | combine [2, 4)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | combine [4, 6)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | combine [6, 8)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | combine [8, 10)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | combine [10, 12)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | combine [12, 14)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | combine [14, 16)&lt;br /&gt;
|-&lt;br /&gt;
! lg N &lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | combine [0, 4)&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | combine [4, 8)&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | combine [8, 12)&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | combine [12, 16)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;amp;#124;&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | combine [0, 8)&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; | combine [8, 16)&lt;br /&gt;
|-&lt;br /&gt;
! v &lt;br /&gt;
| colspan=&amp;quot;16&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | combine [0, 16)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Technically, the span also include log(N) midpoint calculations from the divide steps, this is insignificant next to N, so we focus on just the work/span of the combines.&lt;br /&gt;
&lt;br /&gt;
The grand vast majority of the span for the classic sequential combine merge sort is the work of one combine for each of the levels &amp;lt;span style=&amp;quot;background:#CCCCFF&amp;quot;&amp;gt;(highlighted in blue)&amp;lt;/span&amp;gt;.  This works out to be the [https://en.wikipedia.org/wiki/Geometric_series geometric series]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N + \frac{N}{2} + \frac{N}{4} + \frac{N}{8} + \frac{N}{16} + \cdots + 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;2N&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Classic_parallel_merge_sort_and_quicksort_span.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
As a [[MergeSort_Parallel_Combiner|challenge problem]], we parallelize the combine step, resulting in a span for merge sort of an outstanding &amp;lt;math&amp;gt;\lg^k{}n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=The Core Questions=&lt;br /&gt;
*What are the tasks?&lt;br /&gt;
*What is the data?&lt;br /&gt;
*Is the data mutable?&lt;br /&gt;
*If so, how is it shared?&lt;br /&gt;
&lt;br /&gt;
=Mistakes To Avoid=&lt;br /&gt;
{{warning | Be sure to calculate the midpoint correctly. }}&lt;br /&gt;
{{warning | When checking the base case, remember to account for [minInclusive, maxExclusive).  It is all too easy to get an off by 1 error and stack overflow.}}&lt;br /&gt;
{{warning | When transitioning to sequential, make sure to NOT sort the entire array when you are only responsible for a range [min,max).}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Code to Investigate and Implement=&lt;br /&gt;
You will need to implement merge sort sequentially and in parallel, but you will need to do both implementations recursively. The kernel method should call itself using recursion, but each public mergesort method should only call its kernel once to do the work.&lt;br /&gt;
&lt;br /&gt;
==Combine Step==&lt;br /&gt;
Both the sequential and parallel merge sorts will be passed a [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/sort/merge/core/Combiner.html Combiner].  When you are done with the divide and conquer phases, invoke [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/sort/merge/core/Combiner.html#combine(T%5B%5D,T%5B%5D,java.util.Comparator) combiner.combineRange(a, b, comparator)] to merge the two sorted sub-problem results. For the base case, [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/sort/merge/core/Combiner.html#createSingleItemArray(T) combiner.createSingleItemArray(item)] is provided.&lt;br /&gt;
&lt;br /&gt;
===interface Combiner&amp;lt;T&amp;gt;===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public interface Combiner&amp;lt;T&amp;gt; {&lt;br /&gt;
	IntFunction&amp;lt;T[]&amp;gt; arrayGenerator();&lt;br /&gt;
&lt;br /&gt;
	T[] combine(T[] a, T[] b, Comparator&amp;lt;T&amp;gt; comparator) throws InterruptedException, ExecutionException;&lt;br /&gt;
&lt;br /&gt;
	default T[] createSingleItemArray(T item) {&lt;br /&gt;
		T[] result = arrayGenerator().apply(1);&lt;br /&gt;
		result[0] = item;&lt;br /&gt;
		return result;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===class SequentialCombiner&amp;lt;T&amp;gt;===&lt;br /&gt;
{{CodeToImplement|SequentialCombiner|constructor&amp;lt;br/&amp;gt;arrayGenerator&amp;lt;br/&amp;gt;combine|sort.merge.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Constructor_1param_4deep|arrayGenerator}}&lt;br /&gt;
&lt;br /&gt;
{{Getter_4deep|arrayGenerator}}&lt;br /&gt;
====combine====&lt;br /&gt;
Implement the combine step.&lt;br /&gt;
&lt;br /&gt;
==Sort==&lt;br /&gt;
&lt;br /&gt;
===interface Sorter&amp;lt;T&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public interface Sorter&amp;lt;T&amp;gt; {&lt;br /&gt;
	default void sort(T[] data, Comparator&amp;lt;T&amp;gt; comparator) throws InterruptedException, ExecutionException {&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	void sortRange(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive)&lt;br /&gt;
			throws InterruptedException, ExecutionException;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|Sorter|sort|sort.core}}&lt;br /&gt;
&lt;br /&gt;
The [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/spring22/apidocs/sort/core/Sorter.html Sorter&amp;lt;T&amp;gt;] interface provides two methods: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/spring22/apidocs/sort/core/Sorter.html#sort(T%5B%5D,java.util.Comparator) sort] and [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/spring22/apidocs/sort/core/Sorter.html#sortRange(T%5B%5D,java.util.Comparator,int,int) sortRange].  We will leave sortRange to the classes which implement Sorter.&lt;br /&gt;
&lt;br /&gt;
====sort====&lt;br /&gt;
&lt;br /&gt;
You will provide a default implementation of the sort method, so that all implementing classes do not have to.&lt;br /&gt;
&lt;br /&gt;
{{Sequential|default void sort(T[] data, Comparator&amp;lt;T&amp;gt; comparator)}}&lt;br /&gt;
&lt;br /&gt;
You should simply invoke the sortRange method with the correct arguments. Consider whether you should use data.length-1 or data.length as your max, given that we've named the variable &amp;lt;code&amp;gt;maxExclusive&amp;lt;/code&amp;gt;.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
==SequentialCombiner==&lt;br /&gt;
Merge sort's combine step leverages the fact that it can rely on the two sub-ranges are provided in sorted order.  Use this fact to fill the provided temporary buffer with the sorted contents of the entire range [min, maxExclusive).&lt;br /&gt;
&lt;br /&gt;
Performance note: in an effort to make this part of the assignment more appropriately approachable, a temporary buffer will be created, combined into, and copied back from.  Merge sort can be built such that depths of the recursion alternate which buffer is read from and written to.  The added complexity this performance improvement would incur is not deemed valuable from a pedagogical stand point, but it is worth noting.&lt;br /&gt;
&lt;br /&gt;
=== (Provided) combineRange ===&lt;br /&gt;
The public method &amp;lt;code&amp;gt;combineRange(data, comparator, min, mid, maxExclusive)&amp;lt;/code&amp;gt; is provided.  It creates a temporary buffer and passes it to &amp;lt;code&amp;gt;combineRangeIntoBuffer(buffer, data, comparator, min, mid, maxExclusive)&amp;lt;/code&amp;gt; which you will implement to fill the temporary buffer with the combined sorted contents.  The contents of the temporary buffer is then copied back to the data array with [https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#arraycopy-java.lang.Object-int-java.lang.Object-int-int- System.arraycopy()].&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|combineRange(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int mid, int maxExclusive)|&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;@Override&lt;br /&gt;
public void combineRange(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int mid, int maxExclusive) {&lt;br /&gt;
	int rangeLength = maxExclusive - min;&lt;br /&gt;
	T[] buffer = ArrayUtils.createArray(componentType, rangeLength);&lt;br /&gt;
	combineRangeIntoBuffer(buffer, data, comparator, min, mid, maxExclusive);&lt;br /&gt;
	System.arraycopy(buffer, 0, data, min, maxExclusive - min);&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=== (Exercise) combineRangeIntoBuffer ===&lt;br /&gt;
{{CodeToImplement|SequentialCombiner|combineRangeIntoBuffer|sort.merge.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private static &amp;lt;T&amp;gt; void combineRangeIntoBuffer(T[] buffer, T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int mid, int maxExclusive)}}&lt;br /&gt;
&lt;br /&gt;
Here you will implement the classic sequential merge combine outlined in the [[#Background|Background]] section.  You will read from &amp;lt;code&amp;gt;data&amp;lt;/code&amp;gt; between &amp;lt;code&amp;gt;min&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;maxExclusive&amp;lt;/code&amp;gt; and write to &amp;lt;code&amp;gt;buffer&amp;lt;/code&amp;gt; starting at 0.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===SequentialMergeSorter===&lt;br /&gt;
{{CodeToImplement|SequentialMergeSorter|sortRangeOutOfPlace|sort.merge.exercise}}&lt;br /&gt;
&lt;br /&gt;
Implement the classic divide and conquer sequential algorithm here.  &lt;br /&gt;
&lt;br /&gt;
{{Warning| Be sure to leverage the provided &amp;lt;code&amp;gt;private final Combiner&amp;lt;T&amp;gt; combiner;&amp;lt;/code&amp;gt; field to perform the combine step.}}&lt;br /&gt;
&lt;br /&gt;
===ParallelMergeSorter===&lt;br /&gt;
{{CodeToImplement|ParallelMergeSorter|sortRangeOutOfPlace|sort.merge.exercise}}&lt;br /&gt;
&lt;br /&gt;
Adapt your sequential algorithm to take advantage of what can be parallelized.&lt;br /&gt;
&lt;br /&gt;
What can be in parallel?&lt;br /&gt;
&lt;br /&gt;
What is dependent on the parallel tasks completing?&lt;br /&gt;
&lt;br /&gt;
{{Warning|Leverage the provided &amp;lt;code&amp;gt;private final IntPredicate isParallelDesired;&amp;lt;/code&amp;gt; field to test whether or not parallelism is warranted for the current range length.}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|Fall back to the &amp;lt;code&amp;gt;sortRangeOutOfPlace&amp;lt;/code&amp;gt; method on the provided &amp;lt;code&amp;gt;private final Sorter&amp;lt;T&amp;gt; lowerOverheadSorter;&amp;lt;/code&amp;gt; field when the &amp;lt;code&amp;gt;isParallelDesired&amp;lt;/code&amp;gt; predicate indicates that parallelism is not longer warranted.}}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
{{CodeToImplement|MergeSort|sequentialMergeSort&amp;lt;br&amp;gt;sequentialMergeSortKernel&amp;lt;br&amp;gt;parallelMergeSort&amp;lt;br&amp;gt;parallelMergeSortKernel|sort.studio.merge}}&lt;br /&gt;
==Sequential Implementation==&lt;br /&gt;
The only thing you will need to edit is the kernel method. As this is a sequential implementation, there is no need to call an async or finish. Using the description of merge sort provided to you in the background, try to consider what the base case would be. Then, further consider how to call the method recursively. When the array reaches the base case, you will need to complete the “merging” aspect of merge sort. As mentioned above, there is a method in the utils class that should help with this.&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public static void sequentialMergeSort(int[] data, Combiner combiner)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private static void sequentialMergeSortKernel(int[] data, int lowInclusive, int highExclusive, Combiner combiner)}}&lt;br /&gt;
==Parallel Implementation==&lt;br /&gt;
This approach should behave much like the sequential implementation, but it should also include async/finish calls. Think carefully about where to place them to maximize concurrency.&lt;br /&gt;
&lt;br /&gt;
Test the length of the range with &amp;lt;code&amp;gt;isParallelPredicate&amp;lt;/code&amp;gt; to determine if you should parallelize or fall back to sequential.&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public static void parallelMergeSort(int[] data, IntPredicate isParallelPredicate, Combiner combiner)}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|private static void parallelMergeSortKernel(int[] data, int lowInclusive, int highExclusive, IntPredicate isParallelPredicate, Combiner combiner)}}&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=(Optional) Challenge Parallel Combiner=&lt;br /&gt;
You can divide and conquer the combine step in merge sort.  The work should remain &amp;lt;math&amp;gt;\mathcal{O}(n\lg{}n)&amp;lt;/math&amp;gt; while the critical path length can be cleanly improved from &amp;lt;math&amp;gt;\mathcal{O}(n)&amp;lt;/math&amp;gt; down to &amp;lt;math&amp;gt;\mathcal{O}(\lg^3{}n)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For details on how to complete this challenge, check out: [[MergeSort_Parallel_Combiner]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__MergeSorterTestSuite|sort.merge.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Client=&lt;br /&gt;
{{Client|MergeSortClient|sort.merge.client|main}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|MergeSortClient|&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;String[] data = createShuffledLetterNumberPairs();&lt;br /&gt;
System.out.println(&amp;quot;unsorted: &amp;quot; + Arrays.toString(data));&lt;br /&gt;
Combiner&amp;lt;String&amp;gt; combiner = new SequentialCombiner&amp;lt;&amp;gt;(String.class);&lt;br /&gt;
IntPredicate isParallelDesired = new GreaterThanThreshold(31);&lt;br /&gt;
Sorter&amp;lt;String&amp;gt; sorter = new ParallelMergeSorter&amp;lt;&amp;gt;(combiner, isParallelDesired);&lt;br /&gt;
sorter.sort(data, Comparator.naturalOrder());&lt;br /&gt;
System.out.println(&amp;quot;  sorted: &amp;quot; + Arrays.toString(data));&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Performance=&lt;br /&gt;
{{Performance|SortTiming|sort.merge.performance}}&lt;br /&gt;
&lt;br /&gt;
=Pledge, Acknowledgments, Citations=&lt;br /&gt;
{{Pledge|studio-merge-sort}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Not_Thread_Safe_Hash_Table_Assignment&amp;diff=5453</id>
		<title>Not Thread Safe Hash Table Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Not_Thread_Safe_Hash_Table_Assignment&amp;diff=5453"/>
		<updated>2026-09-29T16:08:17Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* Parameterized Type Array Tip */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
We gain experience implementing an abstract data type.  Specifically, we will build a {{NotThreadSafeLink}} class which implements the java.util.Map&amp;lt;K,V&amp;gt; interface.  Later in the semester, we will implement a {{ThreadSafeLink}} version: [[Concurrent_Hash_Table_Assignment|ConcurrentHashTable]].&lt;br /&gt;
&lt;br /&gt;
We gain experience with generics since the Map interface is parameterized with multiple generic types: K for the key type and V for the value type.&lt;br /&gt;
&lt;br /&gt;
We will build some utilities to support hashing and key-value pairs which we will use throughout the semester.&lt;br /&gt;
&lt;br /&gt;
Finally, the selection of methods we will implement includes two which you may not be familiar with: compute() and computeIfAbsent().  Each of these methods have special roles to play throughout the semester.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
== Hash Tables==&lt;br /&gt;
* [https://en.wikipedia.org/wiki/Hash_table Wikipedia Hash Table]&lt;br /&gt;
* [https://visualgo.net/en/hashtable VisuAlgo Hash Table]&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|How To Use java.util.Map|&amp;lt;youtube&amp;gt;MwPrMAG4bLg&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|CS50 Shorts: Hash Tables|&amp;lt;youtube&amp;gt;nvzVHwrrub0&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|HackerRank Hash Tables|&amp;lt;youtube&amp;gt;shs0KM3wKv8&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
==Java Utilities To Use==&lt;br /&gt;
===Map.Entry&amp;lt;K,V&amp;gt;===&lt;br /&gt;
{{MapEntryWithMethodLinks}}&lt;br /&gt;
===Map&amp;lt;K,V&amp;gt;===&lt;br /&gt;
{{MapWithMethodLinks}}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
===Optional&amp;lt;T&amp;gt;===&lt;br /&gt;
{{OptionalWithMethodsLinks}}&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
===ToIntFunction&amp;lt;T&amp;gt;===&lt;br /&gt;
{{ToIntFunctionWithMethodLinks}}&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
Each of the example clients operate on the words of the [https://rmc.library.cornell.edu/gettysburg/good_cause/transcript.htm Gettysburg Address].&lt;br /&gt;
&lt;br /&gt;
==PutClient==&lt;br /&gt;
{{Client|PutClient|hashtable.notthreadsafe.client|main}}&lt;br /&gt;
&lt;br /&gt;
This client uses repeated calls to {{MapPutLink}}.&lt;br /&gt;
&lt;br /&gt;
Each word is processed by taking its first letter (converted to lower case) and associating that letter with the word.  Since put replaces a previous association, if it exists, the last word starting with a particular letter wins.&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|PutClient|&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
int chainCount = 8;&lt;br /&gt;
Map&amp;lt;Character, String&amp;gt; mapFirstCharacterToLastEncounteredWord = new NotThreadSafeHashTable&amp;lt;&amp;gt;(chainCount, Objects::hashCode);&lt;br /&gt;
&lt;br /&gt;
for (TextSection textSection : TextSectionResource.GETTYSBURG_ADDRESS.textSections()) {&lt;br /&gt;
    for (String word : textSection.words()) {&lt;br /&gt;
        if (word.length() &amp;gt; 0) {&lt;br /&gt;
            char firstCharacterLowerCased = Character.toLowerCase(word.charAt(0));&lt;br /&gt;
            mapFirstCharacterToLastEncounteredWord.put(firstCharacterLowerCased, word);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
for (char ch = 'a'; ch &amp;lt;= 'z'; ++ch) {&lt;br /&gt;
    String value = mapFirstCharacterToLastEncounteredWord.get(ch);&lt;br /&gt;
    System.out.println(ch + &amp;quot;: &amp;quot; + toString(value));&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|PutClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;a: and&lt;br /&gt;
b: by&lt;br /&gt;
c: cause&lt;br /&gt;
d: died&lt;br /&gt;
e: earth&lt;br /&gt;
f: from&lt;br /&gt;
g: government&lt;br /&gt;
h: have&lt;br /&gt;
i: in&lt;br /&gt;
j: &lt;br /&gt;
k: &lt;br /&gt;
l: last&lt;br /&gt;
m: measure&lt;br /&gt;
n: not&lt;br /&gt;
o: of&lt;br /&gt;
p: perish&lt;br /&gt;
q: &lt;br /&gt;
r: resolve&lt;br /&gt;
s: shall&lt;br /&gt;
t: the&lt;br /&gt;
u: under&lt;br /&gt;
v: vain&lt;br /&gt;
w: we&lt;br /&gt;
x: &lt;br /&gt;
y: years&lt;br /&gt;
z: &amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
==ComputeIfAbsentClient==&lt;br /&gt;
{{Client|ComputeIfAbsentClient|hashtable.notthreadsafe.client|main}}&lt;br /&gt;
This client uses repeated calls to {{MapComputeIfAbsentLink}}. &lt;br /&gt;
&lt;br /&gt;
Each word is processed by taking its first letter (converted to lower case) and associating that letter with a list.  The first time a letter is encountered the mappingFunction will be invoked which returns the new list to associate with the letter.  Conveniently, whether it has been associated before or the current iteration, the List associated with the letter is returned from computeIfAbsent.  The current word is then added to that list.  Since the returned list is a reference to mutable data there is no need to re-associate the list with the letter.  When the loops are completed, each letter holds a list of all the encountered words (including any duplicates). &lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|ComputeIfAbsentClient|&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
int chainCount = 8;&lt;br /&gt;
Map&amp;lt;Character, List&amp;lt;String&amp;gt;&amp;gt; mapFirstCharacterToListOfWords = new NotThreadSafeHashTable&amp;lt;&amp;gt;(chainCount, Objects::hashCode);&lt;br /&gt;
&lt;br /&gt;
for (TextSection textSection : TextSectionResource.GETTYSBURG_ADDRESS.textSections()) {&lt;br /&gt;
    for (String word : textSection.words()) {&lt;br /&gt;
        if (word.length() &amp;gt; 0) {&lt;br /&gt;
            char firstCharacterLowerCased = Character.toLowerCase(word.charAt(0));&lt;br /&gt;
            List&amp;lt;String&amp;gt; list = mapFirstCharacterToListOfWords.computeIfAbsent(firstCharacterLowerCased,&lt;br /&gt;
                    (unusedKey) -&amp;gt; {&lt;br /&gt;
                        return new LinkedList&amp;lt;&amp;gt;();&lt;br /&gt;
                    });&lt;br /&gt;
            list.add(word);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
for (char ch = 'a'; ch &amp;lt;= 'z'; ++ch) {&lt;br /&gt;
    List&amp;lt;String&amp;gt; value = mapFirstCharacterToListOfWords.get(ch);&lt;br /&gt;
    System.out.println(ch + &amp;quot;: &amp;quot; + toString(value));&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|ComputeIfAbsentClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;a: [and, ago, a, and, all, are, are, a, any, and, are, a, a, as, a, altogether, and, a, and, above, add, advanced, a, and]&lt;br /&gt;
b: [brought, battle, But, brave, but, be, be, before, birth, by]&lt;br /&gt;
c: [continent, conceived, created, civil, conceived, can, come, can, can, consecrate, can, consecrated, can, cause]&lt;br /&gt;
d: [dedicated, dedicated, dedicate, do, dedicate, dead, detract, did, dedicated, dedicated, dead, devotion, devotion, dead, died]&lt;br /&gt;
e: [equal, engaged, endure, earth]&lt;br /&gt;
f: [Four, fathers, forth, field, field, final, for, fitting, far, forget, for, fought, far, for, from, for, full, freedom, for, from]&lt;br /&gt;
g: [great, great, gave, ground, great, gave, God, government]&lt;br /&gt;
h: [have, here, hallow, here, have, here, here, here, here, have, here, honored, here, highly, have, have]&lt;br /&gt;
i: [in, in, It, is, in, it, it, It, is, It, is, increased, in]&lt;br /&gt;
j: &lt;br /&gt;
k: &lt;br /&gt;
l: [Liberty, long, lives, live, larger, living, little, long, living, last]&lt;br /&gt;
m: [men, met, might, men, measure]&lt;br /&gt;
n: [new, nation, Now, nation, nation, nation, not, not, not, note, nor, never, nobly, not, nation, new, not]&lt;br /&gt;
o: [our, on, or, on, of, of, our, or, of, of, of]&lt;br /&gt;
p: [proposition, portion, place, proper, poor, power, people, people, people, perish]&lt;br /&gt;
q: &lt;br /&gt;
r: [resting, remember, rather, rather, remaining, resolve]&lt;br /&gt;
s: [score, seven, so, so, should, sense, struggled, say, so, shall, shall, shall]&lt;br /&gt;
t: [this, to, the, that, testing, that, that, to, that, those, their, that, that, that, this, this, The, to, The, they, the, to, to, the, they, thus, to, to, the, task, that, these, take, to, that, they, the, that, that, these, that, this, that, the, the, the, the]&lt;br /&gt;
u: [us, unfinished, us, us, under]&lt;br /&gt;
v: [vain]&lt;br /&gt;
w: [we, war, whether, We, war, We, who, we, we, we, we, who, world, will, what, we, what, work, which, who, we, which, we]&lt;br /&gt;
x: &lt;br /&gt;
y: [years]&lt;br /&gt;
z: &amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
==ComputeClient==&lt;br /&gt;
{{Client|ComputeClient|hashtable.notthreadsafe.client|main}}&lt;br /&gt;
&lt;br /&gt;
This client uses repeated calls to {{MapComputeLink}}. &lt;br /&gt;
&lt;br /&gt;
Each word is processed by computing the number of times the (lower case version of the) word has been encountered.  Each time compute is called the remappingFunction is passed the key (which is (as is often the case) unused) and the previous value associated with the key, or null if has no association.  The remapping function returns the value to associate with the key.  Since we are counting the occurrences, we add 1 to the previous value, or simply return 1 if that previous value is null.&lt;br /&gt;
&lt;br /&gt;
When the loops are completed, each word holds the number of times it was encountered.. &lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|ComputeClient|&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
int chainCount = 8;&lt;br /&gt;
Map&amp;lt;String, Integer&amp;gt; mapWordToCount = new NotThreadSafeHashTable&amp;lt;&amp;gt;(chainCount, Objects::hashCode);&lt;br /&gt;
&lt;br /&gt;
for (TextSection textSection : TextSectionResource.GETTYSBURG_ADDRESS.textSections()) {&lt;br /&gt;
	for(String word : textSection.words()) {&lt;br /&gt;
		if (word.length() &amp;gt; 0) {&lt;br /&gt;
			String lowerCaseWord = word.toLowerCase();&lt;br /&gt;
			mapWordToCount.compute(lowerCaseWord, (unusedKey, prevValue) -&amp;gt; {&lt;br /&gt;
				if (prevValue != null) {&lt;br /&gt;
					return prevValue + 1;&lt;br /&gt;
				} else {&lt;br /&gt;
					return 1;&lt;br /&gt;
				}&lt;br /&gt;
			});&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
System.out.println(toString(mapWordToCount));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|ComputeClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;that =&amp;gt; 13&lt;br /&gt;
the =&amp;gt; 11&lt;br /&gt;
we =&amp;gt; 10&lt;br /&gt;
here =&amp;gt; 8&lt;br /&gt;
to =&amp;gt; 8&lt;br /&gt;
a =&amp;gt; 7&lt;br /&gt;
and =&amp;gt; 6&lt;br /&gt;
can =&amp;gt; 5&lt;br /&gt;
have =&amp;gt; 5&lt;br /&gt;
for =&amp;gt; 5&lt;br /&gt;
it =&amp;gt; 5&lt;br /&gt;
not =&amp;gt; 5&lt;br /&gt;
nation =&amp;gt; 5&lt;br /&gt;
of =&amp;gt; 5&lt;br /&gt;
in =&amp;gt; 4&lt;br /&gt;
this =&amp;gt; 4&lt;br /&gt;
dedicated =&amp;gt; 4&lt;br /&gt;
they =&amp;gt; 3&lt;br /&gt;
is =&amp;gt; 3&lt;br /&gt;
are =&amp;gt; 3&lt;br /&gt;
so =&amp;gt; 3&lt;br /&gt;
dead =&amp;gt; 3&lt;br /&gt;
shall =&amp;gt; 3&lt;br /&gt;
great =&amp;gt; 3&lt;br /&gt;
who =&amp;gt; 3&lt;br /&gt;
us =&amp;gt; 3&lt;br /&gt;
people =&amp;gt; 3&lt;br /&gt;
new =&amp;gt; 2&lt;br /&gt;
conceived =&amp;gt; 2&lt;br /&gt;
war =&amp;gt; 2&lt;br /&gt;
rather =&amp;gt; 2&lt;br /&gt;
gave =&amp;gt; 2&lt;br /&gt;
but =&amp;gt; 2&lt;br /&gt;
living =&amp;gt; 2&lt;br /&gt;
field =&amp;gt; 2&lt;br /&gt;
from =&amp;gt; 2&lt;br /&gt;
or =&amp;gt; 2&lt;br /&gt;
be =&amp;gt; 2&lt;br /&gt;
these =&amp;gt; 2&lt;br /&gt;
our =&amp;gt; 2&lt;br /&gt;
long =&amp;gt; 2&lt;br /&gt;
what =&amp;gt; 2&lt;br /&gt;
dedicate =&amp;gt; 2&lt;br /&gt;
which =&amp;gt; 2&lt;br /&gt;
men =&amp;gt; 2&lt;br /&gt;
devotion =&amp;gt; 2&lt;br /&gt;
on =&amp;gt; 2&lt;br /&gt;
far =&amp;gt; 2&lt;br /&gt;
created =&amp;gt; 1&lt;br /&gt;
testing =&amp;gt; 1&lt;br /&gt;
battle =&amp;gt; 1&lt;br /&gt;
proper =&amp;gt; 1&lt;br /&gt;
brave =&amp;gt; 1&lt;br /&gt;
vain =&amp;gt; 1&lt;br /&gt;
under =&amp;gt; 1&lt;br /&gt;
ago =&amp;gt; 1&lt;br /&gt;
all =&amp;gt; 1&lt;br /&gt;
hallow =&amp;gt; 1&lt;br /&gt;
add =&amp;gt; 1&lt;br /&gt;
nor =&amp;gt; 1&lt;br /&gt;
work =&amp;gt; 1&lt;br /&gt;
honored =&amp;gt; 1&lt;br /&gt;
cause =&amp;gt; 1&lt;br /&gt;
government =&amp;gt; 1&lt;br /&gt;
perish =&amp;gt; 1&lt;br /&gt;
score =&amp;gt; 1&lt;br /&gt;
as =&amp;gt; 1&lt;br /&gt;
their =&amp;gt; 1&lt;br /&gt;
poor =&amp;gt; 1&lt;br /&gt;
world =&amp;gt; 1&lt;br /&gt;
will =&amp;gt; 1&lt;br /&gt;
note =&amp;gt; 1&lt;br /&gt;
thus =&amp;gt; 1&lt;br /&gt;
advanced =&amp;gt; 1&lt;br /&gt;
increased =&amp;gt; 1&lt;br /&gt;
earth =&amp;gt; 1&lt;br /&gt;
engaged =&amp;gt; 1&lt;br /&gt;
civil =&amp;gt; 1&lt;br /&gt;
should =&amp;gt; 1&lt;br /&gt;
do =&amp;gt; 1&lt;br /&gt;
detract =&amp;gt; 1&lt;br /&gt;
say =&amp;gt; 1&lt;br /&gt;
unfinished =&amp;gt; 1&lt;br /&gt;
continent =&amp;gt; 1&lt;br /&gt;
equal =&amp;gt; 1&lt;br /&gt;
any =&amp;gt; 1&lt;br /&gt;
met =&amp;gt; 1&lt;br /&gt;
come =&amp;gt; 1&lt;br /&gt;
live =&amp;gt; 1&lt;br /&gt;
never =&amp;gt; 1&lt;br /&gt;
resolve =&amp;gt; 1&lt;br /&gt;
died =&amp;gt; 1&lt;br /&gt;
god =&amp;gt; 1&lt;br /&gt;
seven =&amp;gt; 1&lt;br /&gt;
brought =&amp;gt; 1&lt;br /&gt;
forth =&amp;gt; 1&lt;br /&gt;
whether =&amp;gt; 1&lt;br /&gt;
endure =&amp;gt; 1&lt;br /&gt;
those =&amp;gt; 1&lt;br /&gt;
altogether =&amp;gt; 1&lt;br /&gt;
consecrated =&amp;gt; 1&lt;br /&gt;
above =&amp;gt; 1&lt;br /&gt;
power =&amp;gt; 1&lt;br /&gt;
remember =&amp;gt; 1&lt;br /&gt;
forget =&amp;gt; 1&lt;br /&gt;
task =&amp;gt; 1&lt;br /&gt;
four =&amp;gt; 1&lt;br /&gt;
years =&amp;gt; 1&lt;br /&gt;
proposition =&amp;gt; 1&lt;br /&gt;
now =&amp;gt; 1&lt;br /&gt;
final =&amp;gt; 1&lt;br /&gt;
resting =&amp;gt; 1&lt;br /&gt;
sense =&amp;gt; 1&lt;br /&gt;
little =&amp;gt; 1&lt;br /&gt;
nobly =&amp;gt; 1&lt;br /&gt;
remaining =&amp;gt; 1&lt;br /&gt;
last =&amp;gt; 1&lt;br /&gt;
measure =&amp;gt; 1&lt;br /&gt;
freedom =&amp;gt; 1&lt;br /&gt;
fathers =&amp;gt; 1&lt;br /&gt;
liberty =&amp;gt; 1&lt;br /&gt;
portion =&amp;gt; 1&lt;br /&gt;
place =&amp;gt; 1&lt;br /&gt;
lives =&amp;gt; 1&lt;br /&gt;
might =&amp;gt; 1&lt;br /&gt;
fitting =&amp;gt; 1&lt;br /&gt;
larger =&amp;gt; 1&lt;br /&gt;
consecrate =&amp;gt; 1&lt;br /&gt;
ground =&amp;gt; 1&lt;br /&gt;
struggled =&amp;gt; 1&lt;br /&gt;
did =&amp;gt; 1&lt;br /&gt;
fought =&amp;gt; 1&lt;br /&gt;
before =&amp;gt; 1&lt;br /&gt;
take =&amp;gt; 1&lt;br /&gt;
full =&amp;gt; 1&lt;br /&gt;
highly =&amp;gt; 1&lt;br /&gt;
birth =&amp;gt; 1&lt;br /&gt;
by =&amp;gt; 1 &amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
{{ Tip | It is good practice to declare variables, parameters, and fields to be of the least specific type possible (but no less specific :). This is commonly an interface. }}&lt;br /&gt;
&lt;br /&gt;
{{Warning | Do NOT use &amp;lt;nowiki&amp;gt;==&amp;lt;/nowiki&amp;gt; when comparing Objects (except when comparing to null).  Use [https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#equals-java.lang.Object-java.lang.Object- Objects.equals(a,b)]. }}&lt;br /&gt;
&lt;br /&gt;
{{Warning | Do NOT use % which can return a negative number.  Use [https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#floorMod-int-int- Math.floorMod(x,y)]. }}&lt;br /&gt;
&lt;br /&gt;
{{Warning | Do NOT parallelize. }}&lt;br /&gt;
&lt;br /&gt;
{{Warning | Do NOT simply remove and add for put on a Map.  Look for the entry and set its value if you find it.  Otherwise add it. }}&lt;br /&gt;
&lt;br /&gt;
==DefaultHashFunction==&lt;br /&gt;
{{CodeToImplement|DefaultHashFunction|applyAsInt|hash.exercise}}&lt;br /&gt;
&lt;br /&gt;
Hashing is a powerful tool.  We will use it to build our NotThreadSafeHashTable, as well as the [[Matrix_MapReduce_Framework_Assignment|MatrixMapReduceFramework]] later in the semester.  Hashing relies on evenly distributing keys to provide performance improvements.  As such, it will be useful to specify a particular hash function for our Hash Table (and MatrixMapReduceFramework later) as they will want to be different.&lt;br /&gt;
&lt;br /&gt;
Here we create a default hash function which implements {{ToIntFunctionLink}} which can serve as the hash function for our NotThreadSafeHashTable or wherever we need one.&lt;br /&gt;
&lt;br /&gt;
We will just use the standard mechanism for acquiring a hashCode for an Object.  Investigate [[SmearHashFunction|SmearHashFunction]] which [https://github.com/mraad/dbscan-spark/blob/master/src/main/java/com/esri/dbscan/Smear.java smears] the hash code to provide a different result.&lt;br /&gt;
&lt;br /&gt;
===applyAsInt(key)===&lt;br /&gt;
{{Sequential|public int applyAsInt(K key)}}&lt;br /&gt;
&lt;br /&gt;
Return the result of calling [https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#hashCode-java.lang.Object- Objects.hashCode(key)] which nicely handles null.&lt;br /&gt;
&lt;br /&gt;
==HashUtils==&lt;br /&gt;
{{CodeToImplement|HashUtils|hashCodeOf&amp;lt;br/&amp;gt;arrayIndexForKey&amp;lt;br/&amp;gt;itemInArrayForKey|hash.exercise}}&lt;br /&gt;
&lt;br /&gt;
=== hashCodeOf(key, hashFunction) ===&lt;br /&gt;
{{Sequential|public static &amp;lt;K&amp;gt; int hashCodeOf(K key, ToIntFunction&amp;lt;K&amp;gt; hashFunction)}}&lt;br /&gt;
&lt;br /&gt;
Invoke the hashFunction's [https://docs.oracle.com/javase/8/docs/api/java/util/function/ToIntFunction.html#applyAsInt-T- applyAsInt(value)] method and return the result.&lt;br /&gt;
&lt;br /&gt;
=== arrayIndexForKey(key, arrayLength, hashFunction) ===&lt;br /&gt;
{{Sequential|public static &amp;lt;K&amp;gt; int arrayIndexForKey(K key, int arrayLength, ToIntFunction&amp;lt;K&amp;gt; hashFunction)}}&lt;br /&gt;
&lt;br /&gt;
Using [[Not_Thread_Safe_Hash_Table_Assignment#hashCodeOf.28key.2C_hashFunction.29|hashCodeOf]] with the provided key will return an int value between (inclusive) Integer.MIN_VALUE and Integer.MAX_VALUE, that is: between (inclusive) -2147483648 and 2147483647.&lt;br /&gt;
&lt;br /&gt;
To convert this to a suitable array index, we would like to take the positive remainder of the value and the arrayLength.&lt;br /&gt;
&lt;br /&gt;
Simply using the % operator will not do, as a negative hashCode would result in a negative array index (which would, of course, produce an array [https://docs.oracle.com/javase/8/docs/api/java/lang/IndexOutOfBoundsException.html IndexOutOfBoundsException]).&lt;br /&gt;
&lt;br /&gt;
Using [https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#floorMod-int-int- Math.floorMod(dividend,divisor)] solves this problem by always returning a remainder with the sign of the divisor.&lt;br /&gt;
&lt;br /&gt;
=== itemInArrayForKey(key, array, hashFunction) ===&lt;br /&gt;
{{Sequential|public static &amp;lt;K, E&amp;gt; E itemInArrayForKey(K key, E[] array, ToIntFunction&amp;lt;K&amp;gt; hashFunction)}}&lt;br /&gt;
&lt;br /&gt;
Leverage [[Not_Thread_Safe_Hash_Table_Assignment#arrayIndexForKey.28key.2C_arrayLength.2C_hashFunction.29|arrayIndexForKey]] to find the index in the array, then return the item at that index.&lt;br /&gt;
&lt;br /&gt;
==DefaultEntry&amp;lt;K,V&amp;gt;==&lt;br /&gt;
{{Alert | This is in student-pre-aspect!!!}} &lt;br /&gt;
Whatever language you happen to be programming in there will be a way to associate keys with values.  Whether the data structure is called a [https://docs.python.org/3/tutorial/datastructures.html#dictionaries Dictionary] in Python, a [https://ruby-doc.org/core-3.1.2/Hash.html Hash] in Ruby, an [https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node153.html Association List] in Lisp, or a [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html Map&amp;lt;K,V&amp;gt;] in Java, there will be a way to store the (key, value) pairs.&lt;br /&gt;
&lt;br /&gt;
Java provides the [https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html interface Entry&amp;lt;K, V&amp;gt;] which is used on all of its Maps.  We will implement this interface in class DefaultEntry&amp;lt;K,V&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|DefaultEntry|constructor&amp;lt;br&amp;gt;getKey&amp;lt;br&amp;gt;getValue&amp;lt;br&amp;gt;setValue|entry.exercise|student-pre-aspect}}&lt;br /&gt;
&lt;br /&gt;
===constructor DefaultEntry(key, value)===&lt;br /&gt;
{{Sequential|public DefaultEntry(K key, V value)}}&lt;br /&gt;
&lt;br /&gt;
Hang onto the key and value specified as parameters in instance variables to be returned (and perhaps mutated in the case of value) later.&lt;br /&gt;
&lt;br /&gt;
===getKey()===&lt;br /&gt;
{{Sequential|public K getKey()}}&lt;br /&gt;
&lt;br /&gt;
{{MapEntryGetKeyLink}} specification&lt;br /&gt;
&lt;br /&gt;
===getValue()===&lt;br /&gt;
{{Sequential|public V getValue()}}&lt;br /&gt;
&lt;br /&gt;
{{MapEntryGetValueLink}} specification&lt;br /&gt;
&lt;br /&gt;
===setValue()===&lt;br /&gt;
{{Sequential|public V setValue(V value)}}&lt;br /&gt;
&lt;br /&gt;
{{MapEntrySetValueLink}} specification&lt;br /&gt;
&lt;br /&gt;
==NotThreadSafeHashTable==&lt;br /&gt;
NotThreadSafeHashTable will use an entry’s key to place an entry into one of several buckets.  Each bucket will hold a chain of entries.  We maintain a chain of entries for when collisions naturally occur when multiple keys want to be placed in the same bucket.&lt;br /&gt;
&lt;br /&gt;
HashTables are a popular data structure because we can cut down the runtime of searching for entries within the map by adding more buckets/chains.  The time required to search for a particular entry drops to roughly 1/total_number_of_buckets (in comparison to one long chain which contains every entry).&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/Hash_table hash table on wikipedia]&lt;br /&gt;
&lt;br /&gt;
[[File:Hash_table_5_0_1_1_1_1_1_LL.svg|frame|[https://commons.wikimedia.org/wiki/File:Hash_table_5_0_1_1_1_1_1_LL.svg mediawiki hash table image]]]&lt;br /&gt;
&lt;br /&gt;
In this class, you will use the entry’s key’s hashcode to determine which of the buckets it should fall into. In order to calculate the index of the bucket that the entry belongs in, we will make use of the hash function, which in turn uses Math.floorMod to calculate a modulo (remainder or % in Java) based on the number of buckets and the key’s hashcode. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
A Map (also sometimes referred to as a Dictionary or a Hashtable) is an abstract data structure which has the capabilities of associating a value with a key: &amp;lt;code&amp;gt;map.put(key, value)&amp;lt;/code&amp;gt;, looking up the associated value with a key: &amp;lt;code&amp;gt;map.get(key)&amp;lt;/code&amp;gt;, and removing &amp;lt;code&amp;gt;map.remove(key)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The standard Java Collections Framework [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html Map] provides a number of other nice methods, but put, get, and remove are the core of any Map in any programming language.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since your class will implement Map&amp;lt;K,V&amp;gt;.  We will leave many of the methods as &amp;lt;code&amp;gt;throw new NotRequiredForTheAssignmentException();&amp;lt;/code&amp;gt; while we focus on some critical methods of import. Specifications for these methods can be found in the [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html Map&amp;lt;K,V&amp;gt; javadocs]&lt;br /&gt;
&lt;br /&gt;
{{Warning | Note the specifications of the return values.}}&lt;br /&gt;
&lt;br /&gt;
Further, the standard interface for a key value pair is [https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html Map's Entry] with the critical methods:&lt;br /&gt;
&lt;br /&gt;
* K getKey()&lt;br /&gt;
* V getValue()&lt;br /&gt;
* V setValue(V value)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
We have provided an implementation of Entry for you called [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/util/KeyMutableValuePair.html KeyMutableValuePair&amp;lt;K,V&amp;gt;].&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|NotThreadSafeHashTable|constructor&amp;lt;br/&amp;gt;hashFunction&amp;lt;br/&amp;gt;chains&amp;lt;br/&amp;gt;entryOf&amp;lt;br/&amp;gt;put&amp;lt;br/&amp;gt;get&amp;lt;br/&amp;gt;computeIfAbsent&amp;lt;br/&amp;gt;compute|NotThreadSafeHashTable.notthreadsafe.exercise}}&lt;br /&gt;
&lt;br /&gt;
===constructor: NotThreadSafeHashTable(chainCount, hashFunction)===&lt;br /&gt;
{{Sequential|public NotThreadSafeHashTable(int chainCount, ToIntFunction&amp;lt;K&amp;gt; hashFunction)}}&lt;br /&gt;
&lt;br /&gt;
Initialize &amp;lt;code&amp;gt;chainCount&amp;lt;/code&amp;gt; chains in the form of an array of Collections of entries.  Additionally, hang onto the &amp;lt;code&amp;gt;hashFunction&amp;lt;/code&amp;gt; for later use.&lt;br /&gt;
&lt;br /&gt;
{{Requirement | In order to facilitate testing and debugging, create instances of either [https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/util/ArrayList.html java.util.ArrayList] or [https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/util/LinkedList.html java.util.LinkedList] to fill your array with.}}&lt;br /&gt;
&lt;br /&gt;
{{Warning | Initializing the contents of an array is one of the few cases where the indexing for loop is superior to the for-each loop.}}&lt;br /&gt;
&lt;br /&gt;
Use: &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;for (int i = 0; i &amp;lt; buckets.length; ++i)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
in this case.&lt;br /&gt;
&lt;br /&gt;
Implementing the constructor has a couple of tricky parts.&lt;br /&gt;
&lt;br /&gt;
====Parameterized Type Array Tip====&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
Creating arrays of parameterized types in Java is madness inducing.  Some details are available in [https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createArrays Java Generics Restrictions].&lt;br /&gt;
&lt;br /&gt;
The example below creates an array of List&amp;lt;String&amp;gt;.  The &amp;lt;code&amp;gt;@SuppressWarnings&amp;lt;/code&amp;gt; annotation is optional.&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;@SuppressWarnings(&amp;quot;unchecked&amp;quot;)&lt;br /&gt;
List&amp;lt;String&amp;gt;[] result = new List[length];&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First, you will encounter a much dreaded problem in Java.  One cannot instantiate a new generic array.  While there is [https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createArrays a reason for this limitation], it is still frustrating.&lt;br /&gt;
&lt;br /&gt;
So while you might think you should enter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
buckets = new Collection&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt;[chainCount]; // NOTE: this will not compile&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the error reported says it all &amp;quot;Cannot create a generic array of Collection&amp;lt;Map.Entry&amp;lt;K,V&amp;gt;&amp;gt;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
You must instead create the array without the generic type parameter:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
buckets = new Collection[capacity];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This results in a warning which the @SuppressWarnings(&amp;quot;unchecked&amp;quot;) annotation we added to the constructor takes care of.&lt;br /&gt;
&lt;br /&gt;
===hashFunction()===&lt;br /&gt;
{{Sequential|public ToIntFunction&amp;lt;K&amp;gt; hashFunction()}}&lt;br /&gt;
&lt;br /&gt;
Return the hashFunction provided to the constructor.&lt;br /&gt;
&lt;br /&gt;
===chains()===&lt;br /&gt;
{{Sequential|private Collection&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt;[] chains()}}&lt;br /&gt;
&lt;br /&gt;
Return the chains created in the constructor.  This method is required for the provided entrySet() method and provides the additional benefit of allowing the creation of the chains to be tested before moving on to the public methods (get, put, computeIfAbsent, compute) which have little hope of working correctly if the chains are not correct.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
===chainIndexOf(key)===&lt;br /&gt;
{{Sequential|private int chainIndexOf(K key)}}&lt;br /&gt;
&lt;br /&gt;
For a given key return the index into the array of buckets.  The correct implementation should leverage the [https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode-- key's hash code] to generate a [https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#floorMod-int-int- valid index of the bucket array].&lt;br /&gt;
&lt;br /&gt;
{{Tip | You should have just built a utility method which should be useful here.}}&lt;br /&gt;
&lt;br /&gt;
===chainOf(key)===&lt;br /&gt;
{{Sequential|private Collection&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; chainOf(Object key)}}&lt;br /&gt;
&lt;br /&gt;
chainIndexOf(key) returns the index.  chainOf(key) returns the chain at that index.&lt;br /&gt;
&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===entryOf(chain, key)===&lt;br /&gt;
{{Sequential|private static &amp;lt;K, V&amp;gt; Entry&amp;lt;K, V&amp;gt; entryOf(Collection&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; chain, K key)}}&lt;br /&gt;
&lt;br /&gt;
Many of the methods listed above exist solely for the purpose of testing to assist in debugging.  It can be argued that the abstraction is past any point of reasonable abstraction.  Not so for entryOf(chain, key).  It is perfectly reasonable and useful to build this method as it will be well used by get, put, computeIfAbsent, compute, and many other methods on Map.&lt;br /&gt;
&lt;br /&gt;
{{Requirement&lt;br /&gt;
| Use the sequential for each loop to iterate through the chain.  This is both good style and necessary for the testing.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
===put(key, value)===&lt;br /&gt;
{{Sequential|public V put(K key, V value)}}&lt;br /&gt;
&lt;br /&gt;
Note the required return value in the [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#put-K-V- put(key,value) specification].&lt;br /&gt;
&lt;br /&gt;
{{Requirements4&lt;br /&gt;
| Only the lone correct chain of entries should be searched.&lt;br /&gt;
| The correct chain should be searched only '''once'''.&lt;br /&gt;
| When you find an already existing Entry for a key, do not create a new Entry.  Simply {{MapEntrySetValueLink|set the value of the existing Entry}}.&lt;br /&gt;
| Do not call any of the methods on interface Map (for example get(key)).  Your utility functions should be all that you need.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{Alert | Use instances of DefaultEntry to store your key value pairs. }}&lt;br /&gt;
&lt;br /&gt;
===get(key)===&lt;br /&gt;
{{Sequential|public V get(Object key)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#get-java.lang.Object- get(key) spec]&lt;br /&gt;
&lt;br /&gt;
{{Requirements2&lt;br /&gt;
| Only the lone correct chain of entries should be searched.&lt;br /&gt;
| The correct chain should be searched only '''once'''.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
===computeIfAbsent(key, mappingFunction)===&lt;br /&gt;
While the [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#computeIfAbsent-K-java.util.function.Function- computeIfAbsent(key, mappingFunction) specification] uses invocations of get and put, you should do neither.  You must achieve the computeIfAbsent specification, but do so akin to the approach taken by put: finding the chain for a key, finding the entry for the key (if it exists) in that chain, and so on.&lt;br /&gt;
&lt;br /&gt;
'''!!! NOTE: you do NOT need to handle any cases involving remove.'''&lt;br /&gt;
&lt;br /&gt;
{{Requirements4&lt;br /&gt;
| Only the lone correct chain of entries should be searched.&lt;br /&gt;
| The correct chain should be searched only '''once'''.&lt;br /&gt;
| When you find an already existing Entry for a key, do not create a new Entry.  Simply {{MapEntrySetValueLink|set the value of the existing Entry}}.&lt;br /&gt;
| Do not call any of the methods on interface Map (for example get(key)).  Your utility functions should be all that you need.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{Alert | Use instances of DefaultEntry to store your key value pairs. }}&lt;br /&gt;
&lt;br /&gt;
===compute(key, remappingFunction)===&lt;br /&gt;
While the [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#compute-K-java.util.function.BiFunction- compute(key, remappingFunction) specification] uses invocations of get, put, and remove, you should do none of those things.  You must achieve the compute specification, but do so akin to the approach taken by put: finding the chain for a key, finding the entry for the key (if it exists) in that chain, and so on.&lt;br /&gt;
&lt;br /&gt;
'''!!! NOTE: you do NOT need to handle any cases involving remove.'''&lt;br /&gt;
&lt;br /&gt;
{{Requirements4&lt;br /&gt;
| Only the lone correct chain of entries should be searched.&lt;br /&gt;
| The correct chain should be searched only '''once'''.&lt;br /&gt;
| When you find an already existing Entry for a key, do not create a new Entry.  Simply {{MapEntrySetValueLink|set the value of the existing Entry}}.&lt;br /&gt;
| Do not call any of the methods on interface Map (for example get(key)).  Your utility functions should be all that you need.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{Alert | Use instances of DefaultEntry to store your key value pairs. }}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
===remove(key)===&lt;br /&gt;
{{Sequential|public V remove(Object key)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#remove-java.lang.Object- remove(key) spec]&lt;br /&gt;
&lt;br /&gt;
{{Warning | For an efficient solution (and to avoid a possible ConcurrentModificationException) you will want to use the bucket's iterator directly and leverage its remove method.}}&lt;br /&gt;
&lt;br /&gt;
{{Tip | Only a single bucket's collection should be iterated over once via the iterator() method on appropriate bucket. }}&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
===size()===&lt;br /&gt;
{{Sequential|public int size()}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#size-- size() spec]&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__NotThreadSafeHashTableTestSuite|hashtable.notthreadsafe.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Pledge, Acknowledgments, Citations=&lt;br /&gt;
{{Pledge|exercise-hash-table}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Office_hours&amp;diff=5452</id>
		<title>Office hours</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Office_hours&amp;diff=5452"/>
		<updated>2026-09-23T20:51:54Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{| class=&amp;quot;wikitable mw-collapsible&amp;quot;&lt;br /&gt;
|+nowrap|Schedule&lt;br /&gt;
|-&lt;br /&gt;
! !! Sunday !! Monday !! Tuesday !! Wednesday !! Thursday !! Friday !! Saturday&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 9:00 -  9:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 9:30 - 10:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;10:00 - 10:30&amp;lt;/code&amp;gt;||  ||  || {{ClassTime|McDonnell 361}} ||  || {{ClassTime|McDonnell 361}} || {{OfficeHourInPerson|Prof. Cosgrove|McDonnell 362}} ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;10:30 - 11:00&amp;lt;/code&amp;gt;||  ||  || {{ClassTime|McDonnell 361}} ||  || {{ClassTime|McDonnell 361}} || {{OfficeHourInPerson|Prof. Cosgrove|McDonnell 362}} ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;11:00 - 11:30&amp;lt;/code&amp;gt;||  ||  || {{ClassTime|McDonnell 361}} ||  || {{ClassTime|McDonnell 361}} || {{OfficeHourInPerson|Prof. Cosgrove|McDonnell 362}} ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;11:30 - 12:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  || {{OfficeHourInPerson|Prof. Cosgrove|McDonnell 362}} ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;12:00 - 12:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  || {{OfficeHourInPerson|Prof. Cosgrove|McDonnell 362}} ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;12:30 -  1:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  || {{OfficeHourInPerson|Prof. Cosgrove|McDonnell 362}} ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 1:00 -  1:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  || {{OfficeHourInPerson|Prof. Cosgrove|McDonnell 362}} ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 1:30 -  2:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  || {{OfficeHourInPerson|Prof. Cosgrove|McDonnell 362}} ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 2:00 -  2:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 2:30 -  3:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 3:00 -  3:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 3:30 -  4:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 4:00 -  4:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 4:30 -  5:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 5:00 -  5:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 5:30 -  6:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 6:00 -  6:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 6:30 -  7:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 7:00 -  7:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 7:30 -  8:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 8:00 -  8:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 8:30 -  9:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 9:00 -  9:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 9:30 - 10:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;10:00 - 10:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;10:30 - 11:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;11:00 - 11:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;11:30 - 12:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Spring_2023&amp;diff=5451</id>
		<title>Spring 2023</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Spring_2023&amp;diff=5451"/>
		<updated>2026-09-15T14:24:44Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* Fall 2026 Extensions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Welcome to the wiki for CSE 231S Introduction to Parallel and Concurrent Programming at Washington University in St. Louis.&lt;br /&gt;
&lt;br /&gt;
=Learning Outcomes=&lt;br /&gt;
# Learn how to write programs to make use of multiple processors.&lt;br /&gt;
# Learn to how to fix, and preferably, avoid race conditions.&lt;br /&gt;
# Learn how to implement MapReduce frameworks (the technology developed for the original Google search) to deal with big data.&lt;br /&gt;
# Learn how to use locks to share resources safely.&lt;br /&gt;
# Learn how to use read/write lock for improved parallelism under the right conditions.&lt;br /&gt;
# Learn how to avoid deadlock.&lt;br /&gt;
# Learn when and how to use atomics to build thread-safe data structures with improved performance over using locks.&lt;br /&gt;
# Learn when and how to use phasers for more sophisticated concurrency control.&lt;br /&gt;
# Although, the concepts learned should be widely applicable, the assignments will be implemented in Java and, as such, students will develop advanced Java skill.&lt;br /&gt;
&lt;br /&gt;
=Calendar=&lt;br /&gt;
Click [http://www.cse.wustl.edu/~cosgroved/courses/cse231/current/calendar.html here] to visit our calendar.&lt;br /&gt;
&lt;br /&gt;
=Full Contact Time=&lt;br /&gt;
Prof. Cosgrove has no planned trips which would cause class to be cancelled.  If some unforeseen circumstances arise, such as sickness, class will be moved online and recorded.&lt;br /&gt;
&lt;br /&gt;
=Office hours=&lt;br /&gt;
{{:Office hours}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=Important Dates for Spring '23=&lt;br /&gt;
*&amp;lt;b&amp;gt;Block 1 Assignments Due&amp;lt;/b&amp;gt;: ??&lt;br /&gt;
*&amp;lt;b&amp;gt;Block 2 Assignments Due&amp;lt;/b&amp;gt;: ??&lt;br /&gt;
*&amp;lt;b&amp;gt;Block 3 Assignments Due&amp;lt;/b&amp;gt;: ??&lt;br /&gt;
*&amp;lt;b&amp;gt;Spring Break (No OH available)&amp;lt;/b&amp;gt;: 03/12 to 03/18&lt;br /&gt;
*&amp;lt;b&amp;gt;Last day to submit assignments&amp;lt;/b&amp;gt;: 5/10&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Class Policies=&lt;br /&gt;
{{:Class Policies}}&lt;br /&gt;
&lt;br /&gt;
=Fall 2026 Grading breakdown=&lt;br /&gt;
*Prep S&amp;amp;Q Forms (8%)&lt;br /&gt;
*In-class Worksheets Attendance/Participation (7%)&lt;br /&gt;
*Labs/Exercises (80%)&lt;br /&gt;
*Final Project (5%)&lt;br /&gt;
&lt;br /&gt;
=Fall 2026 Extensions=&lt;br /&gt;
For the Fall 2026 semester: All Exercises will have an eight day, no penalty extension.&lt;br /&gt;
&lt;br /&gt;
To be clear, the due dates are asserted to be reasonable.  The bonus week and a day is intended to account for extreme circumstances.&lt;br /&gt;
&lt;br /&gt;
=Grade Ranges=&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
A+ 	[0.967 - 1.0]&lt;br /&gt;
A	[0.933 - 0.967)&lt;br /&gt;
A-	[0.900 - 0.933)&lt;br /&gt;
B+ 	[0.867 - 0.900)&lt;br /&gt;
B	[0.833 - 0.867)&lt;br /&gt;
B-	[0.800 - 0.833)&lt;br /&gt;
C+ 	[0.767 - 0.700)&lt;br /&gt;
C	[0.733 - 0.767)&lt;br /&gt;
C-	[0.700 - 0.733)&lt;br /&gt;
D+ 	[0.667 - 0.600)&lt;br /&gt;
D	[0.633 - 0.667)&lt;br /&gt;
D-	[0.600 - 0.633)&lt;br /&gt;
F       [0 - 0.600)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=FAQ=&lt;br /&gt;
Every week, we gather the most commonly asked questions from your Q&amp;amp;S forms and add them to our [[FAQ]] section. Please use it as a resource!&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=Course Setup=&lt;br /&gt;
*[[Initial_Setup]]&lt;br /&gt;
*[[Rundown on Everything Java]]&lt;br /&gt;
**[[Generics]]&lt;br /&gt;
**[[Interfaces]]&lt;br /&gt;
**[[Lambdas]]&lt;br /&gt;
*[[Eclipse Tips]]&lt;br /&gt;
--&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Spring_2023&amp;diff=5450</id>
		<title>Spring 2023</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Spring_2023&amp;diff=5450"/>
		<updated>2026-09-15T14:21:02Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* Spring 2026 Grading breakdown */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Welcome to the wiki for CSE 231S Introduction to Parallel and Concurrent Programming at Washington University in St. Louis.&lt;br /&gt;
&lt;br /&gt;
=Learning Outcomes=&lt;br /&gt;
# Learn how to write programs to make use of multiple processors.&lt;br /&gt;
# Learn to how to fix, and preferably, avoid race conditions.&lt;br /&gt;
# Learn how to implement MapReduce frameworks (the technology developed for the original Google search) to deal with big data.&lt;br /&gt;
# Learn how to use locks to share resources safely.&lt;br /&gt;
# Learn how to use read/write lock for improved parallelism under the right conditions.&lt;br /&gt;
# Learn how to avoid deadlock.&lt;br /&gt;
# Learn when and how to use atomics to build thread-safe data structures with improved performance over using locks.&lt;br /&gt;
# Learn when and how to use phasers for more sophisticated concurrency control.&lt;br /&gt;
# Although, the concepts learned should be widely applicable, the assignments will be implemented in Java and, as such, students will develop advanced Java skill.&lt;br /&gt;
&lt;br /&gt;
=Calendar=&lt;br /&gt;
Click [http://www.cse.wustl.edu/~cosgroved/courses/cse231/current/calendar.html here] to visit our calendar.&lt;br /&gt;
&lt;br /&gt;
=Full Contact Time=&lt;br /&gt;
Prof. Cosgrove has no planned trips which would cause class to be cancelled.  If some unforeseen circumstances arise, such as sickness, class will be moved online and recorded.&lt;br /&gt;
&lt;br /&gt;
=Office hours=&lt;br /&gt;
{{:Office hours}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=Important Dates for Spring '23=&lt;br /&gt;
*&amp;lt;b&amp;gt;Block 1 Assignments Due&amp;lt;/b&amp;gt;: ??&lt;br /&gt;
*&amp;lt;b&amp;gt;Block 2 Assignments Due&amp;lt;/b&amp;gt;: ??&lt;br /&gt;
*&amp;lt;b&amp;gt;Block 3 Assignments Due&amp;lt;/b&amp;gt;: ??&lt;br /&gt;
*&amp;lt;b&amp;gt;Spring Break (No OH available)&amp;lt;/b&amp;gt;: 03/12 to 03/18&lt;br /&gt;
*&amp;lt;b&amp;gt;Last day to submit assignments&amp;lt;/b&amp;gt;: 5/10&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Class Policies=&lt;br /&gt;
{{:Class Policies}}&lt;br /&gt;
&lt;br /&gt;
=Fall 2026 Grading breakdown=&lt;br /&gt;
*Prep S&amp;amp;Q Forms (8%)&lt;br /&gt;
*In-class Worksheets Attendance/Participation (7%)&lt;br /&gt;
*Labs/Exercises (80%)&lt;br /&gt;
*Final Project (5%)&lt;br /&gt;
&lt;br /&gt;
=Fall 2026 Extensions=&lt;br /&gt;
For the Fall 2026 semester: All Exercises will have a one week, no penalty extension.&lt;br /&gt;
&lt;br /&gt;
To be clear, the due dates are asserted to be reasonable.  The bonus week is intended to account for extreme circumstances.&lt;br /&gt;
&lt;br /&gt;
=Grade Ranges=&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
A+ 	[0.967 - 1.0]&lt;br /&gt;
A	[0.933 - 0.967)&lt;br /&gt;
A-	[0.900 - 0.933)&lt;br /&gt;
B+ 	[0.867 - 0.900)&lt;br /&gt;
B	[0.833 - 0.867)&lt;br /&gt;
B-	[0.800 - 0.833)&lt;br /&gt;
C+ 	[0.767 - 0.700)&lt;br /&gt;
C	[0.733 - 0.767)&lt;br /&gt;
C-	[0.700 - 0.733)&lt;br /&gt;
D+ 	[0.667 - 0.600)&lt;br /&gt;
D	[0.633 - 0.667)&lt;br /&gt;
D-	[0.600 - 0.633)&lt;br /&gt;
F       [0 - 0.600)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=FAQ=&lt;br /&gt;
Every week, we gather the most commonly asked questions from your Q&amp;amp;S forms and add them to our [[FAQ]] section. Please use it as a resource!&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=Course Setup=&lt;br /&gt;
*[[Initial_Setup]]&lt;br /&gt;
*[[Rundown on Everything Java]]&lt;br /&gt;
**[[Generics]]&lt;br /&gt;
**[[Interfaces]]&lt;br /&gt;
**[[Lambdas]]&lt;br /&gt;
*[[Eclipse Tips]]&lt;br /&gt;
--&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Spring_2023&amp;diff=5449</id>
		<title>Spring 2023</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Spring_2023&amp;diff=5449"/>
		<updated>2026-09-15T14:19:58Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* Spring 2026 Extensions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Welcome to the wiki for CSE 231S Introduction to Parallel and Concurrent Programming at Washington University in St. Louis.&lt;br /&gt;
&lt;br /&gt;
=Learning Outcomes=&lt;br /&gt;
# Learn how to write programs to make use of multiple processors.&lt;br /&gt;
# Learn to how to fix, and preferably, avoid race conditions.&lt;br /&gt;
# Learn how to implement MapReduce frameworks (the technology developed for the original Google search) to deal with big data.&lt;br /&gt;
# Learn how to use locks to share resources safely.&lt;br /&gt;
# Learn how to use read/write lock for improved parallelism under the right conditions.&lt;br /&gt;
# Learn how to avoid deadlock.&lt;br /&gt;
# Learn when and how to use atomics to build thread-safe data structures with improved performance over using locks.&lt;br /&gt;
# Learn when and how to use phasers for more sophisticated concurrency control.&lt;br /&gt;
# Although, the concepts learned should be widely applicable, the assignments will be implemented in Java and, as such, students will develop advanced Java skill.&lt;br /&gt;
&lt;br /&gt;
=Calendar=&lt;br /&gt;
Click [http://www.cse.wustl.edu/~cosgroved/courses/cse231/current/calendar.html here] to visit our calendar.&lt;br /&gt;
&lt;br /&gt;
=Full Contact Time=&lt;br /&gt;
Prof. Cosgrove has no planned trips which would cause class to be cancelled.  If some unforeseen circumstances arise, such as sickness, class will be moved online and recorded.&lt;br /&gt;
&lt;br /&gt;
=Office hours=&lt;br /&gt;
{{:Office hours}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=Important Dates for Spring '23=&lt;br /&gt;
*&amp;lt;b&amp;gt;Block 1 Assignments Due&amp;lt;/b&amp;gt;: ??&lt;br /&gt;
*&amp;lt;b&amp;gt;Block 2 Assignments Due&amp;lt;/b&amp;gt;: ??&lt;br /&gt;
*&amp;lt;b&amp;gt;Block 3 Assignments Due&amp;lt;/b&amp;gt;: ??&lt;br /&gt;
*&amp;lt;b&amp;gt;Spring Break (No OH available)&amp;lt;/b&amp;gt;: 03/12 to 03/18&lt;br /&gt;
*&amp;lt;b&amp;gt;Last day to submit assignments&amp;lt;/b&amp;gt;: 5/10&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Class Policies=&lt;br /&gt;
{{:Class Policies}}&lt;br /&gt;
&lt;br /&gt;
=Spring 2026 Grading breakdown=&lt;br /&gt;
*Prep S&amp;amp;Q Forms (10%)&lt;br /&gt;
*In-class Worksheets Attendance/Participation (10%)&lt;br /&gt;
*Labs/Exercises (80%)&lt;br /&gt;
&lt;br /&gt;
=Fall 2026 Extensions=&lt;br /&gt;
For the Fall 2026 semester: All Exercises will have a one week, no penalty extension.&lt;br /&gt;
&lt;br /&gt;
To be clear, the due dates are asserted to be reasonable.  The bonus week is intended to account for extreme circumstances.&lt;br /&gt;
&lt;br /&gt;
=Grade Ranges=&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
A+ 	[0.967 - 1.0]&lt;br /&gt;
A	[0.933 - 0.967)&lt;br /&gt;
A-	[0.900 - 0.933)&lt;br /&gt;
B+ 	[0.867 - 0.900)&lt;br /&gt;
B	[0.833 - 0.867)&lt;br /&gt;
B-	[0.800 - 0.833)&lt;br /&gt;
C+ 	[0.767 - 0.700)&lt;br /&gt;
C	[0.733 - 0.767)&lt;br /&gt;
C-	[0.700 - 0.733)&lt;br /&gt;
D+ 	[0.667 - 0.600)&lt;br /&gt;
D	[0.633 - 0.667)&lt;br /&gt;
D-	[0.600 - 0.633)&lt;br /&gt;
F       [0 - 0.600)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=FAQ=&lt;br /&gt;
Every week, we gather the most commonly asked questions from your Q&amp;amp;S forms and add them to our [[FAQ]] section. Please use it as a resource!&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=Course Setup=&lt;br /&gt;
*[[Initial_Setup]]&lt;br /&gt;
*[[Rundown on Everything Java]]&lt;br /&gt;
**[[Generics]]&lt;br /&gt;
**[[Interfaces]]&lt;br /&gt;
**[[Lambdas]]&lt;br /&gt;
*[[Eclipse Tips]]&lt;br /&gt;
--&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Office_hours&amp;diff=5448</id>
		<title>Office hours</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Office_hours&amp;diff=5448"/>
		<updated>2026-09-04T13:35:55Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Office Hours TBD&lt;br /&gt;
&amp;lt;!---&lt;br /&gt;
{| class=&amp;quot;wikitable mw-collapsible&amp;quot;&lt;br /&gt;
|+nowrap|Schedule&lt;br /&gt;
|-&lt;br /&gt;
! !! Sunday !! Monday !! Tuesday !! Wednesday !! Thursday !! Friday !! Saturday&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 9:00 -  9:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 9:30 - 10:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;10:00 - 10:30&amp;lt;/code&amp;gt;||  || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourInPerson|Prof. Cosgrove|January 110}} ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;10:30 - 11:00&amp;lt;/code&amp;gt;||  || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourInPerson|Prof. Cosgrove|January 110}} ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;11:00 - 11:30&amp;lt;/code&amp;gt;|| {{OfficeHour|Noah Benschine|98583917095}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourInPerson|Prof. Cosgrove|January 110}} ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;11:30 - 12:00&amp;lt;/code&amp;gt;|| {{OfficeHour|Noah Benschine|98583917095}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourInPerson|Prof. Cosgrove|January 110}} ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;12:00 - 12:30&amp;lt;/code&amp;gt;|| {{OfficeHour|Noah Benschine|98583917095}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourInPerson|Andy Hoette|January 110}} || {{OfficeHourWithPassword|Mustafa Tahir|99260316313|IcXe5XaaJlcAt2lwCUBAJhQdy1chuE.1}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;12:30 -  1:00&amp;lt;/code&amp;gt;|| {{OfficeHour|Noah Benschine|98583917095}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourInPerson|Andy Hoette|January 110}} || {{OfficeHourWithPassword|Mustafa Tahir|99260316313|IcXe5XaaJlcAt2lwCUBAJhQdy1chuE.1}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 1:00 -  1:30&amp;lt;/code&amp;gt;|| {{OfficeHourInPerson|Eileen Li|TBD}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourInPerson|Andy Hoette|January 110}} || {{OfficeHourWithPassword|Mustafa Tahir|99260316313|IcXe5XaaJlcAt2lwCUBAJhQdy1chuE.1}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 1:30 -  2:00&amp;lt;/code&amp;gt;|| {{OfficeHourInPerson|Eileen Li|TBD}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourInPerson|Andy Hoette|January 110}} || {{OfficeHourWithPassword|Mustafa Tahir|99260316313|IcXe5XaaJlcAt2lwCUBAJhQdy1chuE.1}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 2:00 -  2:30&amp;lt;/code&amp;gt;|| {{OfficeHourInPerson|Eileen Li|TBD}} ||  || {{ClassTime|McDonnell 361}} ||  || {{ClassTime|McDonnell 361}} || {{OfficeHourWithPassword|Jack Yang|97337220321|nssUafaMGgnNDwGt4eaI0M6BPDWvS7.1}} || {{OfficeHour|Joesph Molina|97238368362}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 2:30 -  3:00&amp;lt;/code&amp;gt;|| {{OfficeHourInPerson|Eileen Li|TBD}} || {{OfficeHourInPerson|Nancy Patel|Lopata 202}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHourInPerson|Eileen Li|TBD}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHourWithPassword|Jack Yang|97337220321|nssUafaMGgnNDwGt4eaI0M6BPDWvS7.1}} || {{OfficeHour|Joesph Molina|97238368362}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 3:00 -  3:30&amp;lt;/code&amp;gt;|| {{OfficeHour|Joesph Molina|97238368362}} || {{OfficeHourInPerson|Nancy Patel|Lopata 202}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHourInPerson|Eileen Li|TBD}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHourWithPassword|Jack Yang|97337220321|nssUafaMGgnNDwGt4eaI0M6BPDWvS7.1}} || {{OfficeHour|Joesph Molina|97238368362}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 3:30 -  4:00&amp;lt;/code&amp;gt;|| {{OfficeHour|Joesph Molina|97238368362}} || {{OfficeHourInPerson|Nancy Patel|Lopata 202}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHourInPerson|Eileen Li|TBD}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHourWithPassword|Jack Yang|97337220321|nssUafaMGgnNDwGt4eaI0M6BPDWvS7.1}} || {{OfficeHour|Joesph Molina|97238368362}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 4:00 -  4:30&amp;lt;/code&amp;gt;|| {{OfficeHour|Joesph Molina|97238368362}} || {{OfficeHourInPerson|Nancy Patel|Lopata 202}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHourWithPassword|Mustafa Tahir|94469360628|zC5LBXFZcHjumb2quRZOrsWkYQlHJq.1}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHour|Megan Ouyang|5492004415}} || {{OfficeHour|Joesph Molina|97238368362}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 4:30 -  5:00&amp;lt;/code&amp;gt;||  || {{OfficeHourInPerson|Nancy Patel|Lopata 202}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHourWithPassword|Mustafa Tahir|94469360628|zC5LBXFZcHjumb2quRZOrsWkYQlHJq.1}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHour|Megan Ouyang|5492004415}} || {{OfficeHour|Joesph Molina|97238368362}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 5:00 -  5:30&amp;lt;/code&amp;gt;||  || {{OfficeHourInPerson|Nancy Patel|Lopata 202}} || {{OfficeHour|Noah Benschine|93536182747}} || {{OfficeHourWithPassword|Mustafa Tahir|94469360628|zC5LBXFZcHjumb2quRZOrsWkYQlHJq.1}} || {{OfficeHourWithPassword|Jack Yang|97343164185|i8ACoWue5q5u3OKAJUfA5yHAWaAptr.1}} || {{OfficeHour|Megan Ouyang|5492004415}} || {{OfficeHour|Joesph Molina|97238368362}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 5:30 -  6:00&amp;lt;/code&amp;gt;||  ||  || {{OfficeHour|Noah Benschine|93536182747}} || {{OfficeHourWithPassword|Mustafa Tahir|94469360628|zC5LBXFZcHjumb2quRZOrsWkYQlHJq.1}} || {{OfficeHourWithPassword|Jack Yang|97343164185|i8ACoWue5q5u3OKAJUfA5yHAWaAptr.1}} || {{OfficeHour|Megan Ouyang|5492004415}} ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 6:00 -  6:30&amp;lt;/code&amp;gt;||  || {{OfficeHourInPerson|Caleb Huerta|TBD}} || {{OfficeHour|Noah Benschine|93536182747}} || {{OfficeHourWithPassword|Jack Yang|94995588580|xd3WFeBht3pZbofcuCzhEZsmarc5Rt.1}} || {{OfficeHourWithPassword|Jack Yang|97343164185|i8ACoWue5q5u3OKAJUfA5yHAWaAptr.1}} || {{OfficeHour|Megan Ouyang|5492004415}} || {{OfficeHour|Megan Ouyang|5492004415}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 6:30 -  7:00&amp;lt;/code&amp;gt;||  || {{OfficeHourInPerson|Caleb Huerta|TBD}} || {{OfficeHour|Noah Benschine|93536182747}} || {{OfficeHourWithPassword|Jack Yang|94995588580|xd3WFeBht3pZbofcuCzhEZsmarc5Rt.1}} || {{OfficeHourWithPassword|Jack Yang|97343164185|i8ACoWue5q5u3OKAJUfA5yHAWaAptr.1}} ||  || {{OfficeHour|Megan Ouyang|5492004415}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 7:00 -  7:30&amp;lt;/code&amp;gt;||  || {{OfficeHourInPerson|Caleb Huerta|TBD}} || {{OfficeHour|Noah Benschine|93536182747}} || {{OfficeHourWithPassword|Jack Yang|94995588580|xd3WFeBht3pZbofcuCzhEZsmarc5Rt.1}} ||  ||  || {{OfficeHour|Megan Ouyang|5492004415}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 7:30 -  8:00&amp;lt;/code&amp;gt;||  || {{OfficeHourInPerson|Caleb Huerta|TBD}} || {{OfficeHour|Alston Liu|2378167961}} || {{OfficeHourWithPassword|Jack Yang|94995588580|xd3WFeBht3pZbofcuCzhEZsmarc5Rt.1}} ||  ||  || {{OfficeHour|Megan Ouyang|5492004415}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 8:00 -  8:30&amp;lt;/code&amp;gt;||  || {{OfficeHourInPerson|Caleb Huerta|TBD}} || {{OfficeHour|Alston Liu|2378167961}} ||  ||  ||  || {{OfficeHour|Megan Ouyang|5492004415}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 8:30 -  9:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 9:00 -  9:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 9:30 - 10:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;10:00 - 10:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;10:30 - 11:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;11:00 - 11:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;11:30 - 12:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
--&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Module:Chessboard&amp;diff=5447</id>
		<title>Module:Chessboard</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Module:Chessboard&amp;diff=5447"/>
		<updated>2026-05-27T20:49:35Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: Created page with &amp;quot;local p = {}  local cfg, nrows, ncols  local function innerboard(args, size, rev) 	pattern = cfg.pattern or '%w%w' 	local root = mw.html.create('div') 	root:addClass('chess-pi...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;local p = {}&lt;br /&gt;
&lt;br /&gt;
local cfg, nrows, ncols&lt;br /&gt;
&lt;br /&gt;
local function innerboard(args, size, rev)&lt;br /&gt;
	pattern = cfg.pattern or '%w%w'&lt;br /&gt;
	local root = mw.html.create('div')&lt;br /&gt;
	root:addClass('chess-pieces notheme')&lt;br /&gt;
		:css('position', 'relative')&lt;br /&gt;
		:wikitext(cfg.image_board(size))&lt;br /&gt;
	&lt;br /&gt;
	for trow = 1,nrows do&lt;br /&gt;
		local row = rev and trow or ( 1 + nrows - trow )&lt;br /&gt;
		for tcol = 1,ncols do&lt;br /&gt;
			local col = rev and ( 1 + ncols - tcol ) or tcol&lt;br /&gt;
			local piece = args[ncols * ( nrows - row ) + col + 2] or ''&lt;br /&gt;
			if piece:match( pattern ) then&lt;br /&gt;
				local img = cfg.image_square(piece:match(pattern), row, col, size )&lt;br /&gt;
				root:tag('div')&lt;br /&gt;
					:css('top', tostring(( trow - 1 ) * size) .. 'px')&lt;br /&gt;
					:css('left', tostring(( tcol - 1 ) * size) .. 'px')&lt;br /&gt;
					:wikitext(img)&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	return tostring(root)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function chessboard(args, size, rev, letters, numbers, header, footer, align, clear)&lt;br /&gt;
	function letters_row( rev, num_lt, num_rt )&lt;br /&gt;
		local letters = cfg.letters()&lt;br /&gt;
		local root = mw.html.create('')&lt;br /&gt;
		if num_lt then&lt;br /&gt;
			root:tag('td')&lt;br /&gt;
		end&lt;br /&gt;
		for k = 1,ncols do&lt;br /&gt;
			root:tag('td')&lt;br /&gt;
				:css('height', '18px')&lt;br /&gt;
				:css('width', size .. 'px')&lt;br /&gt;
				:wikitext(rev and letters[1+ncols-k] or letters[k])&lt;br /&gt;
		end&lt;br /&gt;
		if num_rt then&lt;br /&gt;
			root:tag('td')&lt;br /&gt;
		end&lt;br /&gt;
		return tostring(root)&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	local letters_top = letters:match( 'both' ) or letters:match( 'top' )&lt;br /&gt;
	local letters_bottom = letters:match( 'both' ) or letters:match( 'bottom' )&lt;br /&gt;
	local numbers_left = numbers:match( 'both' ) or numbers:match( 'left' )&lt;br /&gt;
	local numbers_right = numbers:match( 'both' ) or numbers:match( 'right' )&lt;br /&gt;
	local width = ncols * size + 2&lt;br /&gt;
	if ( numbers_left ) then width = width + 18 end&lt;br /&gt;
	if ( numbers_right ) then width = width + 18 end&lt;br /&gt;
&lt;br /&gt;
	local root = mw.html.create('div')&lt;br /&gt;
		:addClass('chessboard')&lt;br /&gt;
		:addClass('thumb')&lt;br /&gt;
		:addClass('noviewer')&lt;br /&gt;
		:addClass(align)&lt;br /&gt;
	if( header and header ~= '' ) then&lt;br /&gt;
		root:tag('div')&lt;br /&gt;
		:addClass('center')&lt;br /&gt;
		:css('line-height', '130%')&lt;br /&gt;
		:css('margin', '0 auto')&lt;br /&gt;
		:css('max-width', (width + ncols) .. 'px')&lt;br /&gt;
		:wikitext(header)&lt;br /&gt;
	end&lt;br /&gt;
	local div = root:tag('div')&lt;br /&gt;
		:addClass('thumbinner')&lt;br /&gt;
		:css('width', width .. 'px')&lt;br /&gt;
	local b = div:tag('table')&lt;br /&gt;
		:attr('cellpadding', '0')&lt;br /&gt;
		:attr('cellspacing', '0')&lt;br /&gt;
&lt;br /&gt;
	if ( letters_top ) then&lt;br /&gt;
		b:tag('tr')&lt;br /&gt;
			:wikitext(letters_row( rev, numbers_left, numbers_right ))&lt;br /&gt;
	end&lt;br /&gt;
	local tablerow = b:tag('tr')&lt;br /&gt;
	if ( numbers_left ) then &lt;br /&gt;
		tablerow:tag('td')&lt;br /&gt;
			:css('width', '18px')&lt;br /&gt;
			:css('height', size .. 'px')&lt;br /&gt;
			:wikitext(rev and 1 or nrows) &lt;br /&gt;
	end&lt;br /&gt;
	local td = tablerow:tag('td')&lt;br /&gt;
		:attr('colspan', ncols)&lt;br /&gt;
		:attr('rowspan', nrows)&lt;br /&gt;
		:wikitext(innerboard(args, size, rev))&lt;br /&gt;
	&lt;br /&gt;
	if ( numbers_right ) then &lt;br /&gt;
		tablerow:tag('td')&lt;br /&gt;
			:css('width', '18px')&lt;br /&gt;
			:css('height', size .. 'px')&lt;br /&gt;
			:wikitext(rev and 1 or nrows) &lt;br /&gt;
	end&lt;br /&gt;
	if ( numbers_left or numbers_right ) then&lt;br /&gt;
		for trow = 2, nrows do&lt;br /&gt;
			local idx = rev and trow or ( 1 + nrows - trow )&lt;br /&gt;
			tablerow = b:tag('tr')&lt;br /&gt;
			if ( numbers_left ) then &lt;br /&gt;
				tablerow:tag('td')&lt;br /&gt;
					:css('height', size .. 'px')&lt;br /&gt;
					:wikitext(idx)&lt;br /&gt;
			end&lt;br /&gt;
			if ( numbers_right ) then &lt;br /&gt;
				tablerow:tag('td')&lt;br /&gt;
					:css('height', size .. 'px')&lt;br /&gt;
					:wikitext(idx)&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	if ( letters_bottom ) then&lt;br /&gt;
		b:tag('tr')&lt;br /&gt;
			:wikitext(letters_row( rev, numbers_left, numbers_right ))&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	if footer and mw.text.trim(footer)~='' then&lt;br /&gt;
		div:tag('div')&lt;br /&gt;
			:addClass('thumbcaption')&lt;br /&gt;
			:wikitext(footer)&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	return tostring(root) ..&lt;br /&gt;
		mw.getCurrentFrame():extensionTag( 'templatestyles', '', { src = 'Module:Chessboard/styles.css' } )&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function convertFenToArgs( fen )&lt;br /&gt;
	-- converts FEN notation to 64 entry array of positions, offset by 2&lt;br /&gt;
	local res = { ' ', ' ' }&lt;br /&gt;
	-- Loop over rows, which are delimited by /&lt;br /&gt;
	for srow in string.gmatch( &amp;quot;/&amp;quot; .. fen, &amp;quot;/%w+&amp;quot; ) do&lt;br /&gt;
		-- Loop over all letters and numbers in the row&lt;br /&gt;
		for piece in srow:gmatch( &amp;quot;%w&amp;quot; ) do&lt;br /&gt;
			if piece:match( &amp;quot;%d&amp;quot; ) then -- if a digit&lt;br /&gt;
				for k=1,piece do&lt;br /&gt;
					table.insert(res,' ')&lt;br /&gt;
				end&lt;br /&gt;
			else -- not a digit&lt;br /&gt;
				local color = piece:match( '%u' ) and 'l' or 'd'&lt;br /&gt;
				piece = piece:lower()&lt;br /&gt;
				table.insert( res, piece .. color )&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	return res&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function convertArgsToFen( args, offset )&lt;br /&gt;
	function nullOrWhitespace( s ) return not s or s:match( '^%s*(.-)%s*$' ) == '' end&lt;br /&gt;
	function piece( s ) &lt;br /&gt;
		return nullOrWhitespace( s ) and 1&lt;br /&gt;
		or s:gsub( '%s*(%a)(%a)%s*', function( a, b ) return b == 'l' and a:upper() or a end )&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	local res = ''&lt;br /&gt;
	offset = offset or 0&lt;br /&gt;
	for row = 1, 8 do&lt;br /&gt;
		for file = 1, 8 do&lt;br /&gt;
			res = res .. piece( args[8*(row - 1) + file + offset] )&lt;br /&gt;
		end&lt;br /&gt;
		if row &amp;lt; 8 then res = res .. '/' end&lt;br /&gt;
	end&lt;br /&gt;
	return mw.ustring.gsub(res, '1+', function( s ) return #s end )&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p.board(frame)&lt;br /&gt;
	local args = frame.args&lt;br /&gt;
	local pargs = frame:getParent().args&lt;br /&gt;
	local style = args.style or pargs.style or 'Chess'&lt;br /&gt;
	cfg = require('Module:Chessboard/' .. style)&lt;br /&gt;
	nrows, ncols = cfg.dims()&lt;br /&gt;
	&lt;br /&gt;
	local size = args.size or pargs.size or '26'&lt;br /&gt;
	local reverse = ( args.reverse or pargs.reverse or '' ):lower() == &amp;quot;true&amp;quot;&lt;br /&gt;
	local letters = ( args.letters or pargs.letters or 'both' ):lower() &lt;br /&gt;
	local numbers = ( args.numbers or pargs.numbers or 'both' ):lower() &lt;br /&gt;
	local header = args[2] or pargs[2] or ''&lt;br /&gt;
	local footer = args[nrows*ncols + 3] or pargs[nrows*ncols + 3] or ''&lt;br /&gt;
	local align = ( args[1] or pargs[1] or 'floatright' ):lower()&lt;br /&gt;
	local clear = args.clear or pargs.clear or ( align:match('floatright') and 'right' ) or 'none'&lt;br /&gt;
	local fen = args.fen or pargs.fen&lt;br /&gt;
	local pgn = args.pgn or pargs.pgn&lt;br /&gt;
&lt;br /&gt;
	size = mw.ustring.match( size, '[%d]+' ) or '26' -- remove px from size&lt;br /&gt;
	if (pgn) then&lt;br /&gt;
		local pgnModule = require('Module:Pgn')&lt;br /&gt;
		metadata, moves = pgnModule.main(pgn)&lt;br /&gt;
		fen = moves[#moves]&lt;br /&gt;
	end&lt;br /&gt;
	if (fen) then&lt;br /&gt;
		align = args.align or pargs.align or 'floatright'&lt;br /&gt;
		clear = args.clear or pargs.clear or ( align:match('floatright') and 'right' ) or 'none'&lt;br /&gt;
		header = args.header or pargs.header or ''&lt;br /&gt;
		footer = args.footer or pargs.footer or ''&lt;br /&gt;
		return chessboard( convertFenToArgs( fen ), size, reverse, letters, numbers, header, footer, align, clear )&lt;br /&gt;
	end&lt;br /&gt;
	if args[3] then&lt;br /&gt;
		return chessboard(args, size, reverse, letters, numbers, header, footer, align, clear)&lt;br /&gt;
	else&lt;br /&gt;
		return chessboard(pargs, size, reverse, letters, numbers, header, footer, align, clear)&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Class_Policies&amp;diff=5445</id>
		<title>Class Policies</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Class_Policies&amp;diff=5445"/>
		<updated>2026-05-12T17:25:20Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* Exercises */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;===Academic Integrity===&lt;br /&gt;
{{:Academic_Integrity}}&lt;br /&gt;
&lt;br /&gt;
===Attendance/Participation===&lt;br /&gt;
Attendance is required.  You are expected to attend class on both Tuesdays and Thursdays for the section you are registered for. There are many benefits for coming to class (asking TA questions about assignments, clearing up any concepts with Prof. Cosgrove, etc.), so it is highly encouraged for you to come.&lt;br /&gt;
&lt;br /&gt;
Attendance/Participation will be assessed via submission of in-class worksheets.   You will only be penalized after missing more than &amp;lt;s&amp;gt;three (3)&amp;lt;/s&amp;gt; &amp;lt;b&amp;gt;six (6) in-class worksheets&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===S&amp;amp;Q (Synthesis and Question) Forms===&lt;br /&gt;
There will usually be an S&amp;amp;Q form due before every class. We expect you to complete these the night before classes. You will only be penalized after missing more than &amp;lt;b&amp;gt;three (3) S&amp;amp;Q forms&amp;lt;/b&amp;gt;. If you fail to turn these in on time for class, you can still receive 70% credit by turning them in by 11:59PM Central on Friday.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
===Labs===&lt;br /&gt;
Labs come with a list of JUnit tests to help guide students past common problems. Passing all of the tests does not guarantee anything when it comes to a grade on the assignment. Labs are read by the professor and TAs. Students are provided feedback and given an initial grade. Students then have a week from that feedback to turn in revisions which will then be reviewed and given a final grade.&lt;br /&gt;
&lt;br /&gt;
====Late Lab Submissions====&lt;br /&gt;
Labs have an original submission date.  Students then receive feedback on that submission and have a week to make revisions. Students who pass (shenanigans free) all of the JUnit tests by the original submission have the additional benefit of getting their revisions checked out in class to avoid any confusion.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!&lt;br /&gt;
!Portion of Grade&lt;br /&gt;
!Evaluation Technique&lt;br /&gt;
|-&lt;br /&gt;
|Original Submission&lt;br /&gt;
|style=&amp;quot;text-align:center;&amp;quot;|25%&lt;br /&gt;
|Non-shenanigans JUnit Test Passing&lt;br /&gt;
|-&lt;br /&gt;
|Final Revisions&lt;br /&gt;
|style=&amp;quot;text-align:center;&amp;quot;|75%&lt;br /&gt;
|Quality Assessment&lt;br /&gt;
|}&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Exercises===&lt;br /&gt;
Exercises are assessed by whether or not the code pushed to bitbucket passes all of the JUnit tests.  Exercises are due the following Saturday one week from their assignment.  If, after human inspection, your solution is deemed insufficient/inappropriate despite passing the JUnit tests, you will be alerted with the specifics and given a week to address the situation.&lt;br /&gt;
&lt;br /&gt;
It should go without saying that any dishonest effort to pass the tests without solving the problem would be an [[Academic_Integrity|academic integrity violation]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
'''Fall 2023 Specific Policy''': all exercises will be granted a one week extension with no penalty.&lt;br /&gt;
--&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5444</id>
		<title>Iterative Averaging</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5444"/>
		<updated>2026-04-23T17:54:19Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* IterativeAveragingRanges */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Iterative Averaging is the process of updating an array to so that each index becomes the average of the indices one before and one after it. After repeating this for many iterations, the array may converge to one set of numbers. For example, when given the following array, we can perform iterations of this algorithm until the array eventually converges:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | [0] || [1] || [2] || [3] || [4] || [5] || [6] || [7] || [8] || [9] || [10]&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.25 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.125 || 0.25 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0625 || 0.125 || 0.375 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.03125 || 0.0625 || 0.21875 || 0.375 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.015625 || 0.03125 || 0.125 || 0.21875 || 0.453125 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0078125 || 0.015625 || 0.0703125 || 0.125 || 0.2890625 || 0.453125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.00390625 || 0.0078125 || 0.0390625 || 0.0703125 || 0.1796875 || 0.2890625 || 0.5078125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.00390625 || 0.021484375 || 0.0390625 || 0.109375 || 0.1796875 || 0.34375 || 0.5078125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.01171875 || 0.021484375 || 0.0654296875 || 0.109375 || 0.2265625 || 0.34375 || 0.548828125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0 &lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.1 || 0.2 || 0.3 || 0.4 || 0.5 || 0.6 || 0.7 || 0.8 || 0.9 || 1.0 &lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | src || dst&lt;br /&gt;
|-&lt;br /&gt;
| phase 0 || &lt;br /&gt;
|-&lt;br /&gt;
| phase 1 || phase 0&lt;br /&gt;
|-&lt;br /&gt;
| phase 2 || phase 1&lt;br /&gt;
|-&lt;br /&gt;
| phase 3 || phase 2&lt;br /&gt;
|-&lt;br /&gt;
| phase 4 || phase 3&lt;br /&gt;
|-&lt;br /&gt;
| phase 5 || phase 4&lt;br /&gt;
|-&lt;br /&gt;
| phase 6 || phase 5&lt;br /&gt;
|-&lt;br /&gt;
| phase 7 || phase 6&lt;br /&gt;
|-&lt;br /&gt;
| phase 8 || phase 7&lt;br /&gt;
|-&lt;br /&gt;
| phase 9 || phase 8&lt;br /&gt;
|-&lt;br /&gt;
| phase 10 || phase 9&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| || phase N-1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the ends of the array remain the same value from the previous iteration. &lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
X10-like Phasers have been added to Java since JDK7.  We will gain some experience with using Phasers in a parallel for-loop context. Phasers allow us to change the structure of our loops and reduce overhead in the algorithm.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
For more information on the algorithm and how we can use Phasers to make it better, review [https://edge.edx.org/courses/RiceX/COMP322/1T2014R/courseware/a900dd0655384de3b5ef01e508ea09d7/6349730bb2a149a0b33fa23db7afddee/13 Topic 3.5] from the RiceX course.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
&amp;lt;youtube&amp;gt;Ixprqn_Xhf4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Java Phaser==&lt;br /&gt;
&amp;lt;youtube&amp;gt;whPAPmylbEU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Reference_Page#Phasers|Check out the reference page on phasers]]&lt;br /&gt;
&lt;br /&gt;
[http://www.baeldung.com/java-phaser Guide to the Java Phaser]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html class Phaser]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#bulkRegister-int- bulkRegister] (also see instructions on [[#ForkLoopWithPhaserIterativeAverager |ForkLoopWithPhaserIterativeAverager]])&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#arriveAndAwaitAdvance-- arriveAndAwaitAdvance]&lt;br /&gt;
&lt;br /&gt;
{{warning | Our use of the forall loop with Phasers does not accurately convey their finicky nature.  More than other features, Phasers seem to require more care to get performance improvements. }}&lt;br /&gt;
&lt;br /&gt;
==Ranges==&lt;br /&gt;
[[Ranges|Ranges (Previous Exercise To Use)]]&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;0c2PZ-ARDDE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
==IterativeAveragingRanges==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingRanges|slice|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
===slice===&lt;br /&gt;
&amp;lt;youtube&amp;gt;_taVHpi1Hpg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each IterativeAverager should slice the data into ranges.  The constructor for each IterativeAverager is passed the number of slices to create.&lt;br /&gt;
&lt;br /&gt;
{{Tip| Use Ranges.slice(min, maxExclusive, numSlices)}}&lt;br /&gt;
&lt;br /&gt;
If you are to create 3 slices an array of length 11:&lt;br /&gt;
&lt;br /&gt;
[[File:IterativeAveraging.png|600px]]&lt;br /&gt;
&lt;br /&gt;
the slices should have these properties:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|||sliceID||minInclusive||maxExclusive&lt;br /&gt;
|-&lt;br /&gt;
|sliceA||0||1||4&lt;br /&gt;
|-&lt;br /&gt;
|sliceB||1||4||7&lt;br /&gt;
|-&lt;br /&gt;
|sliceC||2||7||10&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==IterativeAveragingDoubleBuffer==&lt;br /&gt;
===constructor===&lt;br /&gt;
&lt;br /&gt;
Hang onto (in member a member variable) the original double array and initialize (and hang onto) two buffers of the same length. Make sure to copy over the original data's 0th and last index's value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	private final double[] originalData;&lt;br /&gt;
	private final double[] a;&lt;br /&gt;
	private final double[] b;&lt;br /&gt;
&lt;br /&gt;
	public IterativeAveragingDoubleBuffer(double[] originalData) {&lt;br /&gt;
		Objects.requireNonNull(originalData);&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===srcForPhase(phaseIndex)===&lt;br /&gt;
This returns the buffer to read from for a given phaseIndex.  For 0, this should be the original data.  For all other phaseIndices, it should alternate (odd and even) between the a and b buffers.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
	 &amp;lt;table&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 0:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;originalData&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 1:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 2:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 3:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 4:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 5:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 6:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;...&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;&amp;amp;nbsp;&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	public double[] srcForPhase(int phaseIndex) {&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dstForPhase===&lt;br /&gt;
This returns the buffer to write to a given phaseIndex.  This should be the opposite buffer of what srcForPhase returned.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
	 &amp;lt;table&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 0:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 1:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 2:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 3:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 4:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 5:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 6:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;...&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;&amp;amp;nbsp;&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	public double[] dstForPhase(int phaseIndex) {&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: you may want to handle the special case of someone passing -1 to this method.  In this case it should return the originalData.&lt;br /&gt;
&lt;br /&gt;
==LoopOfForkLoopsIterativeAverager==&lt;br /&gt;
{{CodeToImplement|LoopOfForkLoopsIterativeAverager|sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
For this method, you should not be using Phasers. Instead, implement a parallel version of the [[Sequential_Iterative_Averager_Assignment|Sequential Iterative Averaging warm up]]. Make use of the IterativeAveragingDoubleBuffer class. The return value should be the current version of the array after it has gone through the number of iterations passed in the method.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;sequential loop&lt;br /&gt;
    parallel loop&lt;br /&gt;
        work&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is the work for each task?&lt;br /&gt;
&lt;br /&gt;
[[File:LoopOfForkLoops_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==X10PhasedForkLoopIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|X10PhasedForkLoopIterativeAverager|x10&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;x10 phased parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:X10PhasedForkLoop_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==ForkLoopWithPhaserIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ForkLoopWithPhaserIterativeAverager|phaserCreator&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
Before you get started on this, make sure you review the [[Iterative Averaging#Background | Background section]] in order to understand how to utilize Phasers (it will look different than the X10 implementation). This time, we will use Phasers to create a parallel version of the algorithm that has less overhead. Here are a few notes to keep in mind when working on this assignment:&lt;br /&gt;
*Think carefully as to how the loops in this version will be structured. Drawing out the computation graph may help&lt;br /&gt;
*Bulk registering a Phaser indicates how many times &amp;lt;code&amp;gt;phaser.arriveAndAwaitAdvance()&amp;lt;/code&amp;gt; needs to be called before any of the threads are able to move on. In other words, it equals to how many threads need to arive at the checkpoint before moving on. Make sure to register the right number!&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
*You also bulk register the SwappableDoubleArrays when instantiating it. Similar to Phasers, the integer passed through in the constructor tells the object how many times &amp;lt;code&amp;gt;swap()&amp;lt;/code&amp;gt; needs to be called before the source and destination arrays are actually swapped.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;create phaser&lt;br /&gt;
register phaser for each task&lt;br /&gt;
parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:ForkLoopWithPhaser_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|__RigidIterativeAveragingTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
: {{TestSuite|_IterativeAveragingRangesTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_IterativeAveragingDoubleBufferTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_LoopOfForkLoopsIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_X10PhasedForkLoopIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_ForkLoopWithPhaserIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5443</id>
		<title>Iterative Averaging</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5443"/>
		<updated>2026-04-15T02:08:00Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* LoopOfForkLoopsIterativeAverager */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Iterative Averaging is the process of updating an array to so that each index becomes the average of the indices one before and one after it. After repeating this for many iterations, the array may converge to one set of numbers. For example, when given the following array, we can perform iterations of this algorithm until the array eventually converges:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | [0] || [1] || [2] || [3] || [4] || [5] || [6] || [7] || [8] || [9] || [10]&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.25 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.125 || 0.25 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0625 || 0.125 || 0.375 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.03125 || 0.0625 || 0.21875 || 0.375 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.015625 || 0.03125 || 0.125 || 0.21875 || 0.453125 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0078125 || 0.015625 || 0.0703125 || 0.125 || 0.2890625 || 0.453125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.00390625 || 0.0078125 || 0.0390625 || 0.0703125 || 0.1796875 || 0.2890625 || 0.5078125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.00390625 || 0.021484375 || 0.0390625 || 0.109375 || 0.1796875 || 0.34375 || 0.5078125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.01171875 || 0.021484375 || 0.0654296875 || 0.109375 || 0.2265625 || 0.34375 || 0.548828125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0 &lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.1 || 0.2 || 0.3 || 0.4 || 0.5 || 0.6 || 0.7 || 0.8 || 0.9 || 1.0 &lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | src || dst&lt;br /&gt;
|-&lt;br /&gt;
| phase 0 || &lt;br /&gt;
|-&lt;br /&gt;
| phase 1 || phase 0&lt;br /&gt;
|-&lt;br /&gt;
| phase 2 || phase 1&lt;br /&gt;
|-&lt;br /&gt;
| phase 3 || phase 2&lt;br /&gt;
|-&lt;br /&gt;
| phase 4 || phase 3&lt;br /&gt;
|-&lt;br /&gt;
| phase 5 || phase 4&lt;br /&gt;
|-&lt;br /&gt;
| phase 6 || phase 5&lt;br /&gt;
|-&lt;br /&gt;
| phase 7 || phase 6&lt;br /&gt;
|-&lt;br /&gt;
| phase 8 || phase 7&lt;br /&gt;
|-&lt;br /&gt;
| phase 9 || phase 8&lt;br /&gt;
|-&lt;br /&gt;
| phase 10 || phase 9&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| || phase N-1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the ends of the array remain the same value from the previous iteration. &lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
X10-like Phasers have been added to Java since JDK7.  We will gain some experience with using Phasers in a parallel for-loop context. Phasers allow us to change the structure of our loops and reduce overhead in the algorithm.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
For more information on the algorithm and how we can use Phasers to make it better, review [https://edge.edx.org/courses/RiceX/COMP322/1T2014R/courseware/a900dd0655384de3b5ef01e508ea09d7/6349730bb2a149a0b33fa23db7afddee/13 Topic 3.5] from the RiceX course.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
&amp;lt;youtube&amp;gt;Ixprqn_Xhf4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Java Phaser==&lt;br /&gt;
&amp;lt;youtube&amp;gt;whPAPmylbEU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Reference_Page#Phasers|Check out the reference page on phasers]]&lt;br /&gt;
&lt;br /&gt;
[http://www.baeldung.com/java-phaser Guide to the Java Phaser]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html class Phaser]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#bulkRegister-int- bulkRegister] (also see instructions on [[#ForkLoopWithPhaserIterativeAverager |ForkLoopWithPhaserIterativeAverager]])&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#arriveAndAwaitAdvance-- arriveAndAwaitAdvance]&lt;br /&gt;
&lt;br /&gt;
{{warning | Our use of the forall loop with Phasers does not accurately convey their finicky nature.  More than other features, Phasers seem to require more care to get performance improvements. }}&lt;br /&gt;
&lt;br /&gt;
==Ranges==&lt;br /&gt;
[[Ranges|Ranges (Previous Exercise To Use)]]&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;0c2PZ-ARDDE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
==IterativeAveragingRanges==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|slice|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
===slice===&lt;br /&gt;
&amp;lt;youtube&amp;gt;_taVHpi1Hpg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each IterativeAverager should slice the data into ranges.  The constructor for each IterativeAverager is passed the number of slices to create.&lt;br /&gt;
&lt;br /&gt;
{{Tip| Use Ranges.slice(min, maxExclusive, numSlices)}}&lt;br /&gt;
&lt;br /&gt;
If you are to create 3 slices an array of length 11:&lt;br /&gt;
&lt;br /&gt;
[[File:IterativeAveraging.png|600px]]&lt;br /&gt;
&lt;br /&gt;
the slices should have these properties:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|||sliceID||minInclusive||maxExclusive&lt;br /&gt;
|-&lt;br /&gt;
|sliceA||0||1||4&lt;br /&gt;
|-&lt;br /&gt;
|sliceB||1||4||7&lt;br /&gt;
|-&lt;br /&gt;
|sliceC||2||7||10&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==IterativeAveragingDoubleBuffer==&lt;br /&gt;
===constructor===&lt;br /&gt;
&lt;br /&gt;
Hang onto (in member a member variable) the original double array and initialize (and hang onto) two buffers of the same length. Make sure to copy over the original data's 0th and last index's value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	private final double[] originalData;&lt;br /&gt;
	private final double[] a;&lt;br /&gt;
	private final double[] b;&lt;br /&gt;
&lt;br /&gt;
	public IterativeAveragingDoubleBuffer(double[] originalData) {&lt;br /&gt;
		Objects.requireNonNull(originalData);&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===srcForPhase(phaseIndex)===&lt;br /&gt;
This returns the buffer to read from for a given phaseIndex.  For 0, this should be the original data.  For all other phaseIndices, it should alternate (odd and even) between the a and b buffers.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
	 &amp;lt;table&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 0:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;originalData&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 1:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 2:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 3:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 4:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 5:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 6:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;...&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;&amp;amp;nbsp;&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	public double[] srcForPhase(int phaseIndex) {&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dstForPhase===&lt;br /&gt;
This returns the buffer to write to a given phaseIndex.  This should be the opposite buffer of what srcForPhase returned.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
	 &amp;lt;table&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 0:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 1:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 2:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 3:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 4:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 5:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 6:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;...&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;&amp;amp;nbsp;&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	public double[] dstForPhase(int phaseIndex) {&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: you may want to handle the special case of someone passing -1 to this method.  In this case it should return the originalData.&lt;br /&gt;
&lt;br /&gt;
==LoopOfForkLoopsIterativeAverager==&lt;br /&gt;
{{CodeToImplement|LoopOfForkLoopsIterativeAverager|sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
For this method, you should not be using Phasers. Instead, implement a parallel version of the [[Sequential_Iterative_Averager_Assignment|Sequential Iterative Averaging warm up]]. Make use of the IterativeAveragingDoubleBuffer class. The return value should be the current version of the array after it has gone through the number of iterations passed in the method.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;sequential loop&lt;br /&gt;
    parallel loop&lt;br /&gt;
        work&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is the work for each task?&lt;br /&gt;
&lt;br /&gt;
[[File:LoopOfForkLoops_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==X10PhasedForkLoopIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|X10PhasedForkLoopIterativeAverager|x10&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;x10 phased parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:X10PhasedForkLoop_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==ForkLoopWithPhaserIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ForkLoopWithPhaserIterativeAverager|phaserCreator&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
Before you get started on this, make sure you review the [[Iterative Averaging#Background | Background section]] in order to understand how to utilize Phasers (it will look different than the X10 implementation). This time, we will use Phasers to create a parallel version of the algorithm that has less overhead. Here are a few notes to keep in mind when working on this assignment:&lt;br /&gt;
*Think carefully as to how the loops in this version will be structured. Drawing out the computation graph may help&lt;br /&gt;
*Bulk registering a Phaser indicates how many times &amp;lt;code&amp;gt;phaser.arriveAndAwaitAdvance()&amp;lt;/code&amp;gt; needs to be called before any of the threads are able to move on. In other words, it equals to how many threads need to arive at the checkpoint before moving on. Make sure to register the right number!&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
*You also bulk register the SwappableDoubleArrays when instantiating it. Similar to Phasers, the integer passed through in the constructor tells the object how many times &amp;lt;code&amp;gt;swap()&amp;lt;/code&amp;gt; needs to be called before the source and destination arrays are actually swapped.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;create phaser&lt;br /&gt;
register phaser for each task&lt;br /&gt;
parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:ForkLoopWithPhaser_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|__RigidIterativeAveragingTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
: {{TestSuite|_IterativeAveragingRangesTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_IterativeAveragingDoubleBufferTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_LoopOfForkLoopsIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_X10PhasedForkLoopIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_ForkLoopWithPhaserIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5442</id>
		<title>Iterative Averaging</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5442"/>
		<updated>2026-04-14T13:41:02Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* dstForPhase */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Iterative Averaging is the process of updating an array to so that each index becomes the average of the indices one before and one after it. After repeating this for many iterations, the array may converge to one set of numbers. For example, when given the following array, we can perform iterations of this algorithm until the array eventually converges:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | [0] || [1] || [2] || [3] || [4] || [5] || [6] || [7] || [8] || [9] || [10]&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.25 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.125 || 0.25 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0625 || 0.125 || 0.375 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.03125 || 0.0625 || 0.21875 || 0.375 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.015625 || 0.03125 || 0.125 || 0.21875 || 0.453125 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0078125 || 0.015625 || 0.0703125 || 0.125 || 0.2890625 || 0.453125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.00390625 || 0.0078125 || 0.0390625 || 0.0703125 || 0.1796875 || 0.2890625 || 0.5078125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.00390625 || 0.021484375 || 0.0390625 || 0.109375 || 0.1796875 || 0.34375 || 0.5078125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.01171875 || 0.021484375 || 0.0654296875 || 0.109375 || 0.2265625 || 0.34375 || 0.548828125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0 &lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.1 || 0.2 || 0.3 || 0.4 || 0.5 || 0.6 || 0.7 || 0.8 || 0.9 || 1.0 &lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | src || dst&lt;br /&gt;
|-&lt;br /&gt;
| phase 0 || &lt;br /&gt;
|-&lt;br /&gt;
| phase 1 || phase 0&lt;br /&gt;
|-&lt;br /&gt;
| phase 2 || phase 1&lt;br /&gt;
|-&lt;br /&gt;
| phase 3 || phase 2&lt;br /&gt;
|-&lt;br /&gt;
| phase 4 || phase 3&lt;br /&gt;
|-&lt;br /&gt;
| phase 5 || phase 4&lt;br /&gt;
|-&lt;br /&gt;
| phase 6 || phase 5&lt;br /&gt;
|-&lt;br /&gt;
| phase 7 || phase 6&lt;br /&gt;
|-&lt;br /&gt;
| phase 8 || phase 7&lt;br /&gt;
|-&lt;br /&gt;
| phase 9 || phase 8&lt;br /&gt;
|-&lt;br /&gt;
| phase 10 || phase 9&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| || phase N-1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the ends of the array remain the same value from the previous iteration. &lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
X10-like Phasers have been added to Java since JDK7.  We will gain some experience with using Phasers in a parallel for-loop context. Phasers allow us to change the structure of our loops and reduce overhead in the algorithm.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
For more information on the algorithm and how we can use Phasers to make it better, review [https://edge.edx.org/courses/RiceX/COMP322/1T2014R/courseware/a900dd0655384de3b5ef01e508ea09d7/6349730bb2a149a0b33fa23db7afddee/13 Topic 3.5] from the RiceX course.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
&amp;lt;youtube&amp;gt;Ixprqn_Xhf4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Java Phaser==&lt;br /&gt;
&amp;lt;youtube&amp;gt;whPAPmylbEU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Reference_Page#Phasers|Check out the reference page on phasers]]&lt;br /&gt;
&lt;br /&gt;
[http://www.baeldung.com/java-phaser Guide to the Java Phaser]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html class Phaser]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#bulkRegister-int- bulkRegister] (also see instructions on [[#ForkLoopWithPhaserIterativeAverager |ForkLoopWithPhaserIterativeAverager]])&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#arriveAndAwaitAdvance-- arriveAndAwaitAdvance]&lt;br /&gt;
&lt;br /&gt;
{{warning | Our use of the forall loop with Phasers does not accurately convey their finicky nature.  More than other features, Phasers seem to require more care to get performance improvements. }}&lt;br /&gt;
&lt;br /&gt;
==Ranges==&lt;br /&gt;
[[Ranges|Ranges (Previous Exercise To Use)]]&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;0c2PZ-ARDDE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
==IterativeAveragingRanges==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|slice|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
===slice===&lt;br /&gt;
&amp;lt;youtube&amp;gt;_taVHpi1Hpg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each IterativeAverager should slice the data into ranges.  The constructor for each IterativeAverager is passed the number of slices to create.&lt;br /&gt;
&lt;br /&gt;
{{Tip| Use Ranges.slice(min, maxExclusive, numSlices)}}&lt;br /&gt;
&lt;br /&gt;
If you are to create 3 slices an array of length 11:&lt;br /&gt;
&lt;br /&gt;
[[File:IterativeAveraging.png|600px]]&lt;br /&gt;
&lt;br /&gt;
the slices should have these properties:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|||sliceID||minInclusive||maxExclusive&lt;br /&gt;
|-&lt;br /&gt;
|sliceA||0||1||4&lt;br /&gt;
|-&lt;br /&gt;
|sliceB||1||4||7&lt;br /&gt;
|-&lt;br /&gt;
|sliceC||2||7||10&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==IterativeAveragingDoubleBuffer==&lt;br /&gt;
===constructor===&lt;br /&gt;
&lt;br /&gt;
Hang onto (in member a member variable) the original double array and initialize (and hang onto) two buffers of the same length. Make sure to copy over the original data's 0th and last index's value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	private final double[] originalData;&lt;br /&gt;
	private final double[] a;&lt;br /&gt;
	private final double[] b;&lt;br /&gt;
&lt;br /&gt;
	public IterativeAveragingDoubleBuffer(double[] originalData) {&lt;br /&gt;
		Objects.requireNonNull(originalData);&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===srcForPhase(phaseIndex)===&lt;br /&gt;
This returns the buffer to read from for a given phaseIndex.  For 0, this should be the original data.  For all other phaseIndices, it should alternate (odd and even) between the a and b buffers.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
	 &amp;lt;table&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 0:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;originalData&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 1:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 2:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 3:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 4:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 5:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 6:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;...&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;&amp;amp;nbsp;&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	public double[] srcForPhase(int phaseIndex) {&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dstForPhase===&lt;br /&gt;
This returns the buffer to write to a given phaseIndex.  This should be the opposite buffer of what srcForPhase returned.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
	 &amp;lt;table&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 0:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 1:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 2:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 3:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 4:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 5:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 6:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;...&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;&amp;amp;nbsp;&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	public double[] dstForPhase(int phaseIndex) {&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: you may want to handle the special case of someone passing -1 to this method.  In this case it should return the originalData.&lt;br /&gt;
&lt;br /&gt;
==LoopOfForkLoopsIterativeAverager==&lt;br /&gt;
{{CodeToImplement|LoopOfForkLoopsIterativeAverager|sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
For this method, you should not be using Phasers. Instead, implement a parallel version of the [[Sequential_Iterative_Averager_Assignment|Sequential Iterative Averaging warm up]]. Make use of the PhasableDoubleArrays class. The return value should be the current version of the array after it has gone through the number of iterations passed in the method.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;sequential loop&lt;br /&gt;
    parallel loop&lt;br /&gt;
        work&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is the work for each task?&lt;br /&gt;
&lt;br /&gt;
[[File:LoopOfForkLoops_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==X10PhasedForkLoopIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|X10PhasedForkLoopIterativeAverager|x10&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;x10 phased parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:X10PhasedForkLoop_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==ForkLoopWithPhaserIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ForkLoopWithPhaserIterativeAverager|phaserCreator&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
Before you get started on this, make sure you review the [[Iterative Averaging#Background | Background section]] in order to understand how to utilize Phasers (it will look different than the X10 implementation). This time, we will use Phasers to create a parallel version of the algorithm that has less overhead. Here are a few notes to keep in mind when working on this assignment:&lt;br /&gt;
*Think carefully as to how the loops in this version will be structured. Drawing out the computation graph may help&lt;br /&gt;
*Bulk registering a Phaser indicates how many times &amp;lt;code&amp;gt;phaser.arriveAndAwaitAdvance()&amp;lt;/code&amp;gt; needs to be called before any of the threads are able to move on. In other words, it equals to how many threads need to arive at the checkpoint before moving on. Make sure to register the right number!&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
*You also bulk register the SwappableDoubleArrays when instantiating it. Similar to Phasers, the integer passed through in the constructor tells the object how many times &amp;lt;code&amp;gt;swap()&amp;lt;/code&amp;gt; needs to be called before the source and destination arrays are actually swapped.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;create phaser&lt;br /&gt;
register phaser for each task&lt;br /&gt;
parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:ForkLoopWithPhaser_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|__RigidIterativeAveragingTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
: {{TestSuite|_IterativeAveragingRangesTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_IterativeAveragingDoubleBufferTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_LoopOfForkLoopsIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_X10PhasedForkLoopIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_ForkLoopWithPhaserIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5441</id>
		<title>Iterative Averaging</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5441"/>
		<updated>2026-04-14T13:39:56Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* dstForPhase */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Iterative Averaging is the process of updating an array to so that each index becomes the average of the indices one before and one after it. After repeating this for many iterations, the array may converge to one set of numbers. For example, when given the following array, we can perform iterations of this algorithm until the array eventually converges:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | [0] || [1] || [2] || [3] || [4] || [5] || [6] || [7] || [8] || [9] || [10]&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.25 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.125 || 0.25 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0625 || 0.125 || 0.375 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.03125 || 0.0625 || 0.21875 || 0.375 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.015625 || 0.03125 || 0.125 || 0.21875 || 0.453125 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0078125 || 0.015625 || 0.0703125 || 0.125 || 0.2890625 || 0.453125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.00390625 || 0.0078125 || 0.0390625 || 0.0703125 || 0.1796875 || 0.2890625 || 0.5078125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.00390625 || 0.021484375 || 0.0390625 || 0.109375 || 0.1796875 || 0.34375 || 0.5078125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.01171875 || 0.021484375 || 0.0654296875 || 0.109375 || 0.2265625 || 0.34375 || 0.548828125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0 &lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.1 || 0.2 || 0.3 || 0.4 || 0.5 || 0.6 || 0.7 || 0.8 || 0.9 || 1.0 &lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | src || dst&lt;br /&gt;
|-&lt;br /&gt;
| phase 0 || &lt;br /&gt;
|-&lt;br /&gt;
| phase 1 || phase 0&lt;br /&gt;
|-&lt;br /&gt;
| phase 2 || phase 1&lt;br /&gt;
|-&lt;br /&gt;
| phase 3 || phase 2&lt;br /&gt;
|-&lt;br /&gt;
| phase 4 || phase 3&lt;br /&gt;
|-&lt;br /&gt;
| phase 5 || phase 4&lt;br /&gt;
|-&lt;br /&gt;
| phase 6 || phase 5&lt;br /&gt;
|-&lt;br /&gt;
| phase 7 || phase 6&lt;br /&gt;
|-&lt;br /&gt;
| phase 8 || phase 7&lt;br /&gt;
|-&lt;br /&gt;
| phase 9 || phase 8&lt;br /&gt;
|-&lt;br /&gt;
| phase 10 || phase 9&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| || phase N-1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the ends of the array remain the same value from the previous iteration. &lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
X10-like Phasers have been added to Java since JDK7.  We will gain some experience with using Phasers in a parallel for-loop context. Phasers allow us to change the structure of our loops and reduce overhead in the algorithm.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
For more information on the algorithm and how we can use Phasers to make it better, review [https://edge.edx.org/courses/RiceX/COMP322/1T2014R/courseware/a900dd0655384de3b5ef01e508ea09d7/6349730bb2a149a0b33fa23db7afddee/13 Topic 3.5] from the RiceX course.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
&amp;lt;youtube&amp;gt;Ixprqn_Xhf4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Java Phaser==&lt;br /&gt;
&amp;lt;youtube&amp;gt;whPAPmylbEU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Reference_Page#Phasers|Check out the reference page on phasers]]&lt;br /&gt;
&lt;br /&gt;
[http://www.baeldung.com/java-phaser Guide to the Java Phaser]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html class Phaser]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#bulkRegister-int- bulkRegister] (also see instructions on [[#ForkLoopWithPhaserIterativeAverager |ForkLoopWithPhaserIterativeAverager]])&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#arriveAndAwaitAdvance-- arriveAndAwaitAdvance]&lt;br /&gt;
&lt;br /&gt;
{{warning | Our use of the forall loop with Phasers does not accurately convey their finicky nature.  More than other features, Phasers seem to require more care to get performance improvements. }}&lt;br /&gt;
&lt;br /&gt;
==Ranges==&lt;br /&gt;
[[Ranges|Ranges (Previous Exercise To Use)]]&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;0c2PZ-ARDDE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
==IterativeAveragingRanges==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|slice|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
===slice===&lt;br /&gt;
&amp;lt;youtube&amp;gt;_taVHpi1Hpg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each IterativeAverager should slice the data into ranges.  The constructor for each IterativeAverager is passed the number of slices to create.&lt;br /&gt;
&lt;br /&gt;
{{Tip| Use Ranges.slice(min, maxExclusive, numSlices)}}&lt;br /&gt;
&lt;br /&gt;
If you are to create 3 slices an array of length 11:&lt;br /&gt;
&lt;br /&gt;
[[File:IterativeAveraging.png|600px]]&lt;br /&gt;
&lt;br /&gt;
the slices should have these properties:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|||sliceID||minInclusive||maxExclusive&lt;br /&gt;
|-&lt;br /&gt;
|sliceA||0||1||4&lt;br /&gt;
|-&lt;br /&gt;
|sliceB||1||4||7&lt;br /&gt;
|-&lt;br /&gt;
|sliceC||2||7||10&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==IterativeAveragingDoubleBuffer==&lt;br /&gt;
===constructor===&lt;br /&gt;
&lt;br /&gt;
Hang onto (in member a member variable) the original double array and initialize (and hang onto) two buffers of the same length. Make sure to copy over the original data's 0th and last index's value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	private final double[] originalData;&lt;br /&gt;
	private final double[] a;&lt;br /&gt;
	private final double[] b;&lt;br /&gt;
&lt;br /&gt;
	public IterativeAveragingDoubleBuffer(double[] originalData) {&lt;br /&gt;
		Objects.requireNonNull(originalData);&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===srcForPhase(phaseIndex)===&lt;br /&gt;
This returns the buffer to read from for a given phaseIndex.  For 0, this should be the original data.  For all other phaseIndices, it should alternate (odd and even) between the a and b buffers.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
	 &amp;lt;table&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 0:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;originalData&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 1:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 2:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 3:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 4:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 5:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 6:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;...&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;&amp;amp;nbsp;&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	public double[] srcForPhase(int phaseIndex) {&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dstForPhase===&lt;br /&gt;
This returns the buffer to write to a given phaseIndex.  This should be the opposite buffer of what srcForPhase returned.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
	 &amp;lt;table&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 0:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 1:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 2:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 3:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 4:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 5:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 6:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;...&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;&amp;amp;nbsp;&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	public double[] dstForPhase(int phaseIndex) {&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==LoopOfForkLoopsIterativeAverager==&lt;br /&gt;
{{CodeToImplement|LoopOfForkLoopsIterativeAverager|sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
For this method, you should not be using Phasers. Instead, implement a parallel version of the [[Sequential_Iterative_Averager_Assignment|Sequential Iterative Averaging warm up]]. Make use of the PhasableDoubleArrays class. The return value should be the current version of the array after it has gone through the number of iterations passed in the method.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;sequential loop&lt;br /&gt;
    parallel loop&lt;br /&gt;
        work&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is the work for each task?&lt;br /&gt;
&lt;br /&gt;
[[File:LoopOfForkLoops_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==X10PhasedForkLoopIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|X10PhasedForkLoopIterativeAverager|x10&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;x10 phased parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:X10PhasedForkLoop_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==ForkLoopWithPhaserIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ForkLoopWithPhaserIterativeAverager|phaserCreator&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
Before you get started on this, make sure you review the [[Iterative Averaging#Background | Background section]] in order to understand how to utilize Phasers (it will look different than the X10 implementation). This time, we will use Phasers to create a parallel version of the algorithm that has less overhead. Here are a few notes to keep in mind when working on this assignment:&lt;br /&gt;
*Think carefully as to how the loops in this version will be structured. Drawing out the computation graph may help&lt;br /&gt;
*Bulk registering a Phaser indicates how many times &amp;lt;code&amp;gt;phaser.arriveAndAwaitAdvance()&amp;lt;/code&amp;gt; needs to be called before any of the threads are able to move on. In other words, it equals to how many threads need to arive at the checkpoint before moving on. Make sure to register the right number!&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
*You also bulk register the SwappableDoubleArrays when instantiating it. Similar to Phasers, the integer passed through in the constructor tells the object how many times &amp;lt;code&amp;gt;swap()&amp;lt;/code&amp;gt; needs to be called before the source and destination arrays are actually swapped.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;create phaser&lt;br /&gt;
register phaser for each task&lt;br /&gt;
parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:ForkLoopWithPhaser_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|__RigidIterativeAveragingTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
: {{TestSuite|_IterativeAveragingRangesTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_IterativeAveragingDoubleBufferTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_LoopOfForkLoopsIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_X10PhasedForkLoopIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_ForkLoopWithPhaserIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5440</id>
		<title>Iterative Averaging</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5440"/>
		<updated>2026-04-14T13:39:42Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* srcForPhase(phaseIndex) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Iterative Averaging is the process of updating an array to so that each index becomes the average of the indices one before and one after it. After repeating this for many iterations, the array may converge to one set of numbers. For example, when given the following array, we can perform iterations of this algorithm until the array eventually converges:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | [0] || [1] || [2] || [3] || [4] || [5] || [6] || [7] || [8] || [9] || [10]&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.25 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.125 || 0.25 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0625 || 0.125 || 0.375 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.03125 || 0.0625 || 0.21875 || 0.375 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.015625 || 0.03125 || 0.125 || 0.21875 || 0.453125 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0078125 || 0.015625 || 0.0703125 || 0.125 || 0.2890625 || 0.453125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.00390625 || 0.0078125 || 0.0390625 || 0.0703125 || 0.1796875 || 0.2890625 || 0.5078125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.00390625 || 0.021484375 || 0.0390625 || 0.109375 || 0.1796875 || 0.34375 || 0.5078125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.01171875 || 0.021484375 || 0.0654296875 || 0.109375 || 0.2265625 || 0.34375 || 0.548828125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0 &lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.1 || 0.2 || 0.3 || 0.4 || 0.5 || 0.6 || 0.7 || 0.8 || 0.9 || 1.0 &lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | src || dst&lt;br /&gt;
|-&lt;br /&gt;
| phase 0 || &lt;br /&gt;
|-&lt;br /&gt;
| phase 1 || phase 0&lt;br /&gt;
|-&lt;br /&gt;
| phase 2 || phase 1&lt;br /&gt;
|-&lt;br /&gt;
| phase 3 || phase 2&lt;br /&gt;
|-&lt;br /&gt;
| phase 4 || phase 3&lt;br /&gt;
|-&lt;br /&gt;
| phase 5 || phase 4&lt;br /&gt;
|-&lt;br /&gt;
| phase 6 || phase 5&lt;br /&gt;
|-&lt;br /&gt;
| phase 7 || phase 6&lt;br /&gt;
|-&lt;br /&gt;
| phase 8 || phase 7&lt;br /&gt;
|-&lt;br /&gt;
| phase 9 || phase 8&lt;br /&gt;
|-&lt;br /&gt;
| phase 10 || phase 9&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| || phase N-1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the ends of the array remain the same value from the previous iteration. &lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
X10-like Phasers have been added to Java since JDK7.  We will gain some experience with using Phasers in a parallel for-loop context. Phasers allow us to change the structure of our loops and reduce overhead in the algorithm.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
For more information on the algorithm and how we can use Phasers to make it better, review [https://edge.edx.org/courses/RiceX/COMP322/1T2014R/courseware/a900dd0655384de3b5ef01e508ea09d7/6349730bb2a149a0b33fa23db7afddee/13 Topic 3.5] from the RiceX course.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
&amp;lt;youtube&amp;gt;Ixprqn_Xhf4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Java Phaser==&lt;br /&gt;
&amp;lt;youtube&amp;gt;whPAPmylbEU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Reference_Page#Phasers|Check out the reference page on phasers]]&lt;br /&gt;
&lt;br /&gt;
[http://www.baeldung.com/java-phaser Guide to the Java Phaser]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html class Phaser]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#bulkRegister-int- bulkRegister] (also see instructions on [[#ForkLoopWithPhaserIterativeAverager |ForkLoopWithPhaserIterativeAverager]])&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#arriveAndAwaitAdvance-- arriveAndAwaitAdvance]&lt;br /&gt;
&lt;br /&gt;
{{warning | Our use of the forall loop with Phasers does not accurately convey their finicky nature.  More than other features, Phasers seem to require more care to get performance improvements. }}&lt;br /&gt;
&lt;br /&gt;
==Ranges==&lt;br /&gt;
[[Ranges|Ranges (Previous Exercise To Use)]]&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;0c2PZ-ARDDE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
==IterativeAveragingRanges==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|slice|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
===slice===&lt;br /&gt;
&amp;lt;youtube&amp;gt;_taVHpi1Hpg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each IterativeAverager should slice the data into ranges.  The constructor for each IterativeAverager is passed the number of slices to create.&lt;br /&gt;
&lt;br /&gt;
{{Tip| Use Ranges.slice(min, maxExclusive, numSlices)}}&lt;br /&gt;
&lt;br /&gt;
If you are to create 3 slices an array of length 11:&lt;br /&gt;
&lt;br /&gt;
[[File:IterativeAveraging.png|600px]]&lt;br /&gt;
&lt;br /&gt;
the slices should have these properties:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|||sliceID||minInclusive||maxExclusive&lt;br /&gt;
|-&lt;br /&gt;
|sliceA||0||1||4&lt;br /&gt;
|-&lt;br /&gt;
|sliceB||1||4||7&lt;br /&gt;
|-&lt;br /&gt;
|sliceC||2||7||10&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==IterativeAveragingDoubleBuffer==&lt;br /&gt;
===constructor===&lt;br /&gt;
&lt;br /&gt;
Hang onto (in member a member variable) the original double array and initialize (and hang onto) two buffers of the same length. Make sure to copy over the original data's 0th and last index's value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	private final double[] originalData;&lt;br /&gt;
	private final double[] a;&lt;br /&gt;
	private final double[] b;&lt;br /&gt;
&lt;br /&gt;
	public IterativeAveragingDoubleBuffer(double[] originalData) {&lt;br /&gt;
		Objects.requireNonNull(originalData);&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===srcForPhase(phaseIndex)===&lt;br /&gt;
This returns the buffer to read from for a given phaseIndex.  For 0, this should be the original data.  For all other phaseIndices, it should alternate (odd and even) between the a and b buffers.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
	 &amp;lt;table&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 0:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;originalData&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 1:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 2:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 3:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 4:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 5:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phaseIndex 6:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;...&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;&amp;amp;nbsp;&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	public double[] srcForPhase(int phaseIndex) {&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dstForPhase===&lt;br /&gt;
This returns the buffer to write to a given phaseIndex.  This should be the opposite buffer of what srcForPhase returned.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
	 &amp;lt;table&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 0:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 1:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 2:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 3:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 4:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 5:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 6:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;...&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;&amp;amp;nbsp;&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	public double[] dstForPhase(int phaseIndex) {&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==LoopOfForkLoopsIterativeAverager==&lt;br /&gt;
{{CodeToImplement|LoopOfForkLoopsIterativeAverager|sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
For this method, you should not be using Phasers. Instead, implement a parallel version of the [[Sequential_Iterative_Averager_Assignment|Sequential Iterative Averaging warm up]]. Make use of the PhasableDoubleArrays class. The return value should be the current version of the array after it has gone through the number of iterations passed in the method.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;sequential loop&lt;br /&gt;
    parallel loop&lt;br /&gt;
        work&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is the work for each task?&lt;br /&gt;
&lt;br /&gt;
[[File:LoopOfForkLoops_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==X10PhasedForkLoopIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|X10PhasedForkLoopIterativeAverager|x10&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;x10 phased parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:X10PhasedForkLoop_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==ForkLoopWithPhaserIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ForkLoopWithPhaserIterativeAverager|phaserCreator&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
Before you get started on this, make sure you review the [[Iterative Averaging#Background | Background section]] in order to understand how to utilize Phasers (it will look different than the X10 implementation). This time, we will use Phasers to create a parallel version of the algorithm that has less overhead. Here are a few notes to keep in mind when working on this assignment:&lt;br /&gt;
*Think carefully as to how the loops in this version will be structured. Drawing out the computation graph may help&lt;br /&gt;
*Bulk registering a Phaser indicates how many times &amp;lt;code&amp;gt;phaser.arriveAndAwaitAdvance()&amp;lt;/code&amp;gt; needs to be called before any of the threads are able to move on. In other words, it equals to how many threads need to arive at the checkpoint before moving on. Make sure to register the right number!&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
*You also bulk register the SwappableDoubleArrays when instantiating it. Similar to Phasers, the integer passed through in the constructor tells the object how many times &amp;lt;code&amp;gt;swap()&amp;lt;/code&amp;gt; needs to be called before the source and destination arrays are actually swapped.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;create phaser&lt;br /&gt;
register phaser for each task&lt;br /&gt;
parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:ForkLoopWithPhaser_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|__RigidIterativeAveragingTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
: {{TestSuite|_IterativeAveragingRangesTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_IterativeAveragingDoubleBufferTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_LoopOfForkLoopsIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_X10PhasedForkLoopIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_ForkLoopWithPhaserIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5439</id>
		<title>Iterative Averaging</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5439"/>
		<updated>2026-04-14T13:39:20Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* srcForPhase(phaseIndex) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Iterative Averaging is the process of updating an array to so that each index becomes the average of the indices one before and one after it. After repeating this for many iterations, the array may converge to one set of numbers. For example, when given the following array, we can perform iterations of this algorithm until the array eventually converges:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | [0] || [1] || [2] || [3] || [4] || [5] || [6] || [7] || [8] || [9] || [10]&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.25 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.125 || 0.25 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0625 || 0.125 || 0.375 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.03125 || 0.0625 || 0.21875 || 0.375 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.015625 || 0.03125 || 0.125 || 0.21875 || 0.453125 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0078125 || 0.015625 || 0.0703125 || 0.125 || 0.2890625 || 0.453125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.00390625 || 0.0078125 || 0.0390625 || 0.0703125 || 0.1796875 || 0.2890625 || 0.5078125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.00390625 || 0.021484375 || 0.0390625 || 0.109375 || 0.1796875 || 0.34375 || 0.5078125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.01171875 || 0.021484375 || 0.0654296875 || 0.109375 || 0.2265625 || 0.34375 || 0.548828125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0 &lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.1 || 0.2 || 0.3 || 0.4 || 0.5 || 0.6 || 0.7 || 0.8 || 0.9 || 1.0 &lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | src || dst&lt;br /&gt;
|-&lt;br /&gt;
| phase 0 || &lt;br /&gt;
|-&lt;br /&gt;
| phase 1 || phase 0&lt;br /&gt;
|-&lt;br /&gt;
| phase 2 || phase 1&lt;br /&gt;
|-&lt;br /&gt;
| phase 3 || phase 2&lt;br /&gt;
|-&lt;br /&gt;
| phase 4 || phase 3&lt;br /&gt;
|-&lt;br /&gt;
| phase 5 || phase 4&lt;br /&gt;
|-&lt;br /&gt;
| phase 6 || phase 5&lt;br /&gt;
|-&lt;br /&gt;
| phase 7 || phase 6&lt;br /&gt;
|-&lt;br /&gt;
| phase 8 || phase 7&lt;br /&gt;
|-&lt;br /&gt;
| phase 9 || phase 8&lt;br /&gt;
|-&lt;br /&gt;
| phase 10 || phase 9&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| || phase N-1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the ends of the array remain the same value from the previous iteration. &lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
X10-like Phasers have been added to Java since JDK7.  We will gain some experience with using Phasers in a parallel for-loop context. Phasers allow us to change the structure of our loops and reduce overhead in the algorithm.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
For more information on the algorithm and how we can use Phasers to make it better, review [https://edge.edx.org/courses/RiceX/COMP322/1T2014R/courseware/a900dd0655384de3b5ef01e508ea09d7/6349730bb2a149a0b33fa23db7afddee/13 Topic 3.5] from the RiceX course.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
&amp;lt;youtube&amp;gt;Ixprqn_Xhf4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Java Phaser==&lt;br /&gt;
&amp;lt;youtube&amp;gt;whPAPmylbEU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Reference_Page#Phasers|Check out the reference page on phasers]]&lt;br /&gt;
&lt;br /&gt;
[http://www.baeldung.com/java-phaser Guide to the Java Phaser]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html class Phaser]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#bulkRegister-int- bulkRegister] (also see instructions on [[#ForkLoopWithPhaserIterativeAverager |ForkLoopWithPhaserIterativeAverager]])&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#arriveAndAwaitAdvance-- arriveAndAwaitAdvance]&lt;br /&gt;
&lt;br /&gt;
{{warning | Our use of the forall loop with Phasers does not accurately convey their finicky nature.  More than other features, Phasers seem to require more care to get performance improvements. }}&lt;br /&gt;
&lt;br /&gt;
==Ranges==&lt;br /&gt;
[[Ranges|Ranges (Previous Exercise To Use)]]&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;0c2PZ-ARDDE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
==IterativeAveragingRanges==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|slice|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
===slice===&lt;br /&gt;
&amp;lt;youtube&amp;gt;_taVHpi1Hpg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each IterativeAverager should slice the data into ranges.  The constructor for each IterativeAverager is passed the number of slices to create.&lt;br /&gt;
&lt;br /&gt;
{{Tip| Use Ranges.slice(min, maxExclusive, numSlices)}}&lt;br /&gt;
&lt;br /&gt;
If you are to create 3 slices an array of length 11:&lt;br /&gt;
&lt;br /&gt;
[[File:IterativeAveraging.png|600px]]&lt;br /&gt;
&lt;br /&gt;
the slices should have these properties:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|||sliceID||minInclusive||maxExclusive&lt;br /&gt;
|-&lt;br /&gt;
|sliceA||0||1||4&lt;br /&gt;
|-&lt;br /&gt;
|sliceB||1||4||7&lt;br /&gt;
|-&lt;br /&gt;
|sliceC||2||7||10&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==IterativeAveragingDoubleBuffer==&lt;br /&gt;
===constructor===&lt;br /&gt;
&lt;br /&gt;
Hang onto (in member a member variable) the original double array and initialize (and hang onto) two buffers of the same length. Make sure to copy over the original data's 0th and last index's value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	private final double[] originalData;&lt;br /&gt;
	private final double[] a;&lt;br /&gt;
	private final double[] b;&lt;br /&gt;
&lt;br /&gt;
	public IterativeAveragingDoubleBuffer(double[] originalData) {&lt;br /&gt;
		Objects.requireNonNull(originalData);&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===srcForPhase(phaseIndex)===&lt;br /&gt;
This returns the buffer to read from for a given phaseIndex.  For 0, this should be the original data.  For all other phaseIndices, it should alternate (odd and even) between the a and b buffers.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
	 &amp;lt;table&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 0:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;originalData&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 1:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 2:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 3:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 4:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 5:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 6:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;...&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;&amp;amp;nbsp;&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	public double[] srcForPhase(int phaseIndex) {&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dstForPhase===&lt;br /&gt;
This returns the buffer to write to a given phaseIndex.  This should be the opposite buffer of what srcForPhase returned.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
	 &amp;lt;table&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 0:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 1:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 2:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 3:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 4:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 5:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 6:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;...&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;&amp;amp;nbsp;&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	public double[] dstForPhase(int phaseIndex) {&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==LoopOfForkLoopsIterativeAverager==&lt;br /&gt;
{{CodeToImplement|LoopOfForkLoopsIterativeAverager|sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
For this method, you should not be using Phasers. Instead, implement a parallel version of the [[Sequential_Iterative_Averager_Assignment|Sequential Iterative Averaging warm up]]. Make use of the PhasableDoubleArrays class. The return value should be the current version of the array after it has gone through the number of iterations passed in the method.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;sequential loop&lt;br /&gt;
    parallel loop&lt;br /&gt;
        work&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is the work for each task?&lt;br /&gt;
&lt;br /&gt;
[[File:LoopOfForkLoops_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==X10PhasedForkLoopIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|X10PhasedForkLoopIterativeAverager|x10&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;x10 phased parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:X10PhasedForkLoop_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==ForkLoopWithPhaserIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ForkLoopWithPhaserIterativeAverager|phaserCreator&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
Before you get started on this, make sure you review the [[Iterative Averaging#Background | Background section]] in order to understand how to utilize Phasers (it will look different than the X10 implementation). This time, we will use Phasers to create a parallel version of the algorithm that has less overhead. Here are a few notes to keep in mind when working on this assignment:&lt;br /&gt;
*Think carefully as to how the loops in this version will be structured. Drawing out the computation graph may help&lt;br /&gt;
*Bulk registering a Phaser indicates how many times &amp;lt;code&amp;gt;phaser.arriveAndAwaitAdvance()&amp;lt;/code&amp;gt; needs to be called before any of the threads are able to move on. In other words, it equals to how many threads need to arive at the checkpoint before moving on. Make sure to register the right number!&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
*You also bulk register the SwappableDoubleArrays when instantiating it. Similar to Phasers, the integer passed through in the constructor tells the object how many times &amp;lt;code&amp;gt;swap()&amp;lt;/code&amp;gt; needs to be called before the source and destination arrays are actually swapped.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;create phaser&lt;br /&gt;
register phaser for each task&lt;br /&gt;
parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:ForkLoopWithPhaser_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|__RigidIterativeAveragingTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
: {{TestSuite|_IterativeAveragingRangesTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_IterativeAveragingDoubleBufferTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_LoopOfForkLoopsIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_X10PhasedForkLoopIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_ForkLoopWithPhaserIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5438</id>
		<title>Iterative Averaging</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5438"/>
		<updated>2026-04-14T13:38:46Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* dstForPhase */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Iterative Averaging is the process of updating an array to so that each index becomes the average of the indices one before and one after it. After repeating this for many iterations, the array may converge to one set of numbers. For example, when given the following array, we can perform iterations of this algorithm until the array eventually converges:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | [0] || [1] || [2] || [3] || [4] || [5] || [6] || [7] || [8] || [9] || [10]&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.25 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.125 || 0.25 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0625 || 0.125 || 0.375 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.03125 || 0.0625 || 0.21875 || 0.375 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.015625 || 0.03125 || 0.125 || 0.21875 || 0.453125 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0078125 || 0.015625 || 0.0703125 || 0.125 || 0.2890625 || 0.453125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.00390625 || 0.0078125 || 0.0390625 || 0.0703125 || 0.1796875 || 0.2890625 || 0.5078125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.00390625 || 0.021484375 || 0.0390625 || 0.109375 || 0.1796875 || 0.34375 || 0.5078125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.01171875 || 0.021484375 || 0.0654296875 || 0.109375 || 0.2265625 || 0.34375 || 0.548828125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0 &lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.1 || 0.2 || 0.3 || 0.4 || 0.5 || 0.6 || 0.7 || 0.8 || 0.9 || 1.0 &lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | src || dst&lt;br /&gt;
|-&lt;br /&gt;
| phase 0 || &lt;br /&gt;
|-&lt;br /&gt;
| phase 1 || phase 0&lt;br /&gt;
|-&lt;br /&gt;
| phase 2 || phase 1&lt;br /&gt;
|-&lt;br /&gt;
| phase 3 || phase 2&lt;br /&gt;
|-&lt;br /&gt;
| phase 4 || phase 3&lt;br /&gt;
|-&lt;br /&gt;
| phase 5 || phase 4&lt;br /&gt;
|-&lt;br /&gt;
| phase 6 || phase 5&lt;br /&gt;
|-&lt;br /&gt;
| phase 7 || phase 6&lt;br /&gt;
|-&lt;br /&gt;
| phase 8 || phase 7&lt;br /&gt;
|-&lt;br /&gt;
| phase 9 || phase 8&lt;br /&gt;
|-&lt;br /&gt;
| phase 10 || phase 9&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| || phase N-1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the ends of the array remain the same value from the previous iteration. &lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
X10-like Phasers have been added to Java since JDK7.  We will gain some experience with using Phasers in a parallel for-loop context. Phasers allow us to change the structure of our loops and reduce overhead in the algorithm.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
For more information on the algorithm and how we can use Phasers to make it better, review [https://edge.edx.org/courses/RiceX/COMP322/1T2014R/courseware/a900dd0655384de3b5ef01e508ea09d7/6349730bb2a149a0b33fa23db7afddee/13 Topic 3.5] from the RiceX course.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
&amp;lt;youtube&amp;gt;Ixprqn_Xhf4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Java Phaser==&lt;br /&gt;
&amp;lt;youtube&amp;gt;whPAPmylbEU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Reference_Page#Phasers|Check out the reference page on phasers]]&lt;br /&gt;
&lt;br /&gt;
[http://www.baeldung.com/java-phaser Guide to the Java Phaser]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html class Phaser]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#bulkRegister-int- bulkRegister] (also see instructions on [[#ForkLoopWithPhaserIterativeAverager |ForkLoopWithPhaserIterativeAverager]])&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#arriveAndAwaitAdvance-- arriveAndAwaitAdvance]&lt;br /&gt;
&lt;br /&gt;
{{warning | Our use of the forall loop with Phasers does not accurately convey their finicky nature.  More than other features, Phasers seem to require more care to get performance improvements. }}&lt;br /&gt;
&lt;br /&gt;
==Ranges==&lt;br /&gt;
[[Ranges|Ranges (Previous Exercise To Use)]]&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;0c2PZ-ARDDE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
==IterativeAveragingRanges==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|slice|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
===slice===&lt;br /&gt;
&amp;lt;youtube&amp;gt;_taVHpi1Hpg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each IterativeAverager should slice the data into ranges.  The constructor for each IterativeAverager is passed the number of slices to create.&lt;br /&gt;
&lt;br /&gt;
{{Tip| Use Ranges.slice(min, maxExclusive, numSlices)}}&lt;br /&gt;
&lt;br /&gt;
If you are to create 3 slices an array of length 11:&lt;br /&gt;
&lt;br /&gt;
[[File:IterativeAveraging.png|600px]]&lt;br /&gt;
&lt;br /&gt;
the slices should have these properties:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|||sliceID||minInclusive||maxExclusive&lt;br /&gt;
|-&lt;br /&gt;
|sliceA||0||1||4&lt;br /&gt;
|-&lt;br /&gt;
|sliceB||1||4||7&lt;br /&gt;
|-&lt;br /&gt;
|sliceC||2||7||10&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==IterativeAveragingDoubleBuffer==&lt;br /&gt;
===constructor===&lt;br /&gt;
&lt;br /&gt;
Hang onto (in member a member variable) the original double array and initialize (and hang onto) two buffers of the same length. Make sure to copy over the original data's 0th and last index's value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	private final double[] originalData;&lt;br /&gt;
	private final double[] a;&lt;br /&gt;
	private final double[] b;&lt;br /&gt;
&lt;br /&gt;
	public IterativeAveragingDoubleBuffer(double[] originalData) {&lt;br /&gt;
		Objects.requireNonNull(originalData);&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===srcForPhase(phaseIndex)===&lt;br /&gt;
This returns the buffer to read from for a given phaseIndex.  For 0, this should be the original data.  For all other phaseIndices, it should alternate (odd and even) between the a and b buffers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	public double[] srcForPhase(int phaseIndex) {&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dstForPhase===&lt;br /&gt;
This returns the buffer to write to a given phaseIndex.  This should be the opposite buffer of what srcForPhase returned.&lt;br /&gt;
&lt;br /&gt;
For example: &lt;br /&gt;
&lt;br /&gt;
	 &amp;lt;table&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 0:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 1:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 2:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 3:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 4:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 5:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;b&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;phase 6:&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;a&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;tr&amp;gt; &amp;lt;td&amp;gt;...&amp;lt;/td&amp;gt; &amp;lt;td&amp;gt;&amp;amp;nbsp;&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;
	 &amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	public double[] dstForPhase(int phaseIndex) {&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==LoopOfForkLoopsIterativeAverager==&lt;br /&gt;
{{CodeToImplement|LoopOfForkLoopsIterativeAverager|sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
For this method, you should not be using Phasers. Instead, implement a parallel version of the [[Sequential_Iterative_Averager_Assignment|Sequential Iterative Averaging warm up]]. Make use of the PhasableDoubleArrays class. The return value should be the current version of the array after it has gone through the number of iterations passed in the method.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;sequential loop&lt;br /&gt;
    parallel loop&lt;br /&gt;
        work&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is the work for each task?&lt;br /&gt;
&lt;br /&gt;
[[File:LoopOfForkLoops_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==X10PhasedForkLoopIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|X10PhasedForkLoopIterativeAverager|x10&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;x10 phased parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:X10PhasedForkLoop_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==ForkLoopWithPhaserIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ForkLoopWithPhaserIterativeAverager|phaserCreator&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
Before you get started on this, make sure you review the [[Iterative Averaging#Background | Background section]] in order to understand how to utilize Phasers (it will look different than the X10 implementation). This time, we will use Phasers to create a parallel version of the algorithm that has less overhead. Here are a few notes to keep in mind when working on this assignment:&lt;br /&gt;
*Think carefully as to how the loops in this version will be structured. Drawing out the computation graph may help&lt;br /&gt;
*Bulk registering a Phaser indicates how many times &amp;lt;code&amp;gt;phaser.arriveAndAwaitAdvance()&amp;lt;/code&amp;gt; needs to be called before any of the threads are able to move on. In other words, it equals to how many threads need to arive at the checkpoint before moving on. Make sure to register the right number!&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
*You also bulk register the SwappableDoubleArrays when instantiating it. Similar to Phasers, the integer passed through in the constructor tells the object how many times &amp;lt;code&amp;gt;swap()&amp;lt;/code&amp;gt; needs to be called before the source and destination arrays are actually swapped.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;create phaser&lt;br /&gt;
register phaser for each task&lt;br /&gt;
parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:ForkLoopWithPhaser_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|__RigidIterativeAveragingTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
: {{TestSuite|_IterativeAveragingRangesTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_IterativeAveragingDoubleBufferTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_LoopOfForkLoopsIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_X10PhasedForkLoopIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_ForkLoopWithPhaserIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5437</id>
		<title>Iterative Averaging</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5437"/>
		<updated>2026-04-14T13:36:00Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* srcForPhase */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Iterative Averaging is the process of updating an array to so that each index becomes the average of the indices one before and one after it. After repeating this for many iterations, the array may converge to one set of numbers. For example, when given the following array, we can perform iterations of this algorithm until the array eventually converges:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | [0] || [1] || [2] || [3] || [4] || [5] || [6] || [7] || [8] || [9] || [10]&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.25 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.125 || 0.25 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0625 || 0.125 || 0.375 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.03125 || 0.0625 || 0.21875 || 0.375 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.015625 || 0.03125 || 0.125 || 0.21875 || 0.453125 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0078125 || 0.015625 || 0.0703125 || 0.125 || 0.2890625 || 0.453125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.00390625 || 0.0078125 || 0.0390625 || 0.0703125 || 0.1796875 || 0.2890625 || 0.5078125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.00390625 || 0.021484375 || 0.0390625 || 0.109375 || 0.1796875 || 0.34375 || 0.5078125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.01171875 || 0.021484375 || 0.0654296875 || 0.109375 || 0.2265625 || 0.34375 || 0.548828125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0 &lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.1 || 0.2 || 0.3 || 0.4 || 0.5 || 0.6 || 0.7 || 0.8 || 0.9 || 1.0 &lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | src || dst&lt;br /&gt;
|-&lt;br /&gt;
| phase 0 || &lt;br /&gt;
|-&lt;br /&gt;
| phase 1 || phase 0&lt;br /&gt;
|-&lt;br /&gt;
| phase 2 || phase 1&lt;br /&gt;
|-&lt;br /&gt;
| phase 3 || phase 2&lt;br /&gt;
|-&lt;br /&gt;
| phase 4 || phase 3&lt;br /&gt;
|-&lt;br /&gt;
| phase 5 || phase 4&lt;br /&gt;
|-&lt;br /&gt;
| phase 6 || phase 5&lt;br /&gt;
|-&lt;br /&gt;
| phase 7 || phase 6&lt;br /&gt;
|-&lt;br /&gt;
| phase 8 || phase 7&lt;br /&gt;
|-&lt;br /&gt;
| phase 9 || phase 8&lt;br /&gt;
|-&lt;br /&gt;
| phase 10 || phase 9&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| || phase N-1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the ends of the array remain the same value from the previous iteration. &lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
X10-like Phasers have been added to Java since JDK7.  We will gain some experience with using Phasers in a parallel for-loop context. Phasers allow us to change the structure of our loops and reduce overhead in the algorithm.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
For more information on the algorithm and how we can use Phasers to make it better, review [https://edge.edx.org/courses/RiceX/COMP322/1T2014R/courseware/a900dd0655384de3b5ef01e508ea09d7/6349730bb2a149a0b33fa23db7afddee/13 Topic 3.5] from the RiceX course.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
&amp;lt;youtube&amp;gt;Ixprqn_Xhf4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Java Phaser==&lt;br /&gt;
&amp;lt;youtube&amp;gt;whPAPmylbEU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Reference_Page#Phasers|Check out the reference page on phasers]]&lt;br /&gt;
&lt;br /&gt;
[http://www.baeldung.com/java-phaser Guide to the Java Phaser]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html class Phaser]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#bulkRegister-int- bulkRegister] (also see instructions on [[#ForkLoopWithPhaserIterativeAverager |ForkLoopWithPhaserIterativeAverager]])&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#arriveAndAwaitAdvance-- arriveAndAwaitAdvance]&lt;br /&gt;
&lt;br /&gt;
{{warning | Our use of the forall loop with Phasers does not accurately convey their finicky nature.  More than other features, Phasers seem to require more care to get performance improvements. }}&lt;br /&gt;
&lt;br /&gt;
==Ranges==&lt;br /&gt;
[[Ranges|Ranges (Previous Exercise To Use)]]&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;0c2PZ-ARDDE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
==IterativeAveragingRanges==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|slice|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
===slice===&lt;br /&gt;
&amp;lt;youtube&amp;gt;_taVHpi1Hpg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each IterativeAverager should slice the data into ranges.  The constructor for each IterativeAverager is passed the number of slices to create.&lt;br /&gt;
&lt;br /&gt;
{{Tip| Use Ranges.slice(min, maxExclusive, numSlices)}}&lt;br /&gt;
&lt;br /&gt;
If you are to create 3 slices an array of length 11:&lt;br /&gt;
&lt;br /&gt;
[[File:IterativeAveraging.png|600px]]&lt;br /&gt;
&lt;br /&gt;
the slices should have these properties:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|||sliceID||minInclusive||maxExclusive&lt;br /&gt;
|-&lt;br /&gt;
|sliceA||0||1||4&lt;br /&gt;
|-&lt;br /&gt;
|sliceB||1||4||7&lt;br /&gt;
|-&lt;br /&gt;
|sliceC||2||7||10&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==IterativeAveragingDoubleBuffer==&lt;br /&gt;
===constructor===&lt;br /&gt;
&lt;br /&gt;
Hang onto (in member a member variable) the original double array and initialize (and hang onto) two buffers of the same length. Make sure to copy over the original data's 0th and last index's value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	private final double[] originalData;&lt;br /&gt;
	private final double[] a;&lt;br /&gt;
	private final double[] b;&lt;br /&gt;
&lt;br /&gt;
	public IterativeAveragingDoubleBuffer(double[] originalData) {&lt;br /&gt;
		Objects.requireNonNull(originalData);&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===srcForPhase(phaseIndex)===&lt;br /&gt;
This returns the buffer to read from for a given phaseIndex.  For 0, this should be the original data.  For all other phaseIndices, it should alternate (odd and even) between the a and b buffers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	public double[] srcForPhase(int phaseIndex) {&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===dstForPhase===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	public double[] dstForPhase(int phaseIndex) {&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==LoopOfForkLoopsIterativeAverager==&lt;br /&gt;
{{CodeToImplement|LoopOfForkLoopsIterativeAverager|sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
For this method, you should not be using Phasers. Instead, implement a parallel version of the [[Sequential_Iterative_Averager_Assignment|Sequential Iterative Averaging warm up]]. Make use of the PhasableDoubleArrays class. The return value should be the current version of the array after it has gone through the number of iterations passed in the method.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;sequential loop&lt;br /&gt;
    parallel loop&lt;br /&gt;
        work&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is the work for each task?&lt;br /&gt;
&lt;br /&gt;
[[File:LoopOfForkLoops_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==X10PhasedForkLoopIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|X10PhasedForkLoopIterativeAverager|x10&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;x10 phased parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:X10PhasedForkLoop_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==ForkLoopWithPhaserIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ForkLoopWithPhaserIterativeAverager|phaserCreator&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
Before you get started on this, make sure you review the [[Iterative Averaging#Background | Background section]] in order to understand how to utilize Phasers (it will look different than the X10 implementation). This time, we will use Phasers to create a parallel version of the algorithm that has less overhead. Here are a few notes to keep in mind when working on this assignment:&lt;br /&gt;
*Think carefully as to how the loops in this version will be structured. Drawing out the computation graph may help&lt;br /&gt;
*Bulk registering a Phaser indicates how many times &amp;lt;code&amp;gt;phaser.arriveAndAwaitAdvance()&amp;lt;/code&amp;gt; needs to be called before any of the threads are able to move on. In other words, it equals to how many threads need to arive at the checkpoint before moving on. Make sure to register the right number!&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
*You also bulk register the SwappableDoubleArrays when instantiating it. Similar to Phasers, the integer passed through in the constructor tells the object how many times &amp;lt;code&amp;gt;swap()&amp;lt;/code&amp;gt; needs to be called before the source and destination arrays are actually swapped.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;create phaser&lt;br /&gt;
register phaser for each task&lt;br /&gt;
parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:ForkLoopWithPhaser_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|__RigidIterativeAveragingTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
: {{TestSuite|_IterativeAveragingRangesTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_IterativeAveragingDoubleBufferTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_LoopOfForkLoopsIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_X10PhasedForkLoopIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_ForkLoopWithPhaserIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5436</id>
		<title>Iterative Averaging</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5436"/>
		<updated>2026-04-14T13:33:44Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* IterativeAveragingDoubleBuffer */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Iterative Averaging is the process of updating an array to so that each index becomes the average of the indices one before and one after it. After repeating this for many iterations, the array may converge to one set of numbers. For example, when given the following array, we can perform iterations of this algorithm until the array eventually converges:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | [0] || [1] || [2] || [3] || [4] || [5] || [6] || [7] || [8] || [9] || [10]&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.25 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.125 || 0.25 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0625 || 0.125 || 0.375 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.03125 || 0.0625 || 0.21875 || 0.375 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.015625 || 0.03125 || 0.125 || 0.21875 || 0.453125 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0078125 || 0.015625 || 0.0703125 || 0.125 || 0.2890625 || 0.453125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.00390625 || 0.0078125 || 0.0390625 || 0.0703125 || 0.1796875 || 0.2890625 || 0.5078125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.00390625 || 0.021484375 || 0.0390625 || 0.109375 || 0.1796875 || 0.34375 || 0.5078125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.01171875 || 0.021484375 || 0.0654296875 || 0.109375 || 0.2265625 || 0.34375 || 0.548828125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0 &lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.1 || 0.2 || 0.3 || 0.4 || 0.5 || 0.6 || 0.7 || 0.8 || 0.9 || 1.0 &lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | src || dst&lt;br /&gt;
|-&lt;br /&gt;
| phase 0 || &lt;br /&gt;
|-&lt;br /&gt;
| phase 1 || phase 0&lt;br /&gt;
|-&lt;br /&gt;
| phase 2 || phase 1&lt;br /&gt;
|-&lt;br /&gt;
| phase 3 || phase 2&lt;br /&gt;
|-&lt;br /&gt;
| phase 4 || phase 3&lt;br /&gt;
|-&lt;br /&gt;
| phase 5 || phase 4&lt;br /&gt;
|-&lt;br /&gt;
| phase 6 || phase 5&lt;br /&gt;
|-&lt;br /&gt;
| phase 7 || phase 6&lt;br /&gt;
|-&lt;br /&gt;
| phase 8 || phase 7&lt;br /&gt;
|-&lt;br /&gt;
| phase 9 || phase 8&lt;br /&gt;
|-&lt;br /&gt;
| phase 10 || phase 9&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| || phase N-1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the ends of the array remain the same value from the previous iteration. &lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
X10-like Phasers have been added to Java since JDK7.  We will gain some experience with using Phasers in a parallel for-loop context. Phasers allow us to change the structure of our loops and reduce overhead in the algorithm.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
For more information on the algorithm and how we can use Phasers to make it better, review [https://edge.edx.org/courses/RiceX/COMP322/1T2014R/courseware/a900dd0655384de3b5ef01e508ea09d7/6349730bb2a149a0b33fa23db7afddee/13 Topic 3.5] from the RiceX course.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
&amp;lt;youtube&amp;gt;Ixprqn_Xhf4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Java Phaser==&lt;br /&gt;
&amp;lt;youtube&amp;gt;whPAPmylbEU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Reference_Page#Phasers|Check out the reference page on phasers]]&lt;br /&gt;
&lt;br /&gt;
[http://www.baeldung.com/java-phaser Guide to the Java Phaser]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html class Phaser]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#bulkRegister-int- bulkRegister] (also see instructions on [[#ForkLoopWithPhaserIterativeAverager |ForkLoopWithPhaserIterativeAverager]])&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#arriveAndAwaitAdvance-- arriveAndAwaitAdvance]&lt;br /&gt;
&lt;br /&gt;
{{warning | Our use of the forall loop with Phasers does not accurately convey their finicky nature.  More than other features, Phasers seem to require more care to get performance improvements. }}&lt;br /&gt;
&lt;br /&gt;
==Ranges==&lt;br /&gt;
[[Ranges|Ranges (Previous Exercise To Use)]]&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;0c2PZ-ARDDE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
==IterativeAveragingRanges==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|slice|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
===slice===&lt;br /&gt;
&amp;lt;youtube&amp;gt;_taVHpi1Hpg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each IterativeAverager should slice the data into ranges.  The constructor for each IterativeAverager is passed the number of slices to create.&lt;br /&gt;
&lt;br /&gt;
{{Tip| Use Ranges.slice(min, maxExclusive, numSlices)}}&lt;br /&gt;
&lt;br /&gt;
If you are to create 3 slices an array of length 11:&lt;br /&gt;
&lt;br /&gt;
[[File:IterativeAveraging.png|600px]]&lt;br /&gt;
&lt;br /&gt;
the slices should have these properties:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|||sliceID||minInclusive||maxExclusive&lt;br /&gt;
|-&lt;br /&gt;
|sliceA||0||1||4&lt;br /&gt;
|-&lt;br /&gt;
|sliceB||1||4||7&lt;br /&gt;
|-&lt;br /&gt;
|sliceC||2||7||10&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==IterativeAveragingDoubleBuffer==&lt;br /&gt;
===constructor===&lt;br /&gt;
&lt;br /&gt;
Hang onto (in member a member variable) the original double array and initialize (and hang onto) two buffers of the same length. Make sure to copy over the original data's 0th and last index's value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	private final double[] originalData;&lt;br /&gt;
	private final double[] a;&lt;br /&gt;
	private final double[] b;&lt;br /&gt;
&lt;br /&gt;
	public IterativeAveragingDoubleBuffer(double[] originalData) {&lt;br /&gt;
		Objects.requireNonNull(originalData);&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===srcForPhase===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	public double[] srcForPhase(int phaseIndex) {&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===dstForPhase===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	public double[] dstForPhase(int phaseIndex) {&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==LoopOfForkLoopsIterativeAverager==&lt;br /&gt;
{{CodeToImplement|LoopOfForkLoopsIterativeAverager|sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
For this method, you should not be using Phasers. Instead, implement a parallel version of the [[Sequential_Iterative_Averager_Assignment|Sequential Iterative Averaging warm up]]. Make use of the PhasableDoubleArrays class. The return value should be the current version of the array after it has gone through the number of iterations passed in the method.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;sequential loop&lt;br /&gt;
    parallel loop&lt;br /&gt;
        work&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is the work for each task?&lt;br /&gt;
&lt;br /&gt;
[[File:LoopOfForkLoops_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==X10PhasedForkLoopIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|X10PhasedForkLoopIterativeAverager|x10&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;x10 phased parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:X10PhasedForkLoop_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==ForkLoopWithPhaserIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ForkLoopWithPhaserIterativeAverager|phaserCreator&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
Before you get started on this, make sure you review the [[Iterative Averaging#Background | Background section]] in order to understand how to utilize Phasers (it will look different than the X10 implementation). This time, we will use Phasers to create a parallel version of the algorithm that has less overhead. Here are a few notes to keep in mind when working on this assignment:&lt;br /&gt;
*Think carefully as to how the loops in this version will be structured. Drawing out the computation graph may help&lt;br /&gt;
*Bulk registering a Phaser indicates how many times &amp;lt;code&amp;gt;phaser.arriveAndAwaitAdvance()&amp;lt;/code&amp;gt; needs to be called before any of the threads are able to move on. In other words, it equals to how many threads need to arive at the checkpoint before moving on. Make sure to register the right number!&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
*You also bulk register the SwappableDoubleArrays when instantiating it. Similar to Phasers, the integer passed through in the constructor tells the object how many times &amp;lt;code&amp;gt;swap()&amp;lt;/code&amp;gt; needs to be called before the source and destination arrays are actually swapped.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;create phaser&lt;br /&gt;
register phaser for each task&lt;br /&gt;
parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:ForkLoopWithPhaser_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|__RigidIterativeAveragingTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
: {{TestSuite|_IterativeAveragingRangesTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_IterativeAveragingDoubleBufferTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_LoopOfForkLoopsIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_X10PhasedForkLoopIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_ForkLoopWithPhaserIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5435</id>
		<title>Iterative Averaging</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5435"/>
		<updated>2026-04-14T13:14:03Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* Correctness */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Iterative Averaging is the process of updating an array to so that each index becomes the average of the indices one before and one after it. After repeating this for many iterations, the array may converge to one set of numbers. For example, when given the following array, we can perform iterations of this algorithm until the array eventually converges:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | [0] || [1] || [2] || [3] || [4] || [5] || [6] || [7] || [8] || [9] || [10]&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.25 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.125 || 0.25 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0625 || 0.125 || 0.375 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.03125 || 0.0625 || 0.21875 || 0.375 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.015625 || 0.03125 || 0.125 || 0.21875 || 0.453125 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0078125 || 0.015625 || 0.0703125 || 0.125 || 0.2890625 || 0.453125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.00390625 || 0.0078125 || 0.0390625 || 0.0703125 || 0.1796875 || 0.2890625 || 0.5078125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.00390625 || 0.021484375 || 0.0390625 || 0.109375 || 0.1796875 || 0.34375 || 0.5078125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.01171875 || 0.021484375 || 0.0654296875 || 0.109375 || 0.2265625 || 0.34375 || 0.548828125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0 &lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.1 || 0.2 || 0.3 || 0.4 || 0.5 || 0.6 || 0.7 || 0.8 || 0.9 || 1.0 &lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | src || dst&lt;br /&gt;
|-&lt;br /&gt;
| phase 0 || &lt;br /&gt;
|-&lt;br /&gt;
| phase 1 || phase 0&lt;br /&gt;
|-&lt;br /&gt;
| phase 2 || phase 1&lt;br /&gt;
|-&lt;br /&gt;
| phase 3 || phase 2&lt;br /&gt;
|-&lt;br /&gt;
| phase 4 || phase 3&lt;br /&gt;
|-&lt;br /&gt;
| phase 5 || phase 4&lt;br /&gt;
|-&lt;br /&gt;
| phase 6 || phase 5&lt;br /&gt;
|-&lt;br /&gt;
| phase 7 || phase 6&lt;br /&gt;
|-&lt;br /&gt;
| phase 8 || phase 7&lt;br /&gt;
|-&lt;br /&gt;
| phase 9 || phase 8&lt;br /&gt;
|-&lt;br /&gt;
| phase 10 || phase 9&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| || phase N-1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the ends of the array remain the same value from the previous iteration. &lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
X10-like Phasers have been added to Java since JDK7.  We will gain some experience with using Phasers in a parallel for-loop context. Phasers allow us to change the structure of our loops and reduce overhead in the algorithm.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
For more information on the algorithm and how we can use Phasers to make it better, review [https://edge.edx.org/courses/RiceX/COMP322/1T2014R/courseware/a900dd0655384de3b5ef01e508ea09d7/6349730bb2a149a0b33fa23db7afddee/13 Topic 3.5] from the RiceX course.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
&amp;lt;youtube&amp;gt;Ixprqn_Xhf4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Java Phaser==&lt;br /&gt;
&amp;lt;youtube&amp;gt;whPAPmylbEU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Reference_Page#Phasers|Check out the reference page on phasers]]&lt;br /&gt;
&lt;br /&gt;
[http://www.baeldung.com/java-phaser Guide to the Java Phaser]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html class Phaser]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#bulkRegister-int- bulkRegister] (also see instructions on [[#ForkLoopWithPhaserIterativeAverager |ForkLoopWithPhaserIterativeAverager]])&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#arriveAndAwaitAdvance-- arriveAndAwaitAdvance]&lt;br /&gt;
&lt;br /&gt;
{{warning | Our use of the forall loop with Phasers does not accurately convey their finicky nature.  More than other features, Phasers seem to require more care to get performance improvements. }}&lt;br /&gt;
&lt;br /&gt;
==Ranges==&lt;br /&gt;
[[Ranges|Ranges (Previous Exercise To Use)]]&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;0c2PZ-ARDDE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
==IterativeAveragingRanges==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|slice|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
===slice===&lt;br /&gt;
&amp;lt;youtube&amp;gt;_taVHpi1Hpg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each IterativeAverager should slice the data into ranges.  The constructor for each IterativeAverager is passed the number of slices to create.&lt;br /&gt;
&lt;br /&gt;
{{Tip| Use Ranges.slice(min, maxExclusive, numSlices)}}&lt;br /&gt;
&lt;br /&gt;
If you are to create 3 slices an array of length 11:&lt;br /&gt;
&lt;br /&gt;
[[File:IterativeAveraging.png|600px]]&lt;br /&gt;
&lt;br /&gt;
the slices should have these properties:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|||sliceID||minInclusive||maxExclusive&lt;br /&gt;
|-&lt;br /&gt;
|sliceA||0||1||4&lt;br /&gt;
|-&lt;br /&gt;
|sliceB||1||4||7&lt;br /&gt;
|-&lt;br /&gt;
|sliceC||2||7||10&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==IterativeAveragingDoubleBuffer==&lt;br /&gt;
===constructor===&lt;br /&gt;
&lt;br /&gt;
Hang onto (in member a member variable) the original double array and initialize (and hang onto) two buffers of the same length. Make sure to copy over the original data's 0th and last index's value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	private final double[] originalData;&lt;br /&gt;
	private final double[] a;&lt;br /&gt;
	private final double[] b;&lt;br /&gt;
&lt;br /&gt;
	public IterativeAveragingDoubleBuffer(double[] originalData) {&lt;br /&gt;
		Objects.requireNonNull(originalData);&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===srcForPhase===&lt;br /&gt;
&lt;br /&gt;
===dstForPhase===&lt;br /&gt;
&lt;br /&gt;
==LoopOfForkLoopsIterativeAverager==&lt;br /&gt;
{{CodeToImplement|LoopOfForkLoopsIterativeAverager|sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
For this method, you should not be using Phasers. Instead, implement a parallel version of the [[Sequential_Iterative_Averager_Assignment|Sequential Iterative Averaging warm up]]. Make use of the PhasableDoubleArrays class. The return value should be the current version of the array after it has gone through the number of iterations passed in the method.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;sequential loop&lt;br /&gt;
    parallel loop&lt;br /&gt;
        work&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is the work for each task?&lt;br /&gt;
&lt;br /&gt;
[[File:LoopOfForkLoops_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==X10PhasedForkLoopIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|X10PhasedForkLoopIterativeAverager|x10&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;x10 phased parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:X10PhasedForkLoop_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==ForkLoopWithPhaserIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ForkLoopWithPhaserIterativeAverager|phaserCreator&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
Before you get started on this, make sure you review the [[Iterative Averaging#Background | Background section]] in order to understand how to utilize Phasers (it will look different than the X10 implementation). This time, we will use Phasers to create a parallel version of the algorithm that has less overhead. Here are a few notes to keep in mind when working on this assignment:&lt;br /&gt;
*Think carefully as to how the loops in this version will be structured. Drawing out the computation graph may help&lt;br /&gt;
*Bulk registering a Phaser indicates how many times &amp;lt;code&amp;gt;phaser.arriveAndAwaitAdvance()&amp;lt;/code&amp;gt; needs to be called before any of the threads are able to move on. In other words, it equals to how many threads need to arive at the checkpoint before moving on. Make sure to register the right number!&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
*You also bulk register the SwappableDoubleArrays when instantiating it. Similar to Phasers, the integer passed through in the constructor tells the object how many times &amp;lt;code&amp;gt;swap()&amp;lt;/code&amp;gt; needs to be called before the source and destination arrays are actually swapped.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;create phaser&lt;br /&gt;
register phaser for each task&lt;br /&gt;
parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:ForkLoopWithPhaser_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|__RigidIterativeAveragingTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
: {{TestSuite|_IterativeAveragingRangesTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_IterativeAveragingDoubleBufferTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_LoopOfForkLoopsIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_X10PhasedForkLoopIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_ForkLoopWithPhaserIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5434</id>
		<title>Iterative Averaging</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5434"/>
		<updated>2026-04-14T13:12:37Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* PhasableDoubleArrays */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Iterative Averaging is the process of updating an array to so that each index becomes the average of the indices one before and one after it. After repeating this for many iterations, the array may converge to one set of numbers. For example, when given the following array, we can perform iterations of this algorithm until the array eventually converges:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | [0] || [1] || [2] || [3] || [4] || [5] || [6] || [7] || [8] || [9] || [10]&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.25 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.125 || 0.25 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0625 || 0.125 || 0.375 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.03125 || 0.0625 || 0.21875 || 0.375 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.015625 || 0.03125 || 0.125 || 0.21875 || 0.453125 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0078125 || 0.015625 || 0.0703125 || 0.125 || 0.2890625 || 0.453125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.00390625 || 0.0078125 || 0.0390625 || 0.0703125 || 0.1796875 || 0.2890625 || 0.5078125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.00390625 || 0.021484375 || 0.0390625 || 0.109375 || 0.1796875 || 0.34375 || 0.5078125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.01171875 || 0.021484375 || 0.0654296875 || 0.109375 || 0.2265625 || 0.34375 || 0.548828125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0 &lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.1 || 0.2 || 0.3 || 0.4 || 0.5 || 0.6 || 0.7 || 0.8 || 0.9 || 1.0 &lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | src || dst&lt;br /&gt;
|-&lt;br /&gt;
| phase 0 || &lt;br /&gt;
|-&lt;br /&gt;
| phase 1 || phase 0&lt;br /&gt;
|-&lt;br /&gt;
| phase 2 || phase 1&lt;br /&gt;
|-&lt;br /&gt;
| phase 3 || phase 2&lt;br /&gt;
|-&lt;br /&gt;
| phase 4 || phase 3&lt;br /&gt;
|-&lt;br /&gt;
| phase 5 || phase 4&lt;br /&gt;
|-&lt;br /&gt;
| phase 6 || phase 5&lt;br /&gt;
|-&lt;br /&gt;
| phase 7 || phase 6&lt;br /&gt;
|-&lt;br /&gt;
| phase 8 || phase 7&lt;br /&gt;
|-&lt;br /&gt;
| phase 9 || phase 8&lt;br /&gt;
|-&lt;br /&gt;
| phase 10 || phase 9&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| || phase N-1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the ends of the array remain the same value from the previous iteration. &lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
X10-like Phasers have been added to Java since JDK7.  We will gain some experience with using Phasers in a parallel for-loop context. Phasers allow us to change the structure of our loops and reduce overhead in the algorithm.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
For more information on the algorithm and how we can use Phasers to make it better, review [https://edge.edx.org/courses/RiceX/COMP322/1T2014R/courseware/a900dd0655384de3b5ef01e508ea09d7/6349730bb2a149a0b33fa23db7afddee/13 Topic 3.5] from the RiceX course.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
&amp;lt;youtube&amp;gt;Ixprqn_Xhf4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Java Phaser==&lt;br /&gt;
&amp;lt;youtube&amp;gt;whPAPmylbEU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Reference_Page#Phasers|Check out the reference page on phasers]]&lt;br /&gt;
&lt;br /&gt;
[http://www.baeldung.com/java-phaser Guide to the Java Phaser]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html class Phaser]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#bulkRegister-int- bulkRegister] (also see instructions on [[#ForkLoopWithPhaserIterativeAverager |ForkLoopWithPhaserIterativeAverager]])&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#arriveAndAwaitAdvance-- arriveAndAwaitAdvance]&lt;br /&gt;
&lt;br /&gt;
{{warning | Our use of the forall loop with Phasers does not accurately convey their finicky nature.  More than other features, Phasers seem to require more care to get performance improvements. }}&lt;br /&gt;
&lt;br /&gt;
==Ranges==&lt;br /&gt;
[[Ranges|Ranges (Previous Exercise To Use)]]&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;0c2PZ-ARDDE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
==IterativeAveragingRanges==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|slice|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
===slice===&lt;br /&gt;
&amp;lt;youtube&amp;gt;_taVHpi1Hpg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each IterativeAverager should slice the data into ranges.  The constructor for each IterativeAverager is passed the number of slices to create.&lt;br /&gt;
&lt;br /&gt;
{{Tip| Use Ranges.slice(min, maxExclusive, numSlices)}}&lt;br /&gt;
&lt;br /&gt;
If you are to create 3 slices an array of length 11:&lt;br /&gt;
&lt;br /&gt;
[[File:IterativeAveraging.png|600px]]&lt;br /&gt;
&lt;br /&gt;
the slices should have these properties:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|||sliceID||minInclusive||maxExclusive&lt;br /&gt;
|-&lt;br /&gt;
|sliceA||0||1||4&lt;br /&gt;
|-&lt;br /&gt;
|sliceB||1||4||7&lt;br /&gt;
|-&lt;br /&gt;
|sliceC||2||7||10&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==IterativeAveragingDoubleBuffer==&lt;br /&gt;
===constructor===&lt;br /&gt;
&lt;br /&gt;
Hang onto (in member a member variable) the original double array and initialize (and hang onto) two buffers of the same length. Make sure to copy over the original data's 0th and last index's value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	private final double[] originalData;&lt;br /&gt;
	private final double[] a;&lt;br /&gt;
	private final double[] b;&lt;br /&gt;
&lt;br /&gt;
	public IterativeAveragingDoubleBuffer(double[] originalData) {&lt;br /&gt;
		Objects.requireNonNull(originalData);&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===srcForPhase===&lt;br /&gt;
&lt;br /&gt;
===dstForPhase===&lt;br /&gt;
&lt;br /&gt;
==LoopOfForkLoopsIterativeAverager==&lt;br /&gt;
{{CodeToImplement|LoopOfForkLoopsIterativeAverager|sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
For this method, you should not be using Phasers. Instead, implement a parallel version of the [[Sequential_Iterative_Averager_Assignment|Sequential Iterative Averaging warm up]]. Make use of the PhasableDoubleArrays class. The return value should be the current version of the array after it has gone through the number of iterations passed in the method.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;sequential loop&lt;br /&gt;
    parallel loop&lt;br /&gt;
        work&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is the work for each task?&lt;br /&gt;
&lt;br /&gt;
[[File:LoopOfForkLoops_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==X10PhasedForkLoopIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|X10PhasedForkLoopIterativeAverager|x10&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;x10 phased parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:X10PhasedForkLoop_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==ForkLoopWithPhaserIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ForkLoopWithPhaserIterativeAverager|phaserCreator&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
Before you get started on this, make sure you review the [[Iterative Averaging#Background | Background section]] in order to understand how to utilize Phasers (it will look different than the X10 implementation). This time, we will use Phasers to create a parallel version of the algorithm that has less overhead. Here are a few notes to keep in mind when working on this assignment:&lt;br /&gt;
*Think carefully as to how the loops in this version will be structured. Drawing out the computation graph may help&lt;br /&gt;
*Bulk registering a Phaser indicates how many times &amp;lt;code&amp;gt;phaser.arriveAndAwaitAdvance()&amp;lt;/code&amp;gt; needs to be called before any of the threads are able to move on. In other words, it equals to how many threads need to arive at the checkpoint before moving on. Make sure to register the right number!&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
*You also bulk register the SwappableDoubleArrays when instantiating it. Similar to Phasers, the integer passed through in the constructor tells the object how many times &amp;lt;code&amp;gt;swap()&amp;lt;/code&amp;gt; needs to be called before the source and destination arrays are actually swapped.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;create phaser&lt;br /&gt;
register phaser for each task&lt;br /&gt;
parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:ForkLoopWithPhaser_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|__RigidIterativeAveragingTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
: {{TestSuite|_IterativeAveragingUtilsTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_LoopOfForkLoopsIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_X10PhasedForkLoopIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_ForkLoopWithPhaserIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5433</id>
		<title>Iterative Averaging</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5433"/>
		<updated>2026-04-14T13:12:14Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* constructor */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Iterative Averaging is the process of updating an array to so that each index becomes the average of the indices one before and one after it. After repeating this for many iterations, the array may converge to one set of numbers. For example, when given the following array, we can perform iterations of this algorithm until the array eventually converges:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | [0] || [1] || [2] || [3] || [4] || [5] || [6] || [7] || [8] || [9] || [10]&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.25 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.125 || 0.25 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0625 || 0.125 || 0.375 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.03125 || 0.0625 || 0.21875 || 0.375 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.015625 || 0.03125 || 0.125 || 0.21875 || 0.453125 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0078125 || 0.015625 || 0.0703125 || 0.125 || 0.2890625 || 0.453125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.00390625 || 0.0078125 || 0.0390625 || 0.0703125 || 0.1796875 || 0.2890625 || 0.5078125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.00390625 || 0.021484375 || 0.0390625 || 0.109375 || 0.1796875 || 0.34375 || 0.5078125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.01171875 || 0.021484375 || 0.0654296875 || 0.109375 || 0.2265625 || 0.34375 || 0.548828125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0 &lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.1 || 0.2 || 0.3 || 0.4 || 0.5 || 0.6 || 0.7 || 0.8 || 0.9 || 1.0 &lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | src || dst&lt;br /&gt;
|-&lt;br /&gt;
| phase 0 || &lt;br /&gt;
|-&lt;br /&gt;
| phase 1 || phase 0&lt;br /&gt;
|-&lt;br /&gt;
| phase 2 || phase 1&lt;br /&gt;
|-&lt;br /&gt;
| phase 3 || phase 2&lt;br /&gt;
|-&lt;br /&gt;
| phase 4 || phase 3&lt;br /&gt;
|-&lt;br /&gt;
| phase 5 || phase 4&lt;br /&gt;
|-&lt;br /&gt;
| phase 6 || phase 5&lt;br /&gt;
|-&lt;br /&gt;
| phase 7 || phase 6&lt;br /&gt;
|-&lt;br /&gt;
| phase 8 || phase 7&lt;br /&gt;
|-&lt;br /&gt;
| phase 9 || phase 8&lt;br /&gt;
|-&lt;br /&gt;
| phase 10 || phase 9&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| || phase N-1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the ends of the array remain the same value from the previous iteration. &lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
X10-like Phasers have been added to Java since JDK7.  We will gain some experience with using Phasers in a parallel for-loop context. Phasers allow us to change the structure of our loops and reduce overhead in the algorithm.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
For more information on the algorithm and how we can use Phasers to make it better, review [https://edge.edx.org/courses/RiceX/COMP322/1T2014R/courseware/a900dd0655384de3b5ef01e508ea09d7/6349730bb2a149a0b33fa23db7afddee/13 Topic 3.5] from the RiceX course.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
&amp;lt;youtube&amp;gt;Ixprqn_Xhf4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Java Phaser==&lt;br /&gt;
&amp;lt;youtube&amp;gt;whPAPmylbEU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Reference_Page#Phasers|Check out the reference page on phasers]]&lt;br /&gt;
&lt;br /&gt;
[http://www.baeldung.com/java-phaser Guide to the Java Phaser]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html class Phaser]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#bulkRegister-int- bulkRegister] (also see instructions on [[#ForkLoopWithPhaserIterativeAverager |ForkLoopWithPhaserIterativeAverager]])&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#arriveAndAwaitAdvance-- arriveAndAwaitAdvance]&lt;br /&gt;
&lt;br /&gt;
{{warning | Our use of the forall loop with Phasers does not accurately convey their finicky nature.  More than other features, Phasers seem to require more care to get performance improvements. }}&lt;br /&gt;
&lt;br /&gt;
==PhasableDoubleArrays==&lt;br /&gt;
*[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html class AbstractPhasable&amp;lt;T&amp;gt;]&lt;br /&gt;
**[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html#getSrcForPhase-int- getSrcForPhase(phaseIndex)]&lt;br /&gt;
**[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html#getDstForPhase-int- getDstForPhase(phaseIndex)]&lt;br /&gt;
*[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/PhasableDoubleArrays.html class PhasableDoubleArrays extends AbstractPhasable&amp;lt;double&amp;lt;nowiki&amp;gt;[]&amp;lt;/nowiki&amp;gt;&amp;gt;]&lt;br /&gt;
** [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/PhasableDoubleArrays.html#%3Cinit%3E(double%5B%5D,java.util.function.Function) new PhasableDoubleArrays(originalData, initializerFunction)]&lt;br /&gt;
&lt;br /&gt;
==Ranges==&lt;br /&gt;
[[Ranges|Ranges (Previous Exercise To Use)]]&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;0c2PZ-ARDDE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
==IterativeAveragingRanges==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|slice|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
===slice===&lt;br /&gt;
&amp;lt;youtube&amp;gt;_taVHpi1Hpg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each IterativeAverager should slice the data into ranges.  The constructor for each IterativeAverager is passed the number of slices to create.&lt;br /&gt;
&lt;br /&gt;
{{Tip| Use Ranges.slice(min, maxExclusive, numSlices)}}&lt;br /&gt;
&lt;br /&gt;
If you are to create 3 slices an array of length 11:&lt;br /&gt;
&lt;br /&gt;
[[File:IterativeAveraging.png|600px]]&lt;br /&gt;
&lt;br /&gt;
the slices should have these properties:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|||sliceID||minInclusive||maxExclusive&lt;br /&gt;
|-&lt;br /&gt;
|sliceA||0||1||4&lt;br /&gt;
|-&lt;br /&gt;
|sliceB||1||4||7&lt;br /&gt;
|-&lt;br /&gt;
|sliceC||2||7||10&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==IterativeAveragingDoubleBuffer==&lt;br /&gt;
===constructor===&lt;br /&gt;
&lt;br /&gt;
Hang onto (in member a member variable) the original double array and initialize (and hang onto) two buffers of the same length. Make sure to copy over the original data's 0th and last index's value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	private final double[] originalData;&lt;br /&gt;
	private final double[] a;&lt;br /&gt;
	private final double[] b;&lt;br /&gt;
&lt;br /&gt;
	public IterativeAveragingDoubleBuffer(double[] originalData) {&lt;br /&gt;
		Objects.requireNonNull(originalData);&lt;br /&gt;
&lt;br /&gt;
		throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===srcForPhase===&lt;br /&gt;
&lt;br /&gt;
===dstForPhase===&lt;br /&gt;
&lt;br /&gt;
==LoopOfForkLoopsIterativeAverager==&lt;br /&gt;
{{CodeToImplement|LoopOfForkLoopsIterativeAverager|sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
For this method, you should not be using Phasers. Instead, implement a parallel version of the [[Sequential_Iterative_Averager_Assignment|Sequential Iterative Averaging warm up]]. Make use of the PhasableDoubleArrays class. The return value should be the current version of the array after it has gone through the number of iterations passed in the method.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;sequential loop&lt;br /&gt;
    parallel loop&lt;br /&gt;
        work&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is the work for each task?&lt;br /&gt;
&lt;br /&gt;
[[File:LoopOfForkLoops_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==X10PhasedForkLoopIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|X10PhasedForkLoopIterativeAverager|x10&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;x10 phased parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:X10PhasedForkLoop_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==ForkLoopWithPhaserIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ForkLoopWithPhaserIterativeAverager|phaserCreator&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
Before you get started on this, make sure you review the [[Iterative Averaging#Background | Background section]] in order to understand how to utilize Phasers (it will look different than the X10 implementation). This time, we will use Phasers to create a parallel version of the algorithm that has less overhead. Here are a few notes to keep in mind when working on this assignment:&lt;br /&gt;
*Think carefully as to how the loops in this version will be structured. Drawing out the computation graph may help&lt;br /&gt;
*Bulk registering a Phaser indicates how many times &amp;lt;code&amp;gt;phaser.arriveAndAwaitAdvance()&amp;lt;/code&amp;gt; needs to be called before any of the threads are able to move on. In other words, it equals to how many threads need to arive at the checkpoint before moving on. Make sure to register the right number!&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
*You also bulk register the SwappableDoubleArrays when instantiating it. Similar to Phasers, the integer passed through in the constructor tells the object how many times &amp;lt;code&amp;gt;swap()&amp;lt;/code&amp;gt; needs to be called before the source and destination arrays are actually swapped.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;create phaser&lt;br /&gt;
register phaser for each task&lt;br /&gt;
parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:ForkLoopWithPhaser_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|__RigidIterativeAveragingTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
: {{TestSuite|_IterativeAveragingUtilsTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_LoopOfForkLoopsIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_X10PhasedForkLoopIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_ForkLoopWithPhaserIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5432</id>
		<title>Iterative Averaging</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5432"/>
		<updated>2026-04-14T13:09:24Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* IterativeAveragingDoubleBuffer */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Iterative Averaging is the process of updating an array to so that each index becomes the average of the indices one before and one after it. After repeating this for many iterations, the array may converge to one set of numbers. For example, when given the following array, we can perform iterations of this algorithm until the array eventually converges:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | [0] || [1] || [2] || [3] || [4] || [5] || [6] || [7] || [8] || [9] || [10]&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.25 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.125 || 0.25 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0625 || 0.125 || 0.375 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.03125 || 0.0625 || 0.21875 || 0.375 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.015625 || 0.03125 || 0.125 || 0.21875 || 0.453125 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0078125 || 0.015625 || 0.0703125 || 0.125 || 0.2890625 || 0.453125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.00390625 || 0.0078125 || 0.0390625 || 0.0703125 || 0.1796875 || 0.2890625 || 0.5078125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.00390625 || 0.021484375 || 0.0390625 || 0.109375 || 0.1796875 || 0.34375 || 0.5078125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.01171875 || 0.021484375 || 0.0654296875 || 0.109375 || 0.2265625 || 0.34375 || 0.548828125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0 &lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.1 || 0.2 || 0.3 || 0.4 || 0.5 || 0.6 || 0.7 || 0.8 || 0.9 || 1.0 &lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | src || dst&lt;br /&gt;
|-&lt;br /&gt;
| phase 0 || &lt;br /&gt;
|-&lt;br /&gt;
| phase 1 || phase 0&lt;br /&gt;
|-&lt;br /&gt;
| phase 2 || phase 1&lt;br /&gt;
|-&lt;br /&gt;
| phase 3 || phase 2&lt;br /&gt;
|-&lt;br /&gt;
| phase 4 || phase 3&lt;br /&gt;
|-&lt;br /&gt;
| phase 5 || phase 4&lt;br /&gt;
|-&lt;br /&gt;
| phase 6 || phase 5&lt;br /&gt;
|-&lt;br /&gt;
| phase 7 || phase 6&lt;br /&gt;
|-&lt;br /&gt;
| phase 8 || phase 7&lt;br /&gt;
|-&lt;br /&gt;
| phase 9 || phase 8&lt;br /&gt;
|-&lt;br /&gt;
| phase 10 || phase 9&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| || phase N-1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the ends of the array remain the same value from the previous iteration. &lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
X10-like Phasers have been added to Java since JDK7.  We will gain some experience with using Phasers in a parallel for-loop context. Phasers allow us to change the structure of our loops and reduce overhead in the algorithm.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
For more information on the algorithm and how we can use Phasers to make it better, review [https://edge.edx.org/courses/RiceX/COMP322/1T2014R/courseware/a900dd0655384de3b5ef01e508ea09d7/6349730bb2a149a0b33fa23db7afddee/13 Topic 3.5] from the RiceX course.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
&amp;lt;youtube&amp;gt;Ixprqn_Xhf4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Java Phaser==&lt;br /&gt;
&amp;lt;youtube&amp;gt;whPAPmylbEU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Reference_Page#Phasers|Check out the reference page on phasers]]&lt;br /&gt;
&lt;br /&gt;
[http://www.baeldung.com/java-phaser Guide to the Java Phaser]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html class Phaser]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#bulkRegister-int- bulkRegister] (also see instructions on [[#ForkLoopWithPhaserIterativeAverager |ForkLoopWithPhaserIterativeAverager]])&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#arriveAndAwaitAdvance-- arriveAndAwaitAdvance]&lt;br /&gt;
&lt;br /&gt;
{{warning | Our use of the forall loop with Phasers does not accurately convey their finicky nature.  More than other features, Phasers seem to require more care to get performance improvements. }}&lt;br /&gt;
&lt;br /&gt;
==PhasableDoubleArrays==&lt;br /&gt;
*[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html class AbstractPhasable&amp;lt;T&amp;gt;]&lt;br /&gt;
**[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html#getSrcForPhase-int- getSrcForPhase(phaseIndex)]&lt;br /&gt;
**[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html#getDstForPhase-int- getDstForPhase(phaseIndex)]&lt;br /&gt;
*[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/PhasableDoubleArrays.html class PhasableDoubleArrays extends AbstractPhasable&amp;lt;double&amp;lt;nowiki&amp;gt;[]&amp;lt;/nowiki&amp;gt;&amp;gt;]&lt;br /&gt;
** [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/PhasableDoubleArrays.html#%3Cinit%3E(double%5B%5D,java.util.function.Function) new PhasableDoubleArrays(originalData, initializerFunction)]&lt;br /&gt;
&lt;br /&gt;
==Ranges==&lt;br /&gt;
[[Ranges|Ranges (Previous Exercise To Use)]]&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;0c2PZ-ARDDE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
==IterativeAveragingRanges==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|slice|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
===slice===&lt;br /&gt;
&amp;lt;youtube&amp;gt;_taVHpi1Hpg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each IterativeAverager should slice the data into ranges.  The constructor for each IterativeAverager is passed the number of slices to create.&lt;br /&gt;
&lt;br /&gt;
{{Tip| Use Ranges.slice(min, maxExclusive, numSlices)}}&lt;br /&gt;
&lt;br /&gt;
If you are to create 3 slices an array of length 11:&lt;br /&gt;
&lt;br /&gt;
[[File:IterativeAveraging.png|600px]]&lt;br /&gt;
&lt;br /&gt;
the slices should have these properties:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|||sliceID||minInclusive||maxExclusive&lt;br /&gt;
|-&lt;br /&gt;
|sliceA||0||1||4&lt;br /&gt;
|-&lt;br /&gt;
|sliceB||1||4||7&lt;br /&gt;
|-&lt;br /&gt;
|sliceC||2||7||10&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==IterativeAveragingDoubleBuffer==&lt;br /&gt;
===constructor===&lt;br /&gt;
&lt;br /&gt;
Hang onto (in member a member variable) the original double array and initialize (and hang onto) two buffers of the same length. Make sure to copy over the original data's 0th and last index's value.&lt;br /&gt;
&lt;br /&gt;
===srcForPhase===&lt;br /&gt;
&lt;br /&gt;
===dstForPhase===&lt;br /&gt;
&lt;br /&gt;
==LoopOfForkLoopsIterativeAverager==&lt;br /&gt;
{{CodeToImplement|LoopOfForkLoopsIterativeAverager|sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
For this method, you should not be using Phasers. Instead, implement a parallel version of the [[Sequential_Iterative_Averager_Assignment|Sequential Iterative Averaging warm up]]. Make use of the PhasableDoubleArrays class. The return value should be the current version of the array after it has gone through the number of iterations passed in the method.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;sequential loop&lt;br /&gt;
    parallel loop&lt;br /&gt;
        work&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is the work for each task?&lt;br /&gt;
&lt;br /&gt;
[[File:LoopOfForkLoops_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==X10PhasedForkLoopIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|X10PhasedForkLoopIterativeAverager|x10&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;x10 phased parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:X10PhasedForkLoop_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==ForkLoopWithPhaserIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ForkLoopWithPhaserIterativeAverager|phaserCreator&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
Before you get started on this, make sure you review the [[Iterative Averaging#Background | Background section]] in order to understand how to utilize Phasers (it will look different than the X10 implementation). This time, we will use Phasers to create a parallel version of the algorithm that has less overhead. Here are a few notes to keep in mind when working on this assignment:&lt;br /&gt;
*Think carefully as to how the loops in this version will be structured. Drawing out the computation graph may help&lt;br /&gt;
*Bulk registering a Phaser indicates how many times &amp;lt;code&amp;gt;phaser.arriveAndAwaitAdvance()&amp;lt;/code&amp;gt; needs to be called before any of the threads are able to move on. In other words, it equals to how many threads need to arive at the checkpoint before moving on. Make sure to register the right number!&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
*You also bulk register the SwappableDoubleArrays when instantiating it. Similar to Phasers, the integer passed through in the constructor tells the object how many times &amp;lt;code&amp;gt;swap()&amp;lt;/code&amp;gt; needs to be called before the source and destination arrays are actually swapped.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;create phaser&lt;br /&gt;
register phaser for each task&lt;br /&gt;
parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:ForkLoopWithPhaser_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|__RigidIterativeAveragingTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
: {{TestSuite|_IterativeAveragingUtilsTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_LoopOfForkLoopsIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_X10PhasedForkLoopIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_ForkLoopWithPhaserIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5431</id>
		<title>Iterative Averaging</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=5431"/>
		<updated>2026-04-14T13:07:27Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* Code to Implement */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Iterative Averaging is the process of updating an array to so that each index becomes the average of the indices one before and one after it. After repeating this for many iterations, the array may converge to one set of numbers. For example, when given the following array, we can perform iterations of this algorithm until the array eventually converges:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | [0] || [1] || [2] || [3] || [4] || [5] || [6] || [7] || [8] || [9] || [10]&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.25 || 0.5 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.125 || 0.25 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.0625 || 0.125 || 0.375 || 0.625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.0 || 0.03125 || 0.0625 || 0.21875 || 0.375 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0 || 0.015625 || 0.03125 || 0.125 || 0.21875 || 0.453125 || 0.6875 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.0 || 0.0078125 || 0.015625 || 0.0703125 || 0.125 || 0.2890625 || 0.453125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.0 || 0.00390625 || 0.0078125 || 0.0390625 || 0.0703125 || 0.1796875 || 0.2890625 || 0.5078125 || 0.7265625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.00390625 || 0.021484375 || 0.0390625 || 0.109375 || 0.1796875 || 0.34375 || 0.5078125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.001953125 || 0.01171875 || 0.021484375 || 0.0654296875 || 0.109375 || 0.2265625 || 0.34375 || 0.548828125 || 0.75390625 || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0 &lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;9&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt; || 1.0&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || 0.1 || 0.2 || 0.3 || 0.4 || 0.5 || 0.6 || 0.7 || 0.8 || 0.9 || 1.0 &lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;display: inline-table;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | src || dst&lt;br /&gt;
|-&lt;br /&gt;
| phase 0 || &lt;br /&gt;
|-&lt;br /&gt;
| phase 1 || phase 0&lt;br /&gt;
|-&lt;br /&gt;
| phase 2 || phase 1&lt;br /&gt;
|-&lt;br /&gt;
| phase 3 || phase 2&lt;br /&gt;
|-&lt;br /&gt;
| phase 4 || phase 3&lt;br /&gt;
|-&lt;br /&gt;
| phase 5 || phase 4&lt;br /&gt;
|-&lt;br /&gt;
| phase 6 || phase 5&lt;br /&gt;
|-&lt;br /&gt;
| phase 7 || phase 6&lt;br /&gt;
|-&lt;br /&gt;
| phase 8 || phase 7&lt;br /&gt;
|-&lt;br /&gt;
| phase 9 || phase 8&lt;br /&gt;
|-&lt;br /&gt;
| phase 10 || phase 9&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;text-align:center;&amp;quot; colspan=&amp;quot;2&amp;quot; | &amp;lt;math&amp;gt;\vdots&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| || phase N-1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note that the ends of the array remain the same value from the previous iteration. &lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
X10-like Phasers have been added to Java since JDK7.  We will gain some experience with using Phasers in a parallel for-loop context. Phasers allow us to change the structure of our loops and reduce overhead in the algorithm.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
For more information on the algorithm and how we can use Phasers to make it better, review [https://edge.edx.org/courses/RiceX/COMP322/1T2014R/courseware/a900dd0655384de3b5ef01e508ea09d7/6349730bb2a149a0b33fa23db7afddee/13 Topic 3.5] from the RiceX course.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
&amp;lt;youtube&amp;gt;Ixprqn_Xhf4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Java Phaser==&lt;br /&gt;
&amp;lt;youtube&amp;gt;whPAPmylbEU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Reference_Page#Phasers|Check out the reference page on phasers]]&lt;br /&gt;
&lt;br /&gt;
[http://www.baeldung.com/java-phaser Guide to the Java Phaser]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html class Phaser]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#bulkRegister-int- bulkRegister] (also see instructions on [[#ForkLoopWithPhaserIterativeAverager |ForkLoopWithPhaserIterativeAverager]])&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#arriveAndAwaitAdvance-- arriveAndAwaitAdvance]&lt;br /&gt;
&lt;br /&gt;
{{warning | Our use of the forall loop with Phasers does not accurately convey their finicky nature.  More than other features, Phasers seem to require more care to get performance improvements. }}&lt;br /&gt;
&lt;br /&gt;
==PhasableDoubleArrays==&lt;br /&gt;
*[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html class AbstractPhasable&amp;lt;T&amp;gt;]&lt;br /&gt;
**[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html#getSrcForPhase-int- getSrcForPhase(phaseIndex)]&lt;br /&gt;
**[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html#getDstForPhase-int- getDstForPhase(phaseIndex)]&lt;br /&gt;
*[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/PhasableDoubleArrays.html class PhasableDoubleArrays extends AbstractPhasable&amp;lt;double&amp;lt;nowiki&amp;gt;[]&amp;lt;/nowiki&amp;gt;&amp;gt;]&lt;br /&gt;
** [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/PhasableDoubleArrays.html#%3Cinit%3E(double%5B%5D,java.util.function.Function) new PhasableDoubleArrays(originalData, initializerFunction)]&lt;br /&gt;
&lt;br /&gt;
==Ranges==&lt;br /&gt;
[[Ranges|Ranges (Previous Exercise To Use)]]&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;0c2PZ-ARDDE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
==IterativeAveragingRanges==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|slice|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
===slice===&lt;br /&gt;
&amp;lt;youtube&amp;gt;_taVHpi1Hpg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each IterativeAverager should slice the data into ranges.  The constructor for each IterativeAverager is passed the number of slices to create.&lt;br /&gt;
&lt;br /&gt;
{{Tip| Use Ranges.slice(min, maxExclusive, numSlices)}}&lt;br /&gt;
&lt;br /&gt;
If you are to create 3 slices an array of length 11:&lt;br /&gt;
&lt;br /&gt;
[[File:IterativeAveraging.png|600px]]&lt;br /&gt;
&lt;br /&gt;
the slices should have these properties:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|||sliceID||minInclusive||maxExclusive&lt;br /&gt;
|-&lt;br /&gt;
|sliceA||0||1||4&lt;br /&gt;
|-&lt;br /&gt;
|sliceB||1||4||7&lt;br /&gt;
|-&lt;br /&gt;
|sliceC||2||7||10&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==IterativeAveragingDoubleBuffer==&lt;br /&gt;
===constructor===&lt;br /&gt;
===srcForPhase===&lt;br /&gt;
===dstForPhase===&lt;br /&gt;
&amp;lt;youtube&amp;gt;-SBcwicgyOE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TL;DR: Initialize a double[] and make sure to copy over the original data's 0th and last index's value.&lt;br /&gt;
&lt;br /&gt;
==LoopOfForkLoopsIterativeAverager==&lt;br /&gt;
{{CodeToImplement|LoopOfForkLoopsIterativeAverager|sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
For this method, you should not be using Phasers. Instead, implement a parallel version of the [[Sequential_Iterative_Averager_Assignment|Sequential Iterative Averaging warm up]]. Make use of the PhasableDoubleArrays class. The return value should be the current version of the array after it has gone through the number of iterations passed in the method.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;sequential loop&lt;br /&gt;
    parallel loop&lt;br /&gt;
        work&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What is the work for each task?&lt;br /&gt;
&lt;br /&gt;
[[File:LoopOfForkLoops_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==X10PhasedForkLoopIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|X10PhasedForkLoopIterativeAverager|x10&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;x10 phased parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:X10PhasedForkLoop_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
==ForkLoopWithPhaserIterativeAverager==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ForkLoopWithPhaserIterativeAverager|phaserCreator&amp;lt;br/&amp;gt;sliceCount&amp;lt;br/&amp;gt;iterativelyAverage|iterativeaveraging.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public double[] iterativelyAverage(double[] originalArray, int iterationCount)}}&lt;br /&gt;
&lt;br /&gt;
Before you get started on this, make sure you review the [[Iterative Averaging#Background | Background section]] in order to understand how to utilize Phasers (it will look different than the X10 implementation). This time, we will use Phasers to create a parallel version of the algorithm that has less overhead. Here are a few notes to keep in mind when working on this assignment:&lt;br /&gt;
*Think carefully as to how the loops in this version will be structured. Drawing out the computation graph may help&lt;br /&gt;
*Bulk registering a Phaser indicates how many times &amp;lt;code&amp;gt;phaser.arriveAndAwaitAdvance()&amp;lt;/code&amp;gt; needs to be called before any of the threads are able to move on. In other words, it equals to how many threads need to arive at the checkpoint before moving on. Make sure to register the right number!&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
*You also bulk register the SwappableDoubleArrays when instantiating it. Similar to Phasers, the integer passed through in the constructor tells the object how many times &amp;lt;code&amp;gt;swap()&amp;lt;/code&amp;gt; needs to be called before the source and destination arrays are actually swapped.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;create phaser&lt;br /&gt;
register phaser for each task&lt;br /&gt;
parallel loop&lt;br /&gt;
    sequential loop&lt;br /&gt;
        work&lt;br /&gt;
        arrive and await advance on phaser&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is the work for each iteration of a task?&lt;br /&gt;
&lt;br /&gt;
[[File:ForkLoopWithPhaser_IterativeAverager.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|__RigidIterativeAveragingTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
: {{TestSuite|_IterativeAveragingUtilsTestSuite|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_LoopOfForkLoopsIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_X10PhasedForkLoopIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_ForkLoopWithPhaserIterativeAveragerTestSuite|iterativeaveraging.rigid.exercise}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Balance_Assignment&amp;diff=5430</id>
		<title>K Mer Balance Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Balance_Assignment&amp;diff=5430"/>
		<updated>2026-04-14T01:52:12Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* DefaultKMerBalancer */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Balancing the work load is not always straightforward.  A sample chromosome from the NIH has enough inconclusive reads that it is split into 50 sub-sequences.  The lengths of these sub-sequences vary wildly, from ~1,000 to ~10,000,00.&lt;br /&gt;
&lt;br /&gt;
[[File:Chromosome sub sequence lengths.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/spreadsheets/d/1d2qqxXZ_Mhp6DL9HxkhRrHq61GQoRzpf-Sf-4ZitsGo/ spreadsheet]&lt;br /&gt;
&lt;br /&gt;
While creating a task for each of the 50 sub-sequences would be in our target range of 2X to 10X the the number of processors for an 8 processor machine, the workload balance would be terrible.  Over 40 of the sequences are tiny, and only 3 are of lengths of any real significance.  Thus, most of the processors would be sitting around idle while the last 3 processors finished up the 3 longest sub-sequences... then only 2 would be working, and finally the last one would finish up.  Once could, one supposes, use the Ranges exercise from before to slice up each of the sub-sequences, but we only really want to slice up the long sub-sequences and leave the short ones unsliced.&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
interface KMerBalancer provides a default implementation of createReasonableSlices(sequences, k) which you will use in each of your parallel k-mer counters.  You will note, that createReasonableSlices invokes two methods you will implement: calculateReasonableThreshold and createSlices.&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|createReasonableSlices|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
default List&amp;lt;ByteArrayRange&amp;gt; createReasonableSlices(List&amp;lt;byte[]&amp;gt; sequences, int k) {&lt;br /&gt;
	int reasonableThreshold = calculateReasonableThreshold(sequences, k);&lt;br /&gt;
	return createSlices(sequences, k, (rangeLength) -&amp;gt; {&lt;br /&gt;
		return rangeLength &amp;gt; reasonableThreshold;&lt;br /&gt;
	});&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==DefaultByteArrayRange==&lt;br /&gt;
As in our previous exercise where we implemented the class [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/range/exercise/Range.html Range], we will need to keep track of the min and maxExclusive.  Since we will also need to track the byte array which the range is associated with, we will need to handle originalCompleteSequence.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|DefaultByteArrayRange|constructor&amp;lt;br/&amp;gt;originalCompleteSequence&amp;lt;br/&amp;gt;min&amp;lt;br/&amp;gt;maxExclusive|kmer.balance.exercise}}&lt;br /&gt;
&lt;br /&gt;
===constructor and instance variables===&lt;br /&gt;
{{SequentialConstructor|public DefaultByteArrayRange(byte[] originalCompleteSequence, int min, int maxExclusive)}}&lt;br /&gt;
&lt;br /&gt;
Store each of the parameters in private final instance variables.&lt;br /&gt;
&lt;br /&gt;
{{Alert|As the name suggests, we are to hang onto the originalCompleteSequence.  Do not copy it or create a sub-array.  Just hang onto the reference in an instance variable. }}&lt;br /&gt;
&lt;br /&gt;
===originalCompleteSequence===&lt;br /&gt;
{{Sequential|public byte[] originalCompleteSequence()}}&lt;br /&gt;
&lt;br /&gt;
Return the originalCompleteSequence passed to the constructor.&lt;br /&gt;
&lt;br /&gt;
===min===&lt;br /&gt;
{{Sequential|public int min()}}&lt;br /&gt;
&lt;br /&gt;
Return the min passed to the constructor.&lt;br /&gt;
&lt;br /&gt;
===maxExclusive===&lt;br /&gt;
{{Sequential|int maxExclusive()}}&lt;br /&gt;
&lt;br /&gt;
Return the maxExclusive passed to the constructor.&lt;br /&gt;
&lt;br /&gt;
==DefaultKMerBalancer==&lt;br /&gt;
&lt;br /&gt;
DefaultKMerBalancer exists to balance the workload of a list of dna sub-sequences.  We have balanced workload before by breaking up data into as equally sized slices in the [[Ranges]] assignment.  We used the sliced up Ranges in everything from the [[Coarsening_Nucleobase_Count_Assignment|Coarsening Nucleobase Count]] to [[Matrix_MapReduce_Framework_Assignment|Matrix MapReduce]].&lt;br /&gt;
&lt;br /&gt;
K-mer counting presents an additional challenge of having to deal with sub-sequences of radically different lengths.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|DefaultKMerBalancer|sliceKernel&amp;lt;br/&amp;gt;createSlices&amp;lt;br/&amp;gt;calculateReasonableThreshold|kmer.balance.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{ tip | Get DefaultKMerBalancer working and then use it to balance all of your parallel KMerCounter implementations. }}&lt;br /&gt;
&lt;br /&gt;
{{ tip | You will implement two methods: &amp;lt;code&amp;gt;createSlices&amp;lt;/code&amp;gt; which will invoke the second method &amp;lt;code&amp;gt;sliceKernel&amp;lt;/code&amp;gt; for each of the sequences.}}&lt;br /&gt;
&lt;br /&gt;
===createSlices===&lt;br /&gt;
&lt;br /&gt;
{{Sequential|List&amp;lt;ByteArrayRange&amp;gt; createSlices(List&amp;lt;byte[]&amp;gt; sequences, int k, IntPredicate slicePredicate)}}&lt;br /&gt;
&lt;br /&gt;
{{warning | be sure to slice the offset (a.k.a. startingIndex) space}}&lt;br /&gt;
&lt;br /&gt;
for N=10 and K=3, you should be slicing like this:&lt;br /&gt;
&lt;br /&gt;
[[File:ThresholdSlices.svg|320px]]&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|ThresholdSlices|&amp;lt;youtube&amp;gt;m56ee5spxiw&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
NOTE: you will want to invoke sliceKernel with each sequence.&lt;br /&gt;
&lt;br /&gt;
===sliceKernel===&lt;br /&gt;
{{Sequential|private static void sliceKernel(List&amp;lt;ByteArrayRange&amp;gt; slices, byte[] sequence, int min, int max,&lt;br /&gt;
			IntPredicate slicePredicate)}}&lt;br /&gt;
&lt;br /&gt;
Here we will employ a divide-and-conquer like strategy to fill the slices list with ByteArrayRanges that are all not too long.  We leverage the provided slicePredicate to test whether or not a range length from min to max is long enough to warrant dividing further.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&lt;br /&gt;
This class is designed to take in a list of bytes and convert it into a list of slices. The slice class should be familiar to you from earlier in the semester, but feel free to refer back to the documentation on [[Slices]] for a quick recap. You should perform this task recursively and there are two methods you will need to implement. The createSlices method should just call the kernel (the &amp;quot;helper&amp;quot; method), and the kernel performs the recursion.&lt;br /&gt;
&lt;br /&gt;
In the createSlices method, you should call the recursive kernel for each sequence that is longer than k in the list of sequences. When defining the min and max values, keep the information mentioned in the &amp;quot;Background&amp;quot; section in mind. As for the kernel, it should add a new slice to the collection of slices once the slice length dips below the provided threshold, but it should otherwise split itself into two smaller slices using recursion.&lt;br /&gt;
&lt;br /&gt;
In our previous uses of Slices we leveraged sliceIndexId (and not originalData) since it was a n-way split/coarsening scenario.  Here we are recursively splitting each item in a collection of sub-sequences so originalData will be used (and sliceIndexId is unnecessary and can be specified as invalid (-1))..&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===threshold calculation===&lt;br /&gt;
{{Sequential|public static int calculateReasonableThreshold(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
Define the threshold of slice length used to define an appropriate slicePredicate. &lt;br /&gt;
&lt;br /&gt;
Question to ask yourself: given a list of sequences which are to be k-mer counted, how would you calculate a reasonable threshold?&lt;br /&gt;
&lt;br /&gt;
In addition to an estimation of the work required to count the specified sequences, knowing the number of [https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#availableProcessors() available processors] will also be necessary.&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|availableProcessors|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
int processorCount = Runtime.getRuntime().availableProcessors();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__BalanceTestSuite|kmer.balance.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Pledge, Acknowledgments, Citations=&lt;br /&gt;
{{Pledge|kmer-balance}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Balance_Assignment&amp;diff=5429</id>
		<title>K Mer Balance Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Balance_Assignment&amp;diff=5429"/>
		<updated>2026-04-14T01:50:58Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* DefaultKMerBalancer */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Balancing the work load is not always straightforward.  A sample chromosome from the NIH has enough inconclusive reads that it is split into 50 sub-sequences.  The lengths of these sub-sequences vary wildly, from ~1,000 to ~10,000,00.&lt;br /&gt;
&lt;br /&gt;
[[File:Chromosome sub sequence lengths.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/spreadsheets/d/1d2qqxXZ_Mhp6DL9HxkhRrHq61GQoRzpf-Sf-4ZitsGo/ spreadsheet]&lt;br /&gt;
&lt;br /&gt;
While creating a task for each of the 50 sub-sequences would be in our target range of 2X to 10X the the number of processors for an 8 processor machine, the workload balance would be terrible.  Over 40 of the sequences are tiny, and only 3 are of lengths of any real significance.  Thus, most of the processors would be sitting around idle while the last 3 processors finished up the 3 longest sub-sequences... then only 2 would be working, and finally the last one would finish up.  Once could, one supposes, use the Ranges exercise from before to slice up each of the sub-sequences, but we only really want to slice up the long sub-sequences and leave the short ones unsliced.&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
interface KMerBalancer provides a default implementation of createReasonableSlices(sequences, k) which you will use in each of your parallel k-mer counters.  You will note, that createReasonableSlices invokes two methods you will implement: calculateReasonableThreshold and createSlices.&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|createReasonableSlices|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
default List&amp;lt;ByteArrayRange&amp;gt; createReasonableSlices(List&amp;lt;byte[]&amp;gt; sequences, int k) {&lt;br /&gt;
	int reasonableThreshold = calculateReasonableThreshold(sequences, k);&lt;br /&gt;
	return createSlices(sequences, k, (rangeLength) -&amp;gt; {&lt;br /&gt;
		return rangeLength &amp;gt; reasonableThreshold;&lt;br /&gt;
	});&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==DefaultByteArrayRange==&lt;br /&gt;
As in our previous exercise where we implemented the class [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/range/exercise/Range.html Range], we will need to keep track of the min and maxExclusive.  Since we will also need to track the byte array which the range is associated with, we will need to handle originalCompleteSequence.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|DefaultByteArrayRange|constructor&amp;lt;br/&amp;gt;originalCompleteSequence&amp;lt;br/&amp;gt;min&amp;lt;br/&amp;gt;maxExclusive|kmer.balance.exercise}}&lt;br /&gt;
&lt;br /&gt;
===constructor and instance variables===&lt;br /&gt;
{{SequentialConstructor|public DefaultByteArrayRange(byte[] originalCompleteSequence, int min, int maxExclusive)}}&lt;br /&gt;
&lt;br /&gt;
Store each of the parameters in private final instance variables.&lt;br /&gt;
&lt;br /&gt;
{{Alert|As the name suggests, we are to hang onto the originalCompleteSequence.  Do not copy it or create a sub-array.  Just hang onto the reference in an instance variable. }}&lt;br /&gt;
&lt;br /&gt;
===originalCompleteSequence===&lt;br /&gt;
{{Sequential|public byte[] originalCompleteSequence()}}&lt;br /&gt;
&lt;br /&gt;
Return the originalCompleteSequence passed to the constructor.&lt;br /&gt;
&lt;br /&gt;
===min===&lt;br /&gt;
{{Sequential|public int min()}}&lt;br /&gt;
&lt;br /&gt;
Return the min passed to the constructor.&lt;br /&gt;
&lt;br /&gt;
===maxExclusive===&lt;br /&gt;
{{Sequential|int maxExclusive()}}&lt;br /&gt;
&lt;br /&gt;
Return the maxExclusive passed to the constructor.&lt;br /&gt;
&lt;br /&gt;
==DefaultKMerBalancer==&lt;br /&gt;
&lt;br /&gt;
DefaultKMerBalancer exists to balance the workload of a list of dna sub-sequences.  We have balanced workload before by breaking up data into as equally sized slices in the [[Ranges]] assignment.  We used the sliced up Ranges in everything from the [[Coarsening_Nucleobase_Count_Assignment|Coarsening Nucleobase Count]] to [[Matrix_MapReduce_Framework_Assignment|Matrix MapReduce]].&lt;br /&gt;
&lt;br /&gt;
K-mer counting presents an additional challenge of having to deal with sub-sequences of radically different lengths.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|DefaultKMerBalancer|sliceKernel&amp;lt;br/&amp;gt;createSlices&amp;lt;br/&amp;gt;calculateReasonableThreshold|kmer.balance.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{ tip | Get DefaultKMerBalancer working and then use it to balance all of your parallel KMerCounter implementations. }}&lt;br /&gt;
&lt;br /&gt;
'''You will implement two methods: &amp;lt;code&amp;gt;createSlices&amp;lt;/code&amp;gt; which will invoke the second method &amp;lt;code&amp;gt;sliceKernel&amp;lt;/code&amp;gt; for each of the sequences.'''&lt;br /&gt;
&lt;br /&gt;
===createSlices===&lt;br /&gt;
&lt;br /&gt;
{{Sequential|List&amp;lt;ByteArrayRange&amp;gt; createSlices(List&amp;lt;byte[]&amp;gt; sequences, int k, IntPredicate slicePredicate)}}&lt;br /&gt;
&lt;br /&gt;
{{warning | be sure to slice the offset (a.k.a. startingIndex) space}}&lt;br /&gt;
&lt;br /&gt;
for N=10 and K=3, you should be slicing like this:&lt;br /&gt;
&lt;br /&gt;
[[File:ThresholdSlices.svg|320px]]&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|ThresholdSlices|&amp;lt;youtube&amp;gt;m56ee5spxiw&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
NOTE: you will want to invoke sliceKernel with each sequence.&lt;br /&gt;
&lt;br /&gt;
===sliceKernel===&lt;br /&gt;
{{Sequential|private static void sliceKernel(List&amp;lt;ByteArrayRange&amp;gt; slices, byte[] sequence, int min, int max,&lt;br /&gt;
			IntPredicate slicePredicate)}}&lt;br /&gt;
&lt;br /&gt;
Here we will employ a divide-and-conquer like strategy to fill the slices list with ByteArrayRanges that are all not too long.  We leverage the provided slicePredicate to test whether or not a range length from min to max is long enough to warrant dividing further.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&lt;br /&gt;
This class is designed to take in a list of bytes and convert it into a list of slices. The slice class should be familiar to you from earlier in the semester, but feel free to refer back to the documentation on [[Slices]] for a quick recap. You should perform this task recursively and there are two methods you will need to implement. The createSlices method should just call the kernel (the &amp;quot;helper&amp;quot; method), and the kernel performs the recursion.&lt;br /&gt;
&lt;br /&gt;
In the createSlices method, you should call the recursive kernel for each sequence that is longer than k in the list of sequences. When defining the min and max values, keep the information mentioned in the &amp;quot;Background&amp;quot; section in mind. As for the kernel, it should add a new slice to the collection of slices once the slice length dips below the provided threshold, but it should otherwise split itself into two smaller slices using recursion.&lt;br /&gt;
&lt;br /&gt;
In our previous uses of Slices we leveraged sliceIndexId (and not originalData) since it was a n-way split/coarsening scenario.  Here we are recursively splitting each item in a collection of sub-sequences so originalData will be used (and sliceIndexId is unnecessary and can be specified as invalid (-1))..&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===threshold calculation===&lt;br /&gt;
{{Sequential|public static int calculateReasonableThreshold(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
Define the threshold of slice length used to define an appropriate slicePredicate. &lt;br /&gt;
&lt;br /&gt;
Question to ask yourself: given a list of sequences which are to be k-mer counted, how would you calculate a reasonable threshold?&lt;br /&gt;
&lt;br /&gt;
In addition to an estimation of the work required to count the specified sequences, knowing the number of [https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#availableProcessors() available processors] will also be necessary.&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|availableProcessors|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
int processorCount = Runtime.getRuntime().availableProcessors();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__BalanceTestSuite|kmer.balance.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Pledge, Acknowledgments, Citations=&lt;br /&gt;
{{Pledge|kmer-balance}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Balance_Assignment&amp;diff=5428</id>
		<title>K Mer Balance Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Balance_Assignment&amp;diff=5428"/>
		<updated>2026-04-14T01:49:52Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* DefaultKMerBalancer */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Balancing the work load is not always straightforward.  A sample chromosome from the NIH has enough inconclusive reads that it is split into 50 sub-sequences.  The lengths of these sub-sequences vary wildly, from ~1,000 to ~10,000,00.&lt;br /&gt;
&lt;br /&gt;
[[File:Chromosome sub sequence lengths.png|600px]]&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/spreadsheets/d/1d2qqxXZ_Mhp6DL9HxkhRrHq61GQoRzpf-Sf-4ZitsGo/ spreadsheet]&lt;br /&gt;
&lt;br /&gt;
While creating a task for each of the 50 sub-sequences would be in our target range of 2X to 10X the the number of processors for an 8 processor machine, the workload balance would be terrible.  Over 40 of the sequences are tiny, and only 3 are of lengths of any real significance.  Thus, most of the processors would be sitting around idle while the last 3 processors finished up the 3 longest sub-sequences... then only 2 would be working, and finally the last one would finish up.  Once could, one supposes, use the Ranges exercise from before to slice up each of the sub-sequences, but we only really want to slice up the long sub-sequences and leave the short ones unsliced.&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
interface KMerBalancer provides a default implementation of createReasonableSlices(sequences, k) which you will use in each of your parallel k-mer counters.  You will note, that createReasonableSlices invokes two methods you will implement: calculateReasonableThreshold and createSlices.&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|createReasonableSlices|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
default List&amp;lt;ByteArrayRange&amp;gt; createReasonableSlices(List&amp;lt;byte[]&amp;gt; sequences, int k) {&lt;br /&gt;
	int reasonableThreshold = calculateReasonableThreshold(sequences, k);&lt;br /&gt;
	return createSlices(sequences, k, (rangeLength) -&amp;gt; {&lt;br /&gt;
		return rangeLength &amp;gt; reasonableThreshold;&lt;br /&gt;
	});&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==DefaultByteArrayRange==&lt;br /&gt;
As in our previous exercise where we implemented the class [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/range/exercise/Range.html Range], we will need to keep track of the min and maxExclusive.  Since we will also need to track the byte array which the range is associated with, we will need to handle originalCompleteSequence.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|DefaultByteArrayRange|constructor&amp;lt;br/&amp;gt;originalCompleteSequence&amp;lt;br/&amp;gt;min&amp;lt;br/&amp;gt;maxExclusive|kmer.balance.exercise}}&lt;br /&gt;
&lt;br /&gt;
===constructor and instance variables===&lt;br /&gt;
{{SequentialConstructor|public DefaultByteArrayRange(byte[] originalCompleteSequence, int min, int maxExclusive)}}&lt;br /&gt;
&lt;br /&gt;
Store each of the parameters in private final instance variables.&lt;br /&gt;
&lt;br /&gt;
{{Alert|As the name suggests, we are to hang onto the originalCompleteSequence.  Do not copy it or create a sub-array.  Just hang onto the reference in an instance variable. }}&lt;br /&gt;
&lt;br /&gt;
===originalCompleteSequence===&lt;br /&gt;
{{Sequential|public byte[] originalCompleteSequence()}}&lt;br /&gt;
&lt;br /&gt;
Return the originalCompleteSequence passed to the constructor.&lt;br /&gt;
&lt;br /&gt;
===min===&lt;br /&gt;
{{Sequential|public int min()}}&lt;br /&gt;
&lt;br /&gt;
Return the min passed to the constructor.&lt;br /&gt;
&lt;br /&gt;
===maxExclusive===&lt;br /&gt;
{{Sequential|int maxExclusive()}}&lt;br /&gt;
&lt;br /&gt;
Return the maxExclusive passed to the constructor.&lt;br /&gt;
&lt;br /&gt;
==DefaultKMerBalancer==&lt;br /&gt;
&lt;br /&gt;
DefaultKMerBalancer exists to balance the workload of a list of dna sub-sequences.  We have balanced workload before by breaking up data into as equally sized slices in the [[Ranges]] assignment.  We used the sliced up Ranges in everything from the [[Coarsening_Nucleobase_Count_Assignment|Coarsening Nucleobase Count]] to [[Matrix_MapReduce_Framework_Assignment|Matrix MapReduce]].&lt;br /&gt;
&lt;br /&gt;
K-mer counting presents an additional challenge of having to deal with sub-sequences of radically different lengths.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|DefaultKMerBalancer|sliceKernel&amp;lt;br/&amp;gt;createSlices&amp;lt;br/&amp;gt;calculateReasonableThreshold|kmer.balance.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{ tip | Get DefaultKMerBalancer working and then use it to balance all of your parallel KMerCounter implementations. }}&lt;br /&gt;
&lt;br /&gt;
'''Note: you will implement two methods: createSlices which will invoke the second method sliceKernel for each of the sequences.'''&lt;br /&gt;
&lt;br /&gt;
===createSlices===&lt;br /&gt;
&lt;br /&gt;
{{Sequential|List&amp;lt;ByteArrayRange&amp;gt; createSlices(List&amp;lt;byte[]&amp;gt; sequences, int k, IntPredicate slicePredicate)}}&lt;br /&gt;
&lt;br /&gt;
{{warning | be sure to slice the offset (a.k.a. startingIndex) space}}&lt;br /&gt;
&lt;br /&gt;
for N=10 and K=3, you should be slicing like this:&lt;br /&gt;
&lt;br /&gt;
[[File:ThresholdSlices.svg|320px]]&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|ThresholdSlices|&amp;lt;youtube&amp;gt;m56ee5spxiw&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
NOTE: you will want to invoke sliceKernel with each sequence.&lt;br /&gt;
&lt;br /&gt;
===sliceKernel===&lt;br /&gt;
{{Sequential|private static void sliceKernel(List&amp;lt;ByteArrayRange&amp;gt; slices, byte[] sequence, int min, int max,&lt;br /&gt;
			IntPredicate slicePredicate)}}&lt;br /&gt;
&lt;br /&gt;
Here we will employ a divide-and-conquer like strategy to fill the slices list with ByteArrayRanges that are all not too long.  We leverage the provided slicePredicate to test whether or not a range length from min to max is long enough to warrant dividing further.&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&lt;br /&gt;
This class is designed to take in a list of bytes and convert it into a list of slices. The slice class should be familiar to you from earlier in the semester, but feel free to refer back to the documentation on [[Slices]] for a quick recap. You should perform this task recursively and there are two methods you will need to implement. The createSlices method should just call the kernel (the &amp;quot;helper&amp;quot; method), and the kernel performs the recursion.&lt;br /&gt;
&lt;br /&gt;
In the createSlices method, you should call the recursive kernel for each sequence that is longer than k in the list of sequences. When defining the min and max values, keep the information mentioned in the &amp;quot;Background&amp;quot; section in mind. As for the kernel, it should add a new slice to the collection of slices once the slice length dips below the provided threshold, but it should otherwise split itself into two smaller slices using recursion.&lt;br /&gt;
&lt;br /&gt;
In our previous uses of Slices we leveraged sliceIndexId (and not originalData) since it was a n-way split/coarsening scenario.  Here we are recursively splitting each item in a collection of sub-sequences so originalData will be used (and sliceIndexId is unnecessary and can be specified as invalid (-1))..&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===threshold calculation===&lt;br /&gt;
{{Sequential|public static int calculateReasonableThreshold(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
Define the threshold of slice length used to define an appropriate slicePredicate. &lt;br /&gt;
&lt;br /&gt;
Question to ask yourself: given a list of sequences which are to be k-mer counted, how would you calculate a reasonable threshold?&lt;br /&gt;
&lt;br /&gt;
In addition to an estimation of the work required to count the specified sequences, knowing the number of [https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#availableProcessors() available processors] will also be necessary.&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|availableProcessors|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
int processorCount = Runtime.getRuntime().availableProcessors();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__BalanceTestSuite|kmer.balance.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Pledge, Acknowledgments, Citations=&lt;br /&gt;
{{Pledge|kmer-balance}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=String_Map_K_Mer_Assignment&amp;diff=5427</id>
		<title>String Map K Mer Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=String_Map_K_Mer_Assignment&amp;diff=5427"/>
		<updated>2026-04-14T00:25:17Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* StringConcurrentMapUnbalancedKMerCounter */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Group Assignment=&lt;br /&gt;
This is a group assignment.&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
&lt;br /&gt;
==java.util==&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html HashMap]&lt;br /&gt;
==java.util.concurrent==&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html ConcurrentHashMap]&lt;br /&gt;
&lt;br /&gt;
==StringKMers==&lt;br /&gt;
The following method should be useful as you build the assignment.&lt;br /&gt;
&lt;br /&gt;
===String toString(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Stores the information from the given sequence into a String. For example, if&lt;br /&gt;
 * you had the sequence, &amp;quot;ACCTGTCAAAA&amp;quot; and you called this method with an offset&lt;br /&gt;
 * of 1 and a k of 4, it would return &amp;quot;CCTG&amp;quot;.&lt;br /&gt;
 * &lt;br /&gt;
 * @param sequence the sequence of nucleobases to draw the bytes from&lt;br /&gt;
 * @param offset   the offset for where to start looking for bytes&lt;br /&gt;
 * @param k        the length of the k-mer to make a String for&lt;br /&gt;
 * @return a String representation of the k-mer at the desired position&lt;br /&gt;
 */&lt;br /&gt;
public static String toString(byte[] sequence, int offset, int k) {&lt;br /&gt;
	return new String(sequence, offset, k, StandardCharsets.UTF_8);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==KMerResults==&lt;br /&gt;
When we have completed our k-mer counting, we need to return the results.  Since we will have multiple k-mer counters with several different data structures, we are provided with a common [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/KMerResults.html interface KMerResults].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public interface KMerResults {&lt;br /&gt;
	Iterable&amp;lt;byte[]&amp;gt; foundKMers();&lt;br /&gt;
&lt;br /&gt;
	int getCount(byte[] kMer);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===StringMapKMerResults===&lt;br /&gt;
For this particular assignment we will be using instances of Map&amp;lt;String, Integer&amp;gt;, so the provided [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/results/StringMapKMerResults.html class StringMapKMerResults] will serve well:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public final class StringMapKMerResults extends AbstractMapKMerResults&amp;lt;String&amp;gt; {&lt;br /&gt;
	public StringMapKMerResults(int k, Map&amp;lt;String, Integer&amp;gt; map) {&lt;br /&gt;
		super(k, map, StringKMerCodec.INSTANCE);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==StringMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|StringMapKMerCounter|parse|kmer.group}}&lt;br /&gt;
&lt;br /&gt;
===parse===&lt;br /&gt;
{{Sequential|public StringMapKMerResults parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
In this completely sequential implementation, you will have to write the parse method. The method takes in a list of arrays of bytes and a k-mer length. It should return an instance of StringMapKMerResults(which takes in a map), a class provided to you which does exactly what its name suggests.&lt;br /&gt;
&lt;br /&gt;
parse should go through the amount of possible k-mers for every byte array in the list of sequences. As it goes through the bytes in the array, use the [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/fall22/apidocs/kmer/util/StringKMers.html#toString(byte%5B%5D,int,int) StringKMers.toString(sequence, offset, k)] method to create a string to use as a key for the HashMap. The map should take in a String as the key and an Integer as the value. We recommend using the map.compute() method and reviewing how to use lambdas.&lt;br /&gt;
&lt;br /&gt;
==StringConcurrentMapUnbalancedKMerCounter==&lt;br /&gt;
{{CodeToImplement|StringConcurrentMapUnbalancedKMerCounter|parse|kmer.group}}&lt;br /&gt;
&lt;br /&gt;
'''Note: do NOT simply create a StringMapKMerCounter and synchronize it.  Get the per bucket contention relief by recreating the algorithm with a ConcurrentHashMap and compute.'''&lt;br /&gt;
&lt;br /&gt;
===parse===&lt;br /&gt;
{{Parallel|public StringMapKMerResults parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This implementation will make your sequential String HashMap implementation into a parallel one. To do so, you will be making use of Java’s thread-safe version of a Map: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html ConcurrentHashMap]. Like before, you will be need to complete the parse method but this time in parallel.&lt;br /&gt;
&lt;br /&gt;
Recall that the ConcurrentHashMap will NOT prevent data races.  We need to use a method like compute instead of a get/modify/put pattern to prevent atomicity races.&lt;br /&gt;
&lt;br /&gt;
:: ''Get then put is not atomic.''&lt;br /&gt;
:: ''Call compute. Call compute.''&lt;br /&gt;
:: ''Use ConcurrentHashMap. Use ConcurrentHashMap.&lt;br /&gt;
::  ''Or say shoot.  Or say shoot.''&lt;br /&gt;
&lt;br /&gt;
=Videos=&lt;br /&gt;
{{CollapsibleYouTube|ConcurrentHashMap compute|&amp;lt;youtube&amp;gt;4M9abyUyzxU&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|StringHashMapKMerCounter|&amp;lt;youtube&amp;gt;Bwv3QMN0Mk4&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|StringConcurrentHashMapKMerCounter|&amp;lt;youtube&amp;gt;a9CK1BPzESM&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__StringKMerTestSuite|kmer.group}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Balanced_String_Map_K_Mer_Counting_Assignment&amp;diff=5426</id>
		<title>Balanced String Map K Mer Counting Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Balanced_String_Map_K_Mer_Counting_Assignment&amp;diff=5426"/>
		<updated>2026-04-14T00:24:54Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* parse */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Individual Assignment=&lt;br /&gt;
This is an individual assignment.&lt;br /&gt;
&lt;br /&gt;
=Previous Assignment=&lt;br /&gt;
Be sure to complete the [[String_Map_K_Mer_Assignment|unbalanced String Map K-mer Counting group warmup]] first.&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
==StringConcurrentMapBalancedKMerCounter==&lt;br /&gt;
{{CodeToImplement|StringConcurrentMapBalancedKMerCounter|constructor&amp;lt;br/&amp;gt;balancer&amp;lt;br/&amp;gt;concurrentMapFactory&amp;lt;br/&amp;gt;createConcurrentMap&amp;lt;br/&amp;gt;parse|kmer.warmup}}&lt;br /&gt;
&lt;br /&gt;
Note: this class forces a somewhat ridiculous amount of abstraction when it comes to constructing new ConcurrentMaps.  The constructor will be passed a KMerBalancer and Supplier to be used instead of, for example, constructing a DefaultKMerBalancer and ConcurrentHashMaps directly.  This allows the testing to catch errors sooner in an effort to aid debugging.&lt;br /&gt;
&lt;br /&gt;
===constructor and instance variable===&lt;br /&gt;
Hang onto the balancer passed to the constructor in an instance variable so you can use them later.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public StringConcurrentMapBalancedKMerCounter(KMerBalancer balancer)&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===balancer===&lt;br /&gt;
&lt;br /&gt;
Return the balancer passed to the constructor.&lt;br /&gt;
&lt;br /&gt;
===parse===&lt;br /&gt;
{{Parallel|public StringMapKMerResults parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This implementation will convert your StringConcurrentMapUnbalancedKMerCounter implementation into a balanced one.&lt;br /&gt;
&lt;br /&gt;
'''Note: do NOT simply copy the ByteArrayRanges and pass them along to your StringConcurrentMapUnbalancedKMerCounter.  For starters, it will waste a ton of time copying the ranges.  More importantly, it will not work due to the change in how n-k+1 is handled.'''&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|_StringConcurrentMapBalancedKMerTestSuite|kmer.warmup}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Balanced_String_Map_K_Mer_Counting_Assignment&amp;diff=5425</id>
		<title>Balanced String Map K Mer Counting Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Balanced_String_Map_K_Mer_Counting_Assignment&amp;diff=5425"/>
		<updated>2026-04-14T00:24:40Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* parse */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Individual Assignment=&lt;br /&gt;
This is an individual assignment.&lt;br /&gt;
&lt;br /&gt;
=Previous Assignment=&lt;br /&gt;
Be sure to complete the [[String_Map_K_Mer_Assignment|unbalanced String Map K-mer Counting group warmup]] first.&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
==StringConcurrentMapBalancedKMerCounter==&lt;br /&gt;
{{CodeToImplement|StringConcurrentMapBalancedKMerCounter|constructor&amp;lt;br/&amp;gt;balancer&amp;lt;br/&amp;gt;concurrentMapFactory&amp;lt;br/&amp;gt;createConcurrentMap&amp;lt;br/&amp;gt;parse|kmer.warmup}}&lt;br /&gt;
&lt;br /&gt;
Note: this class forces a somewhat ridiculous amount of abstraction when it comes to constructing new ConcurrentMaps.  The constructor will be passed a KMerBalancer and Supplier to be used instead of, for example, constructing a DefaultKMerBalancer and ConcurrentHashMaps directly.  This allows the testing to catch errors sooner in an effort to aid debugging.&lt;br /&gt;
&lt;br /&gt;
===constructor and instance variable===&lt;br /&gt;
Hang onto the balancer passed to the constructor in an instance variable so you can use them later.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public StringConcurrentMapBalancedKMerCounter(KMerBalancer balancer)&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===balancer===&lt;br /&gt;
&lt;br /&gt;
Return the balancer passed to the constructor.&lt;br /&gt;
&lt;br /&gt;
===parse===&lt;br /&gt;
{{Parallel|public StringMapKMerResults parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This implementation will convert your StringConcurrentMapUnbalancedKMerCounter implementation into a balanced one.&lt;br /&gt;
&lt;br /&gt;
''Note: do NOT simply copy the ByteArrayRanges and pass them along to your StringConcurrentMapUnbalancedKMerCounter.  For starters, it will waste a ton of time copying the ranges.  More importantly, it will not work due to the change in how n-k+1 is handled.''&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|_StringConcurrentMapBalancedKMerTestSuite|kmer.warmup}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=String_Map_K_Mer_Assignment&amp;diff=5424</id>
		<title>String Map K Mer Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=String_Map_K_Mer_Assignment&amp;diff=5424"/>
		<updated>2026-04-14T00:21:57Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* StringConcurrentMapUnbalancedKMerCounter */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Group Assignment=&lt;br /&gt;
This is a group assignment.&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
&lt;br /&gt;
==java.util==&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html HashMap]&lt;br /&gt;
==java.util.concurrent==&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html ConcurrentHashMap]&lt;br /&gt;
&lt;br /&gt;
==StringKMers==&lt;br /&gt;
The following method should be useful as you build the assignment.&lt;br /&gt;
&lt;br /&gt;
===String toString(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * Stores the information from the given sequence into a String. For example, if&lt;br /&gt;
 * you had the sequence, &amp;quot;ACCTGTCAAAA&amp;quot; and you called this method with an offset&lt;br /&gt;
 * of 1 and a k of 4, it would return &amp;quot;CCTG&amp;quot;.&lt;br /&gt;
 * &lt;br /&gt;
 * @param sequence the sequence of nucleobases to draw the bytes from&lt;br /&gt;
 * @param offset   the offset for where to start looking for bytes&lt;br /&gt;
 * @param k        the length of the k-mer to make a String for&lt;br /&gt;
 * @return a String representation of the k-mer at the desired position&lt;br /&gt;
 */&lt;br /&gt;
public static String toString(byte[] sequence, int offset, int k) {&lt;br /&gt;
	return new String(sequence, offset, k, StandardCharsets.UTF_8);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==KMerResults==&lt;br /&gt;
When we have completed our k-mer counting, we need to return the results.  Since we will have multiple k-mer counters with several different data structures, we are provided with a common [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/KMerResults.html interface KMerResults].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public interface KMerResults {&lt;br /&gt;
	Iterable&amp;lt;byte[]&amp;gt; foundKMers();&lt;br /&gt;
&lt;br /&gt;
	int getCount(byte[] kMer);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===StringMapKMerResults===&lt;br /&gt;
For this particular assignment we will be using instances of Map&amp;lt;String, Integer&amp;gt;, so the provided [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/results/StringMapKMerResults.html class StringMapKMerResults] will serve well:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public final class StringMapKMerResults extends AbstractMapKMerResults&amp;lt;String&amp;gt; {&lt;br /&gt;
	public StringMapKMerResults(int k, Map&amp;lt;String, Integer&amp;gt; map) {&lt;br /&gt;
		super(k, map, StringKMerCodec.INSTANCE);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==StringMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|StringMapKMerCounter|parse|kmer.group}}&lt;br /&gt;
&lt;br /&gt;
===parse===&lt;br /&gt;
{{Sequential|public StringMapKMerResults parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
In this completely sequential implementation, you will have to write the parse method. The method takes in a list of arrays of bytes and a k-mer length. It should return an instance of StringMapKMerResults(which takes in a map), a class provided to you which does exactly what its name suggests.&lt;br /&gt;
&lt;br /&gt;
parse should go through the amount of possible k-mers for every byte array in the list of sequences. As it goes through the bytes in the array, use the [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/fall22/apidocs/kmer/util/StringKMers.html#toString(byte%5B%5D,int,int) StringKMers.toString(sequence, offset, k)] method to create a string to use as a key for the HashMap. The map should take in a String as the key and an Integer as the value. We recommend using the map.compute() method and reviewing how to use lambdas.&lt;br /&gt;
&lt;br /&gt;
==StringConcurrentMapUnbalancedKMerCounter==&lt;br /&gt;
{{CodeToImplement|StringConcurrentMapUnbalancedKMerCounter|parse|kmer.group}}&lt;br /&gt;
&lt;br /&gt;
''Note: do NOT simply create a StringConcurrentMapUnbalancedKMerCounter and synchronize it.  Get the per bucket contention relief by recreating the algorithm with a ConcurrentHashMap and compute.''&lt;br /&gt;
&lt;br /&gt;
===parse===&lt;br /&gt;
{{Parallel|public StringMapKMerResults parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This implementation will make your sequential String HashMap implementation into a parallel one. To do so, you will be making use of Java’s thread-safe version of a Map: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html ConcurrentHashMap]. Like before, you will be need to complete the parse method but this time in parallel.&lt;br /&gt;
&lt;br /&gt;
Recall that the ConcurrentHashMap will NOT prevent data races.  We need to use a method like compute instead of a get/modify/put pattern to prevent atomicity races.&lt;br /&gt;
&lt;br /&gt;
:: ''Get then put is not atomic.''&lt;br /&gt;
:: ''Call compute. Call compute.''&lt;br /&gt;
:: ''Use ConcurrentHashMap. Use ConcurrentHashMap.&lt;br /&gt;
::  ''Or say shoot.  Or say shoot.''&lt;br /&gt;
&lt;br /&gt;
=Videos=&lt;br /&gt;
{{CollapsibleYouTube|ConcurrentHashMap compute|&amp;lt;youtube&amp;gt;4M9abyUyzxU&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|StringHashMapKMerCounter|&amp;lt;youtube&amp;gt;Bwv3QMN0Mk4&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|StringConcurrentHashMapKMerCounter|&amp;lt;youtube&amp;gt;a9CK1BPzESM&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__StringKMerTestSuite|kmer.group}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Quicksort_Assignment&amp;diff=5423</id>
		<title>Quicksort Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Quicksort_Assignment&amp;diff=5423"/>
		<updated>2026-03-20T18:55:46Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* Partition */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Sorting is a problem that is well solved by divide and conquer algorithms.  Quicksort is an oldie but a goodie.  Developed in 1959 by Tony Hoare, it is still the fastest sort after all these years.  As a bonus, the conquer portion can be easily parallelized.  The divide portion can also be parallelized, although not trivially (which left as an optional fun challenge for those so inclined).&lt;br /&gt;
=Background=&lt;br /&gt;
In computer science, quicksort is a sorting algorithm which selects a pivot value, partitions all the values less than the pivot value on one side and the values greater on the other.  By recursively performing this operation on the ever decreasing range lengths on the left and right, the array is sorted when the range length reaches a size of 1. &lt;br /&gt;
&lt;br /&gt;
For more information on how this process works, visit the [https://en.wikipedia.org/wiki/Quicksort wikipedia page on quicksort].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
{{CollapsibleYouTube|Quicksort|&amp;lt;youtube&amp;gt;SLauY6PpjW4&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|Parallel Quicksort|&amp;lt;youtube&amp;gt;iqnvIeIhGPw&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
==Visualization==&lt;br /&gt;
If you are unclear on how merge sort works, take a look at the [https://visualgo.net/en/sorting?slide=12 visualgo explanation and visualization of quicksort].&lt;br /&gt;
&lt;br /&gt;
[[File:Visalgo quicksort.png|link=https://visualgo.net/en/sorting?slide=12]]&lt;br /&gt;
&lt;br /&gt;
== Work and Span ==&lt;br /&gt;
Partition does the work of quicksort.  The top-level partition must do N work.  The next level does two ~N/2 partitions.  The level below that performs four ~N/4 partitions.  And so on.  The table is &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; wide and &amp;lt;math&amp;gt;\log N&amp;lt;/math&amp;gt; high.  Thus the work of quicksort is &amp;lt;math&amp;gt;O(N\log N)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For N=16:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center;&amp;quot;&lt;br /&gt;
! &lt;br /&gt;
! colspan=&amp;quot;16&amp;quot; | &amp;lt;- - - - - - - - - - - - - - N - - - - - - - - - - - - - -&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! ^ &lt;br /&gt;
| colspan=&amp;quot;16&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 16)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;amp;#124;&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 8)&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; | partition [8, 16)&lt;br /&gt;
|-&lt;br /&gt;
! lg N &lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 4)&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | partition [4, 8)&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | partition [8, 12)&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | partition [12, 16)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;amp;#124;&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 2)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [2, 4)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [4, 6)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [6, 8)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [8, 10)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [10, 12)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [12, 14)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [14, 16)&lt;br /&gt;
|-&lt;br /&gt;
! v &lt;br /&gt;
| style=&amp;quot;background-color:#CCCCFF;&amp;quot; | base&lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The span for the classic sequential partition is one partition of each of the levels &amp;lt;span style=&amp;quot;background:#CCCCFF&amp;quot;&amp;gt;(highlighted in blue)&amp;lt;/span&amp;gt;.  This works out to be the [https://en.wikipedia.org/wiki/Geometric_series geometric series]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N + \frac{N}{2} + \frac{N}{4} + \frac{N}{8} + \frac{N}{16} + \cdots + 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;2N&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Classic_parallel_merge_sort_and_quicksort_span.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
As a [[Quicksort_Parallel_Partitioner|challenge problem]], we parallelize the partition step, resulting in a span for quicksort of an outstanding &amp;lt;math&amp;gt;\lg^k{}n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=Mistakes To Avoid=&lt;br /&gt;
{{warning | Be sure to invoke the Partitioner provided to the Sorter's constructor's partitionRange method.  Do not implement SequentialPartioner and then re-implement it in the Sorters.}}&lt;br /&gt;
{{warning | When checking the base case, remember to account for [minInclusive, maxExclusive).  It is all too easy to get an off by 1 error and stack overflow.}}&lt;br /&gt;
{{warning | When transitioning to sequential, make sure to NOT sort the entire array when you are only responsible for a range [min,max).}}&lt;br /&gt;
&lt;br /&gt;
=WarmUp=&lt;br /&gt;
The [[Join_All_Assignment|JoinAll WarmUp]] prepares you for getting around with ConcurrentLinkedQueue's weakly consistent iterators.  It is highly recommended.&lt;br /&gt;
&lt;br /&gt;
=Client=&lt;br /&gt;
{{Client|QuicksortClient|sort|sort.quick.client}}&lt;br /&gt;
&lt;br /&gt;
Note: Quicksort is [https://en.wikipedia.org/wiki/Sorting_algorithm#Stability not stable].  You can see that after sorting by name, this is not preserved when sorting by length.  For example, the 3 of length 4 come out Hera, Ares, Zeus.&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|QuicksortClient|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
// https://en.wikipedia.org/wiki/Twelve_Olympians&lt;br /&gt;
String[] olympians = {&lt;br /&gt;
        &amp;quot;Zeus&amp;quot;,&lt;br /&gt;
        &amp;quot;Hera&amp;quot;,&lt;br /&gt;
        &amp;quot;Athena&amp;quot;,&lt;br /&gt;
        &amp;quot;Apollo&amp;quot;,&lt;br /&gt;
        &amp;quot;Poseidon&amp;quot;,&lt;br /&gt;
        &amp;quot;Ares&amp;quot;,&lt;br /&gt;
        &amp;quot;Artemis&amp;quot;,&lt;br /&gt;
        &amp;quot;Demeter&amp;quot;,&lt;br /&gt;
        &amp;quot;Aphrodite&amp;quot;,&lt;br /&gt;
        &amp;quot;Dionysus&amp;quot;,&lt;br /&gt;
        &amp;quot;Hermes&amp;quot;,&lt;br /&gt;
        &amp;quot;Hephaestus&amp;quot;,&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
Shuffler&amp;lt;String&amp;gt; shuffler = new LinearShuffler&amp;lt;&amp;gt;(new Random(System.currentTimeMillis()));&lt;br /&gt;
Partitioner&amp;lt;String&amp;gt; partitioner = new SequentialPartitioner&amp;lt;&amp;gt;();&lt;br /&gt;
IntPredicate isParallelDesired = (rangeLength) -&amp;gt; rangeLength &amp;gt; 3;&lt;br /&gt;
Sorter&amp;lt;String&amp;gt; sequentialQuicksorter = new SequentialQuicksorter&amp;lt;&amp;gt;(shuffler, partitioner);&lt;br /&gt;
&lt;br /&gt;
Sorter&amp;lt;String&amp;gt; parallelQuicksorter = new ParallelQuicksorter&amp;lt;&amp;gt;(shuffler, partitioner, isParallelDesired, sequentialQuicksorter);&lt;br /&gt;
&lt;br /&gt;
System.out.println(&amp;quot;        original: &amp;quot; + Arrays.toString(olympians));&lt;br /&gt;
&lt;br /&gt;
parallelQuicksorter.sort(olympians, Comparator.naturalOrder());&lt;br /&gt;
System.out.println(&amp;quot;  sorted by name: &amp;quot; + Arrays.toString(olympians));&lt;br /&gt;
&lt;br /&gt;
Comparator&amp;lt;String&amp;gt; lengthComparator = (a, b) -&amp;gt; b.length() - a.length();&lt;br /&gt;
parallelQuicksorter.sort(olympians, lengthComparator);&lt;br /&gt;
System.out.println(&amp;quot;sorted by length: &amp;quot; + Arrays.toString(olympians));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|QuicksortClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;        original: [Zeus, Hera, Athena, Apollo, Poseidon, Ares, Artemis, Demeter, Aphrodite, Dionysus, Hermes, Hephaestus]&lt;br /&gt;
  sorted by name: [Aphrodite, Apollo, Ares, Artemis, Athena, Demeter, Dionysus, Hephaestus, Hera, Hermes, Poseidon, Zeus]&lt;br /&gt;
sorted by length: [Hephaestus, Aphrodite, Dionysus, Poseidon, Artemis, Demeter, Athena, Hermes, Apollo, Hera, Ares, Zeus]&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Code to Investigate=&lt;br /&gt;
==Index Selection==&lt;br /&gt;
===InitialIndexSelector&amp;lt;T&amp;gt;===&lt;br /&gt;
The partition step of quicksort starts by selecting an index in an array to serve as the pivot value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public interface InitialIndexSelector&amp;lt;T&amp;gt; {&lt;br /&gt;
	int selectInitialIndex(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===RandomInitialIndexSelector&amp;lt;T&amp;gt;===&lt;br /&gt;
Randomly selecting the initial index is the go to strategy to achieve provable runtime performance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public final class RandomInitialIndexSelector&amp;lt;T&amp;gt; implements InitialIndexSelector&amp;lt;T&amp;gt; {&lt;br /&gt;
	private final Random random;&lt;br /&gt;
&lt;br /&gt;
	public RandomInitialIndexSelector(Random random) {&lt;br /&gt;
		this.random = random;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public int selectInitialIndex(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive) {&lt;br /&gt;
		int range = maxExclusive - min;&lt;br /&gt;
		return min + random.nextInt(range);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Partition Sub Range Pair==&lt;br /&gt;
===SubRangePair===&lt;br /&gt;
The partition step of quicksort... well... partitions the array.  After a pivot value is selected, partition will not necessarily sort the array, but all of the values less than the pivot value will be in the lower part of the range and all of the values greater than the pivot value will be in the higher part of the array.&lt;br /&gt;
&lt;br /&gt;
There are several different approaches to partition and there can be different pivot location characteristics.  [https://www.youtube.com/watch?v=SLauY6PpjW4 Another approach] does even place the pivot value itself if at the pivot location and therefore the range of the pivot location is 0 in length!  Another approach places the single value at the selected index at the pivot location so the range length is 1.  Finally, one partition approach addresses the [https://en.wikipedia.org/wiki/Dutch_national_flag_problem Dutch National Flag Problem] by placing all values equal to the pivot value together in the center, therefore the range length of the pivot location can be &amp;gt;= 1.  &lt;br /&gt;
&lt;br /&gt;
To account for this variable pivot location range length, a class PivotLocation is provided:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public record SubRangePair(Range lowerSubRange, Range upperSubRange) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If a partitioner is called upon to partition a range [min, maxExclusive) it will return a SubRangePair.  The lowerSubRange will always have partition's min as its min.  The upperSubRange will always have the partition's maxExclusive as its maxExclusive.  The elements in the lowerSubRange must be less than the pivot value.  The elements between the lowerSubRange and upperSubRange must be equal to the pivot value. The elements in the upperSubRange must  be greater than the pivot value.&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
Both the sequential and parallel quicksorts will be passed a [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/sort/quick/core/Partitioner.html Partitioner].  Partitioning occurs during the divide phase of divide-and-conquer of the general recursive case.  &lt;br /&gt;
&lt;br /&gt;
You will need to implement quicksort sequentially and in parallel.  Both implementations will naturally be recursive. The kernel method should call itself using recursion, but each public quicksort method should only call its kernel once to do the work.&lt;br /&gt;
==Partition==&lt;br /&gt;
Partition performs the grand majority of the work in quicksort.  A sequential partitioner is required.  A parallel partitioner is left as a challenge problem.  Thus, there are separate classes to implement.&lt;br /&gt;
&lt;br /&gt;
==AbstractPartitioner==&lt;br /&gt;
AbstractPartitioner simply keeps track of the InitialIndexSelector.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|AbstractPartitioner|constructor&amp;lt;br&amp;gt;initialIndexSelector|sort.quick.exercise}}&lt;br /&gt;
&lt;br /&gt;
===constructor and instance variable===&lt;br /&gt;
Simply hang onto the specified initialIndexSelector in an instance variable for later use.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public AbstractPartitioner(InitialIndexSelector&amp;lt;T&amp;gt; initialIndexSelector) {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===initialIndexSelector===&lt;br /&gt;
Return the previously specified to the constructor initialIndexSelector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public InitialIndexSelector&amp;lt;T&amp;gt; initialIndexSelector() {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==SequentialPartitioner==&lt;br /&gt;
{{CodeToImplement|SequentialPartitioner|partitionRange|sort.quick.exercise}}&lt;br /&gt;
&lt;br /&gt;
===partitionRange===&lt;br /&gt;
{{Sequential|public SubRangePair partitionRange(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive)}}&lt;br /&gt;
&lt;br /&gt;
There are [https://en.wikipedia.org/wiki/Quicksort#Algorithm many approaches to partition] with different performance characteristics.&lt;br /&gt;
&lt;br /&gt;
{{Requirements4|All values left of the returned PivotLocation must be less than or equal to the value at the PivotLocation|All values right of the returned PivotLocation must be greater than or equal to the value at the PivotLocation.|All values at the Pivot Location must be equal.|The pivot location must divide the problem into smaller sub-problems.}}&lt;br /&gt;
&lt;br /&gt;
{{Alert|The HackerRank approach from the prep does not maintain the pivot value at the PivotLocation.&amp;lt;br/&amp;gt;Thus the range between the pivot location's maxExclusiveOfLesserSide and minOfGreaterSide will be 0 in length.&amp;lt;br/&amp;gt;As a result, care must be taken to ensure that partition produces a pivot such that there are always smaller sub-problems.}}&lt;br /&gt;
&lt;br /&gt;
{{Alert|The HackerRank approach from the prep chooses the value at the midpoint as its pivot value.&amp;lt;br/&amp;gt;Critically, it calculates the midpoint based on the *inclusive* maximum.&amp;lt;br/&amp;gt;Believe it or not, the entire algorithm death spirals if you choose the midpoint based on the exclusive max. }}&lt;br /&gt;
&lt;br /&gt;
Although an interesting challenge, you are not required handle the [https://en.wikipedia.org/wiki/Dutch_national_flag_problem Dutch national flag problem].&lt;br /&gt;
&lt;br /&gt;
==Sort==&lt;br /&gt;
==interface Sorter==&lt;br /&gt;
{{CodeToImplement|Sorter|sort|sort.core}}&lt;br /&gt;
&lt;br /&gt;
===sort===&lt;br /&gt;
{{Sequential|default void sort(T[] data, Comparator&amp;lt;T&amp;gt; comparator)}}&lt;br /&gt;
&lt;br /&gt;
This should be already complete from the [[MergeSort|Merge Sort Exercise]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==class SequentialQuicksorter==&lt;br /&gt;
{{CodeToImplement|SequentialQuicksorter|sortRange|sort.quick.exercise}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public class SequentialQuicksorter&amp;lt;T&amp;gt; extends AbstractQuicksorter&amp;lt;T&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===constructor and instance variable===&lt;br /&gt;
Simply hang onto the specified partitioner in an instance variable for later use.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public SequentialQuicksorter(Partitioner&amp;lt;T&amp;gt; partitioner) {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===partitioner===&lt;br /&gt;
Return the previously specified to the constructor partitioner&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public Partitioner&amp;lt;T&amp;gt; partitioner() {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===sortRange===&lt;br /&gt;
{{Sequential|public void sortRange(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive)}}&lt;br /&gt;
&lt;br /&gt;
Implement the classic divide and conquer sequential algorithm here.  &lt;br /&gt;
&lt;br /&gt;
{{Alert|Be sure to leverage the Partitioner passed to the constructor to perform the divide step.}}&lt;br /&gt;
&lt;br /&gt;
==class ParallelQuicksorter==&lt;br /&gt;
{{CodeToImplement|ParallelQuicksorter|partitioner&amp;lt;br/&amp;gt;isParallelDesired&amp;lt;br/&amp;gt;sequentialSorter&amp;lt;br/&amp;gt;sortShuffledRangeHelper&amp;lt;br/&amp;gt;sortShuffledRange|sort.quick.exercise}}&lt;br /&gt;
&lt;br /&gt;
Adapt your sequential algorithm to take advantage of what can be parallelized.&lt;br /&gt;
&lt;br /&gt;
What can be in parallel?&lt;br /&gt;
&lt;br /&gt;
What is dependent on the parallel tasks completing?&lt;br /&gt;
&lt;br /&gt;
{{Warning|Leverage the provided &amp;lt;code&amp;gt;private final IntPredicate isParallelDesired;&amp;lt;/code&amp;gt; field to test whether or not parallelism is warranted for the current range length.}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|Fall back to the provided &amp;lt;code&amp;gt;private final Sorter&amp;lt;T&amp;gt; sequentialSorter;&amp;lt;/code&amp;gt; field when the &amp;lt;code&amp;gt;isParallelDesired&amp;lt;/code&amp;gt; predicate indicates that parallelism is not longer warranted.}}&lt;br /&gt;
&lt;br /&gt;
===constructor and instance variables===&lt;br /&gt;
Hang onto each of the specified parameters in instance variables for later use.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public ParallelQuicksorter(Partitioner&amp;lt;T&amp;gt; partitioner, IntPredicate isParallelDesired, Sorter&amp;lt;T&amp;gt; sequentialSorter) {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===partitioner===&lt;br /&gt;
{{Sequential|public Partitioner&amp;lt;T&amp;gt; partitioner()}}&lt;br /&gt;
&lt;br /&gt;
===isParallelDesired===&lt;br /&gt;
{{Sequential|public IntPredicate isParallelDesired()}}&lt;br /&gt;
&lt;br /&gt;
===sequentialSorter===&lt;br /&gt;
{{Sequential|public Sorter&amp;lt;T&amp;gt; sequentialSorter()}}&lt;br /&gt;
&lt;br /&gt;
===sortRangeHelper===&lt;br /&gt;
{{Parallel|private void sortRangeHelper(Queue&amp;lt;Future&amp;lt;Void&amp;gt;&amp;gt; futures, T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive)}}&lt;br /&gt;
&lt;br /&gt;
This method implements the essence of quicksort.&lt;br /&gt;
&lt;br /&gt;
Test this isParallelDesired predicate on the range length first.  If it passes, invoke the partitioner to perform the majority of the work followed by appropriately parallel recursive tasks.  If the predicate does not pass the test, simply invoke the sequentialSorter.&lt;br /&gt;
&lt;br /&gt;
{{Requirement|Do not invoke the partitioner if the predicate fails the test.  Let the sequential sorter handle it from here.}}&lt;br /&gt;
&lt;br /&gt;
{{Requirements2|All forked tasks must be added to the futures queue.|Do not join the tasks in this method.  Joining is the responsibility of the sortShuffledRange method.}}&lt;br /&gt;
&lt;br /&gt;
===sortRange===&lt;br /&gt;
This method is partially provided.  Invokes sortRangeHelper on the complete range, and passes an additional ConcurrentLinkedQueue to capture the Futures associated with any and all forked tasks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
protected void sortRange(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive) throws InterruptedException, ExecutionException {&lt;br /&gt;
	Queue&amp;lt;Future&amp;lt;Void&amp;gt;&amp;gt; futures = new ConcurrentLinkedQueue&amp;lt;&amp;gt;();&lt;br /&gt;
	sortShuffledRangeHelper(futures, data, comparator, min, maxExclusive);&lt;br /&gt;
&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When sortRangeHelper is complete, each task in the queue must be joined.  Since ConcurrentLinkedQueue's iterators are weakly consistent, and all of the tasks may not yet be in the queue, we must join all the tasks as in the [[Join_All_Assignment|Join All warmup]].&lt;br /&gt;
&lt;br /&gt;
{{Alert|do not call sortRange recursively.  sortRangeHelper should do the recursive work.}}&lt;br /&gt;
&lt;br /&gt;
=Challenge=&lt;br /&gt;
You can divide and conquer the divide step in quicksort.  The pan can be improved from &amp;lt;math&amp;gt;\mathcal{O}(n)&amp;lt;/math&amp;gt; down to &amp;lt;math&amp;gt;\mathcal{O}(\lg^k{}n)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For details on how to complete this challenge, check out: [[Parallel_Partition|Parallel Partition]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__QuicksortTestSuite|sort.quick.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Pledge, Acknowledgments, Citations=&lt;br /&gt;
{{Pledge|quicksort}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Quicksort_Assignment&amp;diff=5422</id>
		<title>Quicksort Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Quicksort_Assignment&amp;diff=5422"/>
		<updated>2026-03-20T18:11:58Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* Partition */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Sorting is a problem that is well solved by divide and conquer algorithms.  Quicksort is an oldie but a goodie.  Developed in 1959 by Tony Hoare, it is still the fastest sort after all these years.  As a bonus, the conquer portion can be easily parallelized.  The divide portion can also be parallelized, although not trivially (which left as an optional fun challenge for those so inclined).&lt;br /&gt;
=Background=&lt;br /&gt;
In computer science, quicksort is a sorting algorithm which selects a pivot value, partitions all the values less than the pivot value on one side and the values greater on the other.  By recursively performing this operation on the ever decreasing range lengths on the left and right, the array is sorted when the range length reaches a size of 1. &lt;br /&gt;
&lt;br /&gt;
For more information on how this process works, visit the [https://en.wikipedia.org/wiki/Quicksort wikipedia page on quicksort].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
{{CollapsibleYouTube|Quicksort|&amp;lt;youtube&amp;gt;SLauY6PpjW4&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|Parallel Quicksort|&amp;lt;youtube&amp;gt;iqnvIeIhGPw&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
==Visualization==&lt;br /&gt;
If you are unclear on how merge sort works, take a look at the [https://visualgo.net/en/sorting?slide=12 visualgo explanation and visualization of quicksort].&lt;br /&gt;
&lt;br /&gt;
[[File:Visalgo quicksort.png|link=https://visualgo.net/en/sorting?slide=12]]&lt;br /&gt;
&lt;br /&gt;
== Work and Span ==&lt;br /&gt;
Partition does the work of quicksort.  The top-level partition must do N work.  The next level does two ~N/2 partitions.  The level below that performs four ~N/4 partitions.  And so on.  The table is &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; wide and &amp;lt;math&amp;gt;\log N&amp;lt;/math&amp;gt; high.  Thus the work of quicksort is &amp;lt;math&amp;gt;O(N\log N)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For N=16:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center;&amp;quot;&lt;br /&gt;
! &lt;br /&gt;
! colspan=&amp;quot;16&amp;quot; | &amp;lt;- - - - - - - - - - - - - - N - - - - - - - - - - - - - -&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! ^ &lt;br /&gt;
| colspan=&amp;quot;16&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 16)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;amp;#124;&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 8)&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; | partition [8, 16)&lt;br /&gt;
|-&lt;br /&gt;
! lg N &lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 4)&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | partition [4, 8)&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | partition [8, 12)&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | partition [12, 16)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;amp;#124;&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 2)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [2, 4)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [4, 6)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [6, 8)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [8, 10)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [10, 12)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [12, 14)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [14, 16)&lt;br /&gt;
|-&lt;br /&gt;
! v &lt;br /&gt;
| style=&amp;quot;background-color:#CCCCFF;&amp;quot; | base&lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The span for the classic sequential partition is one partition of each of the levels &amp;lt;span style=&amp;quot;background:#CCCCFF&amp;quot;&amp;gt;(highlighted in blue)&amp;lt;/span&amp;gt;.  This works out to be the [https://en.wikipedia.org/wiki/Geometric_series geometric series]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N + \frac{N}{2} + \frac{N}{4} + \frac{N}{8} + \frac{N}{16} + \cdots + 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;2N&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Classic_parallel_merge_sort_and_quicksort_span.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
As a [[Quicksort_Parallel_Partitioner|challenge problem]], we parallelize the partition step, resulting in a span for quicksort of an outstanding &amp;lt;math&amp;gt;\lg^k{}n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=Mistakes To Avoid=&lt;br /&gt;
{{warning | Be sure to invoke the Partitioner provided to the Sorter's constructor's partitionRange method.  Do not implement SequentialPartioner and then re-implement it in the Sorters.}}&lt;br /&gt;
{{warning | When checking the base case, remember to account for [minInclusive, maxExclusive).  It is all too easy to get an off by 1 error and stack overflow.}}&lt;br /&gt;
{{warning | When transitioning to sequential, make sure to NOT sort the entire array when you are only responsible for a range [min,max).}}&lt;br /&gt;
&lt;br /&gt;
=WarmUp=&lt;br /&gt;
The [[Join_All_Assignment|JoinAll WarmUp]] prepares you for getting around with ConcurrentLinkedQueue's weakly consistent iterators.  It is highly recommended.&lt;br /&gt;
&lt;br /&gt;
=Client=&lt;br /&gt;
{{Client|QuicksortClient|sort|sort.quick.client}}&lt;br /&gt;
&lt;br /&gt;
Note: Quicksort is [https://en.wikipedia.org/wiki/Sorting_algorithm#Stability not stable].  You can see that after sorting by name, this is not preserved when sorting by length.  For example, the 3 of length 4 come out Hera, Ares, Zeus.&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|QuicksortClient|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
// https://en.wikipedia.org/wiki/Twelve_Olympians&lt;br /&gt;
String[] olympians = {&lt;br /&gt;
        &amp;quot;Zeus&amp;quot;,&lt;br /&gt;
        &amp;quot;Hera&amp;quot;,&lt;br /&gt;
        &amp;quot;Athena&amp;quot;,&lt;br /&gt;
        &amp;quot;Apollo&amp;quot;,&lt;br /&gt;
        &amp;quot;Poseidon&amp;quot;,&lt;br /&gt;
        &amp;quot;Ares&amp;quot;,&lt;br /&gt;
        &amp;quot;Artemis&amp;quot;,&lt;br /&gt;
        &amp;quot;Demeter&amp;quot;,&lt;br /&gt;
        &amp;quot;Aphrodite&amp;quot;,&lt;br /&gt;
        &amp;quot;Dionysus&amp;quot;,&lt;br /&gt;
        &amp;quot;Hermes&amp;quot;,&lt;br /&gt;
        &amp;quot;Hephaestus&amp;quot;,&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
Shuffler&amp;lt;String&amp;gt; shuffler = new LinearShuffler&amp;lt;&amp;gt;(new Random(System.currentTimeMillis()));&lt;br /&gt;
Partitioner&amp;lt;String&amp;gt; partitioner = new SequentialPartitioner&amp;lt;&amp;gt;();&lt;br /&gt;
IntPredicate isParallelDesired = (rangeLength) -&amp;gt; rangeLength &amp;gt; 3;&lt;br /&gt;
Sorter&amp;lt;String&amp;gt; sequentialQuicksorter = new SequentialQuicksorter&amp;lt;&amp;gt;(shuffler, partitioner);&lt;br /&gt;
&lt;br /&gt;
Sorter&amp;lt;String&amp;gt; parallelQuicksorter = new ParallelQuicksorter&amp;lt;&amp;gt;(shuffler, partitioner, isParallelDesired, sequentialQuicksorter);&lt;br /&gt;
&lt;br /&gt;
System.out.println(&amp;quot;        original: &amp;quot; + Arrays.toString(olympians));&lt;br /&gt;
&lt;br /&gt;
parallelQuicksorter.sort(olympians, Comparator.naturalOrder());&lt;br /&gt;
System.out.println(&amp;quot;  sorted by name: &amp;quot; + Arrays.toString(olympians));&lt;br /&gt;
&lt;br /&gt;
Comparator&amp;lt;String&amp;gt; lengthComparator = (a, b) -&amp;gt; b.length() - a.length();&lt;br /&gt;
parallelQuicksorter.sort(olympians, lengthComparator);&lt;br /&gt;
System.out.println(&amp;quot;sorted by length: &amp;quot; + Arrays.toString(olympians));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|QuicksortClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;        original: [Zeus, Hera, Athena, Apollo, Poseidon, Ares, Artemis, Demeter, Aphrodite, Dionysus, Hermes, Hephaestus]&lt;br /&gt;
  sorted by name: [Aphrodite, Apollo, Ares, Artemis, Athena, Demeter, Dionysus, Hephaestus, Hera, Hermes, Poseidon, Zeus]&lt;br /&gt;
sorted by length: [Hephaestus, Aphrodite, Dionysus, Poseidon, Artemis, Demeter, Athena, Hermes, Apollo, Hera, Ares, Zeus]&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Code to Investigate=&lt;br /&gt;
==Index Selection==&lt;br /&gt;
===InitialIndexSelector&amp;lt;T&amp;gt;===&lt;br /&gt;
The partition step of quicksort starts by selecting an index in an array to serve as the pivot value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public interface InitialIndexSelector&amp;lt;T&amp;gt; {&lt;br /&gt;
	int selectInitialIndex(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===RandomInitialIndexSelector&amp;lt;T&amp;gt;===&lt;br /&gt;
Randomly selecting the initial index is the go to strategy to achieve provable runtime performance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public final class RandomInitialIndexSelector&amp;lt;T&amp;gt; implements InitialIndexSelector&amp;lt;T&amp;gt; {&lt;br /&gt;
	private final Random random;&lt;br /&gt;
&lt;br /&gt;
	public RandomInitialIndexSelector(Random random) {&lt;br /&gt;
		this.random = random;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public int selectInitialIndex(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive) {&lt;br /&gt;
		int range = maxExclusive - min;&lt;br /&gt;
		return min + random.nextInt(range);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Partition Sub Range Pair==&lt;br /&gt;
===SubRangePair===&lt;br /&gt;
The partition step of quicksort... well... partitions the array.  After a pivot value is selected, partition will not necessarily sort the array, but all of the values less than the pivot value will be in the lower part of the range and all of the values greater than the pivot value will be in the higher part of the array.&lt;br /&gt;
&lt;br /&gt;
There are several different approaches to partition and there can be different pivot location characteristics.  [https://www.youtube.com/watch?v=SLauY6PpjW4 Another approach] does even place the pivot value itself if at the pivot location and therefore the range of the pivot location is 0 in length!  Another approach places the single value at the selected index at the pivot location so the range length is 1.  Finally, one partition approach addresses the [https://en.wikipedia.org/wiki/Dutch_national_flag_problem Dutch National Flag Problem] by placing all values equal to the pivot value together in the center, therefore the range length of the pivot location can be &amp;gt;= 1.  &lt;br /&gt;
&lt;br /&gt;
To account for this variable pivot location range length, a class PivotLocation is provided:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public record SubRangePair(Range lowerSubRange, Range upperSubRange) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If a partitioner is called upon to partition a range [min, maxExclusive) it will return a SubRangePair.  The lowerSubRange will always have partition's min as its min.  The upperSubRange will always have the partition's maxExclusive as its maxExclusive.  The elements in the lowerSubRange must be less than the pivot value.  The elements between the lowerSubRange and upperSubRange must be equal to the pivot value. The elements in the upperSubRange must  be greater than the pivot value.&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
Both the sequential and parallel quicksorts will be passed a [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/sort/quick/core/Partitioner.html Partitioner].  Partitioning occurs during the divide phase of divide-and-conquer of the general recursive case.  &lt;br /&gt;
&lt;br /&gt;
You will need to implement quicksort sequentially and in parallel.  Both implementations will naturally be recursive. The kernel method should call itself using recursion, but each public quicksort method should only call its kernel once to do the work.&lt;br /&gt;
==Partition==&lt;br /&gt;
Partition performs the grand majority of the work in quicksort.  A sequential partitioner is required.  A [[Quicksort_Parallel_Partitioner|parallel partitioner is left as a challenge problem]].  Thus, there are separate classes to implement.&lt;br /&gt;
&lt;br /&gt;
==AbstractPartitioner==&lt;br /&gt;
AbstractPartitioner simply keeps track of the InitialIndexSelector.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|AbstractPartitioner|constructor&amp;lt;br&amp;gt;initialIndexSelector|sort.quick.exercise}}&lt;br /&gt;
&lt;br /&gt;
===constructor and instance variable===&lt;br /&gt;
Simply hang onto the specified initialIndexSelector in an instance variable for later use.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public AbstractPartitioner(InitialIndexSelector&amp;lt;T&amp;gt; initialIndexSelector) {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===initialIndexSelector===&lt;br /&gt;
Return the previously specified to the constructor initialIndexSelector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public InitialIndexSelector&amp;lt;T&amp;gt; initialIndexSelector() {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==SequentialPartitioner==&lt;br /&gt;
{{CodeToImplement|SequentialPartitioner|partitionRange|sort.quick.exercise}}&lt;br /&gt;
&lt;br /&gt;
===partitionRange===&lt;br /&gt;
{{Sequential|public SubRangePair partitionRange(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive)}}&lt;br /&gt;
&lt;br /&gt;
There are [https://en.wikipedia.org/wiki/Quicksort#Algorithm many approaches to partition] with different performance characteristics.&lt;br /&gt;
&lt;br /&gt;
{{Requirements4|All values left of the returned PivotLocation must be less than or equal to the value at the PivotLocation|All values right of the returned PivotLocation must be greater than or equal to the value at the PivotLocation.|All values at the Pivot Location must be equal.|The pivot location must divide the problem into smaller sub-problems.}}&lt;br /&gt;
&lt;br /&gt;
{{Alert|The HackerRank approach from the prep does not maintain the pivot value at the PivotLocation.&amp;lt;br/&amp;gt;Thus the range between the pivot location's maxExclusiveOfLesserSide and minOfGreaterSide will be 0 in length.&amp;lt;br/&amp;gt;As a result, care must be taken to ensure that partition produces a pivot such that there are always smaller sub-problems.}}&lt;br /&gt;
&lt;br /&gt;
{{Alert|The HackerRank approach from the prep chooses the value at the midpoint as its pivot value.&amp;lt;br/&amp;gt;Critically, it calculates the midpoint based on the *inclusive* maximum.&amp;lt;br/&amp;gt;Believe it or not, the entire algorithm death spirals if you choose the midpoint based on the exclusive max. }}&lt;br /&gt;
&lt;br /&gt;
Although an interesting challenge, you are not required handle the [https://en.wikipedia.org/wiki/Dutch_national_flag_problem Dutch national flag problem].&lt;br /&gt;
&lt;br /&gt;
==Sort==&lt;br /&gt;
==interface Sorter==&lt;br /&gt;
{{CodeToImplement|Sorter|sort|sort.core}}&lt;br /&gt;
&lt;br /&gt;
===sort===&lt;br /&gt;
{{Sequential|default void sort(T[] data, Comparator&amp;lt;T&amp;gt; comparator)}}&lt;br /&gt;
&lt;br /&gt;
This should be already complete from the [[MergeSort|Merge Sort Exercise]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==class SequentialQuicksorter==&lt;br /&gt;
{{CodeToImplement|SequentialQuicksorter|sortRange|sort.quick.exercise}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public class SequentialQuicksorter&amp;lt;T&amp;gt; extends AbstractQuicksorter&amp;lt;T&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===constructor and instance variable===&lt;br /&gt;
Simply hang onto the specified partitioner in an instance variable for later use.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public SequentialQuicksorter(Partitioner&amp;lt;T&amp;gt; partitioner) {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===partitioner===&lt;br /&gt;
Return the previously specified to the constructor partitioner&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public Partitioner&amp;lt;T&amp;gt; partitioner() {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===sortRange===&lt;br /&gt;
{{Sequential|public void sortRange(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive)}}&lt;br /&gt;
&lt;br /&gt;
Implement the classic divide and conquer sequential algorithm here.  &lt;br /&gt;
&lt;br /&gt;
{{Alert|Be sure to leverage the Partitioner passed to the constructor to perform the divide step.}}&lt;br /&gt;
&lt;br /&gt;
==class ParallelQuicksorter==&lt;br /&gt;
{{CodeToImplement|ParallelQuicksorter|partitioner&amp;lt;br/&amp;gt;isParallelDesired&amp;lt;br/&amp;gt;sequentialSorter&amp;lt;br/&amp;gt;sortShuffledRangeHelper&amp;lt;br/&amp;gt;sortShuffledRange|sort.quick.exercise}}&lt;br /&gt;
&lt;br /&gt;
Adapt your sequential algorithm to take advantage of what can be parallelized.&lt;br /&gt;
&lt;br /&gt;
What can be in parallel?&lt;br /&gt;
&lt;br /&gt;
What is dependent on the parallel tasks completing?&lt;br /&gt;
&lt;br /&gt;
{{Warning|Leverage the provided &amp;lt;code&amp;gt;private final IntPredicate isParallelDesired;&amp;lt;/code&amp;gt; field to test whether or not parallelism is warranted for the current range length.}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|Fall back to the provided &amp;lt;code&amp;gt;private final Sorter&amp;lt;T&amp;gt; sequentialSorter;&amp;lt;/code&amp;gt; field when the &amp;lt;code&amp;gt;isParallelDesired&amp;lt;/code&amp;gt; predicate indicates that parallelism is not longer warranted.}}&lt;br /&gt;
&lt;br /&gt;
===constructor and instance variables===&lt;br /&gt;
Hang onto each of the specified parameters in instance variables for later use.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public ParallelQuicksorter(Partitioner&amp;lt;T&amp;gt; partitioner, IntPredicate isParallelDesired, Sorter&amp;lt;T&amp;gt; sequentialSorter) {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===partitioner===&lt;br /&gt;
{{Sequential|public Partitioner&amp;lt;T&amp;gt; partitioner()}}&lt;br /&gt;
&lt;br /&gt;
===isParallelDesired===&lt;br /&gt;
{{Sequential|public IntPredicate isParallelDesired()}}&lt;br /&gt;
&lt;br /&gt;
===sequentialSorter===&lt;br /&gt;
{{Sequential|public Sorter&amp;lt;T&amp;gt; sequentialSorter()}}&lt;br /&gt;
&lt;br /&gt;
===sortRangeHelper===&lt;br /&gt;
{{Parallel|private void sortRangeHelper(Queue&amp;lt;Future&amp;lt;Void&amp;gt;&amp;gt; futures, T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive)}}&lt;br /&gt;
&lt;br /&gt;
This method implements the essence of quicksort.&lt;br /&gt;
&lt;br /&gt;
Test this isParallelDesired predicate on the range length first.  If it passes, invoke the partitioner to perform the majority of the work followed by appropriately parallel recursive tasks.  If the predicate does not pass the test, simply invoke the sequentialSorter.&lt;br /&gt;
&lt;br /&gt;
{{Requirement|Do not invoke the partitioner if the predicate fails the test.  Let the sequential sorter handle it from here.}}&lt;br /&gt;
&lt;br /&gt;
{{Requirements2|All forked tasks must be added to the futures queue.|Do not join the tasks in this method.  Joining is the responsibility of the sortShuffledRange method.}}&lt;br /&gt;
&lt;br /&gt;
===sortRange===&lt;br /&gt;
This method is partially provided.  Invokes sortRangeHelper on the complete range, and passes an additional ConcurrentLinkedQueue to capture the Futures associated with any and all forked tasks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
protected void sortRange(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive) throws InterruptedException, ExecutionException {&lt;br /&gt;
	Queue&amp;lt;Future&amp;lt;Void&amp;gt;&amp;gt; futures = new ConcurrentLinkedQueue&amp;lt;&amp;gt;();&lt;br /&gt;
	sortShuffledRangeHelper(futures, data, comparator, min, maxExclusive);&lt;br /&gt;
&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When sortRangeHelper is complete, each task in the queue must be joined.  Since ConcurrentLinkedQueue's iterators are weakly consistent, and all of the tasks may not yet be in the queue, we must join all the tasks as in the [[Join_All_Assignment|Join All warmup]].&lt;br /&gt;
&lt;br /&gt;
{{Alert|do not call sortRange recursively.  sortRangeHelper should do the recursive work.}}&lt;br /&gt;
&lt;br /&gt;
=Challenge=&lt;br /&gt;
You can divide and conquer the divide step in quicksort.  The pan can be improved from &amp;lt;math&amp;gt;\mathcal{O}(n)&amp;lt;/math&amp;gt; down to &amp;lt;math&amp;gt;\mathcal{O}(\lg^k{}n)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For details on how to complete this challenge, check out: [[Parallel_Partition|Parallel Partition]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__QuicksortTestSuite|sort.quick.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Pledge, Acknowledgments, Citations=&lt;br /&gt;
{{Pledge|quicksort}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Quicksort_Parallel_Partitioner&amp;diff=5421</id>
		<title>Quicksort Parallel Partitioner</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Quicksort_Parallel_Partitioner&amp;diff=5421"/>
		<updated>2026-03-20T18:08:10Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: Changed redirect target from Parallel Partitioner to Parallel Partition&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT [[Parallel_Partition]]&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Parallel_Partition&amp;diff=5420</id>
		<title>Parallel Partition</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Parallel_Partition&amp;diff=5420"/>
		<updated>2026-03-20T18:06:40Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Partition does almost all of the work of quicksort.  Further, the classic sequential partition, although wonderful, dominates the span of the quicksort algorithm.  If we can parallelize partition, the span can be improved from &amp;lt;math&amp;gt;\mathcal{O}(n)&amp;lt;/math&amp;gt; down to &amp;lt;math&amp;gt;\mathcal{O}(\lg^k{}n)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For N=16:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center;&amp;quot;&lt;br /&gt;
! &lt;br /&gt;
! colspan=&amp;quot;16&amp;quot; | &amp;lt;- - - - - - - - - - - - - - N - - - - - - - - - - - - - -&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! ^ &lt;br /&gt;
| colspan=&amp;quot;16&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 16)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;amp;#124;&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 8)&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; | partition [8, 16)&lt;br /&gt;
|-&lt;br /&gt;
! lg N &lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 4)&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | partition [4, 8)&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | partition [8, 12)&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | partition [12, 16)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;amp;#124;&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 2)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [2, 4)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [4, 6)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [6, 8)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [8, 10)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [10, 12)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [12, 14)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [14, 16)&lt;br /&gt;
|-&lt;br /&gt;
! v &lt;br /&gt;
| style=&amp;quot;background-color:#CCCCFF;&amp;quot; | base&lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==(Optional) Parallel Partition Challenge==&lt;br /&gt;
The partitioning step can also be done [https://www.cse.wustl.edu/~angelee/archive/cse341/fall14/handouts/lecture08.pdf in parallel] with [[Scan|Scan]] remarkably similar to how [[Pack|Pack]] worked.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ParallelPartitioner|partitionRange|sort.quick.challenge}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|SubRangePair partitionRange(int[] data, int min, int maxExclusive)}}&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|_ParallelPartitionerTestSuite|sort.quick.challenge}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Quicksort_Assignment&amp;diff=5419</id>
		<title>Quicksort Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Quicksort_Assignment&amp;diff=5419"/>
		<updated>2026-03-20T17:47:25Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* SequentialPartitioner */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Sorting is a problem that is well solved by divide and conquer algorithms.  Quicksort is an oldie but a goodie.  Developed in 1959 by Tony Hoare, it is still the fastest sort after all these years.  As a bonus, the conquer portion can be easily parallelized.  The divide portion can also be parallelized, although not trivially (which left as an optional fun challenge for those so inclined).&lt;br /&gt;
=Background=&lt;br /&gt;
In computer science, quicksort is a sorting algorithm which selects a pivot value, partitions all the values less than the pivot value on one side and the values greater on the other.  By recursively performing this operation on the ever decreasing range lengths on the left and right, the array is sorted when the range length reaches a size of 1. &lt;br /&gt;
&lt;br /&gt;
For more information on how this process works, visit the [https://en.wikipedia.org/wiki/Quicksort wikipedia page on quicksort].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
{{CollapsibleYouTube|Quicksort|&amp;lt;youtube&amp;gt;SLauY6PpjW4&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|Parallel Quicksort|&amp;lt;youtube&amp;gt;iqnvIeIhGPw&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
==Visualization==&lt;br /&gt;
If you are unclear on how merge sort works, take a look at the [https://visualgo.net/en/sorting?slide=12 visualgo explanation and visualization of quicksort].&lt;br /&gt;
&lt;br /&gt;
[[File:Visalgo quicksort.png|link=https://visualgo.net/en/sorting?slide=12]]&lt;br /&gt;
&lt;br /&gt;
== Work and Span ==&lt;br /&gt;
Partition does the work of quicksort.  The top-level partition must do N work.  The next level does two ~N/2 partitions.  The level below that performs four ~N/4 partitions.  And so on.  The table is &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; wide and &amp;lt;math&amp;gt;\log N&amp;lt;/math&amp;gt; high.  Thus the work of quicksort is &amp;lt;math&amp;gt;O(N\log N)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For N=16:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center;&amp;quot;&lt;br /&gt;
! &lt;br /&gt;
! colspan=&amp;quot;16&amp;quot; | &amp;lt;- - - - - - - - - - - - - - N - - - - - - - - - - - - - -&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! ^ &lt;br /&gt;
| colspan=&amp;quot;16&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 16)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;amp;#124;&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 8)&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; | partition [8, 16)&lt;br /&gt;
|-&lt;br /&gt;
! lg N &lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 4)&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | partition [4, 8)&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | partition [8, 12)&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | partition [12, 16)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;amp;#124;&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 2)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [2, 4)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [4, 6)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [6, 8)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [8, 10)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [10, 12)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [12, 14)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [14, 16)&lt;br /&gt;
|-&lt;br /&gt;
! v &lt;br /&gt;
| style=&amp;quot;background-color:#CCCCFF;&amp;quot; | base&lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The span for the classic sequential partition is one partition of each of the levels &amp;lt;span style=&amp;quot;background:#CCCCFF&amp;quot;&amp;gt;(highlighted in blue)&amp;lt;/span&amp;gt;.  This works out to be the [https://en.wikipedia.org/wiki/Geometric_series geometric series]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N + \frac{N}{2} + \frac{N}{4} + \frac{N}{8} + \frac{N}{16} + \cdots + 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;2N&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Classic_parallel_merge_sort_and_quicksort_span.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
As a [[Quicksort_Parallel_Partitioner|challenge problem]], we parallelize the partition step, resulting in a span for quicksort of an outstanding &amp;lt;math&amp;gt;\lg^k{}n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=Mistakes To Avoid=&lt;br /&gt;
{{warning | Be sure to invoke the Partitioner provided to the Sorter's constructor's partitionRange method.  Do not implement SequentialPartioner and then re-implement it in the Sorters.}}&lt;br /&gt;
{{warning | When checking the base case, remember to account for [minInclusive, maxExclusive).  It is all too easy to get an off by 1 error and stack overflow.}}&lt;br /&gt;
{{warning | When transitioning to sequential, make sure to NOT sort the entire array when you are only responsible for a range [min,max).}}&lt;br /&gt;
&lt;br /&gt;
=WarmUp=&lt;br /&gt;
The [[Join_All_Assignment|JoinAll WarmUp]] prepares you for getting around with ConcurrentLinkedQueue's weakly consistent iterators.  It is highly recommended.&lt;br /&gt;
&lt;br /&gt;
=Client=&lt;br /&gt;
{{Client|QuicksortClient|sort|sort.quick.client}}&lt;br /&gt;
&lt;br /&gt;
Note: Quicksort is [https://en.wikipedia.org/wiki/Sorting_algorithm#Stability not stable].  You can see that after sorting by name, this is not preserved when sorting by length.  For example, the 3 of length 4 come out Hera, Ares, Zeus.&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|QuicksortClient|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
// https://en.wikipedia.org/wiki/Twelve_Olympians&lt;br /&gt;
String[] olympians = {&lt;br /&gt;
        &amp;quot;Zeus&amp;quot;,&lt;br /&gt;
        &amp;quot;Hera&amp;quot;,&lt;br /&gt;
        &amp;quot;Athena&amp;quot;,&lt;br /&gt;
        &amp;quot;Apollo&amp;quot;,&lt;br /&gt;
        &amp;quot;Poseidon&amp;quot;,&lt;br /&gt;
        &amp;quot;Ares&amp;quot;,&lt;br /&gt;
        &amp;quot;Artemis&amp;quot;,&lt;br /&gt;
        &amp;quot;Demeter&amp;quot;,&lt;br /&gt;
        &amp;quot;Aphrodite&amp;quot;,&lt;br /&gt;
        &amp;quot;Dionysus&amp;quot;,&lt;br /&gt;
        &amp;quot;Hermes&amp;quot;,&lt;br /&gt;
        &amp;quot;Hephaestus&amp;quot;,&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
Shuffler&amp;lt;String&amp;gt; shuffler = new LinearShuffler&amp;lt;&amp;gt;(new Random(System.currentTimeMillis()));&lt;br /&gt;
Partitioner&amp;lt;String&amp;gt; partitioner = new SequentialPartitioner&amp;lt;&amp;gt;();&lt;br /&gt;
IntPredicate isParallelDesired = (rangeLength) -&amp;gt; rangeLength &amp;gt; 3;&lt;br /&gt;
Sorter&amp;lt;String&amp;gt; sequentialQuicksorter = new SequentialQuicksorter&amp;lt;&amp;gt;(shuffler, partitioner);&lt;br /&gt;
&lt;br /&gt;
Sorter&amp;lt;String&amp;gt; parallelQuicksorter = new ParallelQuicksorter&amp;lt;&amp;gt;(shuffler, partitioner, isParallelDesired, sequentialQuicksorter);&lt;br /&gt;
&lt;br /&gt;
System.out.println(&amp;quot;        original: &amp;quot; + Arrays.toString(olympians));&lt;br /&gt;
&lt;br /&gt;
parallelQuicksorter.sort(olympians, Comparator.naturalOrder());&lt;br /&gt;
System.out.println(&amp;quot;  sorted by name: &amp;quot; + Arrays.toString(olympians));&lt;br /&gt;
&lt;br /&gt;
Comparator&amp;lt;String&amp;gt; lengthComparator = (a, b) -&amp;gt; b.length() - a.length();&lt;br /&gt;
parallelQuicksorter.sort(olympians, lengthComparator);&lt;br /&gt;
System.out.println(&amp;quot;sorted by length: &amp;quot; + Arrays.toString(olympians));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|QuicksortClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;        original: [Zeus, Hera, Athena, Apollo, Poseidon, Ares, Artemis, Demeter, Aphrodite, Dionysus, Hermes, Hephaestus]&lt;br /&gt;
  sorted by name: [Aphrodite, Apollo, Ares, Artemis, Athena, Demeter, Dionysus, Hephaestus, Hera, Hermes, Poseidon, Zeus]&lt;br /&gt;
sorted by length: [Hephaestus, Aphrodite, Dionysus, Poseidon, Artemis, Demeter, Athena, Hermes, Apollo, Hera, Ares, Zeus]&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Code to Investigate=&lt;br /&gt;
==Index Selection==&lt;br /&gt;
===InitialIndexSelector&amp;lt;T&amp;gt;===&lt;br /&gt;
The partition step of quicksort starts by selecting an index in an array to serve as the pivot value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public interface InitialIndexSelector&amp;lt;T&amp;gt; {&lt;br /&gt;
	int selectInitialIndex(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===RandomInitialIndexSelector&amp;lt;T&amp;gt;===&lt;br /&gt;
Randomly selecting the initial index is the go to strategy to achieve provable runtime performance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public final class RandomInitialIndexSelector&amp;lt;T&amp;gt; implements InitialIndexSelector&amp;lt;T&amp;gt; {&lt;br /&gt;
	private final Random random;&lt;br /&gt;
&lt;br /&gt;
	public RandomInitialIndexSelector(Random random) {&lt;br /&gt;
		this.random = random;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public int selectInitialIndex(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive) {&lt;br /&gt;
		int range = maxExclusive - min;&lt;br /&gt;
		return min + random.nextInt(range);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Partition Sub Range Pair==&lt;br /&gt;
===SubRangePair===&lt;br /&gt;
The partition step of quicksort... well... partitions the array.  After a pivot value is selected, partition will not necessarily sort the array, but all of the values less than the pivot value will be in the lower part of the range and all of the values greater than the pivot value will be in the higher part of the array.&lt;br /&gt;
&lt;br /&gt;
There are several different approaches to partition and there can be different pivot location characteristics.  [https://www.youtube.com/watch?v=SLauY6PpjW4 Another approach] does even place the pivot value itself if at the pivot location and therefore the range of the pivot location is 0 in length!  Another approach places the single value at the selected index at the pivot location so the range length is 1.  Finally, one partition approach addresses the [https://en.wikipedia.org/wiki/Dutch_national_flag_problem Dutch National Flag Problem] by placing all values equal to the pivot value together in the center, therefore the range length of the pivot location can be &amp;gt;= 1.  &lt;br /&gt;
&lt;br /&gt;
To account for this variable pivot location range length, a class PivotLocation is provided:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public record SubRangePair(Range lowerSubRange, Range upperSubRange) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If a partitioner is called upon to partition a range [min, maxExclusive) it will return a SubRangePair.  The lowerSubRange will always have partition's min as its min.  The upperSubRange will always have the partition's maxExclusive as its maxExclusive.  The elements in the lowerSubRange must be less than the pivot value.  The elements between the lowerSubRange and upperSubRange must be equal to the pivot value. The elements in the upperSubRange must  be greater than the pivot value.&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
Both the sequential and parallel quicksorts will be passed a [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/sort/quick/core/Partitioner.html Partitioner].  Partitioning occurs during the divide phase of divide-and-conquer of the general recursive case.  &lt;br /&gt;
&lt;br /&gt;
You will need to implement quicksort sequentially and in parallel.  Both implementations will naturally be recursive. The kernel method should call itself using recursion, but each public quicksort method should only call its kernel once to do the work.&lt;br /&gt;
==Partition==&lt;br /&gt;
Partition performs the grand majority of the work in quicksort.  A sequential partitioner is required.  A parallel partitioner is left as a challenge problem.  Thus, there are separate classes to implement.&lt;br /&gt;
&lt;br /&gt;
==AbstractPartitioner==&lt;br /&gt;
AbstractPartitioner simply keeps track of the InitialIndexSelector.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|AbstractPartitioner|constructor&amp;lt;br&amp;gt;initialIndexSelector|sort.quick.exercise}}&lt;br /&gt;
&lt;br /&gt;
===constructor and instance variable===&lt;br /&gt;
Simply hang onto the specified initialIndexSelector in an instance variable for later use.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public AbstractPartitioner(InitialIndexSelector&amp;lt;T&amp;gt; initialIndexSelector) {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===initialIndexSelector===&lt;br /&gt;
Return the previously specified to the constructor initialIndexSelector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public InitialIndexSelector&amp;lt;T&amp;gt; initialIndexSelector() {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==SequentialPartitioner==&lt;br /&gt;
{{CodeToImplement|SequentialPartitioner|partitionRange|sort.quick.exercise}}&lt;br /&gt;
&lt;br /&gt;
===partitionRange===&lt;br /&gt;
{{Sequential|public SubRangePair partitionRange(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive)}}&lt;br /&gt;
&lt;br /&gt;
There are [https://en.wikipedia.org/wiki/Quicksort#Algorithm many approaches to partition] with different performance characteristics.&lt;br /&gt;
&lt;br /&gt;
{{Requirements4|All values left of the returned PivotLocation must be less than or equal to the value at the PivotLocation|All values right of the returned PivotLocation must be greater than or equal to the value at the PivotLocation.|All values at the Pivot Location must be equal.|The pivot location must divide the problem into smaller sub-problems.}}&lt;br /&gt;
&lt;br /&gt;
{{Alert|The HackerRank approach from the prep does not maintain the pivot value at the PivotLocation.&amp;lt;br/&amp;gt;Thus the range between the pivot location's maxExclusiveOfLesserSide and minOfGreaterSide will be 0 in length.&amp;lt;br/&amp;gt;As a result, care must be taken to ensure that partition produces a pivot such that there are always smaller sub-problems.}}&lt;br /&gt;
&lt;br /&gt;
{{Alert|The HackerRank approach from the prep chooses the value at the midpoint as its pivot value.&amp;lt;br/&amp;gt;Critically, it calculates the midpoint based on the *inclusive* maximum.&amp;lt;br/&amp;gt;Believe it or not, the entire algorithm death spirals if you choose the midpoint based on the exclusive max. }}&lt;br /&gt;
&lt;br /&gt;
Although an interesting challenge, you are not required handle the [https://en.wikipedia.org/wiki/Dutch_national_flag_problem Dutch national flag problem].&lt;br /&gt;
&lt;br /&gt;
==Sort==&lt;br /&gt;
==interface Sorter==&lt;br /&gt;
{{CodeToImplement|Sorter|sort|sort.core}}&lt;br /&gt;
&lt;br /&gt;
===sort===&lt;br /&gt;
{{Sequential|default void sort(T[] data, Comparator&amp;lt;T&amp;gt; comparator)}}&lt;br /&gt;
&lt;br /&gt;
This should be already complete from the [[MergeSort|Merge Sort Exercise]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==class SequentialQuicksorter==&lt;br /&gt;
{{CodeToImplement|SequentialQuicksorter|sortRange|sort.quick.exercise}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public class SequentialQuicksorter&amp;lt;T&amp;gt; extends AbstractQuicksorter&amp;lt;T&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===constructor and instance variable===&lt;br /&gt;
Simply hang onto the specified partitioner in an instance variable for later use.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public SequentialQuicksorter(Partitioner&amp;lt;T&amp;gt; partitioner) {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===partitioner===&lt;br /&gt;
Return the previously specified to the constructor partitioner&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public Partitioner&amp;lt;T&amp;gt; partitioner() {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===sortRange===&lt;br /&gt;
{{Sequential|public void sortRange(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive)}}&lt;br /&gt;
&lt;br /&gt;
Implement the classic divide and conquer sequential algorithm here.  &lt;br /&gt;
&lt;br /&gt;
{{Alert|Be sure to leverage the Partitioner passed to the constructor to perform the divide step.}}&lt;br /&gt;
&lt;br /&gt;
==class ParallelQuicksorter==&lt;br /&gt;
{{CodeToImplement|ParallelQuicksorter|partitioner&amp;lt;br/&amp;gt;isParallelDesired&amp;lt;br/&amp;gt;sequentialSorter&amp;lt;br/&amp;gt;sortShuffledRangeHelper&amp;lt;br/&amp;gt;sortShuffledRange|sort.quick.exercise}}&lt;br /&gt;
&lt;br /&gt;
Adapt your sequential algorithm to take advantage of what can be parallelized.&lt;br /&gt;
&lt;br /&gt;
What can be in parallel?&lt;br /&gt;
&lt;br /&gt;
What is dependent on the parallel tasks completing?&lt;br /&gt;
&lt;br /&gt;
{{Warning|Leverage the provided &amp;lt;code&amp;gt;private final IntPredicate isParallelDesired;&amp;lt;/code&amp;gt; field to test whether or not parallelism is warranted for the current range length.}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|Fall back to the provided &amp;lt;code&amp;gt;private final Sorter&amp;lt;T&amp;gt; sequentialSorter;&amp;lt;/code&amp;gt; field when the &amp;lt;code&amp;gt;isParallelDesired&amp;lt;/code&amp;gt; predicate indicates that parallelism is not longer warranted.}}&lt;br /&gt;
&lt;br /&gt;
===constructor and instance variables===&lt;br /&gt;
Hang onto each of the specified parameters in instance variables for later use.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public ParallelQuicksorter(Partitioner&amp;lt;T&amp;gt; partitioner, IntPredicate isParallelDesired, Sorter&amp;lt;T&amp;gt; sequentialSorter) {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===partitioner===&lt;br /&gt;
{{Sequential|public Partitioner&amp;lt;T&amp;gt; partitioner()}}&lt;br /&gt;
&lt;br /&gt;
===isParallelDesired===&lt;br /&gt;
{{Sequential|public IntPredicate isParallelDesired()}}&lt;br /&gt;
&lt;br /&gt;
===sequentialSorter===&lt;br /&gt;
{{Sequential|public Sorter&amp;lt;T&amp;gt; sequentialSorter()}}&lt;br /&gt;
&lt;br /&gt;
===sortRangeHelper===&lt;br /&gt;
{{Parallel|private void sortRangeHelper(Queue&amp;lt;Future&amp;lt;Void&amp;gt;&amp;gt; futures, T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive)}}&lt;br /&gt;
&lt;br /&gt;
This method implements the essence of quicksort.&lt;br /&gt;
&lt;br /&gt;
Test this isParallelDesired predicate on the range length first.  If it passes, invoke the partitioner to perform the majority of the work followed by appropriately parallel recursive tasks.  If the predicate does not pass the test, simply invoke the sequentialSorter.&lt;br /&gt;
&lt;br /&gt;
{{Requirement|Do not invoke the partitioner if the predicate fails the test.  Let the sequential sorter handle it from here.}}&lt;br /&gt;
&lt;br /&gt;
{{Requirements2|All forked tasks must be added to the futures queue.|Do not join the tasks in this method.  Joining is the responsibility of the sortShuffledRange method.}}&lt;br /&gt;
&lt;br /&gt;
===sortRange===&lt;br /&gt;
This method is partially provided.  Invokes sortRangeHelper on the complete range, and passes an additional ConcurrentLinkedQueue to capture the Futures associated with any and all forked tasks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
protected void sortRange(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive) throws InterruptedException, ExecutionException {&lt;br /&gt;
	Queue&amp;lt;Future&amp;lt;Void&amp;gt;&amp;gt; futures = new ConcurrentLinkedQueue&amp;lt;&amp;gt;();&lt;br /&gt;
	sortShuffledRangeHelper(futures, data, comparator, min, maxExclusive);&lt;br /&gt;
&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When sortRangeHelper is complete, each task in the queue must be joined.  Since ConcurrentLinkedQueue's iterators are weakly consistent, and all of the tasks may not yet be in the queue, we must join all the tasks as in the [[Join_All_Assignment|Join All warmup]].&lt;br /&gt;
&lt;br /&gt;
{{Alert|do not call sortRange recursively.  sortRangeHelper should do the recursive work.}}&lt;br /&gt;
&lt;br /&gt;
=Challenge=&lt;br /&gt;
You can divide and conquer the divide step in quicksort.  The pan can be improved from &amp;lt;math&amp;gt;\mathcal{O}(n)&amp;lt;/math&amp;gt; down to &amp;lt;math&amp;gt;\mathcal{O}(\lg^k{}n)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For details on how to complete this challenge, check out: [[Parallel_Partition|Parallel Partition]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__QuicksortTestSuite|sort.quick.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Pledge, Acknowledgments, Citations=&lt;br /&gt;
{{Pledge|quicksort}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Quicksort_Assignment&amp;diff=5418</id>
		<title>Quicksort Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Quicksort_Assignment&amp;diff=5418"/>
		<updated>2026-03-20T17:46:50Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* Partition Pivot Location Result */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Sorting is a problem that is well solved by divide and conquer algorithms.  Quicksort is an oldie but a goodie.  Developed in 1959 by Tony Hoare, it is still the fastest sort after all these years.  As a bonus, the conquer portion can be easily parallelized.  The divide portion can also be parallelized, although not trivially (which left as an optional fun challenge for those so inclined).&lt;br /&gt;
=Background=&lt;br /&gt;
In computer science, quicksort is a sorting algorithm which selects a pivot value, partitions all the values less than the pivot value on one side and the values greater on the other.  By recursively performing this operation on the ever decreasing range lengths on the left and right, the array is sorted when the range length reaches a size of 1. &lt;br /&gt;
&lt;br /&gt;
For more information on how this process works, visit the [https://en.wikipedia.org/wiki/Quicksort wikipedia page on quicksort].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
{{CollapsibleYouTube|Quicksort|&amp;lt;youtube&amp;gt;SLauY6PpjW4&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|Parallel Quicksort|&amp;lt;youtube&amp;gt;iqnvIeIhGPw&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
==Visualization==&lt;br /&gt;
If you are unclear on how merge sort works, take a look at the [https://visualgo.net/en/sorting?slide=12 visualgo explanation and visualization of quicksort].&lt;br /&gt;
&lt;br /&gt;
[[File:Visalgo quicksort.png|link=https://visualgo.net/en/sorting?slide=12]]&lt;br /&gt;
&lt;br /&gt;
== Work and Span ==&lt;br /&gt;
Partition does the work of quicksort.  The top-level partition must do N work.  The next level does two ~N/2 partitions.  The level below that performs four ~N/4 partitions.  And so on.  The table is &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; wide and &amp;lt;math&amp;gt;\log N&amp;lt;/math&amp;gt; high.  Thus the work of quicksort is &amp;lt;math&amp;gt;O(N\log N)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For N=16:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center;&amp;quot;&lt;br /&gt;
! &lt;br /&gt;
! colspan=&amp;quot;16&amp;quot; | &amp;lt;- - - - - - - - - - - - - - N - - - - - - - - - - - - - -&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! ^ &lt;br /&gt;
| colspan=&amp;quot;16&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 16)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;amp;#124;&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 8)&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; | partition [8, 16)&lt;br /&gt;
|-&lt;br /&gt;
! lg N &lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 4)&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | partition [4, 8)&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | partition [8, 12)&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | partition [12, 16)&lt;br /&gt;
|-&lt;br /&gt;
! &amp;amp;#124;&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; style=&amp;quot;background-color:#CCCCFF;&amp;quot; | partition [0, 2)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [2, 4)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [4, 6)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [6, 8)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [8, 10)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [10, 12)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [12, 14)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | partition [14, 16)&lt;br /&gt;
|-&lt;br /&gt;
! v &lt;br /&gt;
| style=&amp;quot;background-color:#CCCCFF;&amp;quot; | base&lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
| base &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The span for the classic sequential partition is one partition of each of the levels &amp;lt;span style=&amp;quot;background:#CCCCFF&amp;quot;&amp;gt;(highlighted in blue)&amp;lt;/span&amp;gt;.  This works out to be the [https://en.wikipedia.org/wiki/Geometric_series geometric series]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;N + \frac{N}{2} + \frac{N}{4} + \frac{N}{8} + \frac{N}{16} + \cdots + 1&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;2N&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Classic_parallel_merge_sort_and_quicksort_span.svg|600px]]&lt;br /&gt;
&lt;br /&gt;
As a [[Quicksort_Parallel_Partitioner|challenge problem]], we parallelize the partition step, resulting in a span for quicksort of an outstanding &amp;lt;math&amp;gt;\lg^k{}n&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=Mistakes To Avoid=&lt;br /&gt;
{{warning | Be sure to invoke the Partitioner provided to the Sorter's constructor's partitionRange method.  Do not implement SequentialPartioner and then re-implement it in the Sorters.}}&lt;br /&gt;
{{warning | When checking the base case, remember to account for [minInclusive, maxExclusive).  It is all too easy to get an off by 1 error and stack overflow.}}&lt;br /&gt;
{{warning | When transitioning to sequential, make sure to NOT sort the entire array when you are only responsible for a range [min,max).}}&lt;br /&gt;
&lt;br /&gt;
=WarmUp=&lt;br /&gt;
The [[Join_All_Assignment|JoinAll WarmUp]] prepares you for getting around with ConcurrentLinkedQueue's weakly consistent iterators.  It is highly recommended.&lt;br /&gt;
&lt;br /&gt;
=Client=&lt;br /&gt;
{{Client|QuicksortClient|sort|sort.quick.client}}&lt;br /&gt;
&lt;br /&gt;
Note: Quicksort is [https://en.wikipedia.org/wiki/Sorting_algorithm#Stability not stable].  You can see that after sorting by name, this is not preserved when sorting by length.  For example, the 3 of length 4 come out Hera, Ares, Zeus.&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|QuicksortClient|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
// https://en.wikipedia.org/wiki/Twelve_Olympians&lt;br /&gt;
String[] olympians = {&lt;br /&gt;
        &amp;quot;Zeus&amp;quot;,&lt;br /&gt;
        &amp;quot;Hera&amp;quot;,&lt;br /&gt;
        &amp;quot;Athena&amp;quot;,&lt;br /&gt;
        &amp;quot;Apollo&amp;quot;,&lt;br /&gt;
        &amp;quot;Poseidon&amp;quot;,&lt;br /&gt;
        &amp;quot;Ares&amp;quot;,&lt;br /&gt;
        &amp;quot;Artemis&amp;quot;,&lt;br /&gt;
        &amp;quot;Demeter&amp;quot;,&lt;br /&gt;
        &amp;quot;Aphrodite&amp;quot;,&lt;br /&gt;
        &amp;quot;Dionysus&amp;quot;,&lt;br /&gt;
        &amp;quot;Hermes&amp;quot;,&lt;br /&gt;
        &amp;quot;Hephaestus&amp;quot;,&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
Shuffler&amp;lt;String&amp;gt; shuffler = new LinearShuffler&amp;lt;&amp;gt;(new Random(System.currentTimeMillis()));&lt;br /&gt;
Partitioner&amp;lt;String&amp;gt; partitioner = new SequentialPartitioner&amp;lt;&amp;gt;();&lt;br /&gt;
IntPredicate isParallelDesired = (rangeLength) -&amp;gt; rangeLength &amp;gt; 3;&lt;br /&gt;
Sorter&amp;lt;String&amp;gt; sequentialQuicksorter = new SequentialQuicksorter&amp;lt;&amp;gt;(shuffler, partitioner);&lt;br /&gt;
&lt;br /&gt;
Sorter&amp;lt;String&amp;gt; parallelQuicksorter = new ParallelQuicksorter&amp;lt;&amp;gt;(shuffler, partitioner, isParallelDesired, sequentialQuicksorter);&lt;br /&gt;
&lt;br /&gt;
System.out.println(&amp;quot;        original: &amp;quot; + Arrays.toString(olympians));&lt;br /&gt;
&lt;br /&gt;
parallelQuicksorter.sort(olympians, Comparator.naturalOrder());&lt;br /&gt;
System.out.println(&amp;quot;  sorted by name: &amp;quot; + Arrays.toString(olympians));&lt;br /&gt;
&lt;br /&gt;
Comparator&amp;lt;String&amp;gt; lengthComparator = (a, b) -&amp;gt; b.length() - a.length();&lt;br /&gt;
parallelQuicksorter.sort(olympians, lengthComparator);&lt;br /&gt;
System.out.println(&amp;quot;sorted by length: &amp;quot; + Arrays.toString(olympians));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|QuicksortClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;        original: [Zeus, Hera, Athena, Apollo, Poseidon, Ares, Artemis, Demeter, Aphrodite, Dionysus, Hermes, Hephaestus]&lt;br /&gt;
  sorted by name: [Aphrodite, Apollo, Ares, Artemis, Athena, Demeter, Dionysus, Hephaestus, Hera, Hermes, Poseidon, Zeus]&lt;br /&gt;
sorted by length: [Hephaestus, Aphrodite, Dionysus, Poseidon, Artemis, Demeter, Athena, Hermes, Apollo, Hera, Ares, Zeus]&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Code to Investigate=&lt;br /&gt;
==Index Selection==&lt;br /&gt;
===InitialIndexSelector&amp;lt;T&amp;gt;===&lt;br /&gt;
The partition step of quicksort starts by selecting an index in an array to serve as the pivot value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public interface InitialIndexSelector&amp;lt;T&amp;gt; {&lt;br /&gt;
	int selectInitialIndex(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===RandomInitialIndexSelector&amp;lt;T&amp;gt;===&lt;br /&gt;
Randomly selecting the initial index is the go to strategy to achieve provable runtime performance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public final class RandomInitialIndexSelector&amp;lt;T&amp;gt; implements InitialIndexSelector&amp;lt;T&amp;gt; {&lt;br /&gt;
	private final Random random;&lt;br /&gt;
&lt;br /&gt;
	public RandomInitialIndexSelector(Random random) {&lt;br /&gt;
		this.random = random;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public int selectInitialIndex(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive) {&lt;br /&gt;
		int range = maxExclusive - min;&lt;br /&gt;
		return min + random.nextInt(range);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Partition Sub Range Pair==&lt;br /&gt;
===SubRangePair===&lt;br /&gt;
The partition step of quicksort... well... partitions the array.  After a pivot value is selected, partition will not necessarily sort the array, but all of the values less than the pivot value will be in the lower part of the range and all of the values greater than the pivot value will be in the higher part of the array.&lt;br /&gt;
&lt;br /&gt;
There are several different approaches to partition and there can be different pivot location characteristics.  [https://www.youtube.com/watch?v=SLauY6PpjW4 Another approach] does even place the pivot value itself if at the pivot location and therefore the range of the pivot location is 0 in length!  Another approach places the single value at the selected index at the pivot location so the range length is 1.  Finally, one partition approach addresses the [https://en.wikipedia.org/wiki/Dutch_national_flag_problem Dutch National Flag Problem] by placing all values equal to the pivot value together in the center, therefore the range length of the pivot location can be &amp;gt;= 1.  &lt;br /&gt;
&lt;br /&gt;
To account for this variable pivot location range length, a class PivotLocation is provided:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public record SubRangePair(Range lowerSubRange, Range upperSubRange) {&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If a partitioner is called upon to partition a range [min, maxExclusive) it will return a SubRangePair.  The lowerSubRange will always have partition's min as its min.  The upperSubRange will always have the partition's maxExclusive as its maxExclusive.  The elements in the lowerSubRange must be less than the pivot value.  The elements between the lowerSubRange and upperSubRange must be equal to the pivot value. The elements in the upperSubRange must  be greater than the pivot value.&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
Both the sequential and parallel quicksorts will be passed a [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/sort/quick/core/Partitioner.html Partitioner].  Partitioning occurs during the divide phase of divide-and-conquer of the general recursive case.  &lt;br /&gt;
&lt;br /&gt;
You will need to implement quicksort sequentially and in parallel.  Both implementations will naturally be recursive. The kernel method should call itself using recursion, but each public quicksort method should only call its kernel once to do the work.&lt;br /&gt;
==Partition==&lt;br /&gt;
Partition performs the grand majority of the work in quicksort.  A sequential partitioner is required.  A parallel partitioner is left as a challenge problem.  Thus, there are separate classes to implement.&lt;br /&gt;
&lt;br /&gt;
==AbstractPartitioner==&lt;br /&gt;
AbstractPartitioner simply keeps track of the InitialIndexSelector.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|AbstractPartitioner|constructor&amp;lt;br&amp;gt;initialIndexSelector|sort.quick.exercise}}&lt;br /&gt;
&lt;br /&gt;
===constructor and instance variable===&lt;br /&gt;
Simply hang onto the specified initialIndexSelector in an instance variable for later use.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public AbstractPartitioner(InitialIndexSelector&amp;lt;T&amp;gt; initialIndexSelector) {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===initialIndexSelector===&lt;br /&gt;
Return the previously specified to the constructor initialIndexSelector.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public InitialIndexSelector&amp;lt;T&amp;gt; initialIndexSelector() {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==SequentialPartitioner==&lt;br /&gt;
{{CodeToImplement|SequentialPartitioner|partitionRange|sort.quick.exercise}}&lt;br /&gt;
&lt;br /&gt;
===partitionRange===&lt;br /&gt;
{{Sequential|public PivotLocation partitionRange(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive)}}&lt;br /&gt;
&lt;br /&gt;
There are [https://en.wikipedia.org/wiki/Quicksort#Algorithm many approaches to partition] with different performance characteristics.&lt;br /&gt;
&lt;br /&gt;
{{Requirements4|All values left of the returned PivotLocation must be less than or equal to the value at the PivotLocation|All values right of the returned PivotLocation must be greater than or equal to the value at the PivotLocation.|All values at the Pivot Location must be equal.|The pivot location must divide the problem into smaller sub-problems.}}&lt;br /&gt;
&lt;br /&gt;
{{Alert|The HackerRank approach from the prep does not maintain the pivot value at the PivotLocation.&amp;lt;br/&amp;gt;Thus the range between the pivot location's maxExclusiveOfLesserSide and minOfGreaterSide will be 0 in length.&amp;lt;br/&amp;gt;As a result, care must be taken to ensure that partition produces a pivot such that there are always smaller sub-problems.}}&lt;br /&gt;
&lt;br /&gt;
{{Alert|The HackerRank approach from the prep chooses the value at the midpoint as its pivot value.&amp;lt;br/&amp;gt;Critically, it calculates the midpoint based on the *inclusive* maximum.&amp;lt;br/&amp;gt;Believe it or not, the entire algorithm death spirals if you choose the midpoint based on the exclusive max. }}&lt;br /&gt;
&lt;br /&gt;
Although an interesting challenge, you are not required handle the [https://en.wikipedia.org/wiki/Dutch_national_flag_problem Dutch national flag problem].&lt;br /&gt;
&lt;br /&gt;
==Sort==&lt;br /&gt;
==interface Sorter==&lt;br /&gt;
{{CodeToImplement|Sorter|sort|sort.core}}&lt;br /&gt;
&lt;br /&gt;
===sort===&lt;br /&gt;
{{Sequential|default void sort(T[] data, Comparator&amp;lt;T&amp;gt; comparator)}}&lt;br /&gt;
&lt;br /&gt;
This should be already complete from the [[MergeSort|Merge Sort Exercise]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==class SequentialQuicksorter==&lt;br /&gt;
{{CodeToImplement|SequentialQuicksorter|sortRange|sort.quick.exercise}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public class SequentialQuicksorter&amp;lt;T&amp;gt; extends AbstractQuicksorter&amp;lt;T&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===constructor and instance variable===&lt;br /&gt;
Simply hang onto the specified partitioner in an instance variable for later use.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public SequentialQuicksorter(Partitioner&amp;lt;T&amp;gt; partitioner) {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===partitioner===&lt;br /&gt;
Return the previously specified to the constructor partitioner&lt;br /&gt;
&amp;lt;syntaxhighlight lang=java&amp;gt;&lt;br /&gt;
public Partitioner&amp;lt;T&amp;gt; partitioner() {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===sortRange===&lt;br /&gt;
{{Sequential|public void sortRange(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive)}}&lt;br /&gt;
&lt;br /&gt;
Implement the classic divide and conquer sequential algorithm here.  &lt;br /&gt;
&lt;br /&gt;
{{Alert|Be sure to leverage the Partitioner passed to the constructor to perform the divide step.}}&lt;br /&gt;
&lt;br /&gt;
==class ParallelQuicksorter==&lt;br /&gt;
{{CodeToImplement|ParallelQuicksorter|partitioner&amp;lt;br/&amp;gt;isParallelDesired&amp;lt;br/&amp;gt;sequentialSorter&amp;lt;br/&amp;gt;sortShuffledRangeHelper&amp;lt;br/&amp;gt;sortShuffledRange|sort.quick.exercise}}&lt;br /&gt;
&lt;br /&gt;
Adapt your sequential algorithm to take advantage of what can be parallelized.&lt;br /&gt;
&lt;br /&gt;
What can be in parallel?&lt;br /&gt;
&lt;br /&gt;
What is dependent on the parallel tasks completing?&lt;br /&gt;
&lt;br /&gt;
{{Warning|Leverage the provided &amp;lt;code&amp;gt;private final IntPredicate isParallelDesired;&amp;lt;/code&amp;gt; field to test whether or not parallelism is warranted for the current range length.}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|Fall back to the provided &amp;lt;code&amp;gt;private final Sorter&amp;lt;T&amp;gt; sequentialSorter;&amp;lt;/code&amp;gt; field when the &amp;lt;code&amp;gt;isParallelDesired&amp;lt;/code&amp;gt; predicate indicates that parallelism is not longer warranted.}}&lt;br /&gt;
&lt;br /&gt;
===constructor and instance variables===&lt;br /&gt;
Hang onto each of the specified parameters in instance variables for later use.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public ParallelQuicksorter(Partitioner&amp;lt;T&amp;gt; partitioner, IntPredicate isParallelDesired, Sorter&amp;lt;T&amp;gt; sequentialSorter) {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===partitioner===&lt;br /&gt;
{{Sequential|public Partitioner&amp;lt;T&amp;gt; partitioner()}}&lt;br /&gt;
&lt;br /&gt;
===isParallelDesired===&lt;br /&gt;
{{Sequential|public IntPredicate isParallelDesired()}}&lt;br /&gt;
&lt;br /&gt;
===sequentialSorter===&lt;br /&gt;
{{Sequential|public Sorter&amp;lt;T&amp;gt; sequentialSorter()}}&lt;br /&gt;
&lt;br /&gt;
===sortRangeHelper===&lt;br /&gt;
{{Parallel|private void sortRangeHelper(Queue&amp;lt;Future&amp;lt;Void&amp;gt;&amp;gt; futures, T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive)}}&lt;br /&gt;
&lt;br /&gt;
This method implements the essence of quicksort.&lt;br /&gt;
&lt;br /&gt;
Test this isParallelDesired predicate on the range length first.  If it passes, invoke the partitioner to perform the majority of the work followed by appropriately parallel recursive tasks.  If the predicate does not pass the test, simply invoke the sequentialSorter.&lt;br /&gt;
&lt;br /&gt;
{{Requirement|Do not invoke the partitioner if the predicate fails the test.  Let the sequential sorter handle it from here.}}&lt;br /&gt;
&lt;br /&gt;
{{Requirements2|All forked tasks must be added to the futures queue.|Do not join the tasks in this method.  Joining is the responsibility of the sortShuffledRange method.}}&lt;br /&gt;
&lt;br /&gt;
===sortRange===&lt;br /&gt;
This method is partially provided.  Invokes sortRangeHelper on the complete range, and passes an additional ConcurrentLinkedQueue to capture the Futures associated with any and all forked tasks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
protected void sortRange(T[] data, Comparator&amp;lt;T&amp;gt; comparator, int min, int maxExclusive) throws InterruptedException, ExecutionException {&lt;br /&gt;
	Queue&amp;lt;Future&amp;lt;Void&amp;gt;&amp;gt; futures = new ConcurrentLinkedQueue&amp;lt;&amp;gt;();&lt;br /&gt;
	sortShuffledRangeHelper(futures, data, comparator, min, maxExclusive);&lt;br /&gt;
&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When sortRangeHelper is complete, each task in the queue must be joined.  Since ConcurrentLinkedQueue's iterators are weakly consistent, and all of the tasks may not yet be in the queue, we must join all the tasks as in the [[Join_All_Assignment|Join All warmup]].&lt;br /&gt;
&lt;br /&gt;
{{Alert|do not call sortRange recursively.  sortRangeHelper should do the recursive work.}}&lt;br /&gt;
&lt;br /&gt;
=Challenge=&lt;br /&gt;
You can divide and conquer the divide step in quicksort.  The pan can be improved from &amp;lt;math&amp;gt;\mathcal{O}(n)&amp;lt;/math&amp;gt; down to &amp;lt;math&amp;gt;\mathcal{O}(\lg^k{}n)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
For details on how to complete this challenge, check out: [[Parallel_Partition|Parallel Partition]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__QuicksortTestSuite|sort.quick.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Pledge, Acknowledgments, Citations=&lt;br /&gt;
{{Pledge|quicksort}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=5417</id>
		<title>Scan</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=5417"/>
		<updated>2026-03-17T03:42:18Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* ForkJoin */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Scan, also known as parallel prefix, is a fundamental and useful operation in parallel programming.  We will gain experience in building Hillis &amp;amp; Steele scan with an optional work efficient Blellock scan.  &lt;br /&gt;
&lt;br /&gt;
Further, the dependencies in scan:&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;math&amp;gt;y_i = y_{i-1} + x_i&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
make it seem to have little hope for parallelism.  However, simple yet clever approaches can achieve &amp;lt;math&amp;gt;\log ^k n&amp;lt;/math&amp;gt; critical path lengths.&lt;br /&gt;
&lt;br /&gt;
While we will simply implement prefix sum, scan can be used for other associative operations.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum Prefix Sum]&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_1:_Shorter_span,_more_parallel Hillis and Steele Algorithm]&lt;br /&gt;
&lt;br /&gt;
[https://dl.acm.org/citation.cfm?coll=GUIDE&amp;amp;dl=GUIDE&amp;amp;id=7903 Data parallel algorithms] &lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|Hillis and Steele Scan|&amp;lt;youtube&amp;gt;RdfmxfZBHpo&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
[[File:Hillis-Steele_Prefix_Sum.svg]]&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
[https://docs.google.com/presentation/d/1xUsx7-n6Ocvm2pQSQX3canzxqYsMo-xka5bw7qFugNk/pub slides]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;RmekgaW8X8A&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Visualization=&lt;br /&gt;
{{Viz|ScanViz|scan.viz|main}}&lt;br /&gt;
&lt;br /&gt;
[[File:Step_Efficient_Scan_Viz.png]]&lt;br /&gt;
&lt;br /&gt;
=Warmup=&lt;br /&gt;
[[Sequential_Scan_Assignment|Sequential Sum Scan]]&lt;br /&gt;
&lt;br /&gt;
=Client=&lt;br /&gt;
{{Client|StepEfficientParallelSumScannerClient|scan.client|main}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|StepEfficientParallelSumScannerClient|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
OutOfPlaceSumScanner sumScanner = new StepEfficientParallelSumScanner();&lt;br /&gt;
int[] data = {1, 2, 3, 4, 5, 6, 7, 8};&lt;br /&gt;
System.out.println(Arrays.toString(data));&lt;br /&gt;
int[] result = sumScanner.sumScan(data);&lt;br /&gt;
System.out.println(Arrays.toString(result));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|StepEfficientParallelSumScannerClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;[1, 2, 3, 4, 5, 6, 7, 8]&lt;br /&gt;
[1, 3, 6, 10, 15, 21, 28, 36]&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Code To Use=&lt;br /&gt;
==ForkJoin==&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#join_fork_loop_to_int_array(int,int,fj.api.TaskIntFunction) join_fork_loop_to_int_array(min, maxExclusive, toIntFunction)]&lt;br /&gt;
&lt;br /&gt;
==[[PowersOf2Iterable]]==&lt;br /&gt;
This [[PowersOf2Iterable|exercise]] should come in handy here. Recall that we implemented a &amp;lt;code&amp;gt;PowersOfTwoLessThan&amp;lt;/code&amp;gt; class in this exercise.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
for(int v : new PowersOfTwoLessThan(71)) {&lt;br /&gt;
    System.out.println(v);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
==PhasableIntArrays==&lt;br /&gt;
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]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html#srcForPhase(int) srcForPhase(phaseIndex)]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html#dstForPhase(int) drcForPhase(phaseIndex)]&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
This is what &amp;lt;code&amp;gt;PhasableIntArrays&amp;lt;/code&amp;gt; helps us with. By using its &amp;lt;code&amp;gt;dstForPhase(int phaseIndex)&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;srcForPhase(int phaseIndex)&amp;lt;/code&amp;gt;, we can get the source and destination array corresponding to the phase.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!'''phase''' !! src !! dst !! power of 2&lt;br /&gt;
|-&lt;br /&gt;
|0||data||a||1&lt;br /&gt;
|-&lt;br /&gt;
|1||a||b||2&lt;br /&gt;
|-&lt;br /&gt;
|2||b||a||4&lt;br /&gt;
|-&lt;br /&gt;
|3||a||b||8&lt;br /&gt;
|-&lt;br /&gt;
|4||b||a||16&lt;br /&gt;
|-&lt;br /&gt;
|5||a||b||32&lt;br /&gt;
|-&lt;br /&gt;
|6||b||a||64&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''NOTE:''' think about which array is the correct one to return from your scan method given how you write your code.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
{{CodeToImplement|StepEfficientParallelSumScanner|sumScan|scan.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|int[] sumScan(int[] data)}}&lt;br /&gt;
=Challenge Problem=&lt;br /&gt;
[[Work_Efficient_Parallel_Scan_Assignment|Blelloch (Work-efficient) Scan]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__ScanTestSuite|scan.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Pledge, Acknowledgments, Citations=&lt;br /&gt;
{{Pledge|exercise-scan}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=5416</id>
		<title>Scan</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=5416"/>
		<updated>2026-03-17T03:40:21Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* Code To Use */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Scan, also known as parallel prefix, is a fundamental and useful operation in parallel programming.  We will gain experience in building Hillis &amp;amp; Steele scan with an optional work efficient Blellock scan.  &lt;br /&gt;
&lt;br /&gt;
Further, the dependencies in scan:&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;math&amp;gt;y_i = y_{i-1} + x_i&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
make it seem to have little hope for parallelism.  However, simple yet clever approaches can achieve &amp;lt;math&amp;gt;\log ^k n&amp;lt;/math&amp;gt; critical path lengths.&lt;br /&gt;
&lt;br /&gt;
While we will simply implement prefix sum, scan can be used for other associative operations.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum Prefix Sum]&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_1:_Shorter_span,_more_parallel Hillis and Steele Algorithm]&lt;br /&gt;
&lt;br /&gt;
[https://dl.acm.org/citation.cfm?coll=GUIDE&amp;amp;dl=GUIDE&amp;amp;id=7903 Data parallel algorithms] &lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|Hillis and Steele Scan|&amp;lt;youtube&amp;gt;RdfmxfZBHpo&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
[[File:Hillis-Steele_Prefix_Sum.svg]]&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
[https://docs.google.com/presentation/d/1xUsx7-n6Ocvm2pQSQX3canzxqYsMo-xka5bw7qFugNk/pub slides]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;RmekgaW8X8A&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Visualization=&lt;br /&gt;
{{Viz|ScanViz|scan.viz|main}}&lt;br /&gt;
&lt;br /&gt;
[[File:Step_Efficient_Scan_Viz.png]]&lt;br /&gt;
&lt;br /&gt;
=Warmup=&lt;br /&gt;
[[Sequential_Scan_Assignment|Sequential Sum Scan]]&lt;br /&gt;
&lt;br /&gt;
=Client=&lt;br /&gt;
{{Client|StepEfficientParallelSumScannerClient|scan.client|main}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|StepEfficientParallelSumScannerClient|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
OutOfPlaceSumScanner sumScanner = new StepEfficientParallelSumScanner();&lt;br /&gt;
int[] data = {1, 2, 3, 4, 5, 6, 7, 8};&lt;br /&gt;
System.out.println(Arrays.toString(data));&lt;br /&gt;
int[] result = sumScanner.sumScan(data);&lt;br /&gt;
System.out.println(Arrays.toString(result));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|StepEfficientParallelSumScannerClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;[1, 2, 3, 4, 5, 6, 7, 8]&lt;br /&gt;
[1, 3, 6, 10, 15, 21, 28, 36]&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Code To Use=&lt;br /&gt;
==ForkJoin==&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#join_fork_loop_to_int_array(int%5B%5D,fj.api.TaskFunction) join_fork_loop_to_int_array]&lt;br /&gt;
&lt;br /&gt;
==[[PowersOf2Iterable]]==&lt;br /&gt;
This [[PowersOf2Iterable|exercise]] should come in handy here. Recall that we implemented a &amp;lt;code&amp;gt;PowersOfTwoLessThan&amp;lt;/code&amp;gt; class in this exercise.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
for(int v : new PowersOfTwoLessThan(71)) {&lt;br /&gt;
    System.out.println(v);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
==PhasableIntArrays==&lt;br /&gt;
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]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html#srcForPhase(int) srcForPhase(phaseIndex)]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html#dstForPhase(int) drcForPhase(phaseIndex)]&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
This is what &amp;lt;code&amp;gt;PhasableIntArrays&amp;lt;/code&amp;gt; helps us with. By using its &amp;lt;code&amp;gt;dstForPhase(int phaseIndex)&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;srcForPhase(int phaseIndex)&amp;lt;/code&amp;gt;, we can get the source and destination array corresponding to the phase.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!'''phase''' !! src !! dst !! power of 2&lt;br /&gt;
|-&lt;br /&gt;
|0||data||a||1&lt;br /&gt;
|-&lt;br /&gt;
|1||a||b||2&lt;br /&gt;
|-&lt;br /&gt;
|2||b||a||4&lt;br /&gt;
|-&lt;br /&gt;
|3||a||b||8&lt;br /&gt;
|-&lt;br /&gt;
|4||b||a||16&lt;br /&gt;
|-&lt;br /&gt;
|5||a||b||32&lt;br /&gt;
|-&lt;br /&gt;
|6||b||a||64&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''NOTE:''' think about which array is the correct one to return from your scan method given how you write your code.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
{{CodeToImplement|StepEfficientParallelSumScanner|sumScan|scan.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|int[] sumScan(int[] data)}}&lt;br /&gt;
=Challenge Problem=&lt;br /&gt;
[[Work_Efficient_Parallel_Scan_Assignment|Blelloch (Work-efficient) Scan]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__ScanTestSuite|scan.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Pledge, Acknowledgments, Citations=&lt;br /&gt;
{{Pledge|exercise-scan}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=5415</id>
		<title>Scan</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=5415"/>
		<updated>2026-03-17T03:31:19Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* PhasableIntArrays */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Scan, also known as parallel prefix, is a fundamental and useful operation in parallel programming.  We will gain experience in building Hillis &amp;amp; Steele scan with an optional work efficient Blellock scan.  &lt;br /&gt;
&lt;br /&gt;
Further, the dependencies in scan:&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;math&amp;gt;y_i = y_{i-1} + x_i&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
make it seem to have little hope for parallelism.  However, simple yet clever approaches can achieve &amp;lt;math&amp;gt;\log ^k n&amp;lt;/math&amp;gt; critical path lengths.&lt;br /&gt;
&lt;br /&gt;
While we will simply implement prefix sum, scan can be used for other associative operations.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum Prefix Sum]&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_1:_Shorter_span,_more_parallel Hillis and Steele Algorithm]&lt;br /&gt;
&lt;br /&gt;
[https://dl.acm.org/citation.cfm?coll=GUIDE&amp;amp;dl=GUIDE&amp;amp;id=7903 Data parallel algorithms] &lt;br /&gt;
&lt;br /&gt;
{{CollapsibleYouTube|Hillis and Steele Scan|&amp;lt;youtube&amp;gt;RdfmxfZBHpo&amp;lt;/youtube&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
[[File:Hillis-Steele_Prefix_Sum.svg]]&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
[https://docs.google.com/presentation/d/1xUsx7-n6Ocvm2pQSQX3canzxqYsMo-xka5bw7qFugNk/pub slides]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;RmekgaW8X8A&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Visualization=&lt;br /&gt;
{{Viz|ScanViz|scan.viz|main}}&lt;br /&gt;
&lt;br /&gt;
[[File:Step_Efficient_Scan_Viz.png]]&lt;br /&gt;
&lt;br /&gt;
=Warmup=&lt;br /&gt;
[[Sequential_Scan_Assignment|Sequential Sum Scan]]&lt;br /&gt;
&lt;br /&gt;
=Client=&lt;br /&gt;
{{Client|StepEfficientParallelSumScannerClient|scan.client|main}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|StepEfficientParallelSumScannerClient|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
OutOfPlaceSumScanner sumScanner = new StepEfficientParallelSumScanner();&lt;br /&gt;
int[] data = {1, 2, 3, 4, 5, 6, 7, 8};&lt;br /&gt;
System.out.println(Arrays.toString(data));&lt;br /&gt;
int[] result = sumScanner.sumScan(data);&lt;br /&gt;
System.out.println(Arrays.toString(result));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|StepEfficientParallelSumScannerClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;[1, 2, 3, 4, 5, 6, 7, 8]&lt;br /&gt;
[1, 3, 6, 10, 15, 21, 28, 36]&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Code To Use=&lt;br /&gt;
==[[PowersOf2Iterable]]==&lt;br /&gt;
This [[PowersOf2Iterable|exercise]] should come in handy here. Recall that we implemented a &amp;lt;code&amp;gt;PowersOfTwoLessThan&amp;lt;/code&amp;gt; class in this exercise.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
for(int v : new PowersOfTwoLessThan(71)) {&lt;br /&gt;
    System.out.println(v);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
==PhasableIntArrays==&lt;br /&gt;
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]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html#srcForPhase(int) srcForPhase(phaseIndex)]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/phasable/AbstractPhasable.html#dstForPhase(int) drcForPhase(phaseIndex)]&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
This is what &amp;lt;code&amp;gt;PhasableIntArrays&amp;lt;/code&amp;gt; helps us with. By using its &amp;lt;code&amp;gt;dstForPhase(int phaseIndex)&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;srcForPhase(int phaseIndex)&amp;lt;/code&amp;gt;, we can get the source and destination array corresponding to the phase.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!'''phase''' !! src !! dst !! power of 2&lt;br /&gt;
|-&lt;br /&gt;
|0||data||a||1&lt;br /&gt;
|-&lt;br /&gt;
|1||a||b||2&lt;br /&gt;
|-&lt;br /&gt;
|2||b||a||4&lt;br /&gt;
|-&lt;br /&gt;
|3||a||b||8&lt;br /&gt;
|-&lt;br /&gt;
|4||b||a||16&lt;br /&gt;
|-&lt;br /&gt;
|5||a||b||32&lt;br /&gt;
|-&lt;br /&gt;
|6||b||a||64&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''NOTE:''' think about which array is the correct one to return from your scan method given how you write your code.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
{{CodeToImplement|StepEfficientParallelSumScanner|sumScan|scan.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|int[] sumScan(int[] data)}}&lt;br /&gt;
=Challenge Problem=&lt;br /&gt;
[[Work_Efficient_Parallel_Scan_Assignment|Blelloch (Work-efficient) Scan]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__ScanTestSuite|scan.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Pledge, Acknowledgments, Citations=&lt;br /&gt;
{{Pledge|exercise-scan}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Matrix_MapReduce_Framework_Assignment&amp;diff=5414</id>
		<title>Matrix MapReduce Framework Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Matrix_MapReduce_Framework_Assignment&amp;diff=5414"/>
		<updated>2026-03-06T01:55:49Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* return value */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;credit for this assignment: Finn Voichick and Dennis Cosgrove&lt;br /&gt;
=Motivation=&lt;br /&gt;
Dealing with [https://en.wikipedia.org/wiki/Big_data big data] is Hansel-level hot right now.  We will build two implementations to better understand the inner workings of Hadoop and Spark-like frameworks.  Your frameworks will actually be more general than just MapReduce.&lt;br /&gt;
&lt;br /&gt;
The Matrix implementation gives us experience with dividing the work up into thread confined tasks whose results are then combined together.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
[https://en.wikipedia.org/wiki/MapReduce wikipedia article on MapReduce]&lt;br /&gt;
&lt;br /&gt;
As the name suggests, MapReduce refers to the process of mapping then reducing some stream of data. At its core, all a MapReduce algorithm entails is transforming a list of one kind of item before collecting those items and reducing them down to a single values per key using some computation. As you can probably tell, the concept of MapReduce is extremely general and can apply to a wide berth of problems. In this assignment, we will use MapReduce to simulate the Facebook mutual friends algorithm (finding the mutual friends between all friend pairs) as well as pinpointing the offending well in the 1854 Soho Cholera Outbreak.&lt;br /&gt;
&lt;br /&gt;
For more information on the general concept of MapReduce, refer to [https://stackoverflow.com/questions/28982/simple-explanation-of-mapreduce this] article.&lt;br /&gt;
&lt;br /&gt;
=Java Advice=&lt;br /&gt;
==Parameterized Type Array Tip==&lt;br /&gt;
We need to create an array of Maps in this assignment, but creating arrays of parameterized types in Java is madness inducing.  Some details are available in [https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createArrays Java Generics Restrictions].&lt;br /&gt;
&lt;br /&gt;
The example below creates an array of List&amp;lt;String&amp;gt;.  Creating Map&amp;lt;K, V&amp;gt; follows a similar syntax. The &amp;lt;code&amp;gt;@SuppressWarnings&amp;lt;/code&amp;gt; annotation is optional.&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;@SuppressWarnings(&amp;quot;unchecked&amp;quot;)&lt;br /&gt;
List&amp;lt;String&amp;gt;[] result = new List[length];&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use map.entrySet()==&lt;br /&gt;
Prefer the use of [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#entrySet-- map.entrySet()] over map.keySet() followed by looking up the value with map.get(key).&lt;br /&gt;
&lt;br /&gt;
==Use the appropriate version of join_fork_loop==&lt;br /&gt;
there are many overloaded versions of join_fork_loop including:&lt;br /&gt;
&lt;br /&gt;
* [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#join_fork_loop(int,int,fj.api.TaskIntFunction) range of ints from min to maxExclusive]&lt;br /&gt;
* [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#join_fork_loop(T%5B%5D,fj.api.TaskFunction) array]&lt;br /&gt;
* [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#join_fork_loop(java.lang.Iterable,fj.api.TaskFunction) Iterable]&lt;br /&gt;
&lt;br /&gt;
choose the correct one for each situation where &amp;quot;correct&amp;quot; is often the one that produces the cleanest code.&lt;br /&gt;
&lt;br /&gt;
=Mistakes To Avoid=&lt;br /&gt;
{{Warning | Arrays (and Matrices) are initially filled with null.  You must fill them with instances before you act on individual elements.}}&lt;br /&gt;
&lt;br /&gt;
{{Warning | Ensure that your [[Ranges|Ranges exercise]] is conforming to spec.}}&lt;br /&gt;
&lt;br /&gt;
=Code To Use=&lt;br /&gt;
==Mapper&amp;lt;E,K,V&amp;gt;==&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/mapreduce/core/Mapper.html Mapper]&lt;br /&gt;
&lt;br /&gt;
recall your [[MapReduce_Mapper_Assignment|Mapper]] exercise.&lt;br /&gt;
&lt;br /&gt;
==AccumulatorCombinerReducer&amp;lt;V,A,R&amp;gt;==&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/mapreduce/core/AccumulatorCombinerReducer.html AccumulatorCombinerReducer]&lt;br /&gt;
&lt;br /&gt;
recall your [[MapReduce_Reducer_Assignment|AccumulatorCombinerReducer]] exercise.&lt;br /&gt;
&lt;br /&gt;
==Ranges==&lt;br /&gt;
&lt;br /&gt;
recall your [[Ranges|Ranges]] exercise.&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/range/exercise/Range.html class Range]&lt;br /&gt;
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/range/exercise/Range.html#min() min()]&lt;br /&gt;
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/range/exercise/Range.html#maxExclusive() maxExclusive()]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/range/exercise/Ranges.html class Ranges]&lt;br /&gt;
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/range/exercise/Ranges.html#slice(int,int,int) &amp;lt;nowiki&amp;gt;static Range[] slice​(int min, int maxExclusive, int numRanges)&amp;lt;/nowiki&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
==MultiWrapMap==&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/util/MultiWrapMap.html class MultiWrapMap&amp;lt;K,V&amp;gt;] extends [https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/AbstractMap.html AbstractMap&amp;lt;K, V&amp;gt;]&lt;br /&gt;
: constructor [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/util/MultiWrapMap.html#%3Cinit%3E(java.util.Map%5B%5D,java.util.function.ToIntFunction) MultiWrapMap(maps, hashFunction)]&lt;br /&gt;
&lt;br /&gt;
=A Path To Victory=&lt;br /&gt;
* [[Int_Sum_MapReduce_Apps_Studio|Int Sum MR App Exercise]]&lt;br /&gt;
* [[Collector_MapReduce_Studio|Reducer Exercise]]&lt;br /&gt;
* [[Mutual_Friends_MapReduce_Application|Mutual Friends MR App Exercise]]&lt;br /&gt;
* [[Card_Only_Sequential_Map_Reduce_Framework_Assignment|Card Only Sequential Map Reduce Framework Warm Up]]&lt;br /&gt;
* [[Word_Count_Only_Parallel_Map_Reduce_Framework_Assignment|Word Count Only Parallel MapReduce Framework Warm Up]]&lt;br /&gt;
* [[Bottleneck_MapReduce_Framework_Assignment|Bottlenecked MapReduce Framework Exercise]]&lt;br /&gt;
* '''This Exercise'''&lt;br /&gt;
&lt;br /&gt;
==Matrix MapReduce Framework==&lt;br /&gt;
{{CodeToImplement|MatrixMapReduceFramework|mapper&amp;lt;br/&amp;gt;accumulatorCombinerReducer&amp;lt;br/&amp;gt; mapAndAccumulateTaskCount&amp;lt;br/&amp;gt;mapAndAccumulateTaskCount&amp;lt;br/&amp;gt;combineAndReduceTaskCount&amp;lt;br/&amp;gt;hashFunction&amp;lt;br/&amp;gt;mapAndAccumulateAll&amp;lt;br&amp;gt;combineAndFinishAll|mapreduce.framework.matrix.exercise}}&lt;br /&gt;
&lt;br /&gt;
Navigate to the &amp;lt;code&amp;gt;MatrixMapReduceFramework.java&amp;lt;/code&amp;gt; class and there will be several drawing your attention to instance variables you should be keeping methods and two interesting methods for you to complete: mapAndAccumulateAll and combineAndFinishAll. These frameworks are meant to be extremely general and applied to more specific uses of MapReduce.&lt;br /&gt;
&lt;br /&gt;
The matrix framework is much more complex than the bottlenecked framework, but it boosts performance by grouping the map and accumulate stages so that everything can run in parallel. It does so by slicing up the given data into the specified mapTaskCount number of slices and assigns a reduce task number to each entry using the HashUtils arrayIndexForKey() method. This, in effect, creates a matrix of dictionaries, hence the name of the framework. In the combineAndFinishAll stage, the matrix comes in handy by allowing us to go directly down the columns of the matrix (as each key is essentially grouped into a bucket), combining and reducing elements all-in-one. This concept was explained in more depth during class.&lt;br /&gt;
&lt;br /&gt;
===constructor===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
	public MatrixMapReduceFramework(Mapper&amp;lt;E, K, V&amp;gt; mapper,&lt;br /&gt;
			AccumulatorCombinerReducer&amp;lt;V, A, R&amp;gt; accumulatorCombinerReducer,&lt;br /&gt;
			int mapAndAccumulateTaskCount,&lt;br /&gt;
			int combineAndReduceTaskCount,&lt;br /&gt;
			ToIntFunction&amp;lt;K&amp;gt; hashFunction) {&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Hang onto each parameter value in an instance variable.&lt;br /&gt;
&lt;br /&gt;
===mapper===&lt;br /&gt;
This method exists to remind you to use the mapper. &lt;br /&gt;
&lt;br /&gt;
===accumulatorCombinerReducer===&lt;br /&gt;
This method exists to remind you to use the accumulatorCombinerReducer. &lt;br /&gt;
&lt;br /&gt;
===mapAndAccumulateTaskCount===&lt;br /&gt;
This method exists to remind you to use the mapAndAccumulateTaskCount.&lt;br /&gt;
&lt;br /&gt;
Create a row in your matrix for each mapAndAccumulate task.&lt;br /&gt;
&lt;br /&gt;
===combineAndReduceTaskCount===&lt;br /&gt;
This method exists to remind you to use the combineAndReduceTaskCount. &lt;br /&gt;
&lt;br /&gt;
Create a column in your matrix for each combineAndReduc task.&lt;br /&gt;
&lt;br /&gt;
===hashFunction===&lt;br /&gt;
This method exists to remind you to use the hashFunction. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===mapAndAccumulateAll===&lt;br /&gt;
{{Parallel|Map&amp;lt;K, A&amp;gt;[][] mapAndAccumulateAll(E[] input)}}&lt;br /&gt;
&lt;br /&gt;
In this stage, you will map and accumulate a given array of data into a matrix of dictionaries. This method should run in parallel while performing the map and accumulate portions of the bottlenecked framework (which we recommend you complete prior to embarking on this mission). As mentioned previously, the input should be sliced into a mapTaskCount number of IndexedRanges and then mapped/accumulated into its appropriate dictionary in the matrix. Although you could slice up the data into chunks yourself, we require using an identical algorithm as performed the &amp;lt;code&amp;gt;Range&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Ranges&amp;lt;/code&amp;gt; classes introduced earlier in the course.  This will allow us to provide better feedback to allow you to pinpoint bugs sooner.  What is the best way to perform an identical algorithm to your Ranges exercise?  Use your Ranges exercise, of course.&lt;br /&gt;
&lt;br /&gt;
For each slice, the mapper should map the input into its appropriate cell in the matrix and accumulate it into that specific dictionary. Essentially, you will need to nestle the actions of the accumulate method into the mapper. In order to find where the input should go in the matrix, remember that you can either pass &amp;lt;code&amp;gt;Map[][]::new&amp;lt;/code&amp;gt; to [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#join_fork_loop(java.lang.Class,T%5B%5D,fj.api.TaskFunction) &amp;lt;code&amp;gt;join_fork_loop&amp;lt;/code&amp;gt;] or use [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#join_void_fork_loop_with_index(T%5B%5D,fj.api.TaskConsumerWithIndex) &amp;lt;code&amp;gt;join_void_fork_loop_with_index&amp;lt;/code&amp;gt;] and HashUtils has an arrayIndexForKey method.  Which is applicable to the row and which is applicable to the column?&lt;br /&gt;
&lt;br /&gt;
Hint: The number of rows should match the number of Ranges.&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do NOT copy over the Bottlenecked approach.  Perform accumulation live}}&lt;br /&gt;
&lt;br /&gt;
[[File:Matrix map accumulate all.png|400px]] [https://docs.google.com/presentation/d/1bSLKsI5u2e_tFc0d-RSb0BIDwA-kD75U6yXfxvpMf6Y/pub?start=false&amp;amp;loop=false&amp;amp;delayms=3000&amp;amp;slide=id.g7ebdc248f6_0_347 slide]&lt;br /&gt;
&lt;br /&gt;
[[File:Matrix map accumulate art.png|400px]] [https://docs.google.com/presentation/d/1bSLKsI5u2e_tFc0d-RSb0BIDwA-kD75U6yXfxvpMf6Y/pub?start=false&amp;amp;loop=false&amp;amp;delayms=3000&amp;amp;slide=id.g7ebdc248f6_0_388 slide]&lt;br /&gt;
&amp;lt;!-- [[File:Matrix map accumulate death.png]]--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===combineAndReduceAll===&lt;br /&gt;
{{Parallel|Map&amp;lt;K, R&amp;gt; combineAndReduceAll(Map&amp;lt;K, A&amp;gt;[][] input)}}&lt;br /&gt;
&lt;br /&gt;
In this stage, you will take the matrix you just completed and combine all of the separate rows down to one array. Afterward, you will convert this combined array of maps into one final map. This method should run in parallel.&lt;br /&gt;
&lt;br /&gt;
As mentioned previously, you should go directly down the matrix to access the same bucket across the different slices you created in the mapAndAccumulateAll step. For all of the maps in a column, you should go through each entry and combine it down into one row. You will need to make use of the Reducer’s reduce() method again, but you will also need to make use of the combine() method.  Although a combine step was unnecessary for the bottlenecked framework, the reduce() step should mirror what you did previously.&lt;br /&gt;
&lt;br /&gt;
[[File:Matrix combine finish all.png|400px]] [https://docs.google.com/presentation/d/1bSLKsI5u2e_tFc0d-RSb0BIDwA-kD75U6yXfxvpMf6Y/pub?start=false&amp;amp;loop=false&amp;amp;delayms=3000&amp;amp;slide=id.g7ebdc248f6_0_425 slide]&lt;br /&gt;
====return value====&lt;br /&gt;
{{Requirement|You must use the provided [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/util/ForwardingMap.html ForwardingMap] class to collect the output and return it as a valid output. (ForwardingMap implements Map.)}}&lt;br /&gt;
&lt;br /&gt;
To understand how to use ForwardingMap, we can investigate an snippet of its code:&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|MultiWrapMap|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class ForwardingMap&amp;lt;K, V&amp;gt; extends AbstractMap&amp;lt;K, V&amp;gt; {&lt;br /&gt;
	private final Map&amp;lt;K, V&amp;gt;[] wrappees;&lt;br /&gt;
	private final ToIntFunction&amp;lt;K&amp;gt; toArrayIndexFunction;&lt;br /&gt;
	public ForwardingMap(Map&amp;lt;K, V&amp;gt;[] wrappees, ToIntFunction&amp;lt;K&amp;gt; toArrayIndexFunction) {&lt;br /&gt;
		this.wrappees = wrappees;&lt;br /&gt;
		this.toArrayIndexFunction = toArrayIndexFunction;&lt;br /&gt;
	}&lt;br /&gt;
	public ForwardingMap(List&amp;lt;Map&amp;lt;K, V&amp;gt;&amp;gt; wrappees, ToIntFunction&amp;lt;K&amp;gt; toArrayIndexFunction) {&lt;br /&gt;
		this(wrappees.toArray(Map[]::new), toArrayIndexFunction);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	private Map&amp;lt;K, V&amp;gt; wrappeeFor(K key) {&lt;br /&gt;
		int index = toArrayIndexFunction.applyAsInt(key);&lt;br /&gt;
		return wrappees[index];&lt;br /&gt;
	}&lt;br /&gt;
	@Override&lt;br /&gt;
	public V get(Object key) {&lt;br /&gt;
		return wrappeeFor((K)key).get(key);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
The constructor requires an array of Maps and a toArrayIndexFuntion.  What should you pass in as the toArrayIndexFuntion? &lt;br /&gt;
&lt;br /&gt;
'''Hint:''' you cannot just pass this.hashFunction, since the hashFunction does not know the length of the Map&amp;lt;K, R&amp;gt;[]. What function have you previously used to turn the hashFunction's output into an index?&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__MatrixFrameworkTestSuite|mapreduce.framework.matrix.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Pledge, Acknowledgments, Citations=&lt;br /&gt;
{{Pledge|mapreduce-matrix-framework}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Bottleneck_MapReduce_Framework_Assignment&amp;diff=5413</id>
		<title>Bottleneck MapReduce Framework Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Bottleneck_MapReduce_Framework_Assignment&amp;diff=5413"/>
		<updated>2026-02-26T04:52:40Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* reduceAll */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;credit for this assignment: Finn Voichick and Dennis Cosgrove&lt;br /&gt;
=Motivation=&lt;br /&gt;
Dealing with [https://en.wikipedia.org/wiki/Big_data big data] is Hansel-level hot right now.  We will build two implementations to better understand the inner workings of Hadoop and Spark-like frameworks.  Your frameworks will actually be more general than just MapReduce.&lt;br /&gt;
&lt;br /&gt;
This framework will help prepare you for building the [[Matrix_MapReduce_Framework_Assignment|Matrix Framework]] next.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
[https://en.wikipedia.org/wiki/MapReduce wikipedia article on MapReduce]&lt;br /&gt;
&lt;br /&gt;
As the name suggests, MapReduce refers to the process of mapping then reducing some stream of data. At its core, all a MapReduce algorithm entails is transforming a list of one kind of item before collecting those items and reducing them down to a single values per key using some computation. As you can probably tell, the concept of MapReduce is extremely general and can apply to a wide berth of problems. In this assignment, we will build a MapReduce framework which can be used to to simulate the Facebook mutual friends algorithm (finding the mutual friends between all friend pairs) as well as pinpointing the offending well in the 1854 Soho Cholera Outbreak.&lt;br /&gt;
&lt;br /&gt;
For more information on the general concept of MapReduce, refer to [https://stackoverflow.com/questions/28982/simple-explanation-of-mapreduce this] article.&lt;br /&gt;
&lt;br /&gt;
=Java Advice=&lt;br /&gt;
==Use map.entrySet()==&lt;br /&gt;
Prefer the use of [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#entrySet-- map.entrySet()] over map.keySet() followed by looking up the value with map.get(key).&lt;br /&gt;
&lt;br /&gt;
==Use the appropriate version of join_fork_loop==&lt;br /&gt;
there are many overloaded versions of join_fork_loop including:&lt;br /&gt;
&lt;br /&gt;
* [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#join_fork_loop(int,int,fj.api.TaskIntFunction) range of ints from min to maxExclusive]&lt;br /&gt;
* [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#join_fork_loop(T%5B%5D,fj.api.TaskFunction) array]&lt;br /&gt;
* [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#join_fork_loop(java.lang.Iterable,fj.api.TaskFunction) Iterable]&lt;br /&gt;
&lt;br /&gt;
choose the correct one for each situation where &amp;quot;correct&amp;quot; is often the one that produces the cleanest code.&lt;br /&gt;
&lt;br /&gt;
=A Path To Victory=&lt;br /&gt;
* [[MapReduce_Mapper_Assignment|MapReduce Mapper Assignment]]&lt;br /&gt;
* [[MapReduce_Reducer_Assignment|MapReduce Reducer Assignment]]&lt;br /&gt;
* [[Card_Only_Sequential_Map_Reduce_Framework_Assignment|Card Only Sequential Map Reduce Framework Warm Up]]&lt;br /&gt;
* [[Word_Count_Only_Parallel_Map_Reduce_Framework_Assignment|Word Count Only Parallel MapReduce Framework Warm Up]]&lt;br /&gt;
* [[Mutual_Friends_MapReduce_Application|Mutual Friends MR App Exercise]]&lt;br /&gt;
* '''This Exercise'''&lt;br /&gt;
&lt;br /&gt;
=Warm Ups=&lt;br /&gt;
[[Card_Only_Sequential_Map_Reduce_Framework_Assignment|Card Only Sequential Map Reduce Framework]]&lt;br /&gt;
&lt;br /&gt;
[[Word_Count_Only_Parallel_Map_Reduce_Framework_Assignment|Word Count Only Parallel MapReduce Framework]]&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
==WordCountOnlyParallelMapReduceFramework==&lt;br /&gt;
You will build each of the three methods mapReduceAll(input) invokes.  Much can be gleaned from the parameters and return values of these methods.&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|mapReduceAll(TextSection[] input)|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public Map&amp;lt;K, R&amp;gt; mapReduceAll(E[] input) throws InterruptedException, ExecutionException {&lt;br /&gt;
	List&amp;lt;List&amp;lt;Map.Entry&amp;lt;K, V&amp;gt;&amp;gt;&amp;gt; mapAllResult = mapAll(input);&lt;br /&gt;
	Map&amp;lt;K, A&amp;gt; accumulateAllResult = accumulateAll(mapAllResult);&lt;br /&gt;
	return reduceAll(accumulateAllResult);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
===mapAll===&lt;br /&gt;
&lt;br /&gt;
For Account[] input:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| input[0] || Account.parse(&amp;quot;A&amp;quot;, &amp;quot;B, C, D&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| input[1] || Account.parse(&amp;quot;B&amp;quot;, &amp;quot;A, C, D, E&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| input[2] || Account.parse(&amp;quot;C&amp;quot;, &amp;quot;A, B, D, E&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| input[3] || Account.parse(&amp;quot;D&amp;quot;, &amp;quot;A, B, C, E&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| input[4] || Account.parse(&amp;quot;E&amp;quot;, &amp;quot;B, C, D&amp;quot;)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
[[File:MutualFriends MapAllResult.svg]]&lt;br /&gt;
&lt;br /&gt;
===accumulateAll===&lt;br /&gt;
&lt;br /&gt;
[[File:MutualFriends AccumulateAllResult.svg]]&lt;br /&gt;
&lt;br /&gt;
===reduceAll===&lt;br /&gt;
[[File:MutualFriends_ReduceAllResult.svg]]&lt;br /&gt;
&lt;br /&gt;
==Client==&lt;br /&gt;
{{Client|WordCountOnlyParallelMapReduceFrameworkClient|mapreduce.framework.parallel.client|main}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|WordCountOnlyParallelMapReduceFrameworkClient|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
// credit: https://web.archive.org/web/20201109035737/http://stevekrenzel.com/finding-friends-with-mapreduce&lt;br /&gt;
Account[] input = {&lt;br /&gt;
		Account.parse(&amp;quot;A&amp;quot;, &amp;quot;B,C,D&amp;quot;),&lt;br /&gt;
		Account.parse(&amp;quot;B&amp;quot;, &amp;quot;A,C,D,E&amp;quot;),&lt;br /&gt;
		Account.parse(&amp;quot;C&amp;quot;, &amp;quot;A,B,D,E&amp;quot;),&lt;br /&gt;
		Account.parse(&amp;quot;D&amp;quot;, &amp;quot;A,B,C,E&amp;quot;),&lt;br /&gt;
		Account.parse(&amp;quot;E&amp;quot;, &amp;quot;B,C,D&amp;quot;)&lt;br /&gt;
};&lt;br /&gt;
MapReduceFramework&amp;lt;Account, // E&lt;br /&gt;
		OrderedPair&amp;lt;AccountId&amp;gt;, // K&lt;br /&gt;
		Set&amp;lt;AccountId&amp;gt;, // V&lt;br /&gt;
		List&amp;lt;Set&amp;lt;AccountId&amp;gt;&amp;gt;, // A&lt;br /&gt;
		Set&amp;lt;AccountId&amp;gt; // R&lt;br /&gt;
&amp;gt; framework = new BottleneckedMapReduceFramework&amp;lt;&amp;gt;(&lt;br /&gt;
		new MutualFriendsMapper(),&lt;br /&gt;
		new MutualFriendsClassicReducer());&lt;br /&gt;
Map&amp;lt;OrderedPair&amp;lt;AccountId&amp;gt;, Set&amp;lt;AccountId&amp;gt;&amp;gt; map = framework.mapReduceAll(input);&lt;br /&gt;
for (Map.Entry&amp;lt;OrderedPair&amp;lt;AccountId&amp;gt;, Set&amp;lt;AccountId&amp;gt;&amp;gt; entry : map.entrySet()) {&lt;br /&gt;
	System.out.println(entry);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|CardOnlySequentialMapReduceFrameworkClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;(A, B)=[C, D]&lt;br /&gt;
(A, C)=[B, D]&lt;br /&gt;
(D, E)=[B, C]&lt;br /&gt;
(A, D)=[B, C]&lt;br /&gt;
(B, C)=[A, D, E]&lt;br /&gt;
(B, D)=[A, C, E]&lt;br /&gt;
(B, E)=[C, D]&lt;br /&gt;
(C, D)=[A, B, E]&lt;br /&gt;
(C, E)=[B, D]&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
&lt;br /&gt;
==Bottlenecked MapReduce Framework==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|BottleneckedMapReduceFramework|constructor&amp;lt;br/&amp;gt;mapper&amp;lt;br/&amp;gt;accumulatorCombinerReducer&amp;lt;br/&amp;gt;mapAll&amp;lt;br/&amp;gt;accumulateAll&amp;lt;br/&amp;gt;toReductions&amp;lt;br/&amp;gt;toReductionMap|mapreduce.framework.bottlenecked.exercise}}&lt;br /&gt;
&lt;br /&gt;
Navigate to the &amp;lt;code&amp;gt;BottleneckedMapReduceFramework.java&amp;lt;/code&amp;gt; class and there will be three methods for you to complete: mapAll, accumulateAll, and finishAll. These frameworks are meant to be extremely general and applied to more specific uses of MapReduce.  Whereas the warm ups for this lab serve to prepare you to build this required section of the lab, this bottlenecked framework is in many ways a warm up for the matrix implementation.&lt;br /&gt;
&lt;br /&gt;
===constructor, mapper, accumulatorCombinerReducer===&lt;br /&gt;
As usual, store the values passed into the constructor in instance variables and implement the getter methods to retrieve their values.&lt;br /&gt;
&lt;br /&gt;
===mapAll===&lt;br /&gt;
NOTE: If you struggle to get through this method, you are strongly encouraged to try the warm-ups.&lt;br /&gt;
&lt;br /&gt;
{{Parallel|List&amp;lt;List&amp;lt;Map.Entry&amp;lt;K, V&amp;gt;&amp;gt;&amp;gt; mapAll(E[] input)}}&lt;br /&gt;
&lt;br /&gt;
With this method, you will map all of the elements of an array of data into a new array of equivalent size consisting of Lists of key value pairs.  We will leverage the Mapper which is a field/instance variable on this BottleneckedFramework instance.  Invoking the mapper's map method with an element of the input array should return a List of the key value pairs for that element. &lt;br /&gt;
&lt;br /&gt;
Hint: you should create a List of Lists equivalent in size to the original array. Each inner list will contain all of the emitted (that is: returned) (key,value) pairs for its item.&lt;br /&gt;
&lt;br /&gt;
[[File:Bottlenecked map all.png|400px]] [https://docs.google.com/presentation/d/1NqAXEEDasv6ZlCE1TvRnCVU7WgI-gOfny-JAkzoylC8/pub#slide=id.g1f5c0afde45_0_147 slide]&lt;br /&gt;
&lt;br /&gt;
{{Tip|Think about which verion of the [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/fj/FJ.html#join_fork_loop(T%5B%5D,fj.api.TaskFunction) '''join_fork_loop'''] would be the best to use here.}}&lt;br /&gt;
&lt;br /&gt;
====Example: Mutual Friends====&lt;br /&gt;
=====Input=====&lt;br /&gt;
[[File:Mutual_Friends_Input.svg]]&lt;br /&gt;
=====Result of mapAll(input)=====&lt;br /&gt;
[[File:Mutual_Friends_Bottlenecked_Map_All_Result.svg|1024px]]&lt;br /&gt;
&lt;br /&gt;
===accumulateAll===&lt;br /&gt;
{{Sequential|Map&amp;lt;K, A&amp;gt; accumulateAll(List&amp;lt;List&amp;lt;Map.Entry&amp;lt;K, V&amp;gt;&amp;gt;&amp;gt; mapAllResults)}}&lt;br /&gt;
&lt;br /&gt;
This middle step is often excluded in more advanced MapReduce applications. This step introduces a long sequential strand. In the matrix framework implementation, we will do away with this step altogether for the sake of performance.&lt;br /&gt;
&lt;br /&gt;
You will make use of the Reducer provided to you.&lt;br /&gt;
&lt;br /&gt;
In this method, you will take in the list of lists you previously created in mapAll.  You probably noticed that the method must return a map of &amp;lt;K, A&amp;gt;, which differs from the &amp;lt;K, V&amp;gt; lists fed into the method.  The framework is designed this way as the data originally fed into the mapping stage can be collected into a mutable container before reaching the reduction stage.  You will need to create a mutable container of type A for each unique key encountered and associate the key with that mutable container in a Map.  Whether it is the first time a key has been encountered or not, the value must be accumulated into the mutable container.&lt;br /&gt;
&lt;br /&gt;
{{Tip| Look into the [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#computeIfAbsent-K-java.util.function.Function- computeIfAbsent(key, remappingFunction)] method on [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html Map]}}&lt;br /&gt;
&lt;br /&gt;
[[File:Bottlenecked accumulate all.png|400px]] [https://docs.google.com/presentation/d/1Gtpj6_5_8imUMccpwxmfrOxKSED791OKl4lZR6G2Bm4/pub?start=false&amp;amp;loop=false&amp;amp;delayms=3000&amp;amp;slide=id.g7ea283730e_0_165 slide]&lt;br /&gt;
&lt;br /&gt;
===reduceAll===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
private Map&amp;lt;K, R&amp;gt; reduceAll(Map&amp;lt;K, A&amp;gt; accumulateAllResult) throws InterruptedException, ExecutionException {&lt;br /&gt;
	Set&amp;lt;Map.Entry&amp;lt;K, A&amp;gt;&amp;gt; accumulateAllEntries = accumulateAllResult.entrySet();&lt;br /&gt;
	List&amp;lt;Map.Entry&amp;lt;K, R&amp;gt;&amp;gt; reducedEntries = reduceAllEntries(accumulateAllEntries);&lt;br /&gt;
	return new SingleChainMap&amp;lt;&amp;gt;(reducedEntries);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
====reduceAllEntries====&lt;br /&gt;
{{Parallel|List&amp;lt;Map.Entry&amp;lt;K, R&amp;gt;&amp;gt; reduceAllEntries(Set&amp;lt;Map.Entry&amp;lt;K, A&amp;gt;&amp;gt; accumulateAllEntries)}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
This final step reduces the accumulated data and returns the final map in its reduced form. Again, you may notice that the method returns a map of &amp;lt;K, R&amp;gt; instead of the &amp;lt;K, A&amp;gt; which was returned in the accumulateAll method. This happens for the exact same reason as the accumulateAll method, as the framework is designed to handle cases in which the reduced data differs in type from the accumulated data.&lt;br /&gt;
&lt;br /&gt;
To reduce the data down, use the map returned from the accumulateAll stage and put the results of the reduction into a new map. The provided Reducer will come in extremely handy for this stage, more specifically the finisher which can be called using the &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; method. This step should run in parallel and will probably be the easiest of the three methods.&lt;br /&gt;
&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
[[File:Bottlenecked finish all.png|400px]] [https://docs.google.com/presentation/d/1Gtpj6_5_8imUMccpwxmfrOxKSED791OKl4lZR6G2Bm4/pub?start=false&amp;amp;loop=false&amp;amp;delayms=3000&amp;amp;slide=id.g7ea283730e_0_221 slide]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__BottleneckedFrameworkTestSuite|mapreduce.framework.bottlenecked.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Pledge, Acknowledgments, Citations=&lt;br /&gt;
{{Pledge|mapreduce-bottleneck-framework}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Word_Count_Only_Parallel_Map_Reduce_Framework_Assignment&amp;diff=5412</id>
		<title>Word Count Only Parallel Map Reduce Framework Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Word_Count_Only_Parallel_Map_Reduce_Framework_Assignment&amp;diff=5412"/>
		<updated>2026-02-26T04:49:55Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* toReductionMap */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Building On Previous Exercises=&lt;br /&gt;
Be sure to complete the [[Int_Sum_MapReduce_Apps_Studio#Word_Count_Mapper|Word Count Mapper]] and [[Int_Sum_MapReduce_Apps_Studio#IntSumListAccumulatingReducer|IntSumListAccumulatingReducer]] exercises first.&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
==WordCountOnlyParallelMapReduceFramework==&lt;br /&gt;
You will build each of the three methods mapReduceAll(input) invokes.  Much can be gleaned from the parameters and return values of these methods.&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|mapReduceAll(TextSection[] input)|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public Map&amp;lt;String, Integer&amp;gt; mapReduceAll(TextSection[] input) throws InterruptedException, ExecutionException {&lt;br /&gt;
	List&amp;lt;List&amp;lt;Map.Entry&amp;lt;String, Integer&amp;gt;&amp;gt;&amp;gt; mapAllResult = mapAll(input);&lt;br /&gt;
	Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt; accumulateAllResult = accumulateAll(mapAllResult);&lt;br /&gt;
	return reduceAll(accumulateAllResult);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
===mapAll===&lt;br /&gt;
The TextSection[] input:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| input[0] = || new TextSection(&amp;quot;A canner, exceedingly canny,&amp;quot;) &lt;br /&gt;
|-&lt;br /&gt;
| input[1] = || new TextSection(&amp;quot;One morning remarked to his granny,&amp;quot;) &lt;br /&gt;
|-&lt;br /&gt;
| input[2] = || new TextSection(&amp;quot;\&amp;quot;A canner can can&amp;quot;) &lt;br /&gt;
|-&lt;br /&gt;
| input[3] = || new TextSection(&amp;quot;Anything that he can;&amp;quot;) &lt;br /&gt;
|-&lt;br /&gt;
| input[4] = || new TextSection(&amp;quot;But a canner can't can a can, can he?\&amp;quot;&amp;quot;) &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
mapAll(input) would produce:&lt;br /&gt;
&lt;br /&gt;
[[File:Words_MapAllResult.svg]]&lt;br /&gt;
&lt;br /&gt;
=== accumulateAll===&lt;br /&gt;
accumulateAll(mapAllResult) would produce a &amp;lt;code&amp;gt;Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;.  Below we visualize a Map with 16 buckets that places entries based on the key's hashCode().&lt;br /&gt;
&lt;br /&gt;
[[File:Words AccumulateAllResult.svg]]&lt;br /&gt;
&lt;br /&gt;
=== reduceAll ===&lt;br /&gt;
reduceAll(accumulateAllResult) would produce a &amp;lt;code&amp;gt;Map&amp;lt;String, Integer&amp;gt;&amp;lt;/code&amp;gt;.  Below we visualize a Map with 16 buckets that places entries based on the key's hashCode().&lt;br /&gt;
&lt;br /&gt;
'''Note:''' the keys all fall in the same location as in accumulateAllResult, but are associated with the reduced values.&lt;br /&gt;
&lt;br /&gt;
[[File:Words ReduceAllResult.svg]]&lt;br /&gt;
&lt;br /&gt;
==Client==&lt;br /&gt;
{{Client|WordCountOnlyParallelMapReduceFrameworkClient|mapreduce.framework.parallel.client|main}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|WordCountOnlyParallelMapReduceFrameworkClient|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
TextSection[] input = {&lt;br /&gt;
        new TextSection(&amp;quot;A canner, exceedingly canny,&amp;quot;),&lt;br /&gt;
        new TextSection(&amp;quot;One morning remarked to his granny,&amp;quot;),&lt;br /&gt;
        new TextSection(&amp;quot;\&amp;quot;A canner can can&amp;quot;),&lt;br /&gt;
        new TextSection(&amp;quot;Anything that he can;&amp;quot;),&lt;br /&gt;
        new TextSection(&amp;quot;But a canner can't can a can, can he?\&amp;quot;&amp;quot;),&lt;br /&gt;
};&lt;br /&gt;
MapReduceFramework&amp;lt;TextSection, String, Integer, List&amp;lt;Integer&amp;gt;, Integer&amp;gt; framework = new WordCountOnlyParallelMapReduceFramework(new WordCountMapper(), new SummingIntClassicReducer());&lt;br /&gt;
Map&amp;lt;String, Integer&amp;gt; map = framework.mapReduceAll(input);&lt;br /&gt;
for (Map.Entry&amp;lt;String, Integer&amp;gt; entry : map.entrySet()) {&lt;br /&gt;
    System.out.println(entry);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|CardOnlySequentialMapReduceFrameworkClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;but=1&lt;br /&gt;
a=4&lt;br /&gt;
one=1&lt;br /&gt;
anything=1&lt;br /&gt;
morning=1&lt;br /&gt;
that=1&lt;br /&gt;
can=6&lt;br /&gt;
exceedingly=1&lt;br /&gt;
canner=3&lt;br /&gt;
his=1&lt;br /&gt;
granny=1&lt;br /&gt;
remarked=1&lt;br /&gt;
to=1&lt;br /&gt;
canny=1&lt;br /&gt;
can't=1&lt;br /&gt;
he=2&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
&lt;br /&gt;
==WordCountOnlyParallelMapReduceFramework==&lt;br /&gt;
{{CodeToImplement|WordCountOnlyParallelMapReduceFramework|mapper&amp;lt;br/&amp;gt;reducer&amp;lt;br/&amp;gt;mapAll&amp;lt;br/&amp;gt;accumulateAll&amp;lt;br/&amp;gt;toArrayOfReductions&amp;lt;br/&amp;gt;toReductionMap|mapreduce.framework.parallel.warmup}}&lt;br /&gt;
&lt;br /&gt;
===mapper===&lt;br /&gt;
{{Sequential|public Mapper&amp;lt;Deck, Suit, Integer&amp;gt; mapper()}}&lt;br /&gt;
&lt;br /&gt;
This method exists to remind you to use the mapper. The type is a perfect match for the WordCountMapper: &lt;br /&gt;
&lt;br /&gt;
===reducer===&lt;br /&gt;
{{Sequential|public Reducer&amp;lt;Integer, List&amp;lt;Integer&amp;gt;, Integer&amp;gt; reducer()}}&lt;br /&gt;
&lt;br /&gt;
This method exists to remind you to use the accumulatorCombinerReducer. The type is a perfect match for the SummingIntClassicReducer: &lt;br /&gt;
&lt;br /&gt;
===mapAll===&lt;br /&gt;
{{Parallel|private List&amp;lt;List&amp;lt;KeyValuePair&amp;lt;String, Integer&amp;gt;&amp;gt;&amp;gt; mapAll(TextSection[] input)}}&lt;br /&gt;
&lt;br /&gt;
'''Purpose:''' In parallel, map all the TextSection's using the mapper private instance variable&lt;br /&gt;
&lt;br /&gt;
{{Requirement|Use the mapper's map method}}&lt;br /&gt;
&lt;br /&gt;
===accumulateAll===&lt;br /&gt;
{{Sequential|private Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt; accumulateAll(List&amp;lt;List&amp;lt;Map.Entry&amp;lt;String, Integer&amp;gt;&amp;gt;&amp;gt; mapAllResults)}}&lt;br /&gt;
&lt;br /&gt;
'''Purpose:''' Sequentially, assemble a Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt; where the String in this example is some word from a TextSection, and the List&amp;lt;Integer&amp;gt;'s is a list of 1's representing every time that word was found in a TextSection. &lt;br /&gt;
&lt;br /&gt;
To do this: iterate through all of the entries, and use the accumulatorCombinerReducer's methods to create a new mutable container, and accumulate new values into that container. You may find Map.computeIfAbsent() particularly helpful for this. &lt;br /&gt;
&lt;br /&gt;
{{Requirement|Use the accumulatorCombinerReducer's createMutableContainer and accumulate methods}}&lt;br /&gt;
&lt;br /&gt;
===reduceAll===&lt;br /&gt;
'''Provided'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
private Map&amp;lt;String, Integer&amp;gt; reduceAll(Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt; accumulateAllResult) throws InterruptedException, ExecutionException {&lt;br /&gt;
	Set&amp;lt;Map.Entry&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt;&amp;gt; accumulateAllEntries = accumulateAllResult.entrySet();&lt;br /&gt;
	List&amp;lt;Map.Entry&amp;lt;String, Integer&amp;gt;&amp;gt; reducedEntries = reduceAllEntries(accumulateAllEntries);&lt;br /&gt;
	return new SingleChainMap&amp;lt;&amp;gt;(reducedEntries);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===reduceAllEntries===&lt;br /&gt;
{{Parallel|private List&amp;lt;Map.Entry&amp;lt;String, Integer&amp;gt;&amp;gt; reduceAllEntries(Set&amp;lt;Map.Entry&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt;&amp;gt; accumulateAllEntries)}}&lt;br /&gt;
&lt;br /&gt;
{{Requirement|Use the accumulatorCombinerReducer's reduce method}}&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__WordCountOnlyParallelMapReduceFrameworkTestSuite|mapreduce.framework.parallel.warmup}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Word_Count_Only_Parallel_Map_Reduce_Framework_Assignment&amp;diff=5411</id>
		<title>Word Count Only Parallel Map Reduce Framework Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Word_Count_Only_Parallel_Map_Reduce_Framework_Assignment&amp;diff=5411"/>
		<updated>2026-02-26T04:49:41Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* toReductions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Building On Previous Exercises=&lt;br /&gt;
Be sure to complete the [[Int_Sum_MapReduce_Apps_Studio#Word_Count_Mapper|Word Count Mapper]] and [[Int_Sum_MapReduce_Apps_Studio#IntSumListAccumulatingReducer|IntSumListAccumulatingReducer]] exercises first.&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
==WordCountOnlyParallelMapReduceFramework==&lt;br /&gt;
You will build each of the three methods mapReduceAll(input) invokes.  Much can be gleaned from the parameters and return values of these methods.&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|mapReduceAll(TextSection[] input)|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public Map&amp;lt;String, Integer&amp;gt; mapReduceAll(TextSection[] input) throws InterruptedException, ExecutionException {&lt;br /&gt;
	List&amp;lt;List&amp;lt;Map.Entry&amp;lt;String, Integer&amp;gt;&amp;gt;&amp;gt; mapAllResult = mapAll(input);&lt;br /&gt;
	Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt; accumulateAllResult = accumulateAll(mapAllResult);&lt;br /&gt;
	return reduceAll(accumulateAllResult);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
===mapAll===&lt;br /&gt;
The TextSection[] input:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| input[0] = || new TextSection(&amp;quot;A canner, exceedingly canny,&amp;quot;) &lt;br /&gt;
|-&lt;br /&gt;
| input[1] = || new TextSection(&amp;quot;One morning remarked to his granny,&amp;quot;) &lt;br /&gt;
|-&lt;br /&gt;
| input[2] = || new TextSection(&amp;quot;\&amp;quot;A canner can can&amp;quot;) &lt;br /&gt;
|-&lt;br /&gt;
| input[3] = || new TextSection(&amp;quot;Anything that he can;&amp;quot;) &lt;br /&gt;
|-&lt;br /&gt;
| input[4] = || new TextSection(&amp;quot;But a canner can't can a can, can he?\&amp;quot;&amp;quot;) &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
mapAll(input) would produce:&lt;br /&gt;
&lt;br /&gt;
[[File:Words_MapAllResult.svg]]&lt;br /&gt;
&lt;br /&gt;
=== accumulateAll===&lt;br /&gt;
accumulateAll(mapAllResult) would produce a &amp;lt;code&amp;gt;Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;.  Below we visualize a Map with 16 buckets that places entries based on the key's hashCode().&lt;br /&gt;
&lt;br /&gt;
[[File:Words AccumulateAllResult.svg]]&lt;br /&gt;
&lt;br /&gt;
=== reduceAll ===&lt;br /&gt;
reduceAll(accumulateAllResult) would produce a &amp;lt;code&amp;gt;Map&amp;lt;String, Integer&amp;gt;&amp;lt;/code&amp;gt;.  Below we visualize a Map with 16 buckets that places entries based on the key's hashCode().&lt;br /&gt;
&lt;br /&gt;
'''Note:''' the keys all fall in the same location as in accumulateAllResult, but are associated with the reduced values.&lt;br /&gt;
&lt;br /&gt;
[[File:Words ReduceAllResult.svg]]&lt;br /&gt;
&lt;br /&gt;
==Client==&lt;br /&gt;
{{Client|WordCountOnlyParallelMapReduceFrameworkClient|mapreduce.framework.parallel.client|main}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|WordCountOnlyParallelMapReduceFrameworkClient|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
TextSection[] input = {&lt;br /&gt;
        new TextSection(&amp;quot;A canner, exceedingly canny,&amp;quot;),&lt;br /&gt;
        new TextSection(&amp;quot;One morning remarked to his granny,&amp;quot;),&lt;br /&gt;
        new TextSection(&amp;quot;\&amp;quot;A canner can can&amp;quot;),&lt;br /&gt;
        new TextSection(&amp;quot;Anything that he can;&amp;quot;),&lt;br /&gt;
        new TextSection(&amp;quot;But a canner can't can a can, can he?\&amp;quot;&amp;quot;),&lt;br /&gt;
};&lt;br /&gt;
MapReduceFramework&amp;lt;TextSection, String, Integer, List&amp;lt;Integer&amp;gt;, Integer&amp;gt; framework = new WordCountOnlyParallelMapReduceFramework(new WordCountMapper(), new SummingIntClassicReducer());&lt;br /&gt;
Map&amp;lt;String, Integer&amp;gt; map = framework.mapReduceAll(input);&lt;br /&gt;
for (Map.Entry&amp;lt;String, Integer&amp;gt; entry : map.entrySet()) {&lt;br /&gt;
    System.out.println(entry);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|CardOnlySequentialMapReduceFrameworkClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;but=1&lt;br /&gt;
a=4&lt;br /&gt;
one=1&lt;br /&gt;
anything=1&lt;br /&gt;
morning=1&lt;br /&gt;
that=1&lt;br /&gt;
can=6&lt;br /&gt;
exceedingly=1&lt;br /&gt;
canner=3&lt;br /&gt;
his=1&lt;br /&gt;
granny=1&lt;br /&gt;
remarked=1&lt;br /&gt;
to=1&lt;br /&gt;
canny=1&lt;br /&gt;
can't=1&lt;br /&gt;
he=2&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
&lt;br /&gt;
==WordCountOnlyParallelMapReduceFramework==&lt;br /&gt;
{{CodeToImplement|WordCountOnlyParallelMapReduceFramework|mapper&amp;lt;br/&amp;gt;reducer&amp;lt;br/&amp;gt;mapAll&amp;lt;br/&amp;gt;accumulateAll&amp;lt;br/&amp;gt;toArrayOfReductions&amp;lt;br/&amp;gt;toReductionMap|mapreduce.framework.parallel.warmup}}&lt;br /&gt;
&lt;br /&gt;
===mapper===&lt;br /&gt;
{{Sequential|public Mapper&amp;lt;Deck, Suit, Integer&amp;gt; mapper()}}&lt;br /&gt;
&lt;br /&gt;
This method exists to remind you to use the mapper. The type is a perfect match for the WordCountMapper: &lt;br /&gt;
&lt;br /&gt;
===reducer===&lt;br /&gt;
{{Sequential|public Reducer&amp;lt;Integer, List&amp;lt;Integer&amp;gt;, Integer&amp;gt; reducer()}}&lt;br /&gt;
&lt;br /&gt;
This method exists to remind you to use the accumulatorCombinerReducer. The type is a perfect match for the SummingIntClassicReducer: &lt;br /&gt;
&lt;br /&gt;
===mapAll===&lt;br /&gt;
{{Parallel|private List&amp;lt;List&amp;lt;KeyValuePair&amp;lt;String, Integer&amp;gt;&amp;gt;&amp;gt; mapAll(TextSection[] input)}}&lt;br /&gt;
&lt;br /&gt;
'''Purpose:''' In parallel, map all the TextSection's using the mapper private instance variable&lt;br /&gt;
&lt;br /&gt;
{{Requirement|Use the mapper's map method}}&lt;br /&gt;
&lt;br /&gt;
===accumulateAll===&lt;br /&gt;
{{Sequential|private Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt; accumulateAll(List&amp;lt;List&amp;lt;Map.Entry&amp;lt;String, Integer&amp;gt;&amp;gt;&amp;gt; mapAllResults)}}&lt;br /&gt;
&lt;br /&gt;
'''Purpose:''' Sequentially, assemble a Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt; where the String in this example is some word from a TextSection, and the List&amp;lt;Integer&amp;gt;'s is a list of 1's representing every time that word was found in a TextSection. &lt;br /&gt;
&lt;br /&gt;
To do this: iterate through all of the entries, and use the accumulatorCombinerReducer's methods to create a new mutable container, and accumulate new values into that container. You may find Map.computeIfAbsent() particularly helpful for this. &lt;br /&gt;
&lt;br /&gt;
{{Requirement|Use the accumulatorCombinerReducer's createMutableContainer and accumulate methods}}&lt;br /&gt;
&lt;br /&gt;
===reduceAll===&lt;br /&gt;
'''Provided'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
private Map&amp;lt;String, Integer&amp;gt; reduceAll(Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt; accumulateAllResult) throws InterruptedException, ExecutionException {&lt;br /&gt;
	Set&amp;lt;Map.Entry&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt;&amp;gt; accumulateAllEntries = accumulateAllResult.entrySet();&lt;br /&gt;
	List&amp;lt;Map.Entry&amp;lt;String, Integer&amp;gt;&amp;gt; reducedEntries = reduceAllEntries(accumulateAllEntries);&lt;br /&gt;
	return new SingleChainMap&amp;lt;&amp;gt;(reducedEntries);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===reduceAllEntries===&lt;br /&gt;
{{Parallel|private List&amp;lt;Map.Entry&amp;lt;String, Integer&amp;gt;&amp;gt; reduceAllEntries(Set&amp;lt;Map.Entry&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt;&amp;gt; accumulateAllEntries)}}&lt;br /&gt;
&lt;br /&gt;
{{Requirement|Use the accumulatorCombinerReducer's reduce method}}&lt;br /&gt;
&lt;br /&gt;
===toReductionMap===&lt;br /&gt;
{{Sequential|Map&amp;lt;String, Integer&amp;gt; toReductionMap(Entry&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt;[] entries, List&amp;lt;Integer&amp;gt; reductions)}}&lt;br /&gt;
&lt;br /&gt;
Tip: The index of the Entry in the &amp;quot;entries[]&amp;quot; corresponds directly to the index of the Integer reduction of that entry from &amp;quot;reductions&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__WordCountOnlyParallelMapReduceFrameworkTestSuite|mapreduce.framework.parallel.warmup}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Word_Count_Only_Parallel_Map_Reduce_Framework_Assignment&amp;diff=5410</id>
		<title>Word Count Only Parallel Map Reduce Framework Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Word_Count_Only_Parallel_Map_Reduce_Framework_Assignment&amp;diff=5410"/>
		<updated>2026-02-26T04:48:38Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* toArrayOfEntries */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Building On Previous Exercises=&lt;br /&gt;
Be sure to complete the [[Int_Sum_MapReduce_Apps_Studio#Word_Count_Mapper|Word Count Mapper]] and [[Int_Sum_MapReduce_Apps_Studio#IntSumListAccumulatingReducer|IntSumListAccumulatingReducer]] exercises first.&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
==WordCountOnlyParallelMapReduceFramework==&lt;br /&gt;
You will build each of the three methods mapReduceAll(input) invokes.  Much can be gleaned from the parameters and return values of these methods.&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|mapReduceAll(TextSection[] input)|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public Map&amp;lt;String, Integer&amp;gt; mapReduceAll(TextSection[] input) throws InterruptedException, ExecutionException {&lt;br /&gt;
	List&amp;lt;List&amp;lt;Map.Entry&amp;lt;String, Integer&amp;gt;&amp;gt;&amp;gt; mapAllResult = mapAll(input);&lt;br /&gt;
	Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt; accumulateAllResult = accumulateAll(mapAllResult);&lt;br /&gt;
	return reduceAll(accumulateAllResult);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
===mapAll===&lt;br /&gt;
The TextSection[] input:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| input[0] = || new TextSection(&amp;quot;A canner, exceedingly canny,&amp;quot;) &lt;br /&gt;
|-&lt;br /&gt;
| input[1] = || new TextSection(&amp;quot;One morning remarked to his granny,&amp;quot;) &lt;br /&gt;
|-&lt;br /&gt;
| input[2] = || new TextSection(&amp;quot;\&amp;quot;A canner can can&amp;quot;) &lt;br /&gt;
|-&lt;br /&gt;
| input[3] = || new TextSection(&amp;quot;Anything that he can;&amp;quot;) &lt;br /&gt;
|-&lt;br /&gt;
| input[4] = || new TextSection(&amp;quot;But a canner can't can a can, can he?\&amp;quot;&amp;quot;) &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
mapAll(input) would produce:&lt;br /&gt;
&lt;br /&gt;
[[File:Words_MapAllResult.svg]]&lt;br /&gt;
&lt;br /&gt;
=== accumulateAll===&lt;br /&gt;
accumulateAll(mapAllResult) would produce a &amp;lt;code&amp;gt;Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;.  Below we visualize a Map with 16 buckets that places entries based on the key's hashCode().&lt;br /&gt;
&lt;br /&gt;
[[File:Words AccumulateAllResult.svg]]&lt;br /&gt;
&lt;br /&gt;
=== reduceAll ===&lt;br /&gt;
reduceAll(accumulateAllResult) would produce a &amp;lt;code&amp;gt;Map&amp;lt;String, Integer&amp;gt;&amp;lt;/code&amp;gt;.  Below we visualize a Map with 16 buckets that places entries based on the key's hashCode().&lt;br /&gt;
&lt;br /&gt;
'''Note:''' the keys all fall in the same location as in accumulateAllResult, but are associated with the reduced values.&lt;br /&gt;
&lt;br /&gt;
[[File:Words ReduceAllResult.svg]]&lt;br /&gt;
&lt;br /&gt;
==Client==&lt;br /&gt;
{{Client|WordCountOnlyParallelMapReduceFrameworkClient|mapreduce.framework.parallel.client|main}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|WordCountOnlyParallelMapReduceFrameworkClient|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
TextSection[] input = {&lt;br /&gt;
        new TextSection(&amp;quot;A canner, exceedingly canny,&amp;quot;),&lt;br /&gt;
        new TextSection(&amp;quot;One morning remarked to his granny,&amp;quot;),&lt;br /&gt;
        new TextSection(&amp;quot;\&amp;quot;A canner can can&amp;quot;),&lt;br /&gt;
        new TextSection(&amp;quot;Anything that he can;&amp;quot;),&lt;br /&gt;
        new TextSection(&amp;quot;But a canner can't can a can, can he?\&amp;quot;&amp;quot;),&lt;br /&gt;
};&lt;br /&gt;
MapReduceFramework&amp;lt;TextSection, String, Integer, List&amp;lt;Integer&amp;gt;, Integer&amp;gt; framework = new WordCountOnlyParallelMapReduceFramework(new WordCountMapper(), new SummingIntClassicReducer());&lt;br /&gt;
Map&amp;lt;String, Integer&amp;gt; map = framework.mapReduceAll(input);&lt;br /&gt;
for (Map.Entry&amp;lt;String, Integer&amp;gt; entry : map.entrySet()) {&lt;br /&gt;
    System.out.println(entry);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|CardOnlySequentialMapReduceFrameworkClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;but=1&lt;br /&gt;
a=4&lt;br /&gt;
one=1&lt;br /&gt;
anything=1&lt;br /&gt;
morning=1&lt;br /&gt;
that=1&lt;br /&gt;
can=6&lt;br /&gt;
exceedingly=1&lt;br /&gt;
canner=3&lt;br /&gt;
his=1&lt;br /&gt;
granny=1&lt;br /&gt;
remarked=1&lt;br /&gt;
to=1&lt;br /&gt;
canny=1&lt;br /&gt;
can't=1&lt;br /&gt;
he=2&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
&lt;br /&gt;
==WordCountOnlyParallelMapReduceFramework==&lt;br /&gt;
{{CodeToImplement|WordCountOnlyParallelMapReduceFramework|mapper&amp;lt;br/&amp;gt;reducer&amp;lt;br/&amp;gt;mapAll&amp;lt;br/&amp;gt;accumulateAll&amp;lt;br/&amp;gt;toArrayOfReductions&amp;lt;br/&amp;gt;toReductionMap|mapreduce.framework.parallel.warmup}}&lt;br /&gt;
&lt;br /&gt;
===mapper===&lt;br /&gt;
{{Sequential|public Mapper&amp;lt;Deck, Suit, Integer&amp;gt; mapper()}}&lt;br /&gt;
&lt;br /&gt;
This method exists to remind you to use the mapper. The type is a perfect match for the WordCountMapper: &lt;br /&gt;
&lt;br /&gt;
===reducer===&lt;br /&gt;
{{Sequential|public Reducer&amp;lt;Integer, List&amp;lt;Integer&amp;gt;, Integer&amp;gt; reducer()}}&lt;br /&gt;
&lt;br /&gt;
This method exists to remind you to use the accumulatorCombinerReducer. The type is a perfect match for the SummingIntClassicReducer: &lt;br /&gt;
&lt;br /&gt;
===mapAll===&lt;br /&gt;
{{Parallel|private List&amp;lt;List&amp;lt;KeyValuePair&amp;lt;String, Integer&amp;gt;&amp;gt;&amp;gt; mapAll(TextSection[] input)}}&lt;br /&gt;
&lt;br /&gt;
'''Purpose:''' In parallel, map all the TextSection's using the mapper private instance variable&lt;br /&gt;
&lt;br /&gt;
{{Requirement|Use the mapper's map method}}&lt;br /&gt;
&lt;br /&gt;
===accumulateAll===&lt;br /&gt;
{{Sequential|private Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt; accumulateAll(List&amp;lt;List&amp;lt;Map.Entry&amp;lt;String, Integer&amp;gt;&amp;gt;&amp;gt; mapAllResults)}}&lt;br /&gt;
&lt;br /&gt;
'''Purpose:''' Sequentially, assemble a Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt; where the String in this example is some word from a TextSection, and the List&amp;lt;Integer&amp;gt;'s is a list of 1's representing every time that word was found in a TextSection. &lt;br /&gt;
&lt;br /&gt;
To do this: iterate through all of the entries, and use the accumulatorCombinerReducer's methods to create a new mutable container, and accumulate new values into that container. You may find Map.computeIfAbsent() particularly helpful for this. &lt;br /&gt;
&lt;br /&gt;
{{Requirement|Use the accumulatorCombinerReducer's createMutableContainer and accumulate methods}}&lt;br /&gt;
&lt;br /&gt;
===reduceAll===&lt;br /&gt;
'''Provided'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
private Map&amp;lt;String, Integer&amp;gt; reduceAll(Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt; accumulateAllResult) throws InterruptedException, ExecutionException {&lt;br /&gt;
	Set&amp;lt;Map.Entry&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt;&amp;gt; accumulateAllEntries = accumulateAllResult.entrySet();&lt;br /&gt;
	List&amp;lt;Map.Entry&amp;lt;String, Integer&amp;gt;&amp;gt; reducedEntries = reduceAllEntries(accumulateAllEntries);&lt;br /&gt;
	return new SingleChainMap&amp;lt;&amp;gt;(reducedEntries);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===toReductions===&lt;br /&gt;
{{Parallel|private List&amp;lt;Integer&amp;gt; toReductions(Entry&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt;[] entries)}}&lt;br /&gt;
&lt;br /&gt;
'''Purpose:''' Use the reduce() method to return a list of Integers representing the reduced values of each Entry's value, in this case, a List&amp;lt;Integer&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Say we have an Entry&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt; = (&amp;quot;the&amp;quot;, [1, 1, 1, 1, 1]). We would want to reduce the 1's to a 5, and put that in our return list. &lt;br /&gt;
&lt;br /&gt;
{{Requirement|Use the accumulatorCombinerReducer's reduce method}}&lt;br /&gt;
&lt;br /&gt;
===toReductionMap===&lt;br /&gt;
{{Sequential|Map&amp;lt;String, Integer&amp;gt; toReductionMap(Entry&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt;[] entries, List&amp;lt;Integer&amp;gt; reductions)}}&lt;br /&gt;
&lt;br /&gt;
Tip: The index of the Entry in the &amp;quot;entries[]&amp;quot; corresponds directly to the index of the Integer reduction of that entry from &amp;quot;reductions&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__WordCountOnlyParallelMapReduceFrameworkTestSuite|mapreduce.framework.parallel.warmup}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Word_Count_Only_Parallel_Map_Reduce_Framework_Assignment&amp;diff=5409</id>
		<title>Word Count Only Parallel Map Reduce Framework Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Word_Count_Only_Parallel_Map_Reduce_Framework_Assignment&amp;diff=5409"/>
		<updated>2026-02-26T04:48:07Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* reduceAll */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Building On Previous Exercises=&lt;br /&gt;
Be sure to complete the [[Int_Sum_MapReduce_Apps_Studio#Word_Count_Mapper|Word Count Mapper]] and [[Int_Sum_MapReduce_Apps_Studio#IntSumListAccumulatingReducer|IntSumListAccumulatingReducer]] exercises first.&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
==WordCountOnlyParallelMapReduceFramework==&lt;br /&gt;
You will build each of the three methods mapReduceAll(input) invokes.  Much can be gleaned from the parameters and return values of these methods.&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|mapReduceAll(TextSection[] input)|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
@Override&lt;br /&gt;
public Map&amp;lt;String, Integer&amp;gt; mapReduceAll(TextSection[] input) throws InterruptedException, ExecutionException {&lt;br /&gt;
	List&amp;lt;List&amp;lt;Map.Entry&amp;lt;String, Integer&amp;gt;&amp;gt;&amp;gt; mapAllResult = mapAll(input);&lt;br /&gt;
	Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt; accumulateAllResult = accumulateAll(mapAllResult);&lt;br /&gt;
	return reduceAll(accumulateAllResult);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
===mapAll===&lt;br /&gt;
The TextSection[] input:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| input[0] = || new TextSection(&amp;quot;A canner, exceedingly canny,&amp;quot;) &lt;br /&gt;
|-&lt;br /&gt;
| input[1] = || new TextSection(&amp;quot;One morning remarked to his granny,&amp;quot;) &lt;br /&gt;
|-&lt;br /&gt;
| input[2] = || new TextSection(&amp;quot;\&amp;quot;A canner can can&amp;quot;) &lt;br /&gt;
|-&lt;br /&gt;
| input[3] = || new TextSection(&amp;quot;Anything that he can;&amp;quot;) &lt;br /&gt;
|-&lt;br /&gt;
| input[4] = || new TextSection(&amp;quot;But a canner can't can a can, can he?\&amp;quot;&amp;quot;) &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
mapAll(input) would produce:&lt;br /&gt;
&lt;br /&gt;
[[File:Words_MapAllResult.svg]]&lt;br /&gt;
&lt;br /&gt;
=== accumulateAll===&lt;br /&gt;
accumulateAll(mapAllResult) would produce a &amp;lt;code&amp;gt;Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt;&amp;lt;/code&amp;gt;.  Below we visualize a Map with 16 buckets that places entries based on the key's hashCode().&lt;br /&gt;
&lt;br /&gt;
[[File:Words AccumulateAllResult.svg]]&lt;br /&gt;
&lt;br /&gt;
=== reduceAll ===&lt;br /&gt;
reduceAll(accumulateAllResult) would produce a &amp;lt;code&amp;gt;Map&amp;lt;String, Integer&amp;gt;&amp;lt;/code&amp;gt;.  Below we visualize a Map with 16 buckets that places entries based on the key's hashCode().&lt;br /&gt;
&lt;br /&gt;
'''Note:''' the keys all fall in the same location as in accumulateAllResult, but are associated with the reduced values.&lt;br /&gt;
&lt;br /&gt;
[[File:Words ReduceAllResult.svg]]&lt;br /&gt;
&lt;br /&gt;
==Client==&lt;br /&gt;
{{Client|WordCountOnlyParallelMapReduceFrameworkClient|mapreduce.framework.parallel.client|main}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleCode|WordCountOnlyParallelMapReduceFrameworkClient|&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
TextSection[] input = {&lt;br /&gt;
        new TextSection(&amp;quot;A canner, exceedingly canny,&amp;quot;),&lt;br /&gt;
        new TextSection(&amp;quot;One morning remarked to his granny,&amp;quot;),&lt;br /&gt;
        new TextSection(&amp;quot;\&amp;quot;A canner can can&amp;quot;),&lt;br /&gt;
        new TextSection(&amp;quot;Anything that he can;&amp;quot;),&lt;br /&gt;
        new TextSection(&amp;quot;But a canner can't can a can, can he?\&amp;quot;&amp;quot;),&lt;br /&gt;
};&lt;br /&gt;
MapReduceFramework&amp;lt;TextSection, String, Integer, List&amp;lt;Integer&amp;gt;, Integer&amp;gt; framework = new WordCountOnlyParallelMapReduceFramework(new WordCountMapper(), new SummingIntClassicReducer());&lt;br /&gt;
Map&amp;lt;String, Integer&amp;gt; map = framework.mapReduceAll(input);&lt;br /&gt;
for (Map.Entry&amp;lt;String, Integer&amp;gt; entry : map.entrySet()) {&lt;br /&gt;
    System.out.println(entry);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
{{CollapsibleConsole|CardOnlySequentialMapReduceFrameworkClient Output|&amp;lt;pre style=&amp;quot;border: 0px; background: #000; color:#fff;&amp;quot;&amp;gt;but=1&lt;br /&gt;
a=4&lt;br /&gt;
one=1&lt;br /&gt;
anything=1&lt;br /&gt;
morning=1&lt;br /&gt;
that=1&lt;br /&gt;
can=6&lt;br /&gt;
exceedingly=1&lt;br /&gt;
canner=3&lt;br /&gt;
his=1&lt;br /&gt;
granny=1&lt;br /&gt;
remarked=1&lt;br /&gt;
to=1&lt;br /&gt;
canny=1&lt;br /&gt;
can't=1&lt;br /&gt;
he=2&amp;lt;/pre&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
&lt;br /&gt;
==WordCountOnlyParallelMapReduceFramework==&lt;br /&gt;
{{CodeToImplement|WordCountOnlyParallelMapReduceFramework|mapper&amp;lt;br/&amp;gt;reducer&amp;lt;br/&amp;gt;mapAll&amp;lt;br/&amp;gt;accumulateAll&amp;lt;br/&amp;gt;toArrayOfReductions&amp;lt;br/&amp;gt;toReductionMap|mapreduce.framework.parallel.warmup}}&lt;br /&gt;
&lt;br /&gt;
===mapper===&lt;br /&gt;
{{Sequential|public Mapper&amp;lt;Deck, Suit, Integer&amp;gt; mapper()}}&lt;br /&gt;
&lt;br /&gt;
This method exists to remind you to use the mapper. The type is a perfect match for the WordCountMapper: &lt;br /&gt;
&lt;br /&gt;
===reducer===&lt;br /&gt;
{{Sequential|public Reducer&amp;lt;Integer, List&amp;lt;Integer&amp;gt;, Integer&amp;gt; reducer()}}&lt;br /&gt;
&lt;br /&gt;
This method exists to remind you to use the accumulatorCombinerReducer. The type is a perfect match for the SummingIntClassicReducer: &lt;br /&gt;
&lt;br /&gt;
===mapAll===&lt;br /&gt;
{{Parallel|private List&amp;lt;List&amp;lt;KeyValuePair&amp;lt;String, Integer&amp;gt;&amp;gt;&amp;gt; mapAll(TextSection[] input)}}&lt;br /&gt;
&lt;br /&gt;
'''Purpose:''' In parallel, map all the TextSection's using the mapper private instance variable&lt;br /&gt;
&lt;br /&gt;
{{Requirement|Use the mapper's map method}}&lt;br /&gt;
&lt;br /&gt;
===accumulateAll===&lt;br /&gt;
{{Sequential|private Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt; accumulateAll(List&amp;lt;List&amp;lt;Map.Entry&amp;lt;String, Integer&amp;gt;&amp;gt;&amp;gt; mapAllResults)}}&lt;br /&gt;
&lt;br /&gt;
'''Purpose:''' Sequentially, assemble a Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt; where the String in this example is some word from a TextSection, and the List&amp;lt;Integer&amp;gt;'s is a list of 1's representing every time that word was found in a TextSection. &lt;br /&gt;
&lt;br /&gt;
To do this: iterate through all of the entries, and use the accumulatorCombinerReducer's methods to create a new mutable container, and accumulate new values into that container. You may find Map.computeIfAbsent() particularly helpful for this. &lt;br /&gt;
&lt;br /&gt;
{{Requirement|Use the accumulatorCombinerReducer's createMutableContainer and accumulate methods}}&lt;br /&gt;
&lt;br /&gt;
===reduceAll===&lt;br /&gt;
'''Provided'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
private Map&amp;lt;String, Integer&amp;gt; reduceAll(Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt; accumulateAllResult) throws InterruptedException, ExecutionException {&lt;br /&gt;
	Set&amp;lt;Map.Entry&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt;&amp;gt; accumulateAllEntries = accumulateAllResult.entrySet();&lt;br /&gt;
	List&amp;lt;Map.Entry&amp;lt;String, Integer&amp;gt;&amp;gt; reducedEntries = reduceAllEntries(accumulateAllEntries);&lt;br /&gt;
	return new SingleChainMap&amp;lt;&amp;gt;(reducedEntries);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===toArrayOfEntries===&lt;br /&gt;
'''Provided'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
private static Entry&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt;[] toArrayOfEntries(Map&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt; accumulateAllResult) {&lt;br /&gt;
	return accumulateAllResult.entrySet().toArray(Entry[]::new);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===toReductions===&lt;br /&gt;
{{Parallel|private List&amp;lt;Integer&amp;gt; toReductions(Entry&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt;[] entries)}}&lt;br /&gt;
&lt;br /&gt;
'''Purpose:''' Use the reduce() method to return a list of Integers representing the reduced values of each Entry's value, in this case, a List&amp;lt;Integer&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Say we have an Entry&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt; = (&amp;quot;the&amp;quot;, [1, 1, 1, 1, 1]). We would want to reduce the 1's to a 5, and put that in our return list. &lt;br /&gt;
&lt;br /&gt;
{{Requirement|Use the accumulatorCombinerReducer's reduce method}}&lt;br /&gt;
&lt;br /&gt;
===toReductionMap===&lt;br /&gt;
{{Sequential|Map&amp;lt;String, Integer&amp;gt; toReductionMap(Entry&amp;lt;String, List&amp;lt;Integer&amp;gt;&amp;gt;[] entries, List&amp;lt;Integer&amp;gt; reductions)}}&lt;br /&gt;
&lt;br /&gt;
Tip: The index of the Entry in the &amp;quot;entries[]&amp;quot; corresponds directly to the index of the Integer reduction of that entry from &amp;quot;reductions&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|__WordCountOnlyParallelMapReduceFrameworkTestSuite|mapreduce.framework.parallel.warmup}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Floodfill_Application&amp;diff=5408</id>
		<title>Floodfill Application</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Floodfill_Application&amp;diff=5408"/>
		<updated>2026-02-06T20:32:06Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* Code To Investigate and Implement */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Floodfill is an problem that is well-solved recursively.  It allows us an opportunity to think about the similarities and differences between creating a task for each sub-problem and joining all of your tasks with a single X10 style finish.&lt;br /&gt;
&lt;br /&gt;
Further, we will harken back to this exercise later in the semester when we discuss determinism.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
In this studio, you will be using a parallel and recursive method to create an app that fills in a space with color, much like the paint bucket in [https://www.gimp.org/ GNU Image Manipulation Program].&lt;br /&gt;
==Flood Fill==&lt;br /&gt;
&amp;lt;youtube&amp;gt;hoVbNF8CpBk&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:FloodFillTool.png]]&lt;br /&gt;
[[File:FloodFillShape.png]]&lt;br /&gt;
&lt;br /&gt;
https://en.wikipedia.org/wiki/Flood_fill&lt;br /&gt;
&lt;br /&gt;
==X10==&lt;br /&gt;
&lt;br /&gt;
One [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/x10/X10.html#finish(fj.api.TaskSupplier) finish] to join them all.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;dJJiMLPHGgg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Mistakes to Avoid=&lt;br /&gt;
{{Warning | Do NOT use &amp;lt;nowiki&amp;gt;==&amp;lt;/nowiki&amp;gt;  to compare colors.  Use [https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#equals-java.lang.Object-java.lang.Object- Objects.equals(a,b)]. }}&lt;br /&gt;
{{Warning | Do NOT finish more than you need to}}&lt;br /&gt;
&lt;br /&gt;
==Sequential Implementation Increased Stack Size Requirement==&lt;br /&gt;
For this project, if you decide to first build a sequential version, you will need to change your VM Arguments slightly. In addition to the normal argument that needs to be pasted in for every assignment, you need to add &amp;lt;code&amp;gt;-Xss4m&amp;lt;/code&amp;gt;.  This will increase the size of the call stack, essentially increasing the maximum depth of your recursion.&lt;br /&gt;
&lt;br /&gt;
=The Core Questions=&lt;br /&gt;
*What are the tasks?&lt;br /&gt;
*What is the data?&lt;br /&gt;
*Is the data mutable?&lt;br /&gt;
*If so, how is it shared?&lt;br /&gt;
&lt;br /&gt;
=Code To Use=&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/hsv/MutableHsvImage.html MutableHsvImage]&lt;br /&gt;
:[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/hsv/MutableHsvImage.html#isInBounds(int,int) isInBounds(x,y)]&lt;br /&gt;
:[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/image/MutableHsvImage.html#colorAtPixel(int,int) colorAtPixel(x,y)]&lt;br /&gt;
:[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/image/MutableHsvImage.html#setColorAtPixel(int,int,edu.wustl.cse231s.color.HsvColor) setColorAtPixel(x,y,color)]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html Objects]&lt;br /&gt;
:[https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#equals-java.lang.Object-java.lang.Object- Objects.equals(a,b)]&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/hsv/HsvColor.html HsvColor]&lt;br /&gt;
:[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/hsv/HsvColor.html#equals(java.lang.Object) equals(other)]&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate and Implement=&lt;br /&gt;
The class is composed of two methods: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/floodfill/exercise/FloodFiller.html#floodFill(x10.X10,edu.wustl.cse231s.pixel.MutablePixels,java.awt.Color,int,int) floodFill] and floodFillKernel.  The entire process in the kernel is triggered from the floodFill method.   This is so that the kernel can run recursively and in parallel and the floodFill method can wrap the kernel in an all-inclusive finish.&lt;br /&gt;
&lt;br /&gt;
The provided floodFill method need only be investigate and should remain unchanged.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public static void floodFill(X10 x10, MutableHsvImage pixels, HsvColor fillColor, int x, int y)&lt;br /&gt;
		throws InterruptedException, ExecutionException {&lt;br /&gt;
	HsvColor targetColor = pixels.colorAtPixel(x, y);&lt;br /&gt;
	if (targetColor != null) {&lt;br /&gt;
		if (Objects.equals(targetColor, fillColor)) {&lt;br /&gt;
			// pass&lt;br /&gt;
		} else {&lt;br /&gt;
			x10.void_finish(() -&amp;gt; {&lt;br /&gt;
				floodFillKernel(pixels, targetColor, fillColor, x, y);&lt;br /&gt;
			});&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|FloodFiller|floodFillKernel|floodfill.exercise}}&lt;br /&gt;
&lt;br /&gt;
Note: If the recursion ventures outside of the bounds of the mutable pixels, [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/hsv/MutableHsvImage.html#colorAtPixel(int,int) colorAtPixel(x,y)] will return null. &lt;br /&gt;
&lt;br /&gt;
If a valid (present) color is returned, you should check if the color at the current pixel location pixel matches the targetColor (the color we wish to change). If it is, it should then replace the color with the fillColor and asynchronously call the method again to repeat the process for the surrounding pixels. The purpose of the assignment is to understand recursion and how to make it work in parallel.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
private static void floodFillKernel(MutableHsvImage mutableImage, HsvColor targetColor, HsvColor fillColor, int x, int y) {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Parallel|floodFillKernel}}&lt;br /&gt;
&lt;br /&gt;
'''Note:''' X10's async method is not provided as you can simply use void_fork.&lt;br /&gt;
&lt;br /&gt;
'''Note: ''' Recall from lecture that the finish()/join() has been done in the already implemented &amp;lt;code&amp;gt; floodFill()&amp;lt;/code&amp;gt;. This means you are free to void_fork() &amp;quot;[https://idioms.thefreedictionary.com/like+a+drunken+sailor like a drunken sailor]&amp;quot;, as it will all be finished by the function which called &amp;lt;code&amp;gt; floodFillKernel()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Visualization==&lt;br /&gt;
[[File:FloodFillViz.png]]&lt;br /&gt;
&lt;br /&gt;
{{Viz|FloodFillApp|floodfill.viz|main}}&lt;br /&gt;
&lt;br /&gt;
==Correctness==&lt;br /&gt;
A test suite exists but is limited and not all that exciting.&lt;br /&gt;
{{TestSuite|_FloodFillTestSuite|floodfill.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Pledge, Acknowledgments, Citations=&lt;br /&gt;
{{Pledge|exercise-floodfill}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Floodfill_Application&amp;diff=5407</id>
		<title>Floodfill Application</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Floodfill_Application&amp;diff=5407"/>
		<updated>2026-02-06T18:44:05Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* Code To Investigate and Implement */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Floodfill is an problem that is well-solved recursively.  It allows us an opportunity to think about the similarities and differences between creating a task for each sub-problem and joining all of your tasks with a single X10 style finish.&lt;br /&gt;
&lt;br /&gt;
Further, we will harken back to this exercise later in the semester when we discuss determinism.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
In this studio, you will be using a parallel and recursive method to create an app that fills in a space with color, much like the paint bucket in [https://www.gimp.org/ GNU Image Manipulation Program].&lt;br /&gt;
==Flood Fill==&lt;br /&gt;
&amp;lt;youtube&amp;gt;hoVbNF8CpBk&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:FloodFillTool.png]]&lt;br /&gt;
[[File:FloodFillShape.png]]&lt;br /&gt;
&lt;br /&gt;
https://en.wikipedia.org/wiki/Flood_fill&lt;br /&gt;
&lt;br /&gt;
==X10==&lt;br /&gt;
&lt;br /&gt;
One [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/x10/X10.html#finish(fj.api.TaskSupplier) finish] to join them all.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;dJJiMLPHGgg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Mistakes to Avoid=&lt;br /&gt;
{{Warning | Do NOT use &amp;lt;nowiki&amp;gt;==&amp;lt;/nowiki&amp;gt;  to compare colors.  Use [https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#equals-java.lang.Object-java.lang.Object- Objects.equals(a,b)]. }}&lt;br /&gt;
{{Warning | Do NOT finish more than you need to}}&lt;br /&gt;
&lt;br /&gt;
==Sequential Implementation Increased Stack Size Requirement==&lt;br /&gt;
For this project, if you decide to first build a sequential version, you will need to change your VM Arguments slightly. In addition to the normal argument that needs to be pasted in for every assignment, you need to add &amp;lt;code&amp;gt;-Xss4m&amp;lt;/code&amp;gt;.  This will increase the size of the call stack, essentially increasing the maximum depth of your recursion.&lt;br /&gt;
&lt;br /&gt;
=The Core Questions=&lt;br /&gt;
*What are the tasks?&lt;br /&gt;
*What is the data?&lt;br /&gt;
*Is the data mutable?&lt;br /&gt;
*If so, how is it shared?&lt;br /&gt;
&lt;br /&gt;
=Code To Use=&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/hsv/MutableHsvImage.html MutableHsvImage]&lt;br /&gt;
:[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/hsv/MutableHsvImage.html#isInBounds(int,int) isInBounds(x,y)]&lt;br /&gt;
:[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/image/MutableHsvImage.html#colorAtPixel(int,int) colorAtPixel(x,y)]&lt;br /&gt;
:[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/image/MutableHsvImage.html#setColorAtPixel(int,int,edu.wustl.cse231s.color.HsvColor) setColorAtPixel(x,y,color)]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html Objects]&lt;br /&gt;
:[https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#equals-java.lang.Object-java.lang.Object- Objects.equals(a,b)]&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/hsv/HsvColor.html HsvColor]&lt;br /&gt;
:[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/hsv/HsvColor.html#equals(java.lang.Object) equals(other)]&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate and Implement=&lt;br /&gt;
The class is composed of two methods: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/floodfill/exercise/FloodFiller.html#floodFill(x10.X10,edu.wustl.cse231s.pixel.MutablePixels,java.awt.Color,int,int) floodFill] and floodFillKernel.  The entire process in the kernel is triggered from the floodFill method.   This is so that the kernel can run recursively and in parallel and the floodFill method can wrap the kernel in an all-inclusive finish.&lt;br /&gt;
&lt;br /&gt;
The provided floodFill method need only be investigate and should remain unchanged.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public static void floodFill(X10 x10, MutableHsvImage pixels, HsvColor fillColor, int x, int y)&lt;br /&gt;
		throws InterruptedException, ExecutionException {&lt;br /&gt;
	HsvColor targetColor = pixels.colorAtPixel(x, y);&lt;br /&gt;
	if (targetColor != null) {&lt;br /&gt;
		if (Objects.equals(targetColor, fillColor)) {&lt;br /&gt;
			// pass&lt;br /&gt;
		} else {&lt;br /&gt;
			x10.void_finish(() -&amp;gt; {&lt;br /&gt;
				floodFillKernel(pixels, targetColor, fillColor, x, y);&lt;br /&gt;
			});&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|FloodFiller|floodFillKernel|floodfill.exercise}}&lt;br /&gt;
&lt;br /&gt;
Note: If the recursion ventures outside of the bounds of the mutable pixels, [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/hsv/MutableHsvImage.html#colorAtPixel(int,int) colorAtPixel(x,y)] will return null. &lt;br /&gt;
&lt;br /&gt;
If a valid (present) color is returned, you should check if the color at the current pixel location pixel matches the targetColor (the color we wish to change). If it is, it should then replace the color with the fillColor and asynchronously call the method again to repeat the process for the surrounding pixels. The purpose of the assignment is to understand recursion and how to make it work in parallel.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
private static void floodFillKernel(MutableHsvImage mutableImage, HsvColor targetColor, HsvColor fillColor, int x, int y) {&lt;br /&gt;
	throw new NotYetImplementedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Parallel|floodFillKernel}}&lt;br /&gt;
&lt;br /&gt;
'''Note: X10's async method is not provided as you can simply use void_fork.'''&lt;br /&gt;
&lt;br /&gt;
'''Note: ''' Recall from lecture that the finish()/join() has been done in the already implemented &amp;lt;code&amp;gt; floodFill()&amp;lt;/code&amp;gt;. This means you are free to void_fork() &amp;quot;[https://idioms.thefreedictionary.com/like+a+drunken+sailor like a drunken sailor]&amp;quot;, as it will all be finished by the function which called &amp;lt;code&amp;gt; floodFillKernel()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Visualization==&lt;br /&gt;
[[File:FloodFillViz.png]]&lt;br /&gt;
&lt;br /&gt;
{{Viz|FloodFillApp|floodfill.viz|main}}&lt;br /&gt;
&lt;br /&gt;
==Correctness==&lt;br /&gt;
A test suite exists but is limited and not all that exciting.&lt;br /&gt;
{{TestSuite|_FloodFillTestSuite|floodfill.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Pledge, Acknowledgments, Citations=&lt;br /&gt;
{{Pledge|exercise-floodfill}}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Spring_2023&amp;diff=5406</id>
		<title>Spring 2023</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Spring_2023&amp;diff=5406"/>
		<updated>2026-02-03T15:06:20Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* Fall 2025 Extensions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Welcome to the wiki for CSE 231S Introduction to Parallel and Concurrent Programming at Washington University in St. Louis.&lt;br /&gt;
&lt;br /&gt;
=Learning Outcomes=&lt;br /&gt;
# Learn how to write programs to make use of multiple processors.&lt;br /&gt;
# Learn to how to fix, and preferably, avoid race conditions.&lt;br /&gt;
# Learn how to implement MapReduce frameworks (the technology developed for the original Google search) to deal with big data.&lt;br /&gt;
# Learn how to use locks to share resources safely.&lt;br /&gt;
# Learn how to use read/write lock for improved parallelism under the right conditions.&lt;br /&gt;
# Learn how to avoid deadlock.&lt;br /&gt;
# Learn when and how to use atomics to build thread-safe data structures with improved performance over using locks.&lt;br /&gt;
# Learn when and how to use phasers for more sophisticated concurrency control.&lt;br /&gt;
# Although, the concepts learned should be widely applicable, the assignments will be implemented in Java and, as such, students will develop advanced Java skill.&lt;br /&gt;
&lt;br /&gt;
=Calendar=&lt;br /&gt;
Click [http://www.cse.wustl.edu/~cosgroved/courses/cse231/current/calendar.html here] to visit our calendar.&lt;br /&gt;
&lt;br /&gt;
=Full Contact Time=&lt;br /&gt;
Prof. Cosgrove has no planned trips which would cause class to be cancelled.  If some unforeseen circumstances arise, such as sickness, class will be moved online and recorded.&lt;br /&gt;
&lt;br /&gt;
=Office hours=&lt;br /&gt;
{{:Office hours}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=Important Dates for Spring '23=&lt;br /&gt;
*&amp;lt;b&amp;gt;Block 1 Assignments Due&amp;lt;/b&amp;gt;: ??&lt;br /&gt;
*&amp;lt;b&amp;gt;Block 2 Assignments Due&amp;lt;/b&amp;gt;: ??&lt;br /&gt;
*&amp;lt;b&amp;gt;Block 3 Assignments Due&amp;lt;/b&amp;gt;: ??&lt;br /&gt;
*&amp;lt;b&amp;gt;Spring Break (No OH available)&amp;lt;/b&amp;gt;: 03/12 to 03/18&lt;br /&gt;
*&amp;lt;b&amp;gt;Last day to submit assignments&amp;lt;/b&amp;gt;: 5/10&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Class Policies=&lt;br /&gt;
{{:Class Policies}}&lt;br /&gt;
&lt;br /&gt;
=Spring 2026 Grading breakdown=&lt;br /&gt;
*Prep S&amp;amp;Q Forms (10%)&lt;br /&gt;
*In-class Worksheets Attendance/Participation (10%)&lt;br /&gt;
*Labs/Exercises (80%)&lt;br /&gt;
&lt;br /&gt;
=Spring 2026 Extensions=&lt;br /&gt;
For the Spring 2026 semester: All Exercises will have a one week, no penalty extension.&lt;br /&gt;
&lt;br /&gt;
To be clear, the due dates are asserted to be reasonable.  The bonus week is intended to account for extreme circumstances.&lt;br /&gt;
&lt;br /&gt;
=Grade Ranges=&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
A+ 	[0.967 - 1.0]&lt;br /&gt;
A	[0.933 - 0.967)&lt;br /&gt;
A-	[0.900 - 0.933)&lt;br /&gt;
B+ 	[0.867 - 0.900)&lt;br /&gt;
B	[0.833 - 0.867)&lt;br /&gt;
B-	[0.800 - 0.833)&lt;br /&gt;
C+ 	[0.767 - 0.700)&lt;br /&gt;
C	[0.733 - 0.767)&lt;br /&gt;
C-	[0.700 - 0.733)&lt;br /&gt;
D+ 	[0.667 - 0.600)&lt;br /&gt;
D	[0.633 - 0.667)&lt;br /&gt;
D-	[0.600 - 0.633)&lt;br /&gt;
F       [0 - 0.600)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=FAQ=&lt;br /&gt;
Every week, we gather the most commonly asked questions from your Q&amp;amp;S forms and add them to our [[FAQ]] section. Please use it as a resource!&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=Course Setup=&lt;br /&gt;
*[[Initial_Setup]]&lt;br /&gt;
*[[Rundown on Everything Java]]&lt;br /&gt;
**[[Generics]]&lt;br /&gt;
**[[Interfaces]]&lt;br /&gt;
**[[Lambdas]]&lt;br /&gt;
*[[Eclipse Tips]]&lt;br /&gt;
--&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Office_hours&amp;diff=5405</id>
		<title>Office hours</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Office_hours&amp;diff=5405"/>
		<updated>2026-02-03T03:35:24Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{| class=&amp;quot;wikitable mw-collapsible&amp;quot;&lt;br /&gt;
|+nowrap|Schedule&lt;br /&gt;
|-&lt;br /&gt;
! !! Sunday !! Monday !! Tuesday !! Wednesday !! Thursday !! Friday !! Saturday&lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 9:00 -  9:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 9:30 - 10:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;10:00 - 10:30&amp;lt;/code&amp;gt;||  || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourInPerson|Prof. Cosgrove|January 110}} ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;10:30 - 11:00&amp;lt;/code&amp;gt;||  || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourInPerson|Prof. Cosgrove|January 110}} ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;11:00 - 11:30&amp;lt;/code&amp;gt;|| {{OfficeHour|Noah Benschine|98583917095}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourInPerson|Prof. Cosgrove|January 110}} ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;11:30 - 12:00&amp;lt;/code&amp;gt;|| {{OfficeHour|Noah Benschine|98583917095}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourInPerson|Prof. Cosgrove|January 110}} ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;12:00 - 12:30&amp;lt;/code&amp;gt;|| {{OfficeHour|Noah Benschine|98583917095}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourInPerson|Andy Hoette|January 110}} || {{OfficeHourWithPassword|Mustafa Tahir|99260316313|IcXe5XaaJlcAt2lwCUBAJhQdy1chuE.1}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;12:30 -  1:00&amp;lt;/code&amp;gt;|| {{OfficeHour|Noah Benschine|98583917095}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourInPerson|Andy Hoette|January 110}} || {{OfficeHourWithPassword|Mustafa Tahir|99260316313|IcXe5XaaJlcAt2lwCUBAJhQdy1chuE.1}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 1:00 -  1:30&amp;lt;/code&amp;gt;|| {{OfficeHourInPerson|Eileen Li|TBD}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourInPerson|Andy Hoette|January 110}} || {{OfficeHourWithPassword|Mustafa Tahir|99260316313|IcXe5XaaJlcAt2lwCUBAJhQdy1chuE.1}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 1:30 -  2:00&amp;lt;/code&amp;gt;|| {{OfficeHourInPerson|Eileen Li|TBD}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourWithPassword|Cassie Phan|97216507583|qhjbCOdAoiCE2WIuJblWolaaaX5mr0.1}} || {{ClassTime|McDonnell 361}} || {{OfficeHourInPerson|Andy Hoette|January 110}} || {{OfficeHourWithPassword|Mustafa Tahir|99260316313|IcXe5XaaJlcAt2lwCUBAJhQdy1chuE.1}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 2:00 -  2:30&amp;lt;/code&amp;gt;|| {{OfficeHourInPerson|Eileen Li|TBD}} ||  || {{ClassTime|McDonnell 361}} ||  || {{ClassTime|McDonnell 361}} || {{OfficeHourWithPassword|Jack Yang|97337220321|nssUafaMGgnNDwGt4eaI0M6BPDWvS7.1}} || {{OfficeHour|Joesph Molina|97238368362}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 2:30 -  3:00&amp;lt;/code&amp;gt;|| {{OfficeHourInPerson|Eileen Li|TBD}} || {{OfficeHourInPerson|Nancy Patel|Lopata 202}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHourInPerson|Eileen Li|TBD}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHourWithPassword|Jack Yang|97337220321|nssUafaMGgnNDwGt4eaI0M6BPDWvS7.1}} || {{OfficeHour|Joesph Molina|97238368362}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 3:00 -  3:30&amp;lt;/code&amp;gt;|| {{OfficeHour|Joesph Molina|97238368362}} || {{OfficeHourInPerson|Nancy Patel|Lopata 202}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHourInPerson|Eileen Li|TBD}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHourWithPassword|Jack Yang|97337220321|nssUafaMGgnNDwGt4eaI0M6BPDWvS7.1}} || {{OfficeHour|Joesph Molina|97238368362}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 3:30 -  4:00&amp;lt;/code&amp;gt;|| {{OfficeHour|Joesph Molina|97238368362}} || {{OfficeHourInPerson|Nancy Patel|Lopata 202}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHourInPerson|Eileen Li|TBD}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHourWithPassword|Jack Yang|97337220321|nssUafaMGgnNDwGt4eaI0M6BPDWvS7.1}} || {{OfficeHour|Joesph Molina|97238368362}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 4:00 -  4:30&amp;lt;/code&amp;gt;|| {{OfficeHour|Joesph Molina|97238368362}} || {{OfficeHourInPerson|Nancy Patel|Lopata 202}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHourWithPassword|Mustafa Tahir|94469360628|zC5LBXFZcHjumb2quRZOrsWkYQlHJq.1}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHour|Megan Ouyang|5492004415}} || {{OfficeHour|Joesph Molina|97238368362}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 4:30 -  5:00&amp;lt;/code&amp;gt;||  || {{OfficeHourInPerson|Nancy Patel|Lopata 202}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHourWithPassword|Mustafa Tahir|94469360628|zC5LBXFZcHjumb2quRZOrsWkYQlHJq.1}} || {{OfficeHour|Dzenan Zecevic|94635222141}} || {{OfficeHour|Megan Ouyang|5492004415}} || {{OfficeHour|Joesph Molina|97238368362}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 5:00 -  5:30&amp;lt;/code&amp;gt;||  || {{OfficeHourInPerson|Nancy Patel|Lopata 202}} || {{OfficeHour|Noah Benschine|93536182747}} || {{OfficeHourWithPassword|Mustafa Tahir|94469360628|zC5LBXFZcHjumb2quRZOrsWkYQlHJq.1}} || {{OfficeHourWithPassword|Jack Yang|97343164185|i8ACoWue5q5u3OKAJUfA5yHAWaAptr.1}} || {{OfficeHour|Megan Ouyang|5492004415}} || {{OfficeHour|Joesph Molina|97238368362}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 5:30 -  6:00&amp;lt;/code&amp;gt;||  ||  || {{OfficeHour|Noah Benschine|93536182747}} || {{OfficeHourWithPassword|Mustafa Tahir|94469360628|zC5LBXFZcHjumb2quRZOrsWkYQlHJq.1}} || {{OfficeHourWithPassword|Jack Yang|97343164185|i8ACoWue5q5u3OKAJUfA5yHAWaAptr.1}} || {{OfficeHour|Megan Ouyang|5492004415}} ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 6:00 -  6:30&amp;lt;/code&amp;gt;||  || {{OfficeHourInPerson|Caleb Huerta|TBD}} || {{OfficeHour|Noah Benschine|93536182747}} || {{OfficeHourWithPassword|Jack Yang|94995588580|xd3WFeBht3pZbofcuCzhEZsmarc5Rt.1}} || {{OfficeHourWithPassword|Jack Yang|97343164185|i8ACoWue5q5u3OKAJUfA5yHAWaAptr.1}} || {{OfficeHour|Megan Ouyang|5492004415}} || {{OfficeHour|Megan Ouyang|5492004415}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 6:30 -  7:00&amp;lt;/code&amp;gt;||  || {{OfficeHourInPerson|Caleb Huerta|TBD}} || {{OfficeHour|Noah Benschine|93536182747}} || {{OfficeHourWithPassword|Jack Yang|94995588580|xd3WFeBht3pZbofcuCzhEZsmarc5Rt.1}} || {{OfficeHourWithPassword|Jack Yang|97343164185|i8ACoWue5q5u3OKAJUfA5yHAWaAptr.1}} ||  || {{OfficeHour|Megan Ouyang|5492004415}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 7:00 -  7:30&amp;lt;/code&amp;gt;||  || {{OfficeHourInPerson|Caleb Huerta|TBD}} || {{OfficeHour|Noah Benschine|93536182747}} || {{OfficeHourWithPassword|Jack Yang|94995588580|xd3WFeBht3pZbofcuCzhEZsmarc5Rt.1}} ||  ||  || {{OfficeHour|Megan Ouyang|5492004415}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 7:30 -  8:00&amp;lt;/code&amp;gt;||  || {{OfficeHourInPerson|Caleb Huerta|TBD}} || {{OfficeHour|Alston Liu|2378167961}} || {{OfficeHourWithPassword|Jack Yang|94995588580|xd3WFeBht3pZbofcuCzhEZsmarc5Rt.1}} ||  ||  || {{OfficeHour|Megan Ouyang|5492004415}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 8:00 -  8:30&amp;lt;/code&amp;gt;||  || {{OfficeHourInPerson|Caleb Huerta|TBD}} || {{OfficeHour|Alston Liu|2378167961}} ||  ||  ||  || {{OfficeHour|Megan Ouyang|5492004415}} &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 8:30 -  9:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 9:00 -  9:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt; 9:30 - 10:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;10:00 - 10:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;10:30 - 11:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;11:00 - 11:30&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|style=&amp;quot;text-align: right&amp;quot;|&amp;lt;code&amp;gt;11:30 - 12:00&amp;lt;/code&amp;gt;||  ||  ||  ||  ||  ||  ||  &lt;br /&gt;
|-&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Spring_2023&amp;diff=5404</id>
		<title>Spring 2023</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Spring_2023&amp;diff=5404"/>
		<updated>2026-01-30T12:04:14Z</updated>

		<summary type="html">&lt;p&gt;Cosgroved: /* Fall 2025 Grading breakdown */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Welcome to the wiki for CSE 231S Introduction to Parallel and Concurrent Programming at Washington University in St. Louis.&lt;br /&gt;
&lt;br /&gt;
=Learning Outcomes=&lt;br /&gt;
# Learn how to write programs to make use of multiple processors.&lt;br /&gt;
# Learn to how to fix, and preferably, avoid race conditions.&lt;br /&gt;
# Learn how to implement MapReduce frameworks (the technology developed for the original Google search) to deal with big data.&lt;br /&gt;
# Learn how to use locks to share resources safely.&lt;br /&gt;
# Learn how to use read/write lock for improved parallelism under the right conditions.&lt;br /&gt;
# Learn how to avoid deadlock.&lt;br /&gt;
# Learn when and how to use atomics to build thread-safe data structures with improved performance over using locks.&lt;br /&gt;
# Learn when and how to use phasers for more sophisticated concurrency control.&lt;br /&gt;
# Although, the concepts learned should be widely applicable, the assignments will be implemented in Java and, as such, students will develop advanced Java skill.&lt;br /&gt;
&lt;br /&gt;
=Calendar=&lt;br /&gt;
Click [http://www.cse.wustl.edu/~cosgroved/courses/cse231/current/calendar.html here] to visit our calendar.&lt;br /&gt;
&lt;br /&gt;
=Full Contact Time=&lt;br /&gt;
Prof. Cosgrove has no planned trips which would cause class to be cancelled.  If some unforeseen circumstances arise, such as sickness, class will be moved online and recorded.&lt;br /&gt;
&lt;br /&gt;
=Office hours=&lt;br /&gt;
{{:Office hours}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=Important Dates for Spring '23=&lt;br /&gt;
*&amp;lt;b&amp;gt;Block 1 Assignments Due&amp;lt;/b&amp;gt;: ??&lt;br /&gt;
*&amp;lt;b&amp;gt;Block 2 Assignments Due&amp;lt;/b&amp;gt;: ??&lt;br /&gt;
*&amp;lt;b&amp;gt;Block 3 Assignments Due&amp;lt;/b&amp;gt;: ??&lt;br /&gt;
*&amp;lt;b&amp;gt;Spring Break (No OH available)&amp;lt;/b&amp;gt;: 03/12 to 03/18&lt;br /&gt;
*&amp;lt;b&amp;gt;Last day to submit assignments&amp;lt;/b&amp;gt;: 5/10&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Class Policies=&lt;br /&gt;
{{:Class Policies}}&lt;br /&gt;
&lt;br /&gt;
=Spring 2026 Grading breakdown=&lt;br /&gt;
*Prep S&amp;amp;Q Forms (10%)&lt;br /&gt;
*In-class Worksheets Attendance/Participation (10%)&lt;br /&gt;
*Labs/Exercises (80%)&lt;br /&gt;
&lt;br /&gt;
=Fall 2025 Extensions=&lt;br /&gt;
For the Fall 2025 semester: All Exercises will have a one week, no penalty extension.&lt;br /&gt;
&lt;br /&gt;
To be clear, the due dates are asserted to be reasonable.  The bonus week is intended to account for extreme circumstances.&lt;br /&gt;
&lt;br /&gt;
=Grade Ranges=&lt;br /&gt;
 &amp;lt;pre&amp;gt;&lt;br /&gt;
A+ 	[0.967 - 1.0]&lt;br /&gt;
A	[0.933 - 0.967)&lt;br /&gt;
A-	[0.900 - 0.933)&lt;br /&gt;
B+ 	[0.867 - 0.900)&lt;br /&gt;
B	[0.833 - 0.867)&lt;br /&gt;
B-	[0.800 - 0.833)&lt;br /&gt;
C+ 	[0.767 - 0.700)&lt;br /&gt;
C	[0.733 - 0.767)&lt;br /&gt;
C-	[0.700 - 0.733)&lt;br /&gt;
D+ 	[0.667 - 0.600)&lt;br /&gt;
D	[0.633 - 0.667)&lt;br /&gt;
D-	[0.600 - 0.633)&lt;br /&gt;
F       [0 - 0.600)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=FAQ=&lt;br /&gt;
Every week, we gather the most commonly asked questions from your Q&amp;amp;S forms and add them to our [[FAQ]] section. Please use it as a resource!&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=Course Setup=&lt;br /&gt;
*[[Initial_Setup]]&lt;br /&gt;
*[[Rundown on Everything Java]]&lt;br /&gt;
**[[Generics]]&lt;br /&gt;
**[[Interfaces]]&lt;br /&gt;
**[[Lambdas]]&lt;br /&gt;
*[[Eclipse Tips]]&lt;br /&gt;
--&amp;gt;&lt;/div&gt;</summary>
		<author><name>Cosgroved</name></author>
		
	</entry>
</feed>