<?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=AngelaChen</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=AngelaChen"/>
	<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Special:Contributions/AngelaChen"/>
	<updated>2026-11-28T01:44:49Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.34.4</generator>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iced_Cakes_Pipeline&amp;diff=3833</id>
		<title>Iced Cakes Pipeline</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iced_Cakes_Pipeline&amp;diff=3833"/>
		<updated>2022-05-03T02:11:32Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* Required Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Pipelines can increase throughput when processing a stream of data.  We will gain additional experience with Phasers by building a software pipeline.&lt;br /&gt;
&lt;br /&gt;
=Backgroud=&lt;br /&gt;
&amp;lt;youtube&amp;gt;3cqVtc2rDlg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;umTGnCc9cGU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code To Use=&lt;br /&gt;
==Mixer==&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/pipeline/cake/core/Mixer.html class Mixer]&lt;br /&gt;
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/pipeline/cake/core/Mixer.html#mix-int- mix(int cakeIndex)]&lt;br /&gt;
==Oven==&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/pipeline/cake/core/Oven.html class Oven]&lt;br /&gt;
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/pipeline/cake/core/Oven.html#bake-int-pipeline.cake.core.MixedIngredients- bake(int cakeIndex, MixedIngredients mixedIngredients)]&lt;br /&gt;
==Icer==&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/pipeline/cake/core/Icer.html class Icer]&lt;br /&gt;
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/pipeline/cake/core/Icer.html#ice-int-pipeline.cake.core.BakedCake- ice(int cakeIndex, BakedCake bakedCake)]&lt;br /&gt;
&lt;br /&gt;
==Looping==&lt;br /&gt;
[[Reference_Page#Parallel_Loops|Our reference page's parallel loop section]]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html Java For Loop]&lt;br /&gt;
&lt;br /&gt;
==Phasers==&lt;br /&gt;
&amp;lt;youtube&amp;gt;whPAPmylbEU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Reference_Page#Phasers|Our reference page's phasers section]]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html class Phaser] ([http://www.baeldung.com/java-phaser Guide to the Java Phaser])&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#register-- register]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#arrive-- arrive]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html#awaitAdvance-int- awaitAdvance] use via PhaserUtils.awaitAdvanceForPhase(phaser, phase)&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/concurrent/PhaserUtils.html PhaserUtils]&lt;br /&gt;
: [http://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/concurrent/PhaserUtils.html#awaitAdvanceForPhase-java.util.concurrent.Phaser-int- awaitAdvanceForPhase]&lt;br /&gt;
&lt;br /&gt;
If you are on or past the phase you want to await, then calling &amp;lt;code&amp;gt;phaser.awaitAdvance()&amp;lt;/code&amp;gt; directly is fine.  If you might be on a prior phase, then invoke &amp;lt;code&amp;gt;PhaserUtils.awaitAdvanceForPhase(phaser, phase)&amp;lt;/code&amp;gt;.  If in doubt, invoking awaitAdvanceForPhase is safer.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;	public static int awaitAdvanceForPhase(Phaser phaser, int phase) {&lt;br /&gt;
		return awaitAdvanceForPhase(phaser, phase, () -&amp;gt; Thread.yield());&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public static int awaitAdvanceForPhase(Phaser phaser, int phase, Runnable runnable) {&lt;br /&gt;
		while (true) {&lt;br /&gt;
			int currentPhase = phaser.awaitAdvance(phase);&lt;br /&gt;
			if (currentPhase &amp;lt; 0 || currentPhase &amp;gt; phase) {&lt;br /&gt;
				return currentPhase;&lt;br /&gt;
			} else {&lt;br /&gt;
				if (runnable != null) {&lt;br /&gt;
					runnable.run();&lt;br /&gt;
				}&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
	}&amp;lt;/nowiki&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;
Here are some other questions that will help lead you down the right road.&lt;br /&gt;
*What work does each task need to do?&lt;br /&gt;
*What, if anything, does each task depend upon?  That is: what does each task have to wait for before it may proceed?&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==Required Code==&lt;br /&gt;
{{CodeToImplement|CakePipeline|mixBakeAndIceCakes|pipeline.cake.studio}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public static IcedCake[] mixBakeAndIceCakes(Mixer mixer, Oven oven, Icer icer, int cakeCount)}}&lt;br /&gt;
&lt;br /&gt;
Design a simple pipeline using Phasers. In order to have a finished cake, you need to first mix the ingredients, then bake the cake, and finally ice it with frosting. Although you only have one tool for each step (one whisk, one oven, and one icing spatula), there's no problem with icing one cake while another is baking in the oven. That's where the parallelism comes in. Answering the above questions should give you all the information you need to set up this method. Remember that this needs to be able work for any number of cakes passed through! &lt;br /&gt;
&lt;br /&gt;
Note that icer.ice() returns an object of IcedCake. Collect all the 'IcedCake's you make, and return them in an array.&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do not forget to ensure that all of your tasks (mix, bake, and ice) complete before you return. }}&lt;br /&gt;
&lt;br /&gt;
[[File:CakePipeline.svg | 600px]]&lt;br /&gt;
&lt;br /&gt;
==Optional Challenge==&lt;br /&gt;
{{CodeToImplement|MultiOvenCakePipeline|mixBakeAndIceCakes|pipeline.cake.challenge}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public static IcedCake[] mixBakeAndIceCakes(Mixer mixer, List&amp;lt;Oven&amp;gt; ovens, Icer icer, int cakeCount)}}&lt;br /&gt;
&lt;br /&gt;
As we've discussed in class, not all steps of the pipeline always take the same amount of time to complete. In this example, baking a cake in the oven usually takes much longer than simply mixing the ingredients or icing the cake. The Oven is the bottleneck on the performance of this pipeline. One way to further improve the process then is to simply more ovens. This time, build a similar pipeline to what you made earlier, but use all of the Ovens passed to you in order to speed things up. Here's a couple things to take note of:&lt;br /&gt;
*How have the tasks changed? How does this additional oven affect the number of tasks your program will use?&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
*Assume that the only way to get a second oven is to buy an old, used one. It can't get as hot, so it will need more time to bake a cake. Because of this, it is better to set up your solution such that when a set of ingredients has been mixed, it sends them to any open oven, rather than just alternating between the two ovens.&lt;br /&gt;
*Make sure that only one of the two ovens bakes any given cake. Use baker.inUse() and baker.numberAlreadyBaked() to help keep track of which oven has done which cakes.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Visualization==&lt;br /&gt;
{{Viz|CakePipelineVizApp|pipeline.cake.viz}}&lt;br /&gt;
&lt;br /&gt;
[[File:CakePipelineGIF.gif]]&lt;br /&gt;
&lt;br /&gt;
The viz below is for the challenge assignment.&lt;br /&gt;
&lt;br /&gt;
[[File:TwoOvenPipelineGIF.gif]]&lt;br /&gt;
&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|_CakePipelineTestSuite| pipeline.cake.exercise}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Parallel_N_Queens_Assignment&amp;diff=3831</id>
		<title>Parallel N Queens Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Parallel_N_Queens_Assignment&amp;diff=3831"/>
		<updated>2022-05-02T00:45:36Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* ParallelNQueensSolutionCounter(queenLocationsCreator) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Previous Group Warm Up=&lt;br /&gt;
Be sure to complete [[Sequential_N_Queens_Assignment | Sequential N-Queens]] before attempting this exercise.&lt;br /&gt;
&lt;br /&gt;
=Motivation=&lt;br /&gt;
Not everything in the world should be divided and conquered.  Backtracking is a powerful technique which can be readily parallelized.  We will gain experience with backtracking by solving the N-Queens problem sequentially and in parallel.&lt;br /&gt;
&lt;br /&gt;
N-Queens in particular can be used to explain the call stack as the chessboard *IS* the call stack.&lt;br /&gt;
&lt;br /&gt;
[[File:SolvedNQueens8.PNG|250px|thumb|Example solution of N-Queens when n equals 8]]&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
==[https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html Optional]==&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#empty-- empty()]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#of-T- of(value)]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#get-- get()]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#isPresent-- isPresent()]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#isEmpty-- isEmpty()]&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
The n-queens problem is a fundamental coding puzzle which asks: how can N queens be placed on an NxN chessboard so that they cannot attack each other? In chess, a [https://en.wikipedia.org/wiki/Queen_(chess)#Placement_and_movement queen can attack horizontally, vertically, and diagonally] across the board. Thus, to solve the n-queens problem, we must effectively figure out how to place the queens in such a way that no two of them occupy the same row, column, or diagonal. We will be building a method that finds the total number of solutions for n-queens for any given n.&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
==DefaultQueenLocations==&lt;br /&gt;
===DefaultQueenLocations(other, row, col)===&lt;br /&gt;
This constuctor will prove useful for a method you will need to implement on DefaultQueenLocations.  It constructs a new instance of DefaultQueenLocations from another instance of DefaultQueenLocations with an additional queen added to a row and column.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;	private DefaultQueenLocations(DefaultQueenLocations other, int row, int col) {&lt;br /&gt;
		if (other.isLocationThreatFree(row, col)) {&lt;br /&gt;
			int boardSize = other.boardSize();&lt;br /&gt;
			if (row &amp;lt; 0 || row &amp;gt;= boardSize) {&lt;br /&gt;
				throw new IllegalArgumentException(&amp;quot;row &amp;quot; + row + &amp;quot; is not in range [0, &amp;quot; + boardSize + &amp;quot;)&amp;quot;);&lt;br /&gt;
			}&lt;br /&gt;
			if (col &amp;lt; 0 || col &amp;gt;= boardSize) {&lt;br /&gt;
				throw new IllegalArgumentException(&amp;quot;col &amp;quot; + col + &amp;quot; is not in range [0, &amp;quot; + boardSize + &amp;quot;)&amp;quot;);&lt;br /&gt;
			}&lt;br /&gt;
			locations = new Optional[boardSize];&lt;br /&gt;
			for (int r = 0; r &amp;lt; other.boardSize(); ++r) {&lt;br /&gt;
				if (r == row) {&lt;br /&gt;
					locations[row] = Optional.of(col);&lt;br /&gt;
				} else {&lt;br /&gt;
					locations[r] = other.columnOfQueenInRow(r);&lt;br /&gt;
				}&lt;br /&gt;
			}&lt;br /&gt;
		} else {&lt;br /&gt;
			throw new IllegalArgumentException(&lt;br /&gt;
					&amp;quot;Not threat free: (row=&amp;quot; + row + &amp;quot;, column=&amp;quot; + col + &amp;quot;)\nOther board:\n&amp;quot; + other.toString());&lt;br /&gt;
		}&lt;br /&gt;
	}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===isLocationThreatFree(row, col)===&lt;br /&gt;
This method is useful when determining where (if anywhere) an additional queen can be placed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;	@Override&lt;br /&gt;
	public boolean isLocationThreatFree(int row, int col) {&lt;br /&gt;
		int boardSize = this.boardSize();&lt;br /&gt;
		for (int r = 0; r &amp;lt; boardSize; ++r) {&lt;br /&gt;
			Optional&amp;lt;Integer&amp;gt; columnOfQueenInRowR = this.columnOfQueenInRow(r);&lt;br /&gt;
			if (columnOfQueenInRowR.isPresent()) {&lt;br /&gt;
				int c = columnOfQueenInRowR.get();&lt;br /&gt;
&lt;br /&gt;
				// is in same row&lt;br /&gt;
				if (r == row) {&lt;br /&gt;
					// note: we do not check if it is the same column, we return false&lt;br /&gt;
					return false;&lt;br /&gt;
				}&lt;br /&gt;
&lt;br /&gt;
				// is in same column&lt;br /&gt;
				if (c == col) {&lt;br /&gt;
					return false;&lt;br /&gt;
				}&lt;br /&gt;
&lt;br /&gt;
				// is in same diagonal A&lt;br /&gt;
				if (row - r == c - col) {&lt;br /&gt;
					return false;&lt;br /&gt;
				}&lt;br /&gt;
&lt;br /&gt;
				// is in same diagonal B&lt;br /&gt;
				if (row - r == col - c) {&lt;br /&gt;
					return false;&lt;br /&gt;
				}&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		return true;&lt;br /&gt;
	}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==Board State: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/s20/apidocs/nqueens/lab/DefaultImmutableQueenLocations.html DefaultImmutableQueenLocations]==&lt;br /&gt;
{{CodeToImplement|DefaultQueenLocations|createNext&amp;lt;br&amp;gt;getBoardSize&amp;lt;br&amp;gt;getColumnOfQueenInRow&amp;lt;br&amp;gt;getCandidateColumnsInRow|nqueens.lab}}&lt;br /&gt;
&lt;br /&gt;
===[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/nqueens/core/ImmutableQueenLocations.html#createNext(int,int) createNext(row,col)]===&lt;br /&gt;
{{Sequential|public DefaultQueenLocations createNext(int row, int col)}}&lt;br /&gt;
&lt;br /&gt;
There are two constructors for this class.  [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/nqueens/lab/DefaultImmutableQueenLocations.html#DefaultImmutableQueenLocations-int- A public one which creates a fresh new board state with no queens yet placed.] and a private one which creates a new board with the state of a given board which is further constrained by a new queen in the next row.  You need to create a new instance using one of these two constructors.  Which one is it?&lt;br /&gt;
&lt;br /&gt;
Consider this example program which creates a valid 4-queens solution:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;pre&amp;gt;		int boardSize = 4;&lt;br /&gt;
		QueenLocations board0 = new DefaultQueenLocations(boardSize);&lt;br /&gt;
		QueenLocations board1 = board0.createNext(0, 1);&lt;br /&gt;
		QueenLocations board2 = board1.createNext(1, 3);&lt;br /&gt;
		QueenLocations board3 = board2.createNext(2, 0);&lt;br /&gt;
		QueenLocations board4 = board3.createNext(3, 2);&lt;br /&gt;
		System.out.println(board4);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Which board is used to create the next board?&lt;br /&gt;
&lt;br /&gt;
===boardSize()===&lt;br /&gt;
{{Sequential|public int boardSize()}}&lt;br /&gt;
&lt;br /&gt;
Note that we will refer to the standard 8x8 chessboard's size as 8 and not 64.&lt;br /&gt;
&lt;br /&gt;
===[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/nqueens/core/ImmutableQueenLocations.html#getColumnOfQueenInRow(int) columnOfQueenInRow(row)]===&lt;br /&gt;
{{Sequential|public Optional&amp;lt;Integer&amp;gt; columnOfQueenInRow(int row)}}&lt;br /&gt;
&lt;br /&gt;
For an 8x8 board with queens placed in (row=0, col=1), (row=1, col=6), and (row=2, col=4)   &lt;br /&gt;
&lt;br /&gt;
[[File:Queens in rows 012.png|350px]]&lt;br /&gt;
&lt;br /&gt;
* columnOfQueenInRow(0) returns [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#of-T- Optional.of](1)&lt;br /&gt;
* columnOfQueenInRow(1) returns [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#of-T- Optional.of](6)&lt;br /&gt;
* columnOfQueenInRow(2) returns [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#of-T- Optional.of](4)&lt;br /&gt;
* columnOfQueenInRow(3) returns [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#empty-- Optional.empty()]&lt;br /&gt;
* columnOfQueenInRow(4) returns [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#empty-- Optional.empty()]&lt;br /&gt;
* columnOfQueenInRow(5) returns [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#empty-- Optional.empty()]&lt;br /&gt;
* columnOfQueenInRow(6) returns [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#empty-- Optional.empty()]&lt;br /&gt;
* columnOfQueenInRow(7) returns [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#empty-- Optional.empty()]&lt;br /&gt;
&lt;br /&gt;
===candidateColumnsInRow(row)===&lt;br /&gt;
{{Sequential|public List&amp;lt;Integer&amp;gt; candidateColumnsInRow(int row)}}&lt;br /&gt;
&lt;br /&gt;
For an 8x8 board with a single queen placed in (row=0, col=4)&lt;br /&gt;
&lt;br /&gt;
[[File:Queen_r0_c4.png|350px]]&lt;br /&gt;
&lt;br /&gt;
* candidateColumnsInRow(0) returns []&lt;br /&gt;
* candidateColumnsInRow(1) returns [0,1,2,6,7]&lt;br /&gt;
* candidateColumnsInRow(2) returns [0,1,3,5,7]&lt;br /&gt;
* candidateColumnsInRow(3) returns [0,2,3,5,6]&lt;br /&gt;
* candidateColumnsInRow(4) returns [1,2,3,5,6,7]&lt;br /&gt;
* candidateColumnsInRow(5) returns [0,1,2,3,5,6,7]&lt;br /&gt;
* candidateColumnsInRow(6) returns [0,1,2,3,5,6,7]&lt;br /&gt;
* candidateColumnsInRow(7) returns [0,1,2,3,5,6,7]&lt;br /&gt;
&lt;br /&gt;
Note: the provided isLocationThreatFree(row, col) method should be helpful.&lt;br /&gt;
&lt;br /&gt;
==ParallelNQueensSolutionCounter==&lt;br /&gt;
{{CodeToImplement|ParallelNQueensSolutionCounter|constructor&amp;lt;br/&amp;gt;queenLocationsCreator&amp;lt;br/&amp;gt;searchForSolutions&amp;lt;br/&amp;gt;countSolutions|nqueens.parallel.exercise}}&lt;br /&gt;
&lt;br /&gt;
===ParallelNQueensSolutionCounter(queenLocationsCreator)===&lt;br /&gt;
The constructor is passed a queenLocationsCreator.  Hang onto it in an instance variable. Refer to the documentation on [https://docs.oracle.com/javase/8/docs/api/java/util/function/IntFunction.html IntFunction] interface if you are not familiar with it.&lt;br /&gt;
&lt;br /&gt;
===IntFunction&amp;lt;QueenLocations&amp;gt; queenLocationsCreator()===&lt;br /&gt;
Simply return the value in the queenLocationsCreator instance variable.&lt;br /&gt;
&lt;br /&gt;
===searchForSolutions(queenLocations, row)===&lt;br /&gt;
{{Parallel|private static int searchForSolutions(QueenLocations queenLocations, int row)}}&lt;br /&gt;
&lt;br /&gt;
Note: as &amp;lt;code&amp;gt;QueenLocations&amp;lt;/code&amp;gt; is immutable, you will need to create a new instance of the object whenever you move on from one row to the next. This is where &amp;lt;code&amp;gt;createNext&amp;lt;/code&amp;gt; comes in.&lt;br /&gt;
&lt;br /&gt;
Employ backtracking to search for all possible solutions.&lt;br /&gt;
&lt;br /&gt;
All good recursive algorithms need conditions at which to stop.  Backtracking has two: 1) when a solution is found and 2) when the space has constrained to the point of hopelessness.  How will you handle each of these conditions?&lt;br /&gt;
&lt;br /&gt;
What can be performed in parallel?&lt;br /&gt;
&lt;br /&gt;
===countSolutions(boardSize)===&lt;br /&gt;
Use the apply method on queenLocationsCreator to create a new board.&lt;br /&gt;
&lt;br /&gt;
Pass that board along with the appropriate row to begin your search to searchForSolutions to solve this problem.&lt;br /&gt;
&lt;br /&gt;
=Testing=&lt;br /&gt;
{{TestSuite|__NQueensTestSuite|nqueens.parallel.exercise}}&lt;br /&gt;
: {{TestSuite|_QueenLocationsTestSuite|nqueens.parallel.exercise}}&lt;br /&gt;
{{TestSuite|_ParallelNQueensSolutionCountTestSuite|nqueens.parallel.exercise}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Parallel_Raytracer_Assignment&amp;diff=3786</id>
		<title>Parallel Raytracer Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Parallel_Raytracer_Assignment&amp;diff=3786"/>
		<updated>2022-04-26T20:24:00Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* constructor */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Code To Investigate=&lt;br /&gt;
==RayTraceContext==&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public interface RayTraceContext {&lt;br /&gt;
	int width();&lt;br /&gt;
&lt;br /&gt;
	int height();&lt;br /&gt;
&lt;br /&gt;
	void mark(int xMin, int yMin, int xMaxExclusive, int yMaxExclusive);&lt;br /&gt;
&lt;br /&gt;
	void markAndRender(int xMin, int yMin, int xMaxExclusive, int yMaxExclusive);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Scheduler==&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public interface Scheduler {&lt;br /&gt;
	void void_fork(Runnable runnable);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==SplitFourWayRayTracer==&lt;br /&gt;
{{CodeToImplement|SplitFourWayRayTracer|rayTrace|raytrace.exercise}}&lt;br /&gt;
&lt;br /&gt;
===rayTrace===&lt;br /&gt;
{{Parallel|public void rayTrace(RayTraceContext context, Scheduler scheduler)}}&lt;br /&gt;
&lt;br /&gt;
==LoopRayTracer==&lt;br /&gt;
{{CodeToImplement|LoopRayTracer|constructor&amp;lt;br/&amp;gt;sectionsCreator&amp;lt;br/&amp;gt;rayTrace|raytrace.exercise}}&lt;br /&gt;
&lt;br /&gt;
===constructor===&lt;br /&gt;
{{Sequential|public LoopRayTracer(Function&amp;lt;RayTraceContext, List&amp;lt;Section&amp;gt;&amp;gt; sectionsCreator)}}&lt;br /&gt;
&lt;br /&gt;
Hang onto the sectionsCreator in an instance variable.  You will need it. Refer to [https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html Function&amp;lt;T, R&amp;gt; Interface] documentation if you are unfamiliar with Function.&lt;br /&gt;
&lt;br /&gt;
===thresholdPredicate===&lt;br /&gt;
{{Sequential|Function&amp;lt;RayTraceContext, List&amp;lt;Section&amp;gt;&amp;gt; sectionsCreator()}}&lt;br /&gt;
&lt;br /&gt;
Return the sectionsCreator you stored in an instance variable.&lt;br /&gt;
&lt;br /&gt;
===rayTrace===&lt;br /&gt;
{{Parallel|public void rayTrace(RayTraceContext context, Scheduler scheduler)}}&lt;br /&gt;
&lt;br /&gt;
==DivideAndConquerRayTracer==&lt;br /&gt;
{{CodeToImplement|DivideAndConquerRayTracer|constructor&amp;lt;br/&amp;gt;thresholdPredicate&amp;lt;br/&amp;gt;rayTraceKernel|raytrace.exercise}}&lt;br /&gt;
&lt;br /&gt;
===constructor===&lt;br /&gt;
{{Sequential|public DivideAndConquerRayTracer(BiPredicate&amp;lt;Integer, Integer&amp;gt; thresholdPredicate)}}&lt;br /&gt;
&lt;br /&gt;
Hang onto the thresholdPredicate in an instance variable.  You will need it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;thresholdPredicate&amp;lt;/code&amp;gt; determines the threshold for division. If &amp;lt;code&amp;gt;thresholdPredicate.test()&amp;lt;/code&amp;gt; is true, then the section is still large enough for more division of work.&lt;br /&gt;
&lt;br /&gt;
===thresholdPredicate===&lt;br /&gt;
{{Sequential|public BiPredicate&amp;lt;Integer, Integer&amp;gt; thresholdPredicate()}}&lt;br /&gt;
&lt;br /&gt;
Return the thresholdPredicate you stored in an instance variable.&lt;br /&gt;
&lt;br /&gt;
===rayTrace===&lt;br /&gt;
{{Parallel|public void rayTrace(RayTraceContext context, Scheduler scheduler)}}&lt;br /&gt;
&lt;br /&gt;
=Checking Your Solution=&lt;br /&gt;
==Visualization==&lt;br /&gt;
{{Viz|RayTracerViz|raytrace.viz|main}}&lt;br /&gt;
&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|_SequentialRayTracerTestSuite|raytrace.group}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Parallel_Raytracer_Assignment&amp;diff=3785</id>
		<title>Parallel Raytracer Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Parallel_Raytracer_Assignment&amp;diff=3785"/>
		<updated>2022-04-26T19:12:02Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* constructor */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Code To Investigate=&lt;br /&gt;
==RayTraceContext==&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public interface RayTraceContext {&lt;br /&gt;
	int width();&lt;br /&gt;
&lt;br /&gt;
	int height();&lt;br /&gt;
&lt;br /&gt;
	void mark(int xMin, int yMin, int xMaxExclusive, int yMaxExclusive);&lt;br /&gt;
&lt;br /&gt;
	void markAndRender(int xMin, int yMin, int xMaxExclusive, int yMaxExclusive);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Scheduler==&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public interface Scheduler {&lt;br /&gt;
	void void_fork(Runnable runnable);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==SplitFourWayRayTracer==&lt;br /&gt;
{{CodeToImplement|SplitFourWayRayTracer|rayTrace|raytrace.exercise}}&lt;br /&gt;
&lt;br /&gt;
===rayTrace===&lt;br /&gt;
{{Parallel|public void rayTrace(RayTraceContext context, Scheduler scheduler)}}&lt;br /&gt;
&lt;br /&gt;
==LoopRayTracer==&lt;br /&gt;
{{CodeToImplement|LoopRayTracer|constructor&amp;lt;br/&amp;gt;sectionsCreator&amp;lt;br/&amp;gt;rayTrace|raytrace.exercise}}&lt;br /&gt;
&lt;br /&gt;
===constructor===&lt;br /&gt;
{{Sequential|public LoopRayTracer(Function&amp;lt;RayTraceContext, List&amp;lt;Section&amp;gt;&amp;gt; sectionsCreator)}}&lt;br /&gt;
&lt;br /&gt;
Hang onto the sectionsCreator in an instance variable.  You will need it. Refer to [https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html Function&amp;lt;T, R&amp;gt; Interface] documentation if you are unfamiliar with Function.&lt;br /&gt;
&lt;br /&gt;
===thresholdPredicate===&lt;br /&gt;
{{Sequential|Function&amp;lt;RayTraceContext, List&amp;lt;Section&amp;gt;&amp;gt; sectionsCreator()}}&lt;br /&gt;
&lt;br /&gt;
Return the sectionsCreator you stored in an instance variable.&lt;br /&gt;
&lt;br /&gt;
===rayTrace===&lt;br /&gt;
{{Parallel|public void rayTrace(RayTraceContext context, Scheduler scheduler)}}&lt;br /&gt;
&lt;br /&gt;
==DivideAndConquerRayTracer==&lt;br /&gt;
{{CodeToImplement|DivideAndConquerRayTracer|constructor&amp;lt;br/&amp;gt;thresholdPredicate&amp;lt;br/&amp;gt;rayTraceKernel|raytrace.exercise}}&lt;br /&gt;
&lt;br /&gt;
===constructor===&lt;br /&gt;
{{Sequential|public DivideAndConquerRayTracer(BiPredicate&amp;lt;Integer, Integer&amp;gt; thresholdPredicate)}}&lt;br /&gt;
&lt;br /&gt;
Hang onto the thresholdPredicate in an instance variable.  You will need it.&lt;br /&gt;
&lt;br /&gt;
===thresholdPredicate===&lt;br /&gt;
{{Sequential|public BiPredicate&amp;lt;Integer, Integer&amp;gt; thresholdPredicate()}}&lt;br /&gt;
&lt;br /&gt;
Return the thresholdPredicate you stored in an instance variable.&lt;br /&gt;
&lt;br /&gt;
===rayTrace===&lt;br /&gt;
{{Parallel|public void rayTrace(RayTraceContext context, Scheduler scheduler)}}&lt;br /&gt;
&lt;br /&gt;
=Checking Your Solution=&lt;br /&gt;
==Visualization==&lt;br /&gt;
{{Viz|RayTracerViz|raytrace.viz|main}}&lt;br /&gt;
&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|_SequentialRayTracerTestSuite|raytrace.group}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=3766</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=3766"/>
		<updated>2022-04-23T23:58:05Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* Java Phaser */&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;
==IterativeAveragingUtils==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|slice&amp;lt;br/&amp;gt;createPhasableDoubleArrays|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;
===createPhasableDoubleArrays===&lt;br /&gt;
&amp;lt;youtube&amp;gt;-SBcwicgyOE&amp;lt;/youtube&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 Averaging algorithm shown above. 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>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=3765</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=3765"/>
		<updated>2022-04-23T23:57:02Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* Java Phaser */&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 [[#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;
==IterativeAveragingUtils==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|slice&amp;lt;br/&amp;gt;createPhasableDoubleArrays|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;
===createPhasableDoubleArrays===&lt;br /&gt;
&amp;lt;youtube&amp;gt;-SBcwicgyOE&amp;lt;/youtube&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 Averaging algorithm shown above. 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>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=3764</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=3764"/>
		<updated>2022-04-23T23:56:25Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* Java Phaser */&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 [[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;
==IterativeAveragingUtils==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|slice&amp;lt;br/&amp;gt;createPhasableDoubleArrays|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;
===createPhasableDoubleArrays===&lt;br /&gt;
&amp;lt;youtube&amp;gt;-SBcwicgyOE&amp;lt;/youtube&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 Averaging algorithm shown above. 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>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=3763</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=3763"/>
		<updated>2022-04-23T23:50:22Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* ForkLoopWithPhaserIterativeAverager */&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]&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;
==IterativeAveragingUtils==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|slice&amp;lt;br/&amp;gt;createPhasableDoubleArrays|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;
===createPhasableDoubleArrays===&lt;br /&gt;
&amp;lt;youtube&amp;gt;-SBcwicgyOE&amp;lt;/youtube&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 Averaging algorithm shown above. 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>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=3762</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=3762"/>
		<updated>2022-04-23T23:46:15Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* 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]&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;
==IterativeAveragingUtils==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|slice&amp;lt;br/&amp;gt;createPhasableDoubleArrays|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;
===createPhasableDoubleArrays===&lt;br /&gt;
&amp;lt;youtube&amp;gt;-SBcwicgyOE&amp;lt;/youtube&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 Averaging algorithm shown above. 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. 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>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=3761</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=3761"/>
		<updated>2022-04-23T23:45:26Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* IterativeAveragingUtils */&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]&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;
==IterativeAveragingUtils==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|slice&amp;lt;br/&amp;gt;createPhasableDoubleArrays|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;
===createPhasableDoubleArrays===&lt;br /&gt;
&amp;lt;youtube&amp;gt;-SBcwicgyOE&amp;lt;/youtube&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 Iterative Averaging algorithm shown above sequentially. 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. 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>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterative_Averaging&amp;diff=3724</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=3724"/>
		<updated>2022-04-14T19:08:56Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* sliceDoubleArrayIntoRangesForIterativeAveraging */&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]&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;
==IterativeAveragingUtils==&lt;br /&gt;
{{CodeToImplement|IterativeAveragingUtils|sliceDoubleArrayIntoRangesForIterativeAveraging&amp;lt;br/&amp;gt;createPhasableDoubleArraysForIterativeAveraging|iterativeaveraging.util.exercise}}&lt;br /&gt;
&lt;br /&gt;
===sliceDoubleArrayIntoRangesForIterativeAveraging===&lt;br /&gt;
&amp;lt;youtube&amp;gt;_taVHpi1Hpg&amp;lt;/youtube&amp;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 IndexedRanges should have these properties:&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|||sliceIndexID||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;
===createPhasableDoubleArraysForIterativeAveraging===&lt;br /&gt;
&amp;lt;youtube&amp;gt;-SBcwicgyOE&amp;lt;/youtube&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 Iterative Averaging algorithm shown above sequentially. 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. 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>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Counting_Assignment&amp;diff=3684</id>
		<title>K Mer Counting Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Counting_Assignment&amp;diff=3684"/>
		<updated>2022-04-10T20:39:39Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* LongConcurrentMapKMerCounter */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;credit for this assignment: John Gibson and Dennis Cosgrove&lt;br /&gt;
=Motivation=&lt;br /&gt;
K-mer counting is a real world application which provides the opportunity to build thread safe data structures which alleviate contention.&lt;br /&gt;
&lt;br /&gt;
Pulling real data from the National Institutes of Health forces us to address balancing our work.&lt;br /&gt;
&lt;br /&gt;
Finally, k-mer counting provides opportunities for performance engineering.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
The term k-mer refers to a substring of length k and k-mer counting refers to the process of finding the number of occurrences of a k-mer in a given string. This computational problem has many real-world applications, the most notable being in the field of computational genomics. In this assignment, you will design a program that will ultimately take in a human X-chromosome and count the number of k-mers in the string of DNA. You will be making use of everything you have learned thus far, including atomic variables. It is useful to know that given a string of length n, the amount of possible k-mers is equal to '''n - k + 1'''.&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;_oyWzImAx1w&amp;lt;/youtube&amp;gt;&lt;br /&gt;
=Demo=&lt;br /&gt;
{{CodeToInvestigate|OutputKmer|main|kmer.output}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;tFfa7DWLJ_U&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This demo video shows the big picture of KMer Counting. It included example usage of calculateSumOfAllKMers and calculatePossibleKMers method in &amp;lt;code&amp;gt;KMerUtils&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
==Runtime==&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#availableProcessors-- Runtime.getRuntime().availableProcessors()]&lt;br /&gt;
&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;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicIntegerArray.html AtomicIntegerArray].&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html ReentrantReadWriteLock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html#readLock-- readLock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html#writeLock-- writeLock]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html Lock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- unlock]&lt;br /&gt;
&lt;br /&gt;
==KMerUtils==&lt;br /&gt;
The following methods should be useful as you build the assignment.&lt;br /&gt;
&lt;br /&gt;
Check out the Javadocs either [http://www.cse.wustl.edu/~cosgroved/courses/cse231/f17/apidocs/kmer/core/KMerUtils.html on the web], or in the comments in KMerUtils.java for further details on how they work.&lt;br /&gt;
&lt;br /&gt;
===long calculatePossibleKMers(int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long calculatePossibleKMers(int kMerLength) {&lt;br /&gt;
	int bitsPerBase = 2;&lt;br /&gt;
	int bitsPerKMer = kMerLength * bitsPerBase;&lt;br /&gt;
	long possibleKMers = 1L &amp;lt;&amp;lt; bitsPerKMer;&lt;br /&gt;
	return possibleKMers;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toArrayLength(long possibleKMers)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toArrayLength(long possibleKMers) {&lt;br /&gt;
	// maximum array length is VM dependent, but must be indexed by an int.&lt;br /&gt;
	// reportedly some vms reserve some header words (see ArrayList) which puts its&lt;br /&gt;
	// limit as just below 2^31.&lt;br /&gt;
	// we will choose 2^30 (inclusive) as the maximum since it will hold the largest&lt;br /&gt;
	// potential k-mer (4^15)&lt;br /&gt;
	final long MAX_K_MER_ARRAY_LENGTH_INCLUSIVE = 1 &amp;lt;&amp;lt; 30;&lt;br /&gt;
	if (possibleKMers &amp;lt;= MAX_K_MER_ARRAY_LENGTH_INCLUSIVE) {&lt;br /&gt;
		return (int) possibleKMers;&lt;br /&gt;
	} else {&lt;br /&gt;
		throw new IllegalArgumentException(possibleKMers + &amp;quot; &amp;gt; &amp;quot; + MAX_K_MER_ARRAY_LENGTH_INCLUSIVE);&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int calculateSumOfAllKMers(List&amp;lt;byte[]&amp;gt; sequences, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int calculateSumOfAllKMers(List&amp;lt;byte[]&amp;gt; sequences, int kMerLength) {&lt;br /&gt;
	int sum = 0;&lt;br /&gt;
	for (byte[] sequence : sequences) {&lt;br /&gt;
		int i = (sequence.length - kMerLength + 1);&lt;br /&gt;
		if( i &amp;gt; 0 ) {&lt;br /&gt;
			sum += i;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return sum;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toPackedInt(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toPackedInt(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	int result = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		switch (sequence[offset + i]) {&lt;br /&gt;
		// case A:&lt;br /&gt;
		// result |= (A_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
		// break;&lt;br /&gt;
		case T:&lt;br /&gt;
			result |= (T_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case C:&lt;br /&gt;
			result |= (C_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case G:&lt;br /&gt;
			result |= (G_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toPackedInt(byte[] kMer)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toPackedInt(byte[] kMer) {&lt;br /&gt;
	return toPackedInt(kMer, 0, kMer.length);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===byte[] unpackInt(int kMer, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static byte[] unpackInt(int kMer, int kMerLength) {&lt;br /&gt;
	byte[] result = new byte[kMerLength];&lt;br /&gt;
&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		int v = (kMer &amp;gt;&amp;gt; (i + i)) &amp;amp; 0x3;&lt;br /&gt;
		switch (v) {&lt;br /&gt;
		case A_MASK_INT:&lt;br /&gt;
			result[i] = A;&lt;br /&gt;
			break;&lt;br /&gt;
		case T_MASK_INT:&lt;br /&gt;
			result[i] = T;&lt;br /&gt;
			break;&lt;br /&gt;
		case C_MASK_INT:&lt;br /&gt;
			result[i] = C;&lt;br /&gt;
			break;&lt;br /&gt;
		case G_MASK_INT:&lt;br /&gt;
			result[i] = G;&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===long toPackedLong(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long toPackedLong(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	long result = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		switch (sequence[offset + i]) {&lt;br /&gt;
		// case A:&lt;br /&gt;
		// result |= (A_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
		// break;&lt;br /&gt;
		case T:&lt;br /&gt;
			result |= (T_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case C:&lt;br /&gt;
			result |= (C_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case G:&lt;br /&gt;
			result |= (G_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===long toPackedLong(byte[] sequence)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long toPackedLong(byte[] sequence) {&lt;br /&gt;
	return toPackedLong(sequence, 0, sequence.length);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===byte[] unpackLong(long kMer, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static byte[] unpackLong(long kMer, int kMerLength) {&lt;br /&gt;
	byte[] result = new byte[kMerLength];&lt;br /&gt;
&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		long v = (kMer &amp;gt;&amp;gt; (i + i)) &amp;amp; 0x3;&lt;br /&gt;
		switch ((int) v) {&lt;br /&gt;
		case A_MASK_INT:&lt;br /&gt;
			result[i] = A;&lt;br /&gt;
			break;&lt;br /&gt;
		case T_MASK_INT:&lt;br /&gt;
			result[i] = T;&lt;br /&gt;
			break;&lt;br /&gt;
		case C_MASK_INT:&lt;br /&gt;
			result[i] = C;&lt;br /&gt;
			break;&lt;br /&gt;
		case G_MASK_INT:&lt;br /&gt;
			result[i] = G;&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==KMerCounts==&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/KMerCount.html interface KMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/map/MapKMerCount.html class MapKMerCount&amp;lt;T&amp;gt;]&lt;br /&gt;
:: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/map/StringMapKMerCount.html class StringMapKMerCount]&lt;br /&gt;
:: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/map/LongMapKMerCount.html class LongMapKMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/array/IntArrayKMerCount.html class IntArrayKMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/array/AtomicIntegerArrayKMerCount.html class AtomicIntegerArrayKMerCount]&lt;br /&gt;
&lt;br /&gt;
===MapKMerCount===&lt;br /&gt;
In order to provide a KMerCount that works with different k-mer type representations we have added an interface KMerCodec which provides functions to go to an from the byte[] representation:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public interface KMerCodec&amp;lt;T&amp;gt; {&lt;br /&gt;
	BiFunction&amp;lt;T, Integer, byte[]&amp;gt; decoder();&lt;br /&gt;
&lt;br /&gt;
	Function&amp;lt;byte[], T&amp;gt; encoder();&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As an example the instance for long -&amp;gt; byte[] and back simply utilizes the pack and unpack methods in KMerUtils:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public enum LongKMerCodec implements KMerCodec&amp;lt;Long&amp;gt; {&lt;br /&gt;
	INSTANCE;&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public Function&amp;lt;byte[], Long&amp;gt; encoder() {&lt;br /&gt;
		return KMerUtils::toPackedLong;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public BiFunction&amp;lt;Long, Integer, byte[]&amp;gt; decoder() {&lt;br /&gt;
		return KMerUtils::unpackLong;&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Creating an instance of a MapKMerCount to return from your KMerCounters will need to use the appropriate codec.  For example, if you have a map of type &amp;lt;code&amp;gt;Map&amp;lt;Long, Integer&amp;gt;&amp;lt;/code&amp;gt; which you wish to wrap and return, your code would look something like this:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new MapKMerCount&amp;lt;&amp;gt;(k, map, LongKMerCodec.INSTANCE);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===IntArrayKMerCount===&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new IntArrayKMerCount(k, array);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===AtomicIntegerArrayKMerCount===&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new AtomicIntegerArrayKMerCount(k, atomicIntegerArray);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Group Warmup=&lt;br /&gt;
Complete the [[String_Map_K_Mer_Assignment|group warm up]] first.&lt;br /&gt;
&lt;br /&gt;
=Exercise=&lt;br /&gt;
{{ tip | Get ThresholdSlices working and then use it to balance all of your parallel KMerCounter implementations. }}&lt;br /&gt;
==ThresholdSlices==&lt;br /&gt;
===slicing===&lt;br /&gt;
{{CodeToImplement|ThresholdSlices|sliceKernel&amp;lt;br&amp;gt;createSlices&amp;lt;br&amp;gt;calculateReasonableThreshold|kmer.exercise.balance}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|&amp;lt;br/&amp;gt;The javadoc incorrectly specify that &amp;quot;Any sequence whose length is less than or equal to k should be omitted.&amp;quot;&amp;lt;br/&amp;gt;The javadoc should read &amp;quot;Any sequence whose length is less than k should be omitted.&amp;quot;}}&lt;br /&gt;
&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;
{{Sequential|public static List&amp;lt;Slice&amp;lt;byte[]&amp;gt;&amp;gt; createSlices(List&amp;lt;byte[]&amp;gt; sequences, int k, IntPredicate slicePredicate)}}&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;
&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;
&amp;lt;youtube&amp;gt;m56ee5spxiw&amp;lt;/youtube&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 threashold of slice length used in 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;
==LongConcurrentMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|LongConcurrentMapKMerCounter|parse|kmer.exercise.longconcurrentmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will also overlap greatly with everything you have done previously, but instead of using Strings or ByteArrayRange, we will be using Longs to represent DNA data. In order to do this, refer to the KMerUtils class, more specifically the toPackedLong method which will convert a sequence of bytes into a Long for our purposes. Don't forget to balance the workload in this parallel implementation with your ThresholdSlices. Make sure to select the most appropriate parallel fork loop. &lt;br /&gt;
&lt;br /&gt;
{{Warning|Be sure to utilize the &amp;lt;code&amp;gt;Supplier&amp;lt;ConcurrentMap&amp;lt;Long, Integer&amp;gt;&amp;gt; concurrentMapFactory&amp;lt;/code&amp;gt; instance variable to create your ConcurrentMap.}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&amp;lt;youtube&amp;gt;d_Ze-YQ3Thc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Maps in Java often have a way to specify an initialCapacity to the constructor.  When used well, this parameter can give a hint as to how many Entries the Map should be expecting.  &lt;br /&gt;
&lt;br /&gt;
KMerUtils has methods which will calculate what you need to compute this value.  calculatePossibleKmers returns 4^k and calculateSumOfAllKmers returns n-k+1.  Think about how each method might be used to calculate an appropriate initial capacity.&lt;br /&gt;
&lt;br /&gt;
Some questions to ask yourself:&lt;br /&gt;
&lt;br /&gt;
# How many entries should the ConcurrentMap expect for a chromosome a million in length if k is 30?&lt;br /&gt;
# How many entries should the ConcurrentMap expect for a chromosome a million in length if k is 1?&lt;br /&gt;
&lt;br /&gt;
In order to test LongConcurrentMapKMerCounter with both java.util.ConcurrentHashMap&amp;lt;K,V&amp;gt; and your ConcurrentBucketHashMap&amp;lt;K,V&amp;gt; implementation, we pass a concurrentMapFactory to LongConcurrentMapKMerCounter's constructor.  You should use this factory to create your instance of ConcurrentMap via the apply method.  The Optional&amp;lt;Integer&amp;gt; parameter to this apply method is the initialCapacity.&lt;br /&gt;
&lt;br /&gt;
Note: anyone feeling the weight of a pandemic should feel free to pass Optional.empty() to the apply method.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IntArrayKMerCounter==&lt;br /&gt;
{{CodeToImplement|IntArrayKMerCounter|parse|kmer.lab.intarray}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
Like the String HashMap implementation, this implementation should be completed sequentially. However, unlike the previous implementations, we will be using an array instead of a map. This means that you must keep in mind what size to make the array in order to instantiate it. KMerUtils has a method which will calculate this number for you, but think about which one to use, why you would use it, and how that would differ from simply calculating n - k + 1 yourself. Think of using the int array as similar to using the map, but with ints as the key/value pairs. Use KMerUtils.toPackedInt() in order to convert the sequence into an int for our purposes.&lt;br /&gt;
&lt;br /&gt;
Hint: calculatePossibleKmers and calculateSumOfAllKmers are distinct and take different input parameters. calculateSumOfAllKmers performs the n - k + 1 calculation on a list of arrays of sequences and sums the total. It calculate the total number of Kmers in the list of sequences. calculatePossibleKmers only takes the length of the k-mer (k) as the input parameter, meaning it will calculate all possibilities independent of the sequences. Thus, the n - k + 1 calculation would yield a value ranging from 0 (inclusive) to the result of calculatePossibleKmers (exclusive).&lt;br /&gt;
&lt;br /&gt;
==AtomicIntegerArrayKMerCounter==&lt;br /&gt;
{{CodeToImplement|AtomicIntegerArrayKMerCounter|parse|kmer.lab.atomicintegerarray}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This implementation is simply the parallel equivalent of the int array implementation. To do so, you will be making use of Java’s atomic version of an array: an AtomicIntegerArray. Like before, you will be need to complete the parse method.  Don't forget to balance the workload in this parallel implementation with your ThresholdSlices.  Make sure to select the most appropriate parallel fork loop.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
==ConcurrentBucketHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|ConcurrentBucketHashMapKMerCounter|parse|kmer.lab.concurrentbuckethashmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will go a step beyond the previous implementations and divide entries into 1024 buckets. Although a different number of buckets can be used, we will simply stick to 1024. Every time an entry is added to the bucket dictionary, it will create a hash based on the entry's key and place the entry into one of the 1024 buckets. Because we would only need to search one bucket for an entry based on a key's hash, this mainly serves the purpose of cutting down on search time. The compute method of the ConcurrentBucketHashMap should automatically compute the correct bucket for the entry, so this implementation should closely resemble the previous ConcurrentHashMap implementations. When instantiating the ConcurrentBucketHashMap, we recommend making the key/value pairs a Long and an Integer.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=Extra Credit=&lt;br /&gt;
==Open-ended K-mer Counter==&lt;br /&gt;
&lt;br /&gt;
This implementation is worth up to 4.5 extra credit points.&lt;br /&gt;
&lt;br /&gt;
It is intended as an opportunity to build a high-performing, thread-safe data structure.&lt;br /&gt;
&lt;br /&gt;
One possible path was outlined at a high level in class.  However, students are encouraged to think of alternate solutions.&lt;br /&gt;
&lt;br /&gt;
We have explicitly disallowed implementations akin to our matrix map reduce assignment, since we have already done the &amp;quot;give everyone their own data and combine in post&amp;quot; pattern to death.&lt;br /&gt;
&lt;br /&gt;
Partial credit is possible for solutions which demonstrate significant ingenuity.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|__KMerTestSuite|kmer.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Research=&lt;br /&gt;
[https://en.wikipedia.org/wiki/K-mer wikipedia article on k-mers]&lt;br /&gt;
&lt;br /&gt;
paper: [https://academic.oup.com/bioinformatics/article-lookup/doi/10.1093/bioinformatics/btr011 A fast, lock-free approach for efficient parallel counting of occurrences of k-mers] (Jellyfish)&lt;br /&gt;
&lt;br /&gt;
paper: [https://peerj.com/articles/cs-94/ Multiple comparative metagenomics using multiset k-mer counting]&lt;br /&gt;
&lt;br /&gt;
[http://homolog.us/Tutorials/index.php?p=3.7&amp;amp;s=2 Collection of approaches to k-mer counting]&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform Burrows–Wheeler transform]&lt;br /&gt;
&lt;br /&gt;
[http://biology.stackexchange.com/questions/45016/what-is-the-biological-significance-of-k-mer-counting Significance of k-mer Counting]&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Counting_Assignment&amp;diff=3683</id>
		<title>K Mer Counting Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Counting_Assignment&amp;diff=3683"/>
		<updated>2022-04-10T20:36:14Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* LongConcurrentMapKMerCounter */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;credit for this assignment: John Gibson and Dennis Cosgrove&lt;br /&gt;
=Motivation=&lt;br /&gt;
K-mer counting is a real world application which provides the opportunity to build thread safe data structures which alleviate contention.&lt;br /&gt;
&lt;br /&gt;
Pulling real data from the National Institutes of Health forces us to address balancing our work.&lt;br /&gt;
&lt;br /&gt;
Finally, k-mer counting provides opportunities for performance engineering.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
The term k-mer refers to a substring of length k and k-mer counting refers to the process of finding the number of occurrences of a k-mer in a given string. This computational problem has many real-world applications, the most notable being in the field of computational genomics. In this assignment, you will design a program that will ultimately take in a human X-chromosome and count the number of k-mers in the string of DNA. You will be making use of everything you have learned thus far, including atomic variables. It is useful to know that given a string of length n, the amount of possible k-mers is equal to '''n - k + 1'''.&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;_oyWzImAx1w&amp;lt;/youtube&amp;gt;&lt;br /&gt;
=Demo=&lt;br /&gt;
{{CodeToInvestigate|OutputKmer|main|kmer.output}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;tFfa7DWLJ_U&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This demo video shows the big picture of KMer Counting. It included example usage of calculateSumOfAllKMers and calculatePossibleKMers method in &amp;lt;code&amp;gt;KMerUtils&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
==Runtime==&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#availableProcessors-- Runtime.getRuntime().availableProcessors()]&lt;br /&gt;
&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;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicIntegerArray.html AtomicIntegerArray].&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html ReentrantReadWriteLock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html#readLock-- readLock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html#writeLock-- writeLock]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html Lock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- unlock]&lt;br /&gt;
&lt;br /&gt;
==KMerUtils==&lt;br /&gt;
The following methods should be useful as you build the assignment.&lt;br /&gt;
&lt;br /&gt;
Check out the Javadocs either [http://www.cse.wustl.edu/~cosgroved/courses/cse231/f17/apidocs/kmer/core/KMerUtils.html on the web], or in the comments in KMerUtils.java for further details on how they work.&lt;br /&gt;
&lt;br /&gt;
===long calculatePossibleKMers(int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long calculatePossibleKMers(int kMerLength) {&lt;br /&gt;
	int bitsPerBase = 2;&lt;br /&gt;
	int bitsPerKMer = kMerLength * bitsPerBase;&lt;br /&gt;
	long possibleKMers = 1L &amp;lt;&amp;lt; bitsPerKMer;&lt;br /&gt;
	return possibleKMers;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toArrayLength(long possibleKMers)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toArrayLength(long possibleKMers) {&lt;br /&gt;
	// maximum array length is VM dependent, but must be indexed by an int.&lt;br /&gt;
	// reportedly some vms reserve some header words (see ArrayList) which puts its&lt;br /&gt;
	// limit as just below 2^31.&lt;br /&gt;
	// we will choose 2^30 (inclusive) as the maximum since it will hold the largest&lt;br /&gt;
	// potential k-mer (4^15)&lt;br /&gt;
	final long MAX_K_MER_ARRAY_LENGTH_INCLUSIVE = 1 &amp;lt;&amp;lt; 30;&lt;br /&gt;
	if (possibleKMers &amp;lt;= MAX_K_MER_ARRAY_LENGTH_INCLUSIVE) {&lt;br /&gt;
		return (int) possibleKMers;&lt;br /&gt;
	} else {&lt;br /&gt;
		throw new IllegalArgumentException(possibleKMers + &amp;quot; &amp;gt; &amp;quot; + MAX_K_MER_ARRAY_LENGTH_INCLUSIVE);&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int calculateSumOfAllKMers(List&amp;lt;byte[]&amp;gt; sequences, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int calculateSumOfAllKMers(List&amp;lt;byte[]&amp;gt; sequences, int kMerLength) {&lt;br /&gt;
	int sum = 0;&lt;br /&gt;
	for (byte[] sequence : sequences) {&lt;br /&gt;
		int i = (sequence.length - kMerLength + 1);&lt;br /&gt;
		if( i &amp;gt; 0 ) {&lt;br /&gt;
			sum += i;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return sum;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toPackedInt(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toPackedInt(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	int result = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		switch (sequence[offset + i]) {&lt;br /&gt;
		// case A:&lt;br /&gt;
		// result |= (A_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
		// break;&lt;br /&gt;
		case T:&lt;br /&gt;
			result |= (T_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case C:&lt;br /&gt;
			result |= (C_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case G:&lt;br /&gt;
			result |= (G_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toPackedInt(byte[] kMer)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toPackedInt(byte[] kMer) {&lt;br /&gt;
	return toPackedInt(kMer, 0, kMer.length);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===byte[] unpackInt(int kMer, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static byte[] unpackInt(int kMer, int kMerLength) {&lt;br /&gt;
	byte[] result = new byte[kMerLength];&lt;br /&gt;
&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		int v = (kMer &amp;gt;&amp;gt; (i + i)) &amp;amp; 0x3;&lt;br /&gt;
		switch (v) {&lt;br /&gt;
		case A_MASK_INT:&lt;br /&gt;
			result[i] = A;&lt;br /&gt;
			break;&lt;br /&gt;
		case T_MASK_INT:&lt;br /&gt;
			result[i] = T;&lt;br /&gt;
			break;&lt;br /&gt;
		case C_MASK_INT:&lt;br /&gt;
			result[i] = C;&lt;br /&gt;
			break;&lt;br /&gt;
		case G_MASK_INT:&lt;br /&gt;
			result[i] = G;&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===long toPackedLong(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long toPackedLong(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	long result = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		switch (sequence[offset + i]) {&lt;br /&gt;
		// case A:&lt;br /&gt;
		// result |= (A_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
		// break;&lt;br /&gt;
		case T:&lt;br /&gt;
			result |= (T_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case C:&lt;br /&gt;
			result |= (C_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case G:&lt;br /&gt;
			result |= (G_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===long toPackedLong(byte[] sequence)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long toPackedLong(byte[] sequence) {&lt;br /&gt;
	return toPackedLong(sequence, 0, sequence.length);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===byte[] unpackLong(long kMer, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static byte[] unpackLong(long kMer, int kMerLength) {&lt;br /&gt;
	byte[] result = new byte[kMerLength];&lt;br /&gt;
&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		long v = (kMer &amp;gt;&amp;gt; (i + i)) &amp;amp; 0x3;&lt;br /&gt;
		switch ((int) v) {&lt;br /&gt;
		case A_MASK_INT:&lt;br /&gt;
			result[i] = A;&lt;br /&gt;
			break;&lt;br /&gt;
		case T_MASK_INT:&lt;br /&gt;
			result[i] = T;&lt;br /&gt;
			break;&lt;br /&gt;
		case C_MASK_INT:&lt;br /&gt;
			result[i] = C;&lt;br /&gt;
			break;&lt;br /&gt;
		case G_MASK_INT:&lt;br /&gt;
			result[i] = G;&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==KMerCounts==&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/KMerCount.html interface KMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/map/MapKMerCount.html class MapKMerCount&amp;lt;T&amp;gt;]&lt;br /&gt;
:: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/map/StringMapKMerCount.html class StringMapKMerCount]&lt;br /&gt;
:: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/map/LongMapKMerCount.html class LongMapKMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/array/IntArrayKMerCount.html class IntArrayKMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/array/AtomicIntegerArrayKMerCount.html class AtomicIntegerArrayKMerCount]&lt;br /&gt;
&lt;br /&gt;
===MapKMerCount===&lt;br /&gt;
In order to provide a KMerCount that works with different k-mer type representations we have added an interface KMerCodec which provides functions to go to an from the byte[] representation:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public interface KMerCodec&amp;lt;T&amp;gt; {&lt;br /&gt;
	BiFunction&amp;lt;T, Integer, byte[]&amp;gt; decoder();&lt;br /&gt;
&lt;br /&gt;
	Function&amp;lt;byte[], T&amp;gt; encoder();&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As an example the instance for long -&amp;gt; byte[] and back simply utilizes the pack and unpack methods in KMerUtils:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public enum LongKMerCodec implements KMerCodec&amp;lt;Long&amp;gt; {&lt;br /&gt;
	INSTANCE;&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public Function&amp;lt;byte[], Long&amp;gt; encoder() {&lt;br /&gt;
		return KMerUtils::toPackedLong;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public BiFunction&amp;lt;Long, Integer, byte[]&amp;gt; decoder() {&lt;br /&gt;
		return KMerUtils::unpackLong;&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Creating an instance of a MapKMerCount to return from your KMerCounters will need to use the appropriate codec.  For example, if you have a map of type &amp;lt;code&amp;gt;Map&amp;lt;Long, Integer&amp;gt;&amp;lt;/code&amp;gt; which you wish to wrap and return, your code would look something like this:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new MapKMerCount&amp;lt;&amp;gt;(k, map, LongKMerCodec.INSTANCE);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===IntArrayKMerCount===&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new IntArrayKMerCount(k, array);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===AtomicIntegerArrayKMerCount===&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new AtomicIntegerArrayKMerCount(k, atomicIntegerArray);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Group Warmup=&lt;br /&gt;
Complete the [[String_Map_K_Mer_Assignment|group warm up]] first.&lt;br /&gt;
&lt;br /&gt;
=Exercise=&lt;br /&gt;
{{ tip | Get ThresholdSlices working and then use it to balance all of your parallel KMerCounter implementations. }}&lt;br /&gt;
==ThresholdSlices==&lt;br /&gt;
===slicing===&lt;br /&gt;
{{CodeToImplement|ThresholdSlices|sliceKernel&amp;lt;br&amp;gt;createSlices&amp;lt;br&amp;gt;calculateReasonableThreshold|kmer.exercise.balance}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|&amp;lt;br/&amp;gt;The javadoc incorrectly specify that &amp;quot;Any sequence whose length is less than or equal to k should be omitted.&amp;quot;&amp;lt;br/&amp;gt;The javadoc should read &amp;quot;Any sequence whose length is less than k should be omitted.&amp;quot;}}&lt;br /&gt;
&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;
{{Sequential|public static List&amp;lt;Slice&amp;lt;byte[]&amp;gt;&amp;gt; createSlices(List&amp;lt;byte[]&amp;gt; sequences, int k, IntPredicate slicePredicate)}}&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;
&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;
&amp;lt;youtube&amp;gt;m56ee5spxiw&amp;lt;/youtube&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 threashold of slice length used in 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;
==LongConcurrentMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|LongConcurrentMapKMerCounter|parse|kmer.exercise.longconcurrentmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will also overlap greatly with everything you have done previously, but instead of using Strings or SequenceSlice, we will be using Longs to represent DNA data. In order to do this, refer to the KMerUtils class, more specifically the toPackedLong method which will convert a sequence of bytes into a Long for our purposes. Don't forget to balance the workload in this parallel implementation with your ThresholdSlices. Make sure to select the most appropriate parallel fork loop. &lt;br /&gt;
&lt;br /&gt;
{{Warning|Be sure to utilize the &amp;lt;code&amp;gt;Supplier&amp;lt;ConcurrentMap&amp;lt;Long, Integer&amp;gt;&amp;gt; concurrentMapFactory&amp;lt;/code&amp;gt; instance variable to create your ConcurrentMap.}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&amp;lt;youtube&amp;gt;d_Ze-YQ3Thc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Maps in Java often have a way to specify an initialCapacity to the constructor.  When used well, this parameter can give a hint as to how many Entries the Map should be expecting.  &lt;br /&gt;
&lt;br /&gt;
KMerUtils has methods which will calculate what you need to compute this value.  calculatePossibleKmers returns 4^k and calculateSumOfAllKmers returns n-k+1.  Think about how each method might be used to calculate an appropriate initial capacity.&lt;br /&gt;
&lt;br /&gt;
Some questions to ask yourself:&lt;br /&gt;
&lt;br /&gt;
# How many entries should the ConcurrentMap expect for a chromosome a million in length if k is 30?&lt;br /&gt;
# How many entries should the ConcurrentMap expect for a chromosome a million in length if k is 1?&lt;br /&gt;
&lt;br /&gt;
In order to test LongConcurrentMapKMerCounter with both java.util.ConcurrentHashMap&amp;lt;K,V&amp;gt; and your ConcurrentBucketHashMap&amp;lt;K,V&amp;gt; implementation, we pass a concurrentMapFactory to LongConcurrentMapKMerCounter's constructor.  You should use this factory to create your instance of ConcurrentMap via the apply method.  The Optional&amp;lt;Integer&amp;gt; parameter to this apply method is the initialCapacity.&lt;br /&gt;
&lt;br /&gt;
Note: anyone feeling the weight of a pandemic should feel free to pass Optional.empty() to the apply method.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IntArrayKMerCounter==&lt;br /&gt;
{{CodeToImplement|IntArrayKMerCounter|parse|kmer.lab.intarray}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
Like the String HashMap implementation, this implementation should be completed sequentially. However, unlike the previous implementations, we will be using an array instead of a map. This means that you must keep in mind what size to make the array in order to instantiate it. KMerUtils has a method which will calculate this number for you, but think about which one to use, why you would use it, and how that would differ from simply calculating n - k + 1 yourself. Think of using the int array as similar to using the map, but with ints as the key/value pairs. Use KMerUtils.toPackedInt() in order to convert the sequence into an int for our purposes.&lt;br /&gt;
&lt;br /&gt;
Hint: calculatePossibleKmers and calculateSumOfAllKmers are distinct and take different input parameters. calculateSumOfAllKmers performs the n - k + 1 calculation on a list of arrays of sequences and sums the total. It calculate the total number of Kmers in the list of sequences. calculatePossibleKmers only takes the length of the k-mer (k) as the input parameter, meaning it will calculate all possibilities independent of the sequences. Thus, the n - k + 1 calculation would yield a value ranging from 0 (inclusive) to the result of calculatePossibleKmers (exclusive).&lt;br /&gt;
&lt;br /&gt;
==AtomicIntegerArrayKMerCounter==&lt;br /&gt;
{{CodeToImplement|AtomicIntegerArrayKMerCounter|parse|kmer.lab.atomicintegerarray}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This implementation is simply the parallel equivalent of the int array implementation. To do so, you will be making use of Java’s atomic version of an array: an AtomicIntegerArray. Like before, you will be need to complete the parse method.  Don't forget to balance the workload in this parallel implementation with your ThresholdSlices.  Make sure to select the most appropriate parallel fork loop.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
==ConcurrentBucketHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|ConcurrentBucketHashMapKMerCounter|parse|kmer.lab.concurrentbuckethashmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will go a step beyond the previous implementations and divide entries into 1024 buckets. Although a different number of buckets can be used, we will simply stick to 1024. Every time an entry is added to the bucket dictionary, it will create a hash based on the entry's key and place the entry into one of the 1024 buckets. Because we would only need to search one bucket for an entry based on a key's hash, this mainly serves the purpose of cutting down on search time. The compute method of the ConcurrentBucketHashMap should automatically compute the correct bucket for the entry, so this implementation should closely resemble the previous ConcurrentHashMap implementations. When instantiating the ConcurrentBucketHashMap, we recommend making the key/value pairs a Long and an Integer.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=Extra Credit=&lt;br /&gt;
==Open-ended K-mer Counter==&lt;br /&gt;
&lt;br /&gt;
This implementation is worth up to 4.5 extra credit points.&lt;br /&gt;
&lt;br /&gt;
It is intended as an opportunity to build a high-performing, thread-safe data structure.&lt;br /&gt;
&lt;br /&gt;
One possible path was outlined at a high level in class.  However, students are encouraged to think of alternate solutions.&lt;br /&gt;
&lt;br /&gt;
We have explicitly disallowed implementations akin to our matrix map reduce assignment, since we have already done the &amp;quot;give everyone their own data and combine in post&amp;quot; pattern to death.&lt;br /&gt;
&lt;br /&gt;
Partial credit is possible for solutions which demonstrate significant ingenuity.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|__KMerTestSuite|kmer.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Research=&lt;br /&gt;
[https://en.wikipedia.org/wiki/K-mer wikipedia article on k-mers]&lt;br /&gt;
&lt;br /&gt;
paper: [https://academic.oup.com/bioinformatics/article-lookup/doi/10.1093/bioinformatics/btr011 A fast, lock-free approach for efficient parallel counting of occurrences of k-mers] (Jellyfish)&lt;br /&gt;
&lt;br /&gt;
paper: [https://peerj.com/articles/cs-94/ Multiple comparative metagenomics using multiset k-mer counting]&lt;br /&gt;
&lt;br /&gt;
[http://homolog.us/Tutorials/index.php?p=3.7&amp;amp;s=2 Collection of approaches to k-mer counting]&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform Burrows–Wheeler transform]&lt;br /&gt;
&lt;br /&gt;
[http://biology.stackexchange.com/questions/45016/what-is-the-biological-significance-of-k-mer-counting Significance of k-mer Counting]&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Concurrent_Hash_Table_Assignment&amp;diff=3682</id>
		<title>Concurrent Hash Table Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Concurrent_Hash_Table_Assignment&amp;diff=3682"/>
		<updated>2022-04-10T19:54:26Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* getEntry(bucket, key) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
=Motivation=&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;
For this exercise you will create a thread safe data structure.  You have previously created a [[Iterator,_Collection,_and_Map#BucketsHashMap|non-thread safe Map]].  Now, with the help of multiple  [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html ReentrantReadWriteLocks] you will create a thread safe Map that both deals with contention and allows simultaneous gets. &lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ConcurrentBucketHashMap|constructor&amp;lt;br&amp;gt;getIndex&amp;lt;br&amp;gt;getBucket&amp;lt;br&amp;gt;getLock&amp;lt;br&amp;gt;getEntry&amp;lt;br&amp;gt;get&amp;lt;br&amp;gt;put&amp;lt;br&amp;gt;compute|kmer.lab.concurrentbuckethashmap}}&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==constructor==&lt;br /&gt;
{{Sequential|public ConcurrentBucketHashMap(int bucketCount)}}&lt;br /&gt;
&lt;br /&gt;
bucketCount can be treated as a hint of how many entries the Map can expect to receive.&lt;br /&gt;
&lt;br /&gt;
==private support methods==&lt;br /&gt;
{{Tip|Use HashUtils.toIndex(key,N)}}&lt;br /&gt;
&lt;br /&gt;
===getBucket(key)===&lt;br /&gt;
{{Sequential|private List&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; getBucket(Object key)}}&lt;br /&gt;
&lt;br /&gt;
===getLock(key)===&lt;br /&gt;
{{Sequential|private ReadWriteLock getLock(Object key)}}&lt;br /&gt;
&lt;br /&gt;
===getEntry(bucket, key)===&lt;br /&gt;
{{Sequential|private static &amp;lt;K, V&amp;gt; Optional&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; getEntry(List&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; bucket, Object key)}}&lt;br /&gt;
&lt;br /&gt;
Refer to [https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html Optional&amp;lt;T&amp;gt; Class] documentation.&lt;br /&gt;
&lt;br /&gt;
==public methods==&lt;br /&gt;
&lt;br /&gt;
Be sure to make each of these methods thread safe by acquiring the appropriate (read or write) lock for the appropriate bucket.&lt;br /&gt;
&lt;br /&gt;
Which method, [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock()] or [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#tryLock-- tryLock()], is the appropriate one to use for creating a thread safe data structure such as this?&lt;br /&gt;
&amp;lt;spoiler show=&amp;quot;spoiler&amp;quot; hide=&amp;quot;spoiler&amp;quot;&amp;gt;[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock()]&amp;lt;/spoiler&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever you acquire a lock, but sure to leverage [https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html try finally] and [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#unlock-- unlock()] the lock within the finally block.&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|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- spec]&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|public V put(K key, V value)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#put-K-V- spec]&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|public V compute(K key, BiFunction&amp;lt;? super K, ? super V, ? extends V&amp;gt; remappingFunction)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentMap.html#compute-K-java.util.function.BiFunction- spec]&lt;br /&gt;
&lt;br /&gt;
==advice==&lt;br /&gt;
&lt;br /&gt;
{{ warning | Do '''NOT''' call get() from compute().  It will result in a nonintuitive error.  getEntry(bucket, key) gives you everything you need. }}&lt;br /&gt;
&lt;br /&gt;
{{ warning | Do '''NOT''' call put() from compute() either.  Just do the work within the compute method. }}&lt;br /&gt;
&lt;br /&gt;
{{ tip | Be sure to make your ConcurrentBucketHashMap thread-safe.  If any code is going to access shared mutable data, it needs to do so in a thread-safe manner. }}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Concurrent_Hash_Table_Assignment&amp;diff=3681</id>
		<title>Concurrent Hash Table Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Concurrent_Hash_Table_Assignment&amp;diff=3681"/>
		<updated>2022-04-10T05:19:06Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* constructor */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
=Motivation=&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;
For this exercise you will create a thread safe data structure.  You have previously created a [[Iterator,_Collection,_and_Map#BucketsHashMap|non-thread safe Map]].  Now, with the help of multiple  [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html ReentrantReadWriteLocks] you will create a thread safe Map that both deals with contention and allows simultaneous gets. &lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ConcurrentBucketHashMap|constructor&amp;lt;br&amp;gt;getIndex&amp;lt;br&amp;gt;getBucket&amp;lt;br&amp;gt;getLock&amp;lt;br&amp;gt;getEntry&amp;lt;br&amp;gt;get&amp;lt;br&amp;gt;put&amp;lt;br&amp;gt;compute|kmer.lab.concurrentbuckethashmap}}&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==constructor==&lt;br /&gt;
{{Sequential|public ConcurrentBucketHashMap(int bucketCount)}}&lt;br /&gt;
&lt;br /&gt;
bucketCount can be treated as a hint of how many entries the Map can expect to receive.&lt;br /&gt;
&lt;br /&gt;
==private support methods==&lt;br /&gt;
{{Tip|Use HashUtils.toIndex(key,N)}}&lt;br /&gt;
&lt;br /&gt;
===getBucket(key)===&lt;br /&gt;
{{Sequential|private List&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; getBucket(Object key)}}&lt;br /&gt;
&lt;br /&gt;
===getLock(key)===&lt;br /&gt;
{{Sequential|private ReadWriteLock getLock(Object key)}}&lt;br /&gt;
&lt;br /&gt;
===getEntry(bucket, key)===&lt;br /&gt;
{{Sequential|private static &amp;lt;K, V&amp;gt; Optional&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; getEntry(List&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; bucket, Object key)}}&lt;br /&gt;
&lt;br /&gt;
==public methods==&lt;br /&gt;
&lt;br /&gt;
Be sure to make each of these methods thread safe by acquiring the appropriate (read or write) lock for the appropriate bucket.&lt;br /&gt;
&lt;br /&gt;
Which method, [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock()] or [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#tryLock-- tryLock()], is the appropriate one to use for creating a thread safe data structure such as this?&lt;br /&gt;
&amp;lt;spoiler show=&amp;quot;spoiler&amp;quot; hide=&amp;quot;spoiler&amp;quot;&amp;gt;[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock()]&amp;lt;/spoiler&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever you acquire a lock, but sure to leverage [https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html try finally] and [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#unlock-- unlock()] the lock within the finally block.&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|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- spec]&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|public V put(K key, V value)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#put-K-V- spec]&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|public V compute(K key, BiFunction&amp;lt;? super K, ? super V, ? extends V&amp;gt; remappingFunction)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentMap.html#compute-K-java.util.function.BiFunction- spec]&lt;br /&gt;
&lt;br /&gt;
==advice==&lt;br /&gt;
&lt;br /&gt;
{{ warning | Do '''NOT''' call get() from compute().  It will result in a nonintuitive error.  getEntry(bucket, key) gives you everything you need. }}&lt;br /&gt;
&lt;br /&gt;
{{ warning | Do '''NOT''' call put() from compute() either.  Just do the work within the compute method. }}&lt;br /&gt;
&lt;br /&gt;
{{ tip | Be sure to make your ConcurrentBucketHashMap thread-safe.  If any code is going to access shared mutable data, it needs to do so in a thread-safe manner. }}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Counting_Assignment&amp;diff=3671</id>
		<title>K Mer Counting Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Counting_Assignment&amp;diff=3671"/>
		<updated>2022-04-07T03:05:27Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* ThresholdSlices */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;credit for this assignment: John Gibson and Dennis Cosgrove&lt;br /&gt;
=Motivation=&lt;br /&gt;
K-mer counting is a real world application which provides the opportunity to build thread safe data structures which alleviate contention.&lt;br /&gt;
&lt;br /&gt;
Pulling real data from the National Institutes of Health forces us to address balancing our work.&lt;br /&gt;
&lt;br /&gt;
Finally, k-mer counting provides opportunities for performance engineering.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
The term k-mer refers to a substring of length k and k-mer counting refers to the process of finding the number of occurrences of a k-mer in a given string. This computational problem has many real-world applications, the most notable being in the field of computational genomics. In this assignment, you will design a program that will ultimately take in a human X-chromosome and count the number of k-mers in the string of DNA. You will be making use of everything you have learned thus far, including atomic variables. It is useful to know that given a string of length n, the amount of possible k-mers is equal to '''n - k + 1'''.&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;_oyWzImAx1w&amp;lt;/youtube&amp;gt;&lt;br /&gt;
=Demo=&lt;br /&gt;
{{CodeToInvestigate|OutputKmer|main|kmer.output}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;tFfa7DWLJ_U&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This demo video shows the big picture of KMer Counting. It included example usage of calculateSumOfAllKMers and calculatePossibleKMers method in &amp;lt;code&amp;gt;KMerUtils&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
==Runtime==&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#availableProcessors-- Runtime.getRuntime().availableProcessors()]&lt;br /&gt;
&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;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicIntegerArray.html AtomicIntegerArray].&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html ReentrantReadWriteLock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html#readLock-- readLock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html#writeLock-- writeLock]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html Lock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- unlock]&lt;br /&gt;
&lt;br /&gt;
==KMerUtils==&lt;br /&gt;
The following methods should be useful as you build the assignment.&lt;br /&gt;
&lt;br /&gt;
Check out the Javadocs either [http://www.cse.wustl.edu/~cosgroved/courses/cse231/f17/apidocs/kmer/core/KMerUtils.html on the web], or in the comments in KMerUtils.java for further details on how they work.&lt;br /&gt;
&lt;br /&gt;
===long calculatePossibleKMers(int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long calculatePossibleKMers(int kMerLength) {&lt;br /&gt;
	int bitsPerBase = 2;&lt;br /&gt;
	int bitsPerKMer = kMerLength * bitsPerBase;&lt;br /&gt;
	long possibleKMers = 1L &amp;lt;&amp;lt; bitsPerKMer;&lt;br /&gt;
	return possibleKMers;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toArrayLength(long possibleKMers)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toArrayLength(long possibleKMers) {&lt;br /&gt;
	// maximum array length is VM dependent, but must be indexed by an int.&lt;br /&gt;
	// reportedly some vms reserve some header words (see ArrayList) which puts its&lt;br /&gt;
	// limit as just below 2^31.&lt;br /&gt;
	// we will choose 2^30 (inclusive) as the maximum since it will hold the largest&lt;br /&gt;
	// potential k-mer (4^15)&lt;br /&gt;
	final long MAX_K_MER_ARRAY_LENGTH_INCLUSIVE = 1 &amp;lt;&amp;lt; 30;&lt;br /&gt;
	if (possibleKMers &amp;lt;= MAX_K_MER_ARRAY_LENGTH_INCLUSIVE) {&lt;br /&gt;
		return (int) possibleKMers;&lt;br /&gt;
	} else {&lt;br /&gt;
		throw new IllegalArgumentException(possibleKMers + &amp;quot; &amp;gt; &amp;quot; + MAX_K_MER_ARRAY_LENGTH_INCLUSIVE);&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int calculateSumOfAllKMers(List&amp;lt;byte[]&amp;gt; sequences, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int calculateSumOfAllKMers(List&amp;lt;byte[]&amp;gt; sequences, int kMerLength) {&lt;br /&gt;
	int sum = 0;&lt;br /&gt;
	for (byte[] sequence : sequences) {&lt;br /&gt;
		int i = (sequence.length - kMerLength + 1);&lt;br /&gt;
		if( i &amp;gt; 0 ) {&lt;br /&gt;
			sum += i;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return sum;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toPackedInt(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toPackedInt(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	int result = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		switch (sequence[offset + i]) {&lt;br /&gt;
		// case A:&lt;br /&gt;
		// result |= (A_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
		// break;&lt;br /&gt;
		case T:&lt;br /&gt;
			result |= (T_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case C:&lt;br /&gt;
			result |= (C_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case G:&lt;br /&gt;
			result |= (G_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toPackedInt(byte[] kMer)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toPackedInt(byte[] kMer) {&lt;br /&gt;
	return toPackedInt(kMer, 0, kMer.length);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===byte[] unpackInt(int kMer, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static byte[] unpackInt(int kMer, int kMerLength) {&lt;br /&gt;
	byte[] result = new byte[kMerLength];&lt;br /&gt;
&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		int v = (kMer &amp;gt;&amp;gt; (i + i)) &amp;amp; 0x3;&lt;br /&gt;
		switch (v) {&lt;br /&gt;
		case A_MASK_INT:&lt;br /&gt;
			result[i] = A;&lt;br /&gt;
			break;&lt;br /&gt;
		case T_MASK_INT:&lt;br /&gt;
			result[i] = T;&lt;br /&gt;
			break;&lt;br /&gt;
		case C_MASK_INT:&lt;br /&gt;
			result[i] = C;&lt;br /&gt;
			break;&lt;br /&gt;
		case G_MASK_INT:&lt;br /&gt;
			result[i] = G;&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===long toPackedLong(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long toPackedLong(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	long result = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		switch (sequence[offset + i]) {&lt;br /&gt;
		// case A:&lt;br /&gt;
		// result |= (A_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
		// break;&lt;br /&gt;
		case T:&lt;br /&gt;
			result |= (T_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case C:&lt;br /&gt;
			result |= (C_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case G:&lt;br /&gt;
			result |= (G_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===long toPackedLong(byte[] sequence)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long toPackedLong(byte[] sequence) {&lt;br /&gt;
	return toPackedLong(sequence, 0, sequence.length);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===byte[] unpackLong(long kMer, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static byte[] unpackLong(long kMer, int kMerLength) {&lt;br /&gt;
	byte[] result = new byte[kMerLength];&lt;br /&gt;
&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		long v = (kMer &amp;gt;&amp;gt; (i + i)) &amp;amp; 0x3;&lt;br /&gt;
		switch ((int) v) {&lt;br /&gt;
		case A_MASK_INT:&lt;br /&gt;
			result[i] = A;&lt;br /&gt;
			break;&lt;br /&gt;
		case T_MASK_INT:&lt;br /&gt;
			result[i] = T;&lt;br /&gt;
			break;&lt;br /&gt;
		case C_MASK_INT:&lt;br /&gt;
			result[i] = C;&lt;br /&gt;
			break;&lt;br /&gt;
		case G_MASK_INT:&lt;br /&gt;
			result[i] = G;&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==KMerCounts==&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/KMerCount.html interface KMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/map/MapKMerCount.html class MapKMerCount&amp;lt;T&amp;gt;]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/array/IntArrayKMerCount.html class IntArrayKMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/array/AtomicIntegerArrayKMerCount.html class AtomicIntegerArrayKMerCount]&lt;br /&gt;
&lt;br /&gt;
===MapKMerCount===&lt;br /&gt;
In order to provide a KMerCount that works with different k-mer type representations we have added an interface KMerCodec which provides functions to go to an from the byte[] representation:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public interface KMerCodec&amp;lt;T&amp;gt; {&lt;br /&gt;
	BiFunction&amp;lt;T, Integer, byte[]&amp;gt; decoder();&lt;br /&gt;
&lt;br /&gt;
	Function&amp;lt;byte[], T&amp;gt; encoder();&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As an example the instance for long -&amp;gt; byte[] and back simply utilizes the pack and unpack methods in KMerUtils:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public enum LongKMerCodec implements KMerCodec&amp;lt;Long&amp;gt; {&lt;br /&gt;
	INSTANCE;&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public Function&amp;lt;byte[], Long&amp;gt; encoder() {&lt;br /&gt;
		return KMerUtils::toPackedLong;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public BiFunction&amp;lt;Long, Integer, byte[]&amp;gt; decoder() {&lt;br /&gt;
		return KMerUtils::unpackLong;&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Creating an instance of a MapKMerCount to return from your KMerCounters will need to use the appropriate codec.  For example, if you have a map of type &amp;lt;code&amp;gt;Map&amp;lt;Long, Integer&amp;gt;&amp;lt;/code&amp;gt; which you wish to wrap and return, your code would look something like this:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new MapKMerCount&amp;lt;&amp;gt;(k, map, LongKMerCodec.INSTANCE);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===IntArrayKMerCount===&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new IntArrayKMerCount(k, array);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===AtomicIntegerArrayKMerCount===&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new AtomicIntegerArrayKMerCount(k, atomicIntegerArray);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Group Warmup=&lt;br /&gt;
Complete the [[String_Map_K_Mer_Assignment|group warm up]] first.&lt;br /&gt;
&lt;br /&gt;
=Lab=&lt;br /&gt;
{{ tip | Get ThresholdSlices working and then use it to balance all of your parallel KMerCounter implementations. }}&lt;br /&gt;
==ThresholdSlices==&lt;br /&gt;
===slicing===&lt;br /&gt;
{{CodeToImplement|ThresholdSlices|sliceKernel&amp;lt;br&amp;gt;createSlices&amp;lt;br&amp;gt;calculateReasonableThreshold|kmer.exercise.balance}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|&amp;lt;br/&amp;gt;The javadoc incorrectly specify that &amp;quot;Any sequence whose length is less than or equal to k should be omitted.&amp;quot;&amp;lt;br/&amp;gt;The javadoc should read &amp;quot;Any sequence whose length is less than k should be omitted.&amp;quot;}}&lt;br /&gt;
&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;
{{Sequential|public static List&amp;lt;Slice&amp;lt;byte[]&amp;gt;&amp;gt; createSlices(List&amp;lt;byte[]&amp;gt; sequences, int k, IntPredicate slicePredicate)}}&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;
&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;
&amp;lt;youtube&amp;gt;m56ee5spxiw&amp;lt;/youtube&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 threashold of slice length used in 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;
==LongConcurrentMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|LongConcurrentMapKMerCounter|parse|kmer.exercise.longconcurrentmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will also overlap greatly with everything you have done previously, but instead of using Strings or SequenceSlice, we will be using Longs to represent DNA data. In order to do this, refer to the KMerUtils class, more specifically the toPackedLong method which will convert a sequence of bytes into a Long for our purposes. &lt;br /&gt;
&lt;br /&gt;
{{Warning|Be sure to utilize the &amp;lt;code&amp;gt;Supplier&amp;lt;ConcurrentMap&amp;lt;Long, Integer&amp;gt;&amp;gt; concurrentMapFactory&amp;lt;/code&amp;gt; instance variable to create your ConcurrentMap.}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&amp;lt;youtube&amp;gt;d_Ze-YQ3Thc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Maps in Java often have a way to specify an initialCapacity to the constructor.  When used well, this parameter can give a hint as to how many Entries the Map should be expecting.  &lt;br /&gt;
&lt;br /&gt;
KMerUtils has methods which will calculate what you need to compute this value.  calculatePossibleKmers returns 4^k and calculateSumOfAllKmers returns n-k+1.  Think about how each method might be used to calculate an appropriate initial capacity.&lt;br /&gt;
&lt;br /&gt;
Some questions to ask yourself:&lt;br /&gt;
&lt;br /&gt;
# How many entries should the ConcurrentMap expect for a chromosome a million in length if k is 30?&lt;br /&gt;
# How many entries should the ConcurrentMap expect for a chromosome a million in length if k is 1?&lt;br /&gt;
&lt;br /&gt;
In order to test LongConcurrentMapKMerCounter with both java.util.ConcurrentHashMap&amp;lt;K,V&amp;gt; and your ConcurrentBucketHashMap&amp;lt;K,V&amp;gt; implementation, we pass a concurrentMapFactory to LongConcurrentMapKMerCounter's constructor.  You should use this factory to create your instance of ConcurrentMap via the apply method.  The Optional&amp;lt;Integer&amp;gt; parameter to this apply method is the initialCapacity.&lt;br /&gt;
&lt;br /&gt;
Note: anyone feeling the weight of a pandemic should feel free to pass Optional.empty() to the apply method.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IntArrayKMerCounter==&lt;br /&gt;
{{CodeToImplement|IntArrayKMerCounter|parse|kmer.lab.intarray}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
Like the String HashMap implementation, this implementation should be completed sequentially. However, unlike the previous implementations, we will be using an array instead of a map. This means that you must keep in mind what size to make the array in order to instantiate it. KMerUtils has a method which will calculate this number for you, but think about which one to use, why you would use it, and how that would differ from simply calculating n - k + 1 yourself. Think of using the int array as similar to using the map, but with ints as the key/value pairs. Use KMerUtils.toPackedInt() in order to convert the sequence into an int for our purposes.&lt;br /&gt;
&lt;br /&gt;
Hint: calculatePossibleKmers and calculateSumOfAllKmers are distinct and take different input parameters. calculateSumOfAllKmers performs the n - k + 1 calculation on a list of arrays of sequences and sums the total. It calculate the total number of Kmers in the list of sequences. calculatePossibleKmers only takes the length of the k-mer (k) as the input parameter, meaning it will calculate all possibilities independent of the sequences. Thus, the n - k + 1 calculation would yield a value ranging from 0 (inclusive) to the result of calculatePossibleKmers (exclusive).&lt;br /&gt;
&lt;br /&gt;
==AtomicIntegerArrayKMerCounter==&lt;br /&gt;
{{CodeToImplement|AtomicIntegerArrayKMerCounter|parse|kmer.lab.atomicintegerarray}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This implementation is simply the parallel equivalent of the int array implementation. To do so, you will be making use of Java’s atomic version of an array: an AtomicIntegerArray. Like before, you will be need to complete the parse method.  Don't forget to balance the workload in this parallel implementation with your ThresholdSlices.  Make sure to select the most appropriate parallel fork loop.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
==ConcurrentBucketHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|ConcurrentBucketHashMapKMerCounter|parse|kmer.lab.concurrentbuckethashmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will go a step beyond the previous implementations and divide entries into 1024 buckets. Although a different number of buckets can be used, we will simply stick to 1024. Every time an entry is added to the bucket dictionary, it will create a hash based on the entry's key and place the entry into one of the 1024 buckets. Because we would only need to search one bucket for an entry based on a key's hash, this mainly serves the purpose of cutting down on search time. The compute method of the ConcurrentBucketHashMap should automatically compute the correct bucket for the entry, so this implementation should closely resemble the previous ConcurrentHashMap implementations. When instantiating the ConcurrentBucketHashMap, we recommend making the key/value pairs a Long and an Integer.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==ConcurrentBucketHashMap==&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;
For this section of the assignment you will create a thread safe data structure.  You have previously created a [[Iterator,_Collection,_and_Map#BucketsHashMap|non-thread safe Map]].  Now, with the help of multiple  [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html ReentrantReadWriteLocks] you will create a thread safe Map that both deals with contention and allows simultaneous gets. &lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ConcurrentBucketHashMap|constructor&amp;lt;br&amp;gt;getIndex&amp;lt;br&amp;gt;getBucket&amp;lt;br&amp;gt;getLock&amp;lt;br&amp;gt;getEntry&amp;lt;br&amp;gt;get&amp;lt;br&amp;gt;put&amp;lt;br&amp;gt;compute|kmer.lab.concurrentbuckethashmap}}&lt;br /&gt;
&lt;br /&gt;
===constructor===&lt;br /&gt;
{{Sequential|public ConcurrentBucketHashMap(int initialCapacity)}}&lt;br /&gt;
&lt;br /&gt;
initialCapacity can be treated as a hint of how many entries the Map can expect to receive.&lt;br /&gt;
&lt;br /&gt;
===private support methods===&lt;br /&gt;
{{Tip|Use HashUtils.toIndex(key,N)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private List&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; getBucket(Object key)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private ReadWriteLock getLock(Object key)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private static &amp;lt;K, V&amp;gt; Optional&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; getEntry(List&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; bucket, Object key)}}&lt;br /&gt;
&lt;br /&gt;
===public methods===&lt;br /&gt;
&lt;br /&gt;
Be sure to make each of these methods thread safe by acquiring the appropriate (read or write) lock for the appropriate bucket.&lt;br /&gt;
&lt;br /&gt;
Which method, [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock()] or [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#tryLock-- tryLock()], is the appropriate one to use for creating a thread safe data structure such as this?&lt;br /&gt;
&amp;lt;spoiler show=&amp;quot;spoiler&amp;quot; hide=&amp;quot;spoiler&amp;quot;&amp;gt;[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock()]&amp;lt;/spoiler&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever you acquire a lock, but sure to leverage [https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html try finally] and [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#unlock-- unlock()] the lock within the finally block.&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|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- spec]&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|public V put(K key, V value)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#put-K-V- spec]&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|public V compute(K key, BiFunction&amp;lt;? super K, ? super V, ? extends V&amp;gt; remappingFunction)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentMap.html#compute-K-java.util.function.BiFunction- spec]&lt;br /&gt;
&lt;br /&gt;
===advice===&lt;br /&gt;
&lt;br /&gt;
{{ warning | Do '''NOT''' call get() from compute().  It will result in a nonintuitive error.  getEntry(bucket, key) gives you everything you need. }}&lt;br /&gt;
&lt;br /&gt;
{{ warning | Do '''NOT''' call put() from compute() either.  Just do the work within the compute method. }}&lt;br /&gt;
&lt;br /&gt;
{{ tip | Be sure to make your ConcurrentBucketHashMap thread-safe.  If any code is going to access shared mutable data, it needs to do so in a thread-safe manner. }}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
A simple example of how to use the many incantations of isolated:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;@ThreadSafe&lt;br /&gt;
public class IsolatedInteger {&lt;br /&gt;
    private int value;&lt;br /&gt;
&lt;br /&gt;
    public IsolatedInteger(int value) {&lt;br /&gt;
        this.value = value;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public int getValue() {&lt;br /&gt;
        return isolatedWithReturn(objectBased(readMode(this)), () -&amp;gt; {&lt;br /&gt;
            return value;&lt;br /&gt;
        });&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public void increment() {&lt;br /&gt;
        isolated(objectBased(this), () -&amp;gt; {&lt;br /&gt;
            value++;&lt;br /&gt;
        });&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=Extra Credit=&lt;br /&gt;
==Open-ended K-mer Counter==&lt;br /&gt;
&lt;br /&gt;
This implementation is worth up to 4.5 extra credit points.&lt;br /&gt;
&lt;br /&gt;
It is intended as an opportunity to build a high-performing, thread-safe data structure.&lt;br /&gt;
&lt;br /&gt;
One possible path was outlined at a high level in class.  However, students are encouraged to think of alternate solutions.&lt;br /&gt;
&lt;br /&gt;
We have explicitly disallowed implementations akin to our matrix map reduce assignment, since we have already done the &amp;quot;give everyone their own data and combine in post&amp;quot; pattern to death.&lt;br /&gt;
&lt;br /&gt;
Partial credit is possible for solutions which demonstrate significant ingenuity.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|_KMerTestSuite|kmer.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Rubric=&lt;br /&gt;
&lt;br /&gt;
Total points: 100&lt;br /&gt;
&lt;br /&gt;
* Correct ThresholdSlices (10)&lt;br /&gt;
* Correct and Parallel Long ConcurrentHashMap (25)&lt;br /&gt;
* Correct Int Array (15)&lt;br /&gt;
* Correct and Parallel AtomicIntegerArray (15)&lt;br /&gt;
* Correct and ThreadSafe ConcurrentBucketHashMap implementation (25)&lt;br /&gt;
* Clarity and efficiency (10)&lt;br /&gt;
&lt;br /&gt;
=Research=&lt;br /&gt;
[https://en.wikipedia.org/wiki/K-mer wikipedia article on k-mers]&lt;br /&gt;
&lt;br /&gt;
paper: [https://academic.oup.com/bioinformatics/article-lookup/doi/10.1093/bioinformatics/btr011 A fast, lock-free approach for efficient parallel counting of occurrences of k-mers] (Jellyfish)&lt;br /&gt;
&lt;br /&gt;
paper: [https://peerj.com/articles/cs-94/ Multiple comparative metagenomics using multiset k-mer counting]&lt;br /&gt;
&lt;br /&gt;
[http://homolog.us/Tutorials/index.php?p=3.7&amp;amp;s=2 Collection of approaches to k-mer counting]&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform Burrows–Wheeler transform]&lt;br /&gt;
&lt;br /&gt;
[http://biology.stackexchange.com/questions/45016/what-is-the-biological-significance-of-k-mer-counting Significance of k-mer Counting]&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Counting_Assignment&amp;diff=3670</id>
		<title>K Mer Counting Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Counting_Assignment&amp;diff=3670"/>
		<updated>2022-04-07T02:48:52Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* IntArrayKMerCounter */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;credit for this assignment: John Gibson and Dennis Cosgrove&lt;br /&gt;
=Motivation=&lt;br /&gt;
K-mer counting is a real world application which provides the opportunity to build thread safe data structures which alleviate contention.&lt;br /&gt;
&lt;br /&gt;
Pulling real data from the National Institutes of Health forces us to address balancing our work.&lt;br /&gt;
&lt;br /&gt;
Finally, k-mer counting provides opportunities for performance engineering.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
The term k-mer refers to a substring of length k and k-mer counting refers to the process of finding the number of occurrences of a k-mer in a given string. This computational problem has many real-world applications, the most notable being in the field of computational genomics. In this assignment, you will design a program that will ultimately take in a human X-chromosome and count the number of k-mers in the string of DNA. You will be making use of everything you have learned thus far, including atomic variables. It is useful to know that given a string of length n, the amount of possible k-mers is equal to '''n - k + 1'''.&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;_oyWzImAx1w&amp;lt;/youtube&amp;gt;&lt;br /&gt;
=Demo=&lt;br /&gt;
{{CodeToInvestigate|OutputKmer|main|kmer.output}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;tFfa7DWLJ_U&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This demo video shows the big picture of KMer Counting. It included example usage of calculateSumOfAllKMers and calculatePossibleKMers method in &amp;lt;code&amp;gt;KMerUtils&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
==Runtime==&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#availableProcessors-- Runtime.getRuntime().availableProcessors()]&lt;br /&gt;
&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;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicIntegerArray.html AtomicIntegerArray].&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html ReentrantReadWriteLock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html#readLock-- readLock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html#writeLock-- writeLock]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html Lock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- unlock]&lt;br /&gt;
&lt;br /&gt;
==KMerUtils==&lt;br /&gt;
The following methods should be useful as you build the assignment.&lt;br /&gt;
&lt;br /&gt;
Check out the Javadocs either [http://www.cse.wustl.edu/~cosgroved/courses/cse231/f17/apidocs/kmer/core/KMerUtils.html on the web], or in the comments in KMerUtils.java for further details on how they work.&lt;br /&gt;
&lt;br /&gt;
===long calculatePossibleKMers(int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long calculatePossibleKMers(int kMerLength) {&lt;br /&gt;
	int bitsPerBase = 2;&lt;br /&gt;
	int bitsPerKMer = kMerLength * bitsPerBase;&lt;br /&gt;
	long possibleKMers = 1L &amp;lt;&amp;lt; bitsPerKMer;&lt;br /&gt;
	return possibleKMers;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toArrayLength(long possibleKMers)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toArrayLength(long possibleKMers) {&lt;br /&gt;
	// maximum array length is VM dependent, but must be indexed by an int.&lt;br /&gt;
	// reportedly some vms reserve some header words (see ArrayList) which puts its&lt;br /&gt;
	// limit as just below 2^31.&lt;br /&gt;
	// we will choose 2^30 (inclusive) as the maximum since it will hold the largest&lt;br /&gt;
	// potential k-mer (4^15)&lt;br /&gt;
	final long MAX_K_MER_ARRAY_LENGTH_INCLUSIVE = 1 &amp;lt;&amp;lt; 30;&lt;br /&gt;
	if (possibleKMers &amp;lt;= MAX_K_MER_ARRAY_LENGTH_INCLUSIVE) {&lt;br /&gt;
		return (int) possibleKMers;&lt;br /&gt;
	} else {&lt;br /&gt;
		throw new IllegalArgumentException(possibleKMers + &amp;quot; &amp;gt; &amp;quot; + MAX_K_MER_ARRAY_LENGTH_INCLUSIVE);&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int calculateSumOfAllKMers(List&amp;lt;byte[]&amp;gt; sequences, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int calculateSumOfAllKMers(List&amp;lt;byte[]&amp;gt; sequences, int kMerLength) {&lt;br /&gt;
	int sum = 0;&lt;br /&gt;
	for (byte[] sequence : sequences) {&lt;br /&gt;
		int i = (sequence.length - kMerLength + 1);&lt;br /&gt;
		if( i &amp;gt; 0 ) {&lt;br /&gt;
			sum += i;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return sum;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toPackedInt(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toPackedInt(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	int result = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		switch (sequence[offset + i]) {&lt;br /&gt;
		// case A:&lt;br /&gt;
		// result |= (A_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
		// break;&lt;br /&gt;
		case T:&lt;br /&gt;
			result |= (T_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case C:&lt;br /&gt;
			result |= (C_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case G:&lt;br /&gt;
			result |= (G_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toPackedInt(byte[] kMer)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toPackedInt(byte[] kMer) {&lt;br /&gt;
	return toPackedInt(kMer, 0, kMer.length);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===byte[] unpackInt(int kMer, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static byte[] unpackInt(int kMer, int kMerLength) {&lt;br /&gt;
	byte[] result = new byte[kMerLength];&lt;br /&gt;
&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		int v = (kMer &amp;gt;&amp;gt; (i + i)) &amp;amp; 0x3;&lt;br /&gt;
		switch (v) {&lt;br /&gt;
		case A_MASK_INT:&lt;br /&gt;
			result[i] = A;&lt;br /&gt;
			break;&lt;br /&gt;
		case T_MASK_INT:&lt;br /&gt;
			result[i] = T;&lt;br /&gt;
			break;&lt;br /&gt;
		case C_MASK_INT:&lt;br /&gt;
			result[i] = C;&lt;br /&gt;
			break;&lt;br /&gt;
		case G_MASK_INT:&lt;br /&gt;
			result[i] = G;&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===long toPackedLong(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long toPackedLong(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	long result = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		switch (sequence[offset + i]) {&lt;br /&gt;
		// case A:&lt;br /&gt;
		// result |= (A_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
		// break;&lt;br /&gt;
		case T:&lt;br /&gt;
			result |= (T_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case C:&lt;br /&gt;
			result |= (C_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case G:&lt;br /&gt;
			result |= (G_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===long toPackedLong(byte[] sequence)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long toPackedLong(byte[] sequence) {&lt;br /&gt;
	return toPackedLong(sequence, 0, sequence.length);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===byte[] unpackLong(long kMer, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static byte[] unpackLong(long kMer, int kMerLength) {&lt;br /&gt;
	byte[] result = new byte[kMerLength];&lt;br /&gt;
&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		long v = (kMer &amp;gt;&amp;gt; (i + i)) &amp;amp; 0x3;&lt;br /&gt;
		switch ((int) v) {&lt;br /&gt;
		case A_MASK_INT:&lt;br /&gt;
			result[i] = A;&lt;br /&gt;
			break;&lt;br /&gt;
		case T_MASK_INT:&lt;br /&gt;
			result[i] = T;&lt;br /&gt;
			break;&lt;br /&gt;
		case C_MASK_INT:&lt;br /&gt;
			result[i] = C;&lt;br /&gt;
			break;&lt;br /&gt;
		case G_MASK_INT:&lt;br /&gt;
			result[i] = G;&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==KMerCounts==&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/KMerCount.html interface KMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/map/MapKMerCount.html class MapKMerCount&amp;lt;T&amp;gt;]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/array/IntArrayKMerCount.html class IntArrayKMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/array/AtomicIntegerArrayKMerCount.html class AtomicIntegerArrayKMerCount]&lt;br /&gt;
&lt;br /&gt;
===MapKMerCount===&lt;br /&gt;
In order to provide a KMerCount that works with different k-mer type representations we have added an interface KMerCodec which provides functions to go to an from the byte[] representation:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public interface KMerCodec&amp;lt;T&amp;gt; {&lt;br /&gt;
	BiFunction&amp;lt;T, Integer, byte[]&amp;gt; decoder();&lt;br /&gt;
&lt;br /&gt;
	Function&amp;lt;byte[], T&amp;gt; encoder();&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As an example the instance for long -&amp;gt; byte[] and back simply utilizes the pack and unpack methods in KMerUtils:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public enum LongKMerCodec implements KMerCodec&amp;lt;Long&amp;gt; {&lt;br /&gt;
	INSTANCE;&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public Function&amp;lt;byte[], Long&amp;gt; encoder() {&lt;br /&gt;
		return KMerUtils::toPackedLong;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public BiFunction&amp;lt;Long, Integer, byte[]&amp;gt; decoder() {&lt;br /&gt;
		return KMerUtils::unpackLong;&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Creating an instance of a MapKMerCount to return from your KMerCounters will need to use the appropriate codec.  For example, if you have a map of type &amp;lt;code&amp;gt;Map&amp;lt;Long, Integer&amp;gt;&amp;lt;/code&amp;gt; which you wish to wrap and return, your code would look something like this:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new MapKMerCount&amp;lt;&amp;gt;(k, map, LongKMerCodec.INSTANCE);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===IntArrayKMerCount===&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new IntArrayKMerCount(k, array);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===AtomicIntegerArrayKMerCount===&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new AtomicIntegerArrayKMerCount(k, atomicIntegerArray);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Group Warmup=&lt;br /&gt;
Complete the [[String_Map_K_Mer_Assignment|group warm up]] first.&lt;br /&gt;
&lt;br /&gt;
=Lab=&lt;br /&gt;
{{ tip | Get ThresholdSlices working and then use it to balance all of your parallel KMerCounter implementations. }}&lt;br /&gt;
==ThresholdSlices==&lt;br /&gt;
===slicing===&lt;br /&gt;
{{CodeToImplement|ThresholdSlices|sliceKernel&amp;lt;br&amp;gt;createSlices&amp;lt;br&amp;gt;calculateReasonableThreshold|kmer.exercise.balance}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|&amp;lt;br/&amp;gt;The javadoc incorrectly specify that &amp;quot;Any sequence whose length is less than or equal to k should be omitted.&amp;quot;&amp;lt;br/&amp;gt;The javadoc should read &amp;quot;Any sequence whose length is less than k should be omitted.&amp;quot;}}&lt;br /&gt;
&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;
{{Sequential|public static List&amp;lt;Slice&amp;lt;byte[]&amp;gt;&amp;gt; createSlices(List&amp;lt;byte[]&amp;gt; sequences, int k, IntPredicate slicePredicate)}}&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, which is the &amp;quot;helper&amp;quot; method, and the kernel contains the recursion.&lt;br /&gt;
&lt;br /&gt;
In the createSlices method, you should call the recursive kernel for each sequence 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 length of the slice 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;
&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;
&amp;lt;youtube&amp;gt;m56ee5spxiw&amp;lt;/youtube&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 threashold of range to be used in slicePredicate. 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;
==LongConcurrentMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|LongConcurrentMapKMerCounter|parse|kmer.exercise.longconcurrentmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will also overlap greatly with everything you have done previously, but instead of using Strings or SequenceSlice, we will be using Longs to represent DNA data. In order to do this, refer to the KMerUtils class, more specifically the toPackedLong method which will convert a sequence of bytes into a Long for our purposes. &lt;br /&gt;
&lt;br /&gt;
{{Warning|Be sure to utilize the &amp;lt;code&amp;gt;Supplier&amp;lt;ConcurrentMap&amp;lt;Long, Integer&amp;gt;&amp;gt; concurrentMapFactory&amp;lt;/code&amp;gt; instance variable to create your ConcurrentMap.}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&amp;lt;youtube&amp;gt;d_Ze-YQ3Thc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Maps in Java often have a way to specify an initialCapacity to the constructor.  When used well, this parameter can give a hint as to how many Entries the Map should be expecting.  &lt;br /&gt;
&lt;br /&gt;
KMerUtils has methods which will calculate what you need to compute this value.  calculatePossibleKmers returns 4^k and calculateSumOfAllKmers returns n-k+1.  Think about how each method might be used to calculate an appropriate initial capacity.&lt;br /&gt;
&lt;br /&gt;
Some questions to ask yourself:&lt;br /&gt;
&lt;br /&gt;
# How many entries should the ConcurrentMap expect for a chromosome a million in length if k is 30?&lt;br /&gt;
# How many entries should the ConcurrentMap expect for a chromosome a million in length if k is 1?&lt;br /&gt;
&lt;br /&gt;
In order to test LongConcurrentMapKMerCounter with both java.util.ConcurrentHashMap&amp;lt;K,V&amp;gt; and your ConcurrentBucketHashMap&amp;lt;K,V&amp;gt; implementation, we pass a concurrentMapFactory to LongConcurrentMapKMerCounter's constructor.  You should use this factory to create your instance of ConcurrentMap via the apply method.  The Optional&amp;lt;Integer&amp;gt; parameter to this apply method is the initialCapacity.&lt;br /&gt;
&lt;br /&gt;
Note: anyone feeling the weight of a pandemic should feel free to pass Optional.empty() to the apply method.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IntArrayKMerCounter==&lt;br /&gt;
{{CodeToImplement|IntArrayKMerCounter|parse|kmer.lab.intarray}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
Like the String HashMap implementation, this implementation should be completed sequentially. However, unlike the previous implementations, we will be using an array instead of a map. This means that you must keep in mind what size to make the array in order to instantiate it. KMerUtils has a method which will calculate this number for you, but think about which one to use, why you would use it, and how that would differ from simply calculating n - k + 1 yourself. Think of using the int array as similar to using the map, but with ints as the key/value pairs. Use KMerUtils.toPackedInt() in order to convert the sequence into an int for our purposes.&lt;br /&gt;
&lt;br /&gt;
Hint: calculatePossibleKmers and calculateSumOfAllKmers are distinct and take different input parameters. calculateSumOfAllKmers performs the n - k + 1 calculation on a list of arrays of sequences and sums the total. It calculate the total number of Kmers in the list of sequences. calculatePossibleKmers only takes the length of the k-mer (k) as the input parameter, meaning it will calculate all possibilities independent of the sequences. Thus, the n - k + 1 calculation would yield a value ranging from 0 (inclusive) to the result of calculatePossibleKmers (exclusive).&lt;br /&gt;
&lt;br /&gt;
==AtomicIntegerArrayKMerCounter==&lt;br /&gt;
{{CodeToImplement|AtomicIntegerArrayKMerCounter|parse|kmer.lab.atomicintegerarray}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This implementation is simply the parallel equivalent of the int array implementation. To do so, you will be making use of Java’s atomic version of an array: an AtomicIntegerArray. Like before, you will be need to complete the parse method.  Don't forget to balance the workload in this parallel implementation with your ThresholdSlices.  Make sure to select the most appropriate parallel fork loop.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
==ConcurrentBucketHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|ConcurrentBucketHashMapKMerCounter|parse|kmer.lab.concurrentbuckethashmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will go a step beyond the previous implementations and divide entries into 1024 buckets. Although a different number of buckets can be used, we will simply stick to 1024. Every time an entry is added to the bucket dictionary, it will create a hash based on the entry's key and place the entry into one of the 1024 buckets. Because we would only need to search one bucket for an entry based on a key's hash, this mainly serves the purpose of cutting down on search time. The compute method of the ConcurrentBucketHashMap should automatically compute the correct bucket for the entry, so this implementation should closely resemble the previous ConcurrentHashMap implementations. When instantiating the ConcurrentBucketHashMap, we recommend making the key/value pairs a Long and an Integer.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==ConcurrentBucketHashMap==&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;
For this section of the assignment you will create a thread safe data structure.  You have previously created a [[Iterator,_Collection,_and_Map#BucketsHashMap|non-thread safe Map]].  Now, with the help of multiple  [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html ReentrantReadWriteLocks] you will create a thread safe Map that both deals with contention and allows simultaneous gets. &lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ConcurrentBucketHashMap|constructor&amp;lt;br&amp;gt;getIndex&amp;lt;br&amp;gt;getBucket&amp;lt;br&amp;gt;getLock&amp;lt;br&amp;gt;getEntry&amp;lt;br&amp;gt;get&amp;lt;br&amp;gt;put&amp;lt;br&amp;gt;compute|kmer.lab.concurrentbuckethashmap}}&lt;br /&gt;
&lt;br /&gt;
===constructor===&lt;br /&gt;
{{Sequential|public ConcurrentBucketHashMap(int initialCapacity)}}&lt;br /&gt;
&lt;br /&gt;
initialCapacity can be treated as a hint of how many entries the Map can expect to receive.&lt;br /&gt;
&lt;br /&gt;
===private support methods===&lt;br /&gt;
{{Tip|Use HashUtils.toIndex(key,N)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private List&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; getBucket(Object key)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private ReadWriteLock getLock(Object key)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private static &amp;lt;K, V&amp;gt; Optional&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; getEntry(List&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; bucket, Object key)}}&lt;br /&gt;
&lt;br /&gt;
===public methods===&lt;br /&gt;
&lt;br /&gt;
Be sure to make each of these methods thread safe by acquiring the appropriate (read or write) lock for the appropriate bucket.&lt;br /&gt;
&lt;br /&gt;
Which method, [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock()] or [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#tryLock-- tryLock()], is the appropriate one to use for creating a thread safe data structure such as this?&lt;br /&gt;
&amp;lt;spoiler show=&amp;quot;spoiler&amp;quot; hide=&amp;quot;spoiler&amp;quot;&amp;gt;[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock()]&amp;lt;/spoiler&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever you acquire a lock, but sure to leverage [https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html try finally] and [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#unlock-- unlock()] the lock within the finally block.&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|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- spec]&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|public V put(K key, V value)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#put-K-V- spec]&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|public V compute(K key, BiFunction&amp;lt;? super K, ? super V, ? extends V&amp;gt; remappingFunction)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentMap.html#compute-K-java.util.function.BiFunction- spec]&lt;br /&gt;
&lt;br /&gt;
===advice===&lt;br /&gt;
&lt;br /&gt;
{{ warning | Do '''NOT''' call get() from compute().  It will result in a nonintuitive error.  getEntry(bucket, key) gives you everything you need. }}&lt;br /&gt;
&lt;br /&gt;
{{ warning | Do '''NOT''' call put() from compute() either.  Just do the work within the compute method. }}&lt;br /&gt;
&lt;br /&gt;
{{ tip | Be sure to make your ConcurrentBucketHashMap thread-safe.  If any code is going to access shared mutable data, it needs to do so in a thread-safe manner. }}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
A simple example of how to use the many incantations of isolated:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;@ThreadSafe&lt;br /&gt;
public class IsolatedInteger {&lt;br /&gt;
    private int value;&lt;br /&gt;
&lt;br /&gt;
    public IsolatedInteger(int value) {&lt;br /&gt;
        this.value = value;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public int getValue() {&lt;br /&gt;
        return isolatedWithReturn(objectBased(readMode(this)), () -&amp;gt; {&lt;br /&gt;
            return value;&lt;br /&gt;
        });&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public void increment() {&lt;br /&gt;
        isolated(objectBased(this), () -&amp;gt; {&lt;br /&gt;
            value++;&lt;br /&gt;
        });&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=Extra Credit=&lt;br /&gt;
==Open-ended K-mer Counter==&lt;br /&gt;
&lt;br /&gt;
This implementation is worth up to 4.5 extra credit points.&lt;br /&gt;
&lt;br /&gt;
It is intended as an opportunity to build a high-performing, thread-safe data structure.&lt;br /&gt;
&lt;br /&gt;
One possible path was outlined at a high level in class.  However, students are encouraged to think of alternate solutions.&lt;br /&gt;
&lt;br /&gt;
We have explicitly disallowed implementations akin to our matrix map reduce assignment, since we have already done the &amp;quot;give everyone their own data and combine in post&amp;quot; pattern to death.&lt;br /&gt;
&lt;br /&gt;
Partial credit is possible for solutions which demonstrate significant ingenuity.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|_KMerTestSuite|kmer.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Rubric=&lt;br /&gt;
&lt;br /&gt;
Total points: 100&lt;br /&gt;
&lt;br /&gt;
* Correct ThresholdSlices (10)&lt;br /&gt;
* Correct and Parallel Long ConcurrentHashMap (25)&lt;br /&gt;
* Correct Int Array (15)&lt;br /&gt;
* Correct and Parallel AtomicIntegerArray (15)&lt;br /&gt;
* Correct and ThreadSafe ConcurrentBucketHashMap implementation (25)&lt;br /&gt;
* Clarity and efficiency (10)&lt;br /&gt;
&lt;br /&gt;
=Research=&lt;br /&gt;
[https://en.wikipedia.org/wiki/K-mer wikipedia article on k-mers]&lt;br /&gt;
&lt;br /&gt;
paper: [https://academic.oup.com/bioinformatics/article-lookup/doi/10.1093/bioinformatics/btr011 A fast, lock-free approach for efficient parallel counting of occurrences of k-mers] (Jellyfish)&lt;br /&gt;
&lt;br /&gt;
paper: [https://peerj.com/articles/cs-94/ Multiple comparative metagenomics using multiset k-mer counting]&lt;br /&gt;
&lt;br /&gt;
[http://homolog.us/Tutorials/index.php?p=3.7&amp;amp;s=2 Collection of approaches to k-mer counting]&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform Burrows–Wheeler transform]&lt;br /&gt;
&lt;br /&gt;
[http://biology.stackexchange.com/questions/45016/what-is-the-biological-significance-of-k-mer-counting Significance of k-mer Counting]&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Counting_Assignment&amp;diff=3669</id>
		<title>K Mer Counting Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Counting_Assignment&amp;diff=3669"/>
		<updated>2022-04-07T02:34:28Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* LongConcurrentMapKMerCounter */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;credit for this assignment: John Gibson and Dennis Cosgrove&lt;br /&gt;
=Motivation=&lt;br /&gt;
K-mer counting is a real world application which provides the opportunity to build thread safe data structures which alleviate contention.&lt;br /&gt;
&lt;br /&gt;
Pulling real data from the National Institutes of Health forces us to address balancing our work.&lt;br /&gt;
&lt;br /&gt;
Finally, k-mer counting provides opportunities for performance engineering.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
The term k-mer refers to a substring of length k and k-mer counting refers to the process of finding the number of occurrences of a k-mer in a given string. This computational problem has many real-world applications, the most notable being in the field of computational genomics. In this assignment, you will design a program that will ultimately take in a human X-chromosome and count the number of k-mers in the string of DNA. You will be making use of everything you have learned thus far, including atomic variables. It is useful to know that given a string of length n, the amount of possible k-mers is equal to '''n - k + 1'''.&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;_oyWzImAx1w&amp;lt;/youtube&amp;gt;&lt;br /&gt;
=Demo=&lt;br /&gt;
{{CodeToInvestigate|OutputKmer|main|kmer.output}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;tFfa7DWLJ_U&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This demo video shows the big picture of KMer Counting. It included example usage of calculateSumOfAllKMers and calculatePossibleKMers method in &amp;lt;code&amp;gt;KMerUtils&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
==Runtime==&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#availableProcessors-- Runtime.getRuntime().availableProcessors()]&lt;br /&gt;
&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;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicIntegerArray.html AtomicIntegerArray].&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html ReentrantReadWriteLock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html#readLock-- readLock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html#writeLock-- writeLock]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html Lock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- unlock]&lt;br /&gt;
&lt;br /&gt;
==KMerUtils==&lt;br /&gt;
The following methods should be useful as you build the assignment.&lt;br /&gt;
&lt;br /&gt;
Check out the Javadocs either [http://www.cse.wustl.edu/~cosgroved/courses/cse231/f17/apidocs/kmer/core/KMerUtils.html on the web], or in the comments in KMerUtils.java for further details on how they work.&lt;br /&gt;
&lt;br /&gt;
===long calculatePossibleKMers(int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long calculatePossibleKMers(int kMerLength) {&lt;br /&gt;
	int bitsPerBase = 2;&lt;br /&gt;
	int bitsPerKMer = kMerLength * bitsPerBase;&lt;br /&gt;
	long possibleKMers = 1L &amp;lt;&amp;lt; bitsPerKMer;&lt;br /&gt;
	return possibleKMers;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toArrayLength(long possibleKMers)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toArrayLength(long possibleKMers) {&lt;br /&gt;
	// maximum array length is VM dependent, but must be indexed by an int.&lt;br /&gt;
	// reportedly some vms reserve some header words (see ArrayList) which puts its&lt;br /&gt;
	// limit as just below 2^31.&lt;br /&gt;
	// we will choose 2^30 (inclusive) as the maximum since it will hold the largest&lt;br /&gt;
	// potential k-mer (4^15)&lt;br /&gt;
	final long MAX_K_MER_ARRAY_LENGTH_INCLUSIVE = 1 &amp;lt;&amp;lt; 30;&lt;br /&gt;
	if (possibleKMers &amp;lt;= MAX_K_MER_ARRAY_LENGTH_INCLUSIVE) {&lt;br /&gt;
		return (int) possibleKMers;&lt;br /&gt;
	} else {&lt;br /&gt;
		throw new IllegalArgumentException(possibleKMers + &amp;quot; &amp;gt; &amp;quot; + MAX_K_MER_ARRAY_LENGTH_INCLUSIVE);&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int calculateSumOfAllKMers(List&amp;lt;byte[]&amp;gt; sequences, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int calculateSumOfAllKMers(List&amp;lt;byte[]&amp;gt; sequences, int kMerLength) {&lt;br /&gt;
	int sum = 0;&lt;br /&gt;
	for (byte[] sequence : sequences) {&lt;br /&gt;
		int i = (sequence.length - kMerLength + 1);&lt;br /&gt;
		if( i &amp;gt; 0 ) {&lt;br /&gt;
			sum += i;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return sum;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toPackedInt(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toPackedInt(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	int result = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		switch (sequence[offset + i]) {&lt;br /&gt;
		// case A:&lt;br /&gt;
		// result |= (A_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
		// break;&lt;br /&gt;
		case T:&lt;br /&gt;
			result |= (T_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case C:&lt;br /&gt;
			result |= (C_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case G:&lt;br /&gt;
			result |= (G_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toPackedInt(byte[] kMer)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toPackedInt(byte[] kMer) {&lt;br /&gt;
	return toPackedInt(kMer, 0, kMer.length);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===byte[] unpackInt(int kMer, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static byte[] unpackInt(int kMer, int kMerLength) {&lt;br /&gt;
	byte[] result = new byte[kMerLength];&lt;br /&gt;
&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		int v = (kMer &amp;gt;&amp;gt; (i + i)) &amp;amp; 0x3;&lt;br /&gt;
		switch (v) {&lt;br /&gt;
		case A_MASK_INT:&lt;br /&gt;
			result[i] = A;&lt;br /&gt;
			break;&lt;br /&gt;
		case T_MASK_INT:&lt;br /&gt;
			result[i] = T;&lt;br /&gt;
			break;&lt;br /&gt;
		case C_MASK_INT:&lt;br /&gt;
			result[i] = C;&lt;br /&gt;
			break;&lt;br /&gt;
		case G_MASK_INT:&lt;br /&gt;
			result[i] = G;&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===long toPackedLong(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long toPackedLong(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	long result = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		switch (sequence[offset + i]) {&lt;br /&gt;
		// case A:&lt;br /&gt;
		// result |= (A_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
		// break;&lt;br /&gt;
		case T:&lt;br /&gt;
			result |= (T_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case C:&lt;br /&gt;
			result |= (C_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case G:&lt;br /&gt;
			result |= (G_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===long toPackedLong(byte[] sequence)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long toPackedLong(byte[] sequence) {&lt;br /&gt;
	return toPackedLong(sequence, 0, sequence.length);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===byte[] unpackLong(long kMer, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static byte[] unpackLong(long kMer, int kMerLength) {&lt;br /&gt;
	byte[] result = new byte[kMerLength];&lt;br /&gt;
&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		long v = (kMer &amp;gt;&amp;gt; (i + i)) &amp;amp; 0x3;&lt;br /&gt;
		switch ((int) v) {&lt;br /&gt;
		case A_MASK_INT:&lt;br /&gt;
			result[i] = A;&lt;br /&gt;
			break;&lt;br /&gt;
		case T_MASK_INT:&lt;br /&gt;
			result[i] = T;&lt;br /&gt;
			break;&lt;br /&gt;
		case C_MASK_INT:&lt;br /&gt;
			result[i] = C;&lt;br /&gt;
			break;&lt;br /&gt;
		case G_MASK_INT:&lt;br /&gt;
			result[i] = G;&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==KMerCounts==&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/KMerCount.html interface KMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/map/MapKMerCount.html class MapKMerCount&amp;lt;T&amp;gt;]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/array/IntArrayKMerCount.html class IntArrayKMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/array/AtomicIntegerArrayKMerCount.html class AtomicIntegerArrayKMerCount]&lt;br /&gt;
&lt;br /&gt;
===MapKMerCount===&lt;br /&gt;
In order to provide a KMerCount that works with different k-mer type representations we have added an interface KMerCodec which provides functions to go to an from the byte[] representation:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public interface KMerCodec&amp;lt;T&amp;gt; {&lt;br /&gt;
	BiFunction&amp;lt;T, Integer, byte[]&amp;gt; decoder();&lt;br /&gt;
&lt;br /&gt;
	Function&amp;lt;byte[], T&amp;gt; encoder();&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As an example the instance for long -&amp;gt; byte[] and back simply utilizes the pack and unpack methods in KMerUtils:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public enum LongKMerCodec implements KMerCodec&amp;lt;Long&amp;gt; {&lt;br /&gt;
	INSTANCE;&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public Function&amp;lt;byte[], Long&amp;gt; encoder() {&lt;br /&gt;
		return KMerUtils::toPackedLong;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public BiFunction&amp;lt;Long, Integer, byte[]&amp;gt; decoder() {&lt;br /&gt;
		return KMerUtils::unpackLong;&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Creating an instance of a MapKMerCount to return from your KMerCounters will need to use the appropriate codec.  For example, if you have a map of type &amp;lt;code&amp;gt;Map&amp;lt;Long, Integer&amp;gt;&amp;lt;/code&amp;gt; which you wish to wrap and return, your code would look something like this:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new MapKMerCount&amp;lt;&amp;gt;(k, map, LongKMerCodec.INSTANCE);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===IntArrayKMerCount===&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new IntArrayKMerCount(k, array);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===AtomicIntegerArrayKMerCount===&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new AtomicIntegerArrayKMerCount(k, atomicIntegerArray);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Group Warmup=&lt;br /&gt;
Complete the [[String_Map_K_Mer_Assignment|group warm up]] first.&lt;br /&gt;
&lt;br /&gt;
=Lab=&lt;br /&gt;
{{ tip | Get ThresholdSlices working and then use it to balance all of your parallel KMerCounter implementations. }}&lt;br /&gt;
==ThresholdSlices==&lt;br /&gt;
===slicing===&lt;br /&gt;
{{CodeToImplement|ThresholdSlices|sliceKernel&amp;lt;br&amp;gt;createSlices&amp;lt;br&amp;gt;calculateReasonableThreshold|kmer.exercise.balance}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|&amp;lt;br/&amp;gt;The javadoc incorrectly specify that &amp;quot;Any sequence whose length is less than or equal to k should be omitted.&amp;quot;&amp;lt;br/&amp;gt;The javadoc should read &amp;quot;Any sequence whose length is less than k should be omitted.&amp;quot;}}&lt;br /&gt;
&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;
{{Sequential|public static List&amp;lt;Slice&amp;lt;byte[]&amp;gt;&amp;gt; createSlices(List&amp;lt;byte[]&amp;gt; sequences, int k, IntPredicate slicePredicate)}}&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, which is the &amp;quot;helper&amp;quot; method, and the kernel contains the recursion.&lt;br /&gt;
&lt;br /&gt;
In the createSlices method, you should call the recursive kernel for each sequence 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 length of the slice 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;
&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;
&amp;lt;youtube&amp;gt;m56ee5spxiw&amp;lt;/youtube&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 threashold of range to be used in slicePredicate. 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;
==LongConcurrentMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|LongConcurrentMapKMerCounter|parse|kmer.exercise.longconcurrentmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will also overlap greatly with everything you have done previously, but instead of using Strings or SequenceSlice, we will be using Longs to represent DNA data. In order to do this, refer to the KMerUtils class, more specifically the toPackedLong method which will convert a sequence of bytes into a Long for our purposes. &lt;br /&gt;
&lt;br /&gt;
{{Warning|Be sure to utilize the &amp;lt;code&amp;gt;Supplier&amp;lt;ConcurrentMap&amp;lt;Long, Integer&amp;gt;&amp;gt; concurrentMapFactory&amp;lt;/code&amp;gt; instance variable to create your ConcurrentMap.}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&amp;lt;youtube&amp;gt;d_Ze-YQ3Thc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Maps in Java often have a way to specify an initialCapacity to the constructor.  When used well, this parameter can give a hint as to how many Entries the Map should be expecting.  &lt;br /&gt;
&lt;br /&gt;
KMerUtils has methods which will calculate what you need to compute this value.  calculatePossibleKmers returns 4^k and calculateSumOfAllKmers returns n-k+1.  Think about how each method might be used to calculate an appropriate initial capacity.&lt;br /&gt;
&lt;br /&gt;
Some questions to ask yourself:&lt;br /&gt;
&lt;br /&gt;
# How many entries should the ConcurrentMap expect for a chromosome a million in length if k is 30?&lt;br /&gt;
# How many entries should the ConcurrentMap expect for a chromosome a million in length if k is 1?&lt;br /&gt;
&lt;br /&gt;
In order to test LongConcurrentMapKMerCounter with both java.util.ConcurrentHashMap&amp;lt;K,V&amp;gt; and your ConcurrentBucketHashMap&amp;lt;K,V&amp;gt; implementation, we pass a concurrentMapFactory to LongConcurrentMapKMerCounter's constructor.  You should use this factory to create your instance of ConcurrentMap via the apply method.  The Optional&amp;lt;Integer&amp;gt; parameter to this apply method is the initialCapacity.&lt;br /&gt;
&lt;br /&gt;
Note: anyone feeling the weight of a pandemic should feel free to pass Optional.empty() to the apply method.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IntArrayKMerCounter==&lt;br /&gt;
{{CodeToImplement|IntArrayKMerCounter|parse|kmer.lab.intarray}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
Like the String HashMap implementation, this implementation should be completed sequentially. However, unlike the previous implementations, we will be using an array instead of a map. This means that you must keep in mind what size to make the array in order to instantiate it. KMerUtils has a method which will calculate this number for you, but think about which one to use, why you would use it, and how that would differ from simply calculating n - k + 1 yourself. Think of using the int array as similar to using the map, but with ints as the key/value pairs. Use KMerUtils.toPackedInt() in order to convert the sequence into an int for our purposes.&lt;br /&gt;
&lt;br /&gt;
Hint: calculatePossibleKmers and calculateSumOfAllKmers are distinct and take different input parameters. calculateSumOfAllKmers performs the n - k + 1 calculation on a list of arrays of sequences and sums the total. calculatePossibleKmers only takes the length of the k-mer (k) as the input parameter, meaning it will calculate possibilities independent of the size of the sequence. Thus, the n - k + 1 calculation would yield a value ranging from 0 (inclusive) to the result of calculatePossibleKmers (exclusive).&lt;br /&gt;
&lt;br /&gt;
==AtomicIntegerArrayKMerCounter==&lt;br /&gt;
{{CodeToImplement|AtomicIntegerArrayKMerCounter|parse|kmer.lab.atomicintegerarray}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This implementation is simply the parallel equivalent of the int array implementation. To do so, you will be making use of Java’s atomic version of an array: an AtomicIntegerArray. Like before, you will be need to complete the parse method.  Don't forget to balance the workload in this parallel implementation with your ThresholdSlices.  Make sure to select the most appropriate parallel fork loop.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
==ConcurrentBucketHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|ConcurrentBucketHashMapKMerCounter|parse|kmer.lab.concurrentbuckethashmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will go a step beyond the previous implementations and divide entries into 1024 buckets. Although a different number of buckets can be used, we will simply stick to 1024. Every time an entry is added to the bucket dictionary, it will create a hash based on the entry's key and place the entry into one of the 1024 buckets. Because we would only need to search one bucket for an entry based on a key's hash, this mainly serves the purpose of cutting down on search time. The compute method of the ConcurrentBucketHashMap should automatically compute the correct bucket for the entry, so this implementation should closely resemble the previous ConcurrentHashMap implementations. When instantiating the ConcurrentBucketHashMap, we recommend making the key/value pairs a Long and an Integer.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==ConcurrentBucketHashMap==&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;
For this section of the assignment you will create a thread safe data structure.  You have previously created a [[Iterator,_Collection,_and_Map#BucketsHashMap|non-thread safe Map]].  Now, with the help of multiple  [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html ReentrantReadWriteLocks] you will create a thread safe Map that both deals with contention and allows simultaneous gets. &lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ConcurrentBucketHashMap|constructor&amp;lt;br&amp;gt;getIndex&amp;lt;br&amp;gt;getBucket&amp;lt;br&amp;gt;getLock&amp;lt;br&amp;gt;getEntry&amp;lt;br&amp;gt;get&amp;lt;br&amp;gt;put&amp;lt;br&amp;gt;compute|kmer.lab.concurrentbuckethashmap}}&lt;br /&gt;
&lt;br /&gt;
===constructor===&lt;br /&gt;
{{Sequential|public ConcurrentBucketHashMap(int initialCapacity)}}&lt;br /&gt;
&lt;br /&gt;
initialCapacity can be treated as a hint of how many entries the Map can expect to receive.&lt;br /&gt;
&lt;br /&gt;
===private support methods===&lt;br /&gt;
{{Tip|Use HashUtils.toIndex(key,N)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private List&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; getBucket(Object key)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private ReadWriteLock getLock(Object key)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private static &amp;lt;K, V&amp;gt; Optional&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; getEntry(List&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; bucket, Object key)}}&lt;br /&gt;
&lt;br /&gt;
===public methods===&lt;br /&gt;
&lt;br /&gt;
Be sure to make each of these methods thread safe by acquiring the appropriate (read or write) lock for the appropriate bucket.&lt;br /&gt;
&lt;br /&gt;
Which method, [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock()] or [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#tryLock-- tryLock()], is the appropriate one to use for creating a thread safe data structure such as this?&lt;br /&gt;
&amp;lt;spoiler show=&amp;quot;spoiler&amp;quot; hide=&amp;quot;spoiler&amp;quot;&amp;gt;[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock()]&amp;lt;/spoiler&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever you acquire a lock, but sure to leverage [https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html try finally] and [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#unlock-- unlock()] the lock within the finally block.&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|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- spec]&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|public V put(K key, V value)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#put-K-V- spec]&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|public V compute(K key, BiFunction&amp;lt;? super K, ? super V, ? extends V&amp;gt; remappingFunction)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentMap.html#compute-K-java.util.function.BiFunction- spec]&lt;br /&gt;
&lt;br /&gt;
===advice===&lt;br /&gt;
&lt;br /&gt;
{{ warning | Do '''NOT''' call get() from compute().  It will result in a nonintuitive error.  getEntry(bucket, key) gives you everything you need. }}&lt;br /&gt;
&lt;br /&gt;
{{ warning | Do '''NOT''' call put() from compute() either.  Just do the work within the compute method. }}&lt;br /&gt;
&lt;br /&gt;
{{ tip | Be sure to make your ConcurrentBucketHashMap thread-safe.  If any code is going to access shared mutable data, it needs to do so in a thread-safe manner. }}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
A simple example of how to use the many incantations of isolated:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;@ThreadSafe&lt;br /&gt;
public class IsolatedInteger {&lt;br /&gt;
    private int value;&lt;br /&gt;
&lt;br /&gt;
    public IsolatedInteger(int value) {&lt;br /&gt;
        this.value = value;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public int getValue() {&lt;br /&gt;
        return isolatedWithReturn(objectBased(readMode(this)), () -&amp;gt; {&lt;br /&gt;
            return value;&lt;br /&gt;
        });&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public void increment() {&lt;br /&gt;
        isolated(objectBased(this), () -&amp;gt; {&lt;br /&gt;
            value++;&lt;br /&gt;
        });&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=Extra Credit=&lt;br /&gt;
==Open-ended K-mer Counter==&lt;br /&gt;
&lt;br /&gt;
This implementation is worth up to 4.5 extra credit points.&lt;br /&gt;
&lt;br /&gt;
It is intended as an opportunity to build a high-performing, thread-safe data structure.&lt;br /&gt;
&lt;br /&gt;
One possible path was outlined at a high level in class.  However, students are encouraged to think of alternate solutions.&lt;br /&gt;
&lt;br /&gt;
We have explicitly disallowed implementations akin to our matrix map reduce assignment, since we have already done the &amp;quot;give everyone their own data and combine in post&amp;quot; pattern to death.&lt;br /&gt;
&lt;br /&gt;
Partial credit is possible for solutions which demonstrate significant ingenuity.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|_KMerTestSuite|kmer.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Rubric=&lt;br /&gt;
&lt;br /&gt;
Total points: 100&lt;br /&gt;
&lt;br /&gt;
* Correct ThresholdSlices (10)&lt;br /&gt;
* Correct and Parallel Long ConcurrentHashMap (25)&lt;br /&gt;
* Correct Int Array (15)&lt;br /&gt;
* Correct and Parallel AtomicIntegerArray (15)&lt;br /&gt;
* Correct and ThreadSafe ConcurrentBucketHashMap implementation (25)&lt;br /&gt;
* Clarity and efficiency (10)&lt;br /&gt;
&lt;br /&gt;
=Research=&lt;br /&gt;
[https://en.wikipedia.org/wiki/K-mer wikipedia article on k-mers]&lt;br /&gt;
&lt;br /&gt;
paper: [https://academic.oup.com/bioinformatics/article-lookup/doi/10.1093/bioinformatics/btr011 A fast, lock-free approach for efficient parallel counting of occurrences of k-mers] (Jellyfish)&lt;br /&gt;
&lt;br /&gt;
paper: [https://peerj.com/articles/cs-94/ Multiple comparative metagenomics using multiset k-mer counting]&lt;br /&gt;
&lt;br /&gt;
[http://homolog.us/Tutorials/index.php?p=3.7&amp;amp;s=2 Collection of approaches to k-mer counting]&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform Burrows–Wheeler transform]&lt;br /&gt;
&lt;br /&gt;
[http://biology.stackexchange.com/questions/45016/what-is-the-biological-significance-of-k-mer-counting Significance of k-mer Counting]&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Counting_Assignment&amp;diff=3668</id>
		<title>K Mer Counting Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Counting_Assignment&amp;diff=3668"/>
		<updated>2022-04-07T02:33:54Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* slicing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;credit for this assignment: John Gibson and Dennis Cosgrove&lt;br /&gt;
=Motivation=&lt;br /&gt;
K-mer counting is a real world application which provides the opportunity to build thread safe data structures which alleviate contention.&lt;br /&gt;
&lt;br /&gt;
Pulling real data from the National Institutes of Health forces us to address balancing our work.&lt;br /&gt;
&lt;br /&gt;
Finally, k-mer counting provides opportunities for performance engineering.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
The term k-mer refers to a substring of length k and k-mer counting refers to the process of finding the number of occurrences of a k-mer in a given string. This computational problem has many real-world applications, the most notable being in the field of computational genomics. In this assignment, you will design a program that will ultimately take in a human X-chromosome and count the number of k-mers in the string of DNA. You will be making use of everything you have learned thus far, including atomic variables. It is useful to know that given a string of length n, the amount of possible k-mers is equal to '''n - k + 1'''.&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;_oyWzImAx1w&amp;lt;/youtube&amp;gt;&lt;br /&gt;
=Demo=&lt;br /&gt;
{{CodeToInvestigate|OutputKmer|main|kmer.output}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;tFfa7DWLJ_U&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This demo video shows the big picture of KMer Counting. It included example usage of calculateSumOfAllKMers and calculatePossibleKMers method in &amp;lt;code&amp;gt;KMerUtils&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
==Runtime==&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#availableProcessors-- Runtime.getRuntime().availableProcessors()]&lt;br /&gt;
&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;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicIntegerArray.html AtomicIntegerArray].&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html ReentrantReadWriteLock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html#readLock-- readLock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html#writeLock-- writeLock]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html Lock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- unlock]&lt;br /&gt;
&lt;br /&gt;
==KMerUtils==&lt;br /&gt;
The following methods should be useful as you build the assignment.&lt;br /&gt;
&lt;br /&gt;
Check out the Javadocs either [http://www.cse.wustl.edu/~cosgroved/courses/cse231/f17/apidocs/kmer/core/KMerUtils.html on the web], or in the comments in KMerUtils.java for further details on how they work.&lt;br /&gt;
&lt;br /&gt;
===long calculatePossibleKMers(int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long calculatePossibleKMers(int kMerLength) {&lt;br /&gt;
	int bitsPerBase = 2;&lt;br /&gt;
	int bitsPerKMer = kMerLength * bitsPerBase;&lt;br /&gt;
	long possibleKMers = 1L &amp;lt;&amp;lt; bitsPerKMer;&lt;br /&gt;
	return possibleKMers;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toArrayLength(long possibleKMers)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toArrayLength(long possibleKMers) {&lt;br /&gt;
	// maximum array length is VM dependent, but must be indexed by an int.&lt;br /&gt;
	// reportedly some vms reserve some header words (see ArrayList) which puts its&lt;br /&gt;
	// limit as just below 2^31.&lt;br /&gt;
	// we will choose 2^30 (inclusive) as the maximum since it will hold the largest&lt;br /&gt;
	// potential k-mer (4^15)&lt;br /&gt;
	final long MAX_K_MER_ARRAY_LENGTH_INCLUSIVE = 1 &amp;lt;&amp;lt; 30;&lt;br /&gt;
	if (possibleKMers &amp;lt;= MAX_K_MER_ARRAY_LENGTH_INCLUSIVE) {&lt;br /&gt;
		return (int) possibleKMers;&lt;br /&gt;
	} else {&lt;br /&gt;
		throw new IllegalArgumentException(possibleKMers + &amp;quot; &amp;gt; &amp;quot; + MAX_K_MER_ARRAY_LENGTH_INCLUSIVE);&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int calculateSumOfAllKMers(List&amp;lt;byte[]&amp;gt; sequences, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int calculateSumOfAllKMers(List&amp;lt;byte[]&amp;gt; sequences, int kMerLength) {&lt;br /&gt;
	int sum = 0;&lt;br /&gt;
	for (byte[] sequence : sequences) {&lt;br /&gt;
		int i = (sequence.length - kMerLength + 1);&lt;br /&gt;
		if( i &amp;gt; 0 ) {&lt;br /&gt;
			sum += i;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return sum;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toPackedInt(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toPackedInt(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	int result = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		switch (sequence[offset + i]) {&lt;br /&gt;
		// case A:&lt;br /&gt;
		// result |= (A_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
		// break;&lt;br /&gt;
		case T:&lt;br /&gt;
			result |= (T_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case C:&lt;br /&gt;
			result |= (C_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case G:&lt;br /&gt;
			result |= (G_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toPackedInt(byte[] kMer)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toPackedInt(byte[] kMer) {&lt;br /&gt;
	return toPackedInt(kMer, 0, kMer.length);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===byte[] unpackInt(int kMer, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static byte[] unpackInt(int kMer, int kMerLength) {&lt;br /&gt;
	byte[] result = new byte[kMerLength];&lt;br /&gt;
&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		int v = (kMer &amp;gt;&amp;gt; (i + i)) &amp;amp; 0x3;&lt;br /&gt;
		switch (v) {&lt;br /&gt;
		case A_MASK_INT:&lt;br /&gt;
			result[i] = A;&lt;br /&gt;
			break;&lt;br /&gt;
		case T_MASK_INT:&lt;br /&gt;
			result[i] = T;&lt;br /&gt;
			break;&lt;br /&gt;
		case C_MASK_INT:&lt;br /&gt;
			result[i] = C;&lt;br /&gt;
			break;&lt;br /&gt;
		case G_MASK_INT:&lt;br /&gt;
			result[i] = G;&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===long toPackedLong(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long toPackedLong(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	long result = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		switch (sequence[offset + i]) {&lt;br /&gt;
		// case A:&lt;br /&gt;
		// result |= (A_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
		// break;&lt;br /&gt;
		case T:&lt;br /&gt;
			result |= (T_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case C:&lt;br /&gt;
			result |= (C_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case G:&lt;br /&gt;
			result |= (G_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===long toPackedLong(byte[] sequence)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long toPackedLong(byte[] sequence) {&lt;br /&gt;
	return toPackedLong(sequence, 0, sequence.length);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===byte[] unpackLong(long kMer, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static byte[] unpackLong(long kMer, int kMerLength) {&lt;br /&gt;
	byte[] result = new byte[kMerLength];&lt;br /&gt;
&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		long v = (kMer &amp;gt;&amp;gt; (i + i)) &amp;amp; 0x3;&lt;br /&gt;
		switch ((int) v) {&lt;br /&gt;
		case A_MASK_INT:&lt;br /&gt;
			result[i] = A;&lt;br /&gt;
			break;&lt;br /&gt;
		case T_MASK_INT:&lt;br /&gt;
			result[i] = T;&lt;br /&gt;
			break;&lt;br /&gt;
		case C_MASK_INT:&lt;br /&gt;
			result[i] = C;&lt;br /&gt;
			break;&lt;br /&gt;
		case G_MASK_INT:&lt;br /&gt;
			result[i] = G;&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==KMerCounts==&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/KMerCount.html interface KMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/map/MapKMerCount.html class MapKMerCount&amp;lt;T&amp;gt;]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/array/IntArrayKMerCount.html class IntArrayKMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/array/AtomicIntegerArrayKMerCount.html class AtomicIntegerArrayKMerCount]&lt;br /&gt;
&lt;br /&gt;
===MapKMerCount===&lt;br /&gt;
In order to provide a KMerCount that works with different k-mer type representations we have added an interface KMerCodec which provides functions to go to an from the byte[] representation:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public interface KMerCodec&amp;lt;T&amp;gt; {&lt;br /&gt;
	BiFunction&amp;lt;T, Integer, byte[]&amp;gt; decoder();&lt;br /&gt;
&lt;br /&gt;
	Function&amp;lt;byte[], T&amp;gt; encoder();&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As an example the instance for long -&amp;gt; byte[] and back simply utilizes the pack and unpack methods in KMerUtils:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public enum LongKMerCodec implements KMerCodec&amp;lt;Long&amp;gt; {&lt;br /&gt;
	INSTANCE;&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public Function&amp;lt;byte[], Long&amp;gt; encoder() {&lt;br /&gt;
		return KMerUtils::toPackedLong;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public BiFunction&amp;lt;Long, Integer, byte[]&amp;gt; decoder() {&lt;br /&gt;
		return KMerUtils::unpackLong;&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Creating an instance of a MapKMerCount to return from your KMerCounters will need to use the appropriate codec.  For example, if you have a map of type &amp;lt;code&amp;gt;Map&amp;lt;Long, Integer&amp;gt;&amp;lt;/code&amp;gt; which you wish to wrap and return, your code would look something like this:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new MapKMerCount&amp;lt;&amp;gt;(k, map, LongKMerCodec.INSTANCE);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===IntArrayKMerCount===&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new IntArrayKMerCount(k, array);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===AtomicIntegerArrayKMerCount===&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new AtomicIntegerArrayKMerCount(k, atomicIntegerArray);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Group Warmup=&lt;br /&gt;
Complete the [[String_Map_K_Mer_Assignment|group warm up]] first.&lt;br /&gt;
&lt;br /&gt;
=Lab=&lt;br /&gt;
{{ tip | Get ThresholdSlices working and then use it to balance all of your parallel KMerCounter implementations. }}&lt;br /&gt;
==ThresholdSlices==&lt;br /&gt;
===slicing===&lt;br /&gt;
{{CodeToImplement|ThresholdSlices|sliceKernel&amp;lt;br&amp;gt;createSlices&amp;lt;br&amp;gt;calculateReasonableThreshold|kmer.exercise.balance}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|&amp;lt;br/&amp;gt;The javadoc incorrectly specify that &amp;quot;Any sequence whose length is less than or equal to k should be omitted.&amp;quot;&amp;lt;br/&amp;gt;The javadoc should read &amp;quot;Any sequence whose length is less than k should be omitted.&amp;quot;}}&lt;br /&gt;
&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;
{{Sequential|public static List&amp;lt;Slice&amp;lt;byte[]&amp;gt;&amp;gt; createSlices(List&amp;lt;byte[]&amp;gt; sequences, int k, IntPredicate slicePredicate)}}&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, which is the &amp;quot;helper&amp;quot; method, and the kernel contains the recursion.&lt;br /&gt;
&lt;br /&gt;
In the createSlices method, you should call the recursive kernel for each sequence 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 length of the slice 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;
&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;
&amp;lt;youtube&amp;gt;m56ee5spxiw&amp;lt;/youtube&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 threashold of range to be used in slicePredicate. 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;
==LongConcurrentMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|LongConcurrentMapKMerCounter|parse|kmer.lab.longconcurrentmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will also overlap greatly with everything you have done previously, but instead of using Strings or SequenceSlice, we will be using Longs to represent DNA data. In order to do this, refer to the KMerUtils class, more specifically the toPackedLong method which will convert a sequence of bytes into a Long for our purposes. &lt;br /&gt;
&lt;br /&gt;
{{Warning|Be sure to utilize the &amp;lt;code&amp;gt;Supplier&amp;lt;ConcurrentMap&amp;lt;Long, Integer&amp;gt;&amp;gt; concurrentMapFactory&amp;lt;/code&amp;gt; instance variable to create your ConcurrentMap.}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&amp;lt;youtube&amp;gt;d_Ze-YQ3Thc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Maps in Java often have a way to specify an initialCapacity to the constructor.  When used well, this parameter can give a hint as to how many Entries the Map should be expecting.  &lt;br /&gt;
&lt;br /&gt;
KMerUtils has methods which will calculate what you need to compute this value.  calculatePossibleKmers returns 4^k and calculateSumOfAllKmers returns n-k+1.  Think about how each method might be used to calculate an appropriate initial capacity.&lt;br /&gt;
&lt;br /&gt;
Some questions to ask yourself:&lt;br /&gt;
&lt;br /&gt;
# How many entries should the ConcurrentMap expect for a chromosome a million in length if k is 30?&lt;br /&gt;
# How many entries should the ConcurrentMap expect for a chromosome a million in length if k is 1?&lt;br /&gt;
&lt;br /&gt;
In order to test LongConcurrentMapKMerCounter with both java.util.ConcurrentHashMap&amp;lt;K,V&amp;gt; and your ConcurrentBucketHashMap&amp;lt;K,V&amp;gt; implementation, we pass a concurrentMapFactory to LongConcurrentMapKMerCounter's constructor.  You should use this factory to create your instance of ConcurrentMap via the apply method.  The Optional&amp;lt;Integer&amp;gt; parameter to this apply method is the initialCapacity.&lt;br /&gt;
&lt;br /&gt;
Note: anyone feeling the weight of a pandemic should feel free to pass Optional.empty() to the apply method.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IntArrayKMerCounter==&lt;br /&gt;
{{CodeToImplement|IntArrayKMerCounter|parse|kmer.lab.intarray}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
Like the String HashMap implementation, this implementation should be completed sequentially. However, unlike the previous implementations, we will be using an array instead of a map. This means that you must keep in mind what size to make the array in order to instantiate it. KMerUtils has a method which will calculate this number for you, but think about which one to use, why you would use it, and how that would differ from simply calculating n - k + 1 yourself. Think of using the int array as similar to using the map, but with ints as the key/value pairs. Use KMerUtils.toPackedInt() in order to convert the sequence into an int for our purposes.&lt;br /&gt;
&lt;br /&gt;
Hint: calculatePossibleKmers and calculateSumOfAllKmers are distinct and take different input parameters. calculateSumOfAllKmers performs the n - k + 1 calculation on a list of arrays of sequences and sums the total. calculatePossibleKmers only takes the length of the k-mer (k) as the input parameter, meaning it will calculate possibilities independent of the size of the sequence. Thus, the n - k + 1 calculation would yield a value ranging from 0 (inclusive) to the result of calculatePossibleKmers (exclusive).&lt;br /&gt;
&lt;br /&gt;
==AtomicIntegerArrayKMerCounter==&lt;br /&gt;
{{CodeToImplement|AtomicIntegerArrayKMerCounter|parse|kmer.lab.atomicintegerarray}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This implementation is simply the parallel equivalent of the int array implementation. To do so, you will be making use of Java’s atomic version of an array: an AtomicIntegerArray. Like before, you will be need to complete the parse method.  Don't forget to balance the workload in this parallel implementation with your ThresholdSlices.  Make sure to select the most appropriate parallel fork loop.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
==ConcurrentBucketHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|ConcurrentBucketHashMapKMerCounter|parse|kmer.lab.concurrentbuckethashmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will go a step beyond the previous implementations and divide entries into 1024 buckets. Although a different number of buckets can be used, we will simply stick to 1024. Every time an entry is added to the bucket dictionary, it will create a hash based on the entry's key and place the entry into one of the 1024 buckets. Because we would only need to search one bucket for an entry based on a key's hash, this mainly serves the purpose of cutting down on search time. The compute method of the ConcurrentBucketHashMap should automatically compute the correct bucket for the entry, so this implementation should closely resemble the previous ConcurrentHashMap implementations. When instantiating the ConcurrentBucketHashMap, we recommend making the key/value pairs a Long and an Integer.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==ConcurrentBucketHashMap==&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;
For this section of the assignment you will create a thread safe data structure.  You have previously created a [[Iterator,_Collection,_and_Map#BucketsHashMap|non-thread safe Map]].  Now, with the help of multiple  [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html ReentrantReadWriteLocks] you will create a thread safe Map that both deals with contention and allows simultaneous gets. &lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ConcurrentBucketHashMap|constructor&amp;lt;br&amp;gt;getIndex&amp;lt;br&amp;gt;getBucket&amp;lt;br&amp;gt;getLock&amp;lt;br&amp;gt;getEntry&amp;lt;br&amp;gt;get&amp;lt;br&amp;gt;put&amp;lt;br&amp;gt;compute|kmer.lab.concurrentbuckethashmap}}&lt;br /&gt;
&lt;br /&gt;
===constructor===&lt;br /&gt;
{{Sequential|public ConcurrentBucketHashMap(int initialCapacity)}}&lt;br /&gt;
&lt;br /&gt;
initialCapacity can be treated as a hint of how many entries the Map can expect to receive.&lt;br /&gt;
&lt;br /&gt;
===private support methods===&lt;br /&gt;
{{Tip|Use HashUtils.toIndex(key,N)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private List&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; getBucket(Object key)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private ReadWriteLock getLock(Object key)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private static &amp;lt;K, V&amp;gt; Optional&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; getEntry(List&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; bucket, Object key)}}&lt;br /&gt;
&lt;br /&gt;
===public methods===&lt;br /&gt;
&lt;br /&gt;
Be sure to make each of these methods thread safe by acquiring the appropriate (read or write) lock for the appropriate bucket.&lt;br /&gt;
&lt;br /&gt;
Which method, [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock()] or [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#tryLock-- tryLock()], is the appropriate one to use for creating a thread safe data structure such as this?&lt;br /&gt;
&amp;lt;spoiler show=&amp;quot;spoiler&amp;quot; hide=&amp;quot;spoiler&amp;quot;&amp;gt;[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock()]&amp;lt;/spoiler&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever you acquire a lock, but sure to leverage [https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html try finally] and [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#unlock-- unlock()] the lock within the finally block.&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|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- spec]&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|public V put(K key, V value)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#put-K-V- spec]&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|public V compute(K key, BiFunction&amp;lt;? super K, ? super V, ? extends V&amp;gt; remappingFunction)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentMap.html#compute-K-java.util.function.BiFunction- spec]&lt;br /&gt;
&lt;br /&gt;
===advice===&lt;br /&gt;
&lt;br /&gt;
{{ warning | Do '''NOT''' call get() from compute().  It will result in a nonintuitive error.  getEntry(bucket, key) gives you everything you need. }}&lt;br /&gt;
&lt;br /&gt;
{{ warning | Do '''NOT''' call put() from compute() either.  Just do the work within the compute method. }}&lt;br /&gt;
&lt;br /&gt;
{{ tip | Be sure to make your ConcurrentBucketHashMap thread-safe.  If any code is going to access shared mutable data, it needs to do so in a thread-safe manner. }}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
A simple example of how to use the many incantations of isolated:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;@ThreadSafe&lt;br /&gt;
public class IsolatedInteger {&lt;br /&gt;
    private int value;&lt;br /&gt;
&lt;br /&gt;
    public IsolatedInteger(int value) {&lt;br /&gt;
        this.value = value;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public int getValue() {&lt;br /&gt;
        return isolatedWithReturn(objectBased(readMode(this)), () -&amp;gt; {&lt;br /&gt;
            return value;&lt;br /&gt;
        });&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public void increment() {&lt;br /&gt;
        isolated(objectBased(this), () -&amp;gt; {&lt;br /&gt;
            value++;&lt;br /&gt;
        });&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=Extra Credit=&lt;br /&gt;
==Open-ended K-mer Counter==&lt;br /&gt;
&lt;br /&gt;
This implementation is worth up to 4.5 extra credit points.&lt;br /&gt;
&lt;br /&gt;
It is intended as an opportunity to build a high-performing, thread-safe data structure.&lt;br /&gt;
&lt;br /&gt;
One possible path was outlined at a high level in class.  However, students are encouraged to think of alternate solutions.&lt;br /&gt;
&lt;br /&gt;
We have explicitly disallowed implementations akin to our matrix map reduce assignment, since we have already done the &amp;quot;give everyone their own data and combine in post&amp;quot; pattern to death.&lt;br /&gt;
&lt;br /&gt;
Partial credit is possible for solutions which demonstrate significant ingenuity.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|_KMerTestSuite|kmer.exercise}}&lt;br /&gt;
&lt;br /&gt;
=Rubric=&lt;br /&gt;
&lt;br /&gt;
Total points: 100&lt;br /&gt;
&lt;br /&gt;
* Correct ThresholdSlices (10)&lt;br /&gt;
* Correct and Parallel Long ConcurrentHashMap (25)&lt;br /&gt;
* Correct Int Array (15)&lt;br /&gt;
* Correct and Parallel AtomicIntegerArray (15)&lt;br /&gt;
* Correct and ThreadSafe ConcurrentBucketHashMap implementation (25)&lt;br /&gt;
* Clarity and efficiency (10)&lt;br /&gt;
&lt;br /&gt;
=Research=&lt;br /&gt;
[https://en.wikipedia.org/wiki/K-mer wikipedia article on k-mers]&lt;br /&gt;
&lt;br /&gt;
paper: [https://academic.oup.com/bioinformatics/article-lookup/doi/10.1093/bioinformatics/btr011 A fast, lock-free approach for efficient parallel counting of occurrences of k-mers] (Jellyfish)&lt;br /&gt;
&lt;br /&gt;
paper: [https://peerj.com/articles/cs-94/ Multiple comparative metagenomics using multiset k-mer counting]&lt;br /&gt;
&lt;br /&gt;
[http://homolog.us/Tutorials/index.php?p=3.7&amp;amp;s=2 Collection of approaches to k-mer counting]&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform Burrows–Wheeler transform]&lt;br /&gt;
&lt;br /&gt;
[http://biology.stackexchange.com/questions/45016/what-is-the-biological-significance-of-k-mer-counting Significance of k-mer Counting]&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Counting_Assignment&amp;diff=3665</id>
		<title>K Mer Counting Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Counting_Assignment&amp;diff=3665"/>
		<updated>2022-04-06T22:07:59Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* ThresholdSlices */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;credit for this assignment: John Gibson and Dennis Cosgrove&lt;br /&gt;
=Motivation=&lt;br /&gt;
K-mer counting is a real world application which provides the opportunity to build thread safe data structures which alleviate contention.&lt;br /&gt;
&lt;br /&gt;
Pulling real data from the National Institutes of Health forces us to address balancing our work.&lt;br /&gt;
&lt;br /&gt;
Finally, k-mer counting provides opportunities for performance engineering.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
The term k-mer refers to a substring of length k and k-mer counting refers to the process of finding the number of occurrences of a k-mer in a given string. This computational problem has many real-world applications, the most notable being in the field of computational genomics. In this assignment, you will design a program that will ultimately take in a human X-chromosome and count the number of k-mers in the string of DNA. You will be making use of everything you have learned thus far, including atomic variables. It is useful to know that given a string of length n, the amount of possible k-mers is equal to '''n - k + 1'''.&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;_oyWzImAx1w&amp;lt;/youtube&amp;gt;&lt;br /&gt;
=Demo=&lt;br /&gt;
{{CodeToInvestigate|OutputKmer|main|kmer.output}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;tFfa7DWLJ_U&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This demo video shows the big picture of KMer Counting. It included example usage of calculateSumOfAllKMers and calculatePossibleKMers method in &amp;lt;code&amp;gt;KMerUtils&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
==Runtime==&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#availableProcessors-- Runtime.getRuntime().availableProcessors()]&lt;br /&gt;
&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;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicIntegerArray.html AtomicIntegerArray].&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html ReentrantReadWriteLock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html#readLock-- readLock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html#writeLock-- writeLock]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html Lock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- unlock]&lt;br /&gt;
&lt;br /&gt;
==KMerUtils==&lt;br /&gt;
The following methods should be useful as you build the assignment.&lt;br /&gt;
&lt;br /&gt;
Check out the Javadocs either [http://www.cse.wustl.edu/~cosgroved/courses/cse231/f17/apidocs/kmer/core/KMerUtils.html on the web], or in the comments in KMerUtils.java for further details on how they work.&lt;br /&gt;
&lt;br /&gt;
===long calculatePossibleKMers(int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long calculatePossibleKMers(int kMerLength) {&lt;br /&gt;
	int bitsPerBase = 2;&lt;br /&gt;
	int bitsPerKMer = kMerLength * bitsPerBase;&lt;br /&gt;
	long possibleKMers = 1L &amp;lt;&amp;lt; bitsPerKMer;&lt;br /&gt;
	return possibleKMers;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toArrayLength(long possibleKMers)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toArrayLength(long possibleKMers) {&lt;br /&gt;
	// maximum array length is VM dependent, but must be indexed by an int.&lt;br /&gt;
	// reportedly some vms reserve some header words (see ArrayList) which puts its&lt;br /&gt;
	// limit as just below 2^31.&lt;br /&gt;
	// we will choose 2^30 (inclusive) as the maximum since it will hold the largest&lt;br /&gt;
	// potential k-mer (4^15)&lt;br /&gt;
	final long MAX_K_MER_ARRAY_LENGTH_INCLUSIVE = 1 &amp;lt;&amp;lt; 30;&lt;br /&gt;
	if (possibleKMers &amp;lt;= MAX_K_MER_ARRAY_LENGTH_INCLUSIVE) {&lt;br /&gt;
		return (int) possibleKMers;&lt;br /&gt;
	} else {&lt;br /&gt;
		throw new IllegalArgumentException(possibleKMers + &amp;quot; &amp;gt; &amp;quot; + MAX_K_MER_ARRAY_LENGTH_INCLUSIVE);&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int calculateSumOfAllKMers(List&amp;lt;byte[]&amp;gt; sequences, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int calculateSumOfAllKMers(List&amp;lt;byte[]&amp;gt; sequences, int kMerLength) {&lt;br /&gt;
	int sum = 0;&lt;br /&gt;
	for (byte[] sequence : sequences) {&lt;br /&gt;
		int i = (sequence.length - kMerLength + 1);&lt;br /&gt;
		if( i &amp;gt; 0 ) {&lt;br /&gt;
			sum += i;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return sum;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toPackedInt(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toPackedInt(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	int result = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		switch (sequence[offset + i]) {&lt;br /&gt;
		// case A:&lt;br /&gt;
		// result |= (A_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
		// break;&lt;br /&gt;
		case T:&lt;br /&gt;
			result |= (T_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case C:&lt;br /&gt;
			result |= (C_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case G:&lt;br /&gt;
			result |= (G_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toPackedInt(byte[] kMer)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toPackedInt(byte[] kMer) {&lt;br /&gt;
	return toPackedInt(kMer, 0, kMer.length);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===byte[] unpackInt(int kMer, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static byte[] unpackInt(int kMer, int kMerLength) {&lt;br /&gt;
	byte[] result = new byte[kMerLength];&lt;br /&gt;
&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		int v = (kMer &amp;gt;&amp;gt; (i + i)) &amp;amp; 0x3;&lt;br /&gt;
		switch (v) {&lt;br /&gt;
		case A_MASK_INT:&lt;br /&gt;
			result[i] = A;&lt;br /&gt;
			break;&lt;br /&gt;
		case T_MASK_INT:&lt;br /&gt;
			result[i] = T;&lt;br /&gt;
			break;&lt;br /&gt;
		case C_MASK_INT:&lt;br /&gt;
			result[i] = C;&lt;br /&gt;
			break;&lt;br /&gt;
		case G_MASK_INT:&lt;br /&gt;
			result[i] = G;&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===long toPackedLong(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long toPackedLong(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	long result = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		switch (sequence[offset + i]) {&lt;br /&gt;
		// case A:&lt;br /&gt;
		// result |= (A_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
		// break;&lt;br /&gt;
		case T:&lt;br /&gt;
			result |= (T_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case C:&lt;br /&gt;
			result |= (C_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case G:&lt;br /&gt;
			result |= (G_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===long toPackedLong(byte[] sequence)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long toPackedLong(byte[] sequence) {&lt;br /&gt;
	return toPackedLong(sequence, 0, sequence.length);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===byte[] unpackLong(long kMer, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static byte[] unpackLong(long kMer, int kMerLength) {&lt;br /&gt;
	byte[] result = new byte[kMerLength];&lt;br /&gt;
&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		long v = (kMer &amp;gt;&amp;gt; (i + i)) &amp;amp; 0x3;&lt;br /&gt;
		switch ((int) v) {&lt;br /&gt;
		case A_MASK_INT:&lt;br /&gt;
			result[i] = A;&lt;br /&gt;
			break;&lt;br /&gt;
		case T_MASK_INT:&lt;br /&gt;
			result[i] = T;&lt;br /&gt;
			break;&lt;br /&gt;
		case C_MASK_INT:&lt;br /&gt;
			result[i] = C;&lt;br /&gt;
			break;&lt;br /&gt;
		case G_MASK_INT:&lt;br /&gt;
			result[i] = G;&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==KMerCounts==&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/KMerCount.html interface KMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/map/MapKMerCount.html class MapKMerCount&amp;lt;T&amp;gt;]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/array/IntArrayKMerCount.html class IntArrayKMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/array/AtomicIntegerArrayKMerCount.html class AtomicIntegerArrayKMerCount]&lt;br /&gt;
&lt;br /&gt;
===MapKMerCount===&lt;br /&gt;
In order to provide a KMerCount that works with different k-mer type representations we have added an interface KMerCodec which provides functions to go to an from the byte[] representation:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public interface KMerCodec&amp;lt;T&amp;gt; {&lt;br /&gt;
	BiFunction&amp;lt;T, Integer, byte[]&amp;gt; decoder();&lt;br /&gt;
&lt;br /&gt;
	Function&amp;lt;byte[], T&amp;gt; encoder();&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As an example the instance for long -&amp;gt; byte[] and back simply utilizes the pack and unpack methods in KMerUtils:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public enum LongKMerCodec implements KMerCodec&amp;lt;Long&amp;gt; {&lt;br /&gt;
	INSTANCE;&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public Function&amp;lt;byte[], Long&amp;gt; encoder() {&lt;br /&gt;
		return KMerUtils::toPackedLong;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public BiFunction&amp;lt;Long, Integer, byte[]&amp;gt; decoder() {&lt;br /&gt;
		return KMerUtils::unpackLong;&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Creating an instance of a MapKMerCount to return from your KMerCounters will need to use the appropriate codec.  For example, if you have a map of type &amp;lt;code&amp;gt;Map&amp;lt;Long, Integer&amp;gt;&amp;lt;/code&amp;gt; which you wish to wrap and return, your code would look something like this:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new MapKMerCount&amp;lt;&amp;gt;(k, map, LongKMerCodec.INSTANCE);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===IntArrayKMerCount===&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new IntArrayKMerCount(k, array);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===AtomicIntegerArrayKMerCount===&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new AtomicIntegerArrayKMerCount(k, atomicIntegerArray);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Group Warmup=&lt;br /&gt;
Complete the [[String_Map_K_Mer_Assignment|group warm up]] first.&lt;br /&gt;
&lt;br /&gt;
=Lab=&lt;br /&gt;
{{ tip | Get ThresholdSlices working and then use it to balance all of your parallel KMerCounter implementations. }}&lt;br /&gt;
==ThresholdSlices==&lt;br /&gt;
===slicing===&lt;br /&gt;
{{CodeToImplement|ThresholdSlices|sliceKernel&amp;lt;br&amp;gt;createSlices&amp;lt;br&amp;gt;calculateReasonableThreshold|kmer.lab.balance}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|&amp;lt;br/&amp;gt;The javadoc incorrectly specify that &amp;quot;Any sequence whose length is less than or equal to k should be omitted.&amp;quot;&amp;lt;br/&amp;gt;The javadoc should read &amp;quot;Any sequence whose length is less than k should be omitted.&amp;quot;}}&lt;br /&gt;
&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;
{{Sequential|public static List&amp;lt;Slice&amp;lt;byte[]&amp;gt;&amp;gt; createSlices(List&amp;lt;byte[]&amp;gt; sequences, int k, IntPredicate slicePredicate)}}&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, which is the &amp;quot;helper&amp;quot; method, and the kernel contains the recursion.&lt;br /&gt;
&lt;br /&gt;
In the createSlices method, you should call the recursive kernel for each sequence 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 length of the slice 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;
&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;
&amp;lt;youtube&amp;gt;m56ee5spxiw&amp;lt;/youtube&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 threashold of range to be used in slicePredicate. 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;
==LongConcurrentMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|LongConcurrentMapKMerCounter|parse|kmer.lab.longconcurrentmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will also overlap greatly with everything you have done previously, but instead of using Strings or SequenceSlice, we will be using Longs to represent DNA data. In order to do this, refer to the KMerUtils class, more specifically the toPackedLong method which will convert a sequence of bytes into a Long for our purposes. &lt;br /&gt;
&lt;br /&gt;
{{Warning|Be sure to utilize the &amp;lt;code&amp;gt;Supplier&amp;lt;ConcurrentMap&amp;lt;Long, Integer&amp;gt;&amp;gt; concurrentMapFactory&amp;lt;/code&amp;gt; instance variable to create your ConcurrentMap.}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&amp;lt;youtube&amp;gt;d_Ze-YQ3Thc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Maps in Java often have a way to specify an initialCapacity to the constructor.  When used well, this parameter can give a hint as to how many Entries the Map should be expecting.  &lt;br /&gt;
&lt;br /&gt;
KMerUtils has methods which will calculate what you need to compute this value.  calculatePossibleKmers returns 4^k and calculateSumOfAllKmers returns n-k+1.  Think about how each method might be used to calculate an appropriate initial capacity.&lt;br /&gt;
&lt;br /&gt;
Some questions to ask yourself:&lt;br /&gt;
&lt;br /&gt;
# How many entries should the ConcurrentMap expect for a chromosome a million in length if k is 30?&lt;br /&gt;
# How many entries should the ConcurrentMap expect for a chromosome a million in length if k is 1?&lt;br /&gt;
&lt;br /&gt;
In order to test LongConcurrentMapKMerCounter with both java.util.ConcurrentHashMap&amp;lt;K,V&amp;gt; and your ConcurrentBucketHashMap&amp;lt;K,V&amp;gt; implementation, we pass a concurrentMapFactory to LongConcurrentMapKMerCounter's constructor.  You should use this factory to create your instance of ConcurrentMap via the apply method.  The Optional&amp;lt;Integer&amp;gt; parameter to this apply method is the initialCapacity.&lt;br /&gt;
&lt;br /&gt;
Note: anyone feeling the weight of a pandemic should feel free to pass Optional.empty() to the apply method.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IntArrayKMerCounter==&lt;br /&gt;
{{CodeToImplement|IntArrayKMerCounter|parse|kmer.lab.intarray}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
Like the String HashMap implementation, this implementation should be completed sequentially. However, unlike the previous implementations, we will be using an array instead of a map. This means that you must keep in mind what size to make the array in order to instantiate it. KMerUtils has a method which will calculate this number for you, but think about which one to use, why you would use it, and how that would differ from simply calculating n - k + 1 yourself. Think of using the int array as similar to using the map, but with ints as the key/value pairs. Use KMerUtils.toPackedInt() in order to convert the sequence into an int for our purposes.&lt;br /&gt;
&lt;br /&gt;
Hint: calculatePossibleKmers and calculateSumOfAllKmers are distinct and take different input parameters. calculateSumOfAllKmers performs the n - k + 1 calculation on a list of arrays of sequences and sums the total. calculatePossibleKmers only takes the length of the k-mer (k) as the input parameter, meaning it will calculate possibilities independent of the size of the sequence. Thus, the n - k + 1 calculation would yield a value ranging from 0 (inclusive) to the result of calculatePossibleKmers (exclusive).&lt;br /&gt;
&lt;br /&gt;
==AtomicIntegerArrayKMerCounter==&lt;br /&gt;
{{CodeToImplement|AtomicIntegerArrayKMerCounter|parse|kmer.lab.atomicintegerarray}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This implementation is simply the parallel equivalent of the int array implementation. To do so, you will be making use of Java’s atomic version of an array: an AtomicIntegerArray. Like before, you will be need to complete the parse method. You can choose to finish this method with a forall loop or a finish/async approach, which is completely up to you.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
==ConcurrentBucketHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|ConcurrentBucketHashMapKMerCounter|parse|kmer.lab.concurrentbuckethashmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will go a step beyond the previous implementations and divide entries into 1024 buckets. Although a different number of buckets can be used, we will simply stick to 1024. Every time an entry is added to the bucket dictionary, it will create a hash based on the entry's key and place the entry into one of the 1024 buckets. Because we would only need to search one bucket for an entry based on a key's hash, this mainly serves the purpose of cutting down on search time. The compute method of the ConcurrentBucketHashMap should automatically compute the correct bucket for the entry, so this implementation should closely resemble the previous ConcurrentHashMap implementations. When instantiating the ConcurrentBucketHashMap, we recommend making the key/value pairs a Long and an Integer.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
==ConcurrentBucketHashMap==&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;
For this section of the assignment you will create a thread safe data structure.  You have previously created a [[Iterator,_Collection,_and_Map#BucketsHashMap|non-thread safe Map]].  Now, with the help of multiple  [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html ReentrantReadWriteLocks] you will create a thread safe Map that both deals with contention and allows simultaneous gets. &lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ConcurrentBucketHashMap|constructor&amp;lt;br&amp;gt;getIndex&amp;lt;br&amp;gt;getBucket&amp;lt;br&amp;gt;getLock&amp;lt;br&amp;gt;getEntry&amp;lt;br&amp;gt;get&amp;lt;br&amp;gt;put&amp;lt;br&amp;gt;compute|kmer.lab.concurrentbuckethashmap}}&lt;br /&gt;
&lt;br /&gt;
===constructor===&lt;br /&gt;
{{Sequential|public ConcurrentBucketHashMap(int initialCapacity)}}&lt;br /&gt;
&lt;br /&gt;
initialCapacity can be treated as a hint of how many entries the Map can expect to receive.&lt;br /&gt;
&lt;br /&gt;
===private support methods===&lt;br /&gt;
{{Tip|Use HashUtils.toIndex(key,N)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private List&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; getBucket(Object key)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private ReadWriteLock getLock(Object key)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private static &amp;lt;K, V&amp;gt; Optional&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; getEntry(List&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; bucket, Object key)}}&lt;br /&gt;
&lt;br /&gt;
===public methods===&lt;br /&gt;
&lt;br /&gt;
Be sure to make each of these methods thread safe by acquiring the appropriate (read or write) lock for the appropriate bucket.&lt;br /&gt;
&lt;br /&gt;
Which method, [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock()] or [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#tryLock-- tryLock()], is the appropriate one to use for creating a thread safe data structure such as this?&lt;br /&gt;
&amp;lt;spoiler show=&amp;quot;spoiler&amp;quot; hide=&amp;quot;spoiler&amp;quot;&amp;gt;[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock()]&amp;lt;/spoiler&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever you acquire a lock, but sure to leverage [https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html try finally] and [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#unlock-- unlock()] the lock within the finally block.&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|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- spec]&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|public V put(K key, V value)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#put-K-V- spec]&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|public V compute(K key, BiFunction&amp;lt;? super K, ? super V, ? extends V&amp;gt; remappingFunction)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentMap.html#compute-K-java.util.function.BiFunction- spec]&lt;br /&gt;
&lt;br /&gt;
===advice===&lt;br /&gt;
&lt;br /&gt;
{{ warning | Do '''NOT''' call get() from compute().  It will result in a nonintuitive error.  getEntry(bucket, key) gives you everything you need. }}&lt;br /&gt;
&lt;br /&gt;
{{ warning | Do '''NOT''' call put() from compute() either.  Just do the work within the compute method. }}&lt;br /&gt;
&lt;br /&gt;
{{ tip | Be sure to make your ConcurrentBucketHashMap thread-safe.  If any code is going to access shared mutable data, it needs to do so in a thread-safe manner. }}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
A simple example of how to use the many incantations of isolated:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;@ThreadSafe&lt;br /&gt;
public class IsolatedInteger {&lt;br /&gt;
    private int value;&lt;br /&gt;
&lt;br /&gt;
    public IsolatedInteger(int value) {&lt;br /&gt;
        this.value = value;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public int getValue() {&lt;br /&gt;
        return isolatedWithReturn(objectBased(readMode(this)), () -&amp;gt; {&lt;br /&gt;
            return value;&lt;br /&gt;
        });&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public void increment() {&lt;br /&gt;
        isolated(objectBased(this), () -&amp;gt; {&lt;br /&gt;
            value++;&lt;br /&gt;
        });&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=Extra Credit=&lt;br /&gt;
==Open-ended K-mer Counter==&lt;br /&gt;
&lt;br /&gt;
This implementation is worth up to 4.5 extra credit points.&lt;br /&gt;
&lt;br /&gt;
It is intended as an opportunity to build a high-performing, thread-safe data structure.&lt;br /&gt;
&lt;br /&gt;
One possible path was outlined at a high level in class.  However, students are encouraged to think of alternate solutions.&lt;br /&gt;
&lt;br /&gt;
We have explicitly disallowed implementations akin to our matrix map reduce assignment, since we have already done the &amp;quot;give everyone their own data and combine in post&amp;quot; pattern to death.&lt;br /&gt;
&lt;br /&gt;
Partial credit is possible for solutions which demonstrate significant ingenuity.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|KMerTestSuite|kmer.lab}}&lt;br /&gt;
&lt;br /&gt;
=Rubric=&lt;br /&gt;
&lt;br /&gt;
Total points: 100&lt;br /&gt;
&lt;br /&gt;
* Correct ThresholdSlices (10)&lt;br /&gt;
* Correct and Parallel Long ConcurrentHashMap (25)&lt;br /&gt;
* Correct Int Array (15)&lt;br /&gt;
* Correct and Parallel AtomicIntegerArray (15)&lt;br /&gt;
* Correct and ThreadSafe ConcurrentBucketHashMap implementation (25)&lt;br /&gt;
* Clarity and efficiency (10)&lt;br /&gt;
&lt;br /&gt;
=Research=&lt;br /&gt;
[https://en.wikipedia.org/wiki/K-mer wikipedia article on k-mers]&lt;br /&gt;
&lt;br /&gt;
paper: [https://academic.oup.com/bioinformatics/article-lookup/doi/10.1093/bioinformatics/btr011 A fast, lock-free approach for efficient parallel counting of occurrences of k-mers] (Jellyfish)&lt;br /&gt;
&lt;br /&gt;
paper: [https://peerj.com/articles/cs-94/ Multiple comparative metagenomics using multiset k-mer counting]&lt;br /&gt;
&lt;br /&gt;
[http://homolog.us/Tutorials/index.php?p=3.7&amp;amp;s=2 Collection of approaches to k-mer counting]&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform Burrows–Wheeler transform]&lt;br /&gt;
&lt;br /&gt;
[http://biology.stackexchange.com/questions/45016/what-is-the-biological-significance-of-k-mer-counting Significance of k-mer Counting]&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Counting_Assignment&amp;diff=3664</id>
		<title>K Mer Counting Assignment</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=K_Mer_Counting_Assignment&amp;diff=3664"/>
		<updated>2022-04-06T21:25:01Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* Demo */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;credit for this assignment: John Gibson and Dennis Cosgrove&lt;br /&gt;
=Motivation=&lt;br /&gt;
K-mer counting is a real world application which provides the opportunity to build thread safe data structures which alleviate contention.&lt;br /&gt;
&lt;br /&gt;
Pulling real data from the National Institutes of Health forces us to address balancing our work.&lt;br /&gt;
&lt;br /&gt;
Finally, k-mer counting provides opportunities for performance engineering.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
The term k-mer refers to a substring of length k and k-mer counting refers to the process of finding the number of occurrences of a k-mer in a given string. This computational problem has many real-world applications, the most notable being in the field of computational genomics. In this assignment, you will design a program that will ultimately take in a human X-chromosome and count the number of k-mers in the string of DNA. You will be making use of everything you have learned thus far, including atomic variables. It is useful to know that given a string of length n, the amount of possible k-mers is equal to '''n - k + 1'''.&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;_oyWzImAx1w&amp;lt;/youtube&amp;gt;&lt;br /&gt;
=Demo=&lt;br /&gt;
{{CodeToInvestigate|OutputKmer|main|kmer.output}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;tFfa7DWLJ_U&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This demo video shows the big picture of KMer Counting. It included example usage of calculateSumOfAllKMers and calculatePossibleKMers method in &amp;lt;code&amp;gt;KMerUtils&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
==Runtime==&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#availableProcessors-- Runtime.getRuntime().availableProcessors()]&lt;br /&gt;
&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;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicIntegerArray.html AtomicIntegerArray].&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html ReentrantReadWriteLock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html#readLock-- readLock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html#writeLock-- writeLock]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html Lock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- unlock]&lt;br /&gt;
&lt;br /&gt;
==KMerUtils==&lt;br /&gt;
The following methods should be useful as you build the assignment.&lt;br /&gt;
&lt;br /&gt;
Check out the Javadocs either [http://www.cse.wustl.edu/~cosgroved/courses/cse231/f17/apidocs/kmer/core/KMerUtils.html on the web], or in the comments in KMerUtils.java for further details on how they work.&lt;br /&gt;
&lt;br /&gt;
===long calculatePossibleKMers(int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long calculatePossibleKMers(int kMerLength) {&lt;br /&gt;
	int bitsPerBase = 2;&lt;br /&gt;
	int bitsPerKMer = kMerLength * bitsPerBase;&lt;br /&gt;
	long possibleKMers = 1L &amp;lt;&amp;lt; bitsPerKMer;&lt;br /&gt;
	return possibleKMers;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toArrayLength(long possibleKMers)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toArrayLength(long possibleKMers) {&lt;br /&gt;
	// maximum array length is VM dependent, but must be indexed by an int.&lt;br /&gt;
	// reportedly some vms reserve some header words (see ArrayList) which puts its&lt;br /&gt;
	// limit as just below 2^31.&lt;br /&gt;
	// we will choose 2^30 (inclusive) as the maximum since it will hold the largest&lt;br /&gt;
	// potential k-mer (4^15)&lt;br /&gt;
	final long MAX_K_MER_ARRAY_LENGTH_INCLUSIVE = 1 &amp;lt;&amp;lt; 30;&lt;br /&gt;
	if (possibleKMers &amp;lt;= MAX_K_MER_ARRAY_LENGTH_INCLUSIVE) {&lt;br /&gt;
		return (int) possibleKMers;&lt;br /&gt;
	} else {&lt;br /&gt;
		throw new IllegalArgumentException(possibleKMers + &amp;quot; &amp;gt; &amp;quot; + MAX_K_MER_ARRAY_LENGTH_INCLUSIVE);&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int calculateSumOfAllKMers(List&amp;lt;byte[]&amp;gt; sequences, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int calculateSumOfAllKMers(List&amp;lt;byte[]&amp;gt; sequences, int kMerLength) {&lt;br /&gt;
	int sum = 0;&lt;br /&gt;
	for (byte[] sequence : sequences) {&lt;br /&gt;
		int i = (sequence.length - kMerLength + 1);&lt;br /&gt;
		if( i &amp;gt; 0 ) {&lt;br /&gt;
			sum += i;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return sum;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toPackedInt(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toPackedInt(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	int result = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		switch (sequence[offset + i]) {&lt;br /&gt;
		// case A:&lt;br /&gt;
		// result |= (A_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
		// break;&lt;br /&gt;
		case T:&lt;br /&gt;
			result |= (T_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case C:&lt;br /&gt;
			result |= (C_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case G:&lt;br /&gt;
			result |= (G_MASK_INT &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===int toPackedInt(byte[] kMer)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static int toPackedInt(byte[] kMer) {&lt;br /&gt;
	return toPackedInt(kMer, 0, kMer.length);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===byte[] unpackInt(int kMer, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static byte[] unpackInt(int kMer, int kMerLength) {&lt;br /&gt;
	byte[] result = new byte[kMerLength];&lt;br /&gt;
&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		int v = (kMer &amp;gt;&amp;gt; (i + i)) &amp;amp; 0x3;&lt;br /&gt;
		switch (v) {&lt;br /&gt;
		case A_MASK_INT:&lt;br /&gt;
			result[i] = A;&lt;br /&gt;
			break;&lt;br /&gt;
		case T_MASK_INT:&lt;br /&gt;
			result[i] = T;&lt;br /&gt;
			break;&lt;br /&gt;
		case C_MASK_INT:&lt;br /&gt;
			result[i] = C;&lt;br /&gt;
			break;&lt;br /&gt;
		case G_MASK_INT:&lt;br /&gt;
			result[i] = G;&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===long toPackedLong(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long toPackedLong(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	long result = 0;&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		switch (sequence[offset + i]) {&lt;br /&gt;
		// case A:&lt;br /&gt;
		// result |= (A_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
		// break;&lt;br /&gt;
		case T:&lt;br /&gt;
			result |= (T_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case C:&lt;br /&gt;
			result |= (C_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		case G:&lt;br /&gt;
			result |= (G_MASK_LONG &amp;lt;&amp;lt; (i + i));&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===long toPackedLong(byte[] sequence)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static long toPackedLong(byte[] sequence) {&lt;br /&gt;
	return toPackedLong(sequence, 0, sequence.length);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===byte[] unpackLong(long kMer, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static byte[] unpackLong(long kMer, int kMerLength) {&lt;br /&gt;
	byte[] result = new byte[kMerLength];&lt;br /&gt;
&lt;br /&gt;
	for (int i = 0; i &amp;lt; kMerLength; i++) {&lt;br /&gt;
		long v = (kMer &amp;gt;&amp;gt; (i + i)) &amp;amp; 0x3;&lt;br /&gt;
		switch ((int) v) {&lt;br /&gt;
		case A_MASK_INT:&lt;br /&gt;
			result[i] = A;&lt;br /&gt;
			break;&lt;br /&gt;
		case T_MASK_INT:&lt;br /&gt;
			result[i] = T;&lt;br /&gt;
			break;&lt;br /&gt;
		case C_MASK_INT:&lt;br /&gt;
			result[i] = C;&lt;br /&gt;
			break;&lt;br /&gt;
		case G_MASK_INT:&lt;br /&gt;
			result[i] = G;&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	return result;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==KMerCounts==&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/KMerCount.html interface KMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/map/MapKMerCount.html class MapKMerCount&amp;lt;T&amp;gt;]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/array/IntArrayKMerCount.html class IntArrayKMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/kmer/core/array/AtomicIntegerArrayKMerCount.html class AtomicIntegerArrayKMerCount]&lt;br /&gt;
&lt;br /&gt;
===MapKMerCount===&lt;br /&gt;
In order to provide a KMerCount that works with different k-mer type representations we have added an interface KMerCodec which provides functions to go to an from the byte[] representation:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public interface KMerCodec&amp;lt;T&amp;gt; {&lt;br /&gt;
	BiFunction&amp;lt;T, Integer, byte[]&amp;gt; decoder();&lt;br /&gt;
&lt;br /&gt;
	Function&amp;lt;byte[], T&amp;gt; encoder();&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As an example the instance for long -&amp;gt; byte[] and back simply utilizes the pack and unpack methods in KMerUtils:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public enum LongKMerCodec implements KMerCodec&amp;lt;Long&amp;gt; {&lt;br /&gt;
	INSTANCE;&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public Function&amp;lt;byte[], Long&amp;gt; encoder() {&lt;br /&gt;
		return KMerUtils::toPackedLong;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	@Override&lt;br /&gt;
	public BiFunction&amp;lt;Long, Integer, byte[]&amp;gt; decoder() {&lt;br /&gt;
		return KMerUtils::unpackLong;&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Creating an instance of a MapKMerCount to return from your KMerCounters will need to use the appropriate codec.  For example, if you have a map of type &amp;lt;code&amp;gt;Map&amp;lt;Long, Integer&amp;gt;&amp;lt;/code&amp;gt; which you wish to wrap and return, your code would look something like this:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new MapKMerCount&amp;lt;&amp;gt;(k, map, LongKMerCodec.INSTANCE);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===IntArrayKMerCount===&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new IntArrayKMerCount(k, array);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===AtomicIntegerArrayKMerCount===&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;return new AtomicIntegerArrayKMerCount(k, atomicIntegerArray);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Group Warmup=&lt;br /&gt;
Complete the [[String_Map_K_Mer_Assignment|group warm up]] first.&lt;br /&gt;
&lt;br /&gt;
=Lab=&lt;br /&gt;
{{ tip | Get ThresholdSlices working and then use it to balance all of your parallel KMerCounter implementations. }}&lt;br /&gt;
==ThresholdSlices==&lt;br /&gt;
===slicing===&lt;br /&gt;
{{CodeToImplement|ThresholdSlices|sliceKernel&amp;lt;br&amp;gt;createSlices&amp;lt;br&amp;gt;calculateReasonableThreshold|kmer.lab.balance}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|&amp;lt;br/&amp;gt;The javadoc incorrectly specify that &amp;quot;Any sequence whose length is less than or equal to k should be omitted.&amp;quot;&amp;lt;br/&amp;gt;The javadoc should read &amp;quot;Any sequence whose length is less than k should be omitted.&amp;quot;}}&lt;br /&gt;
&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;
{{Sequential|public static List&amp;lt;Slice&amp;lt;byte[]&amp;gt;&amp;gt; createSlices(List&amp;lt;byte[]&amp;gt; sequences, int k, IntPredicate slicePredicate)}}&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 call the sliceKernel once, but the kernel should take over the recursion from there.&lt;br /&gt;
&lt;br /&gt;
In the createSlices method, you should call the recursive kernel for each sequence 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 length of the slice 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;
&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;
&amp;lt;youtube&amp;gt;m56ee5spxiw&amp;lt;/youtube&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;
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;
==LongConcurrentMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|LongConcurrentMapKMerCounter|parse|kmer.lab.longconcurrentmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will also overlap greatly with everything you have done previously, but instead of using Strings or SequenceSlice, we will be using Longs to represent DNA data. In order to do this, refer to the KMerUtils class, more specifically the toPackedLong method which will convert a sequence of bytes into a Long for our purposes. &lt;br /&gt;
&lt;br /&gt;
{{Warning|Be sure to utilize the &amp;lt;code&amp;gt;Supplier&amp;lt;ConcurrentMap&amp;lt;Long, Integer&amp;gt;&amp;gt; concurrentMapFactory&amp;lt;/code&amp;gt; instance variable to create your ConcurrentMap.}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
&amp;lt;youtube&amp;gt;d_Ze-YQ3Thc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Maps in Java often have a way to specify an initialCapacity to the constructor.  When used well, this parameter can give a hint as to how many Entries the Map should be expecting.  &lt;br /&gt;
&lt;br /&gt;
KMerUtils has methods which will calculate what you need to compute this value.  calculatePossibleKmers returns 4^k and calculateSumOfAllKmers returns n-k+1.  Think about how each method might be used to calculate an appropriate initial capacity.&lt;br /&gt;
&lt;br /&gt;
Some questions to ask yourself:&lt;br /&gt;
&lt;br /&gt;
# How many entries should the ConcurrentMap expect for a chromosome a million in length if k is 30?&lt;br /&gt;
# How many entries should the ConcurrentMap expect for a chromosome a million in length if k is 1?&lt;br /&gt;
&lt;br /&gt;
In order to test LongConcurrentMapKMerCounter with both java.util.ConcurrentHashMap&amp;lt;K,V&amp;gt; and your ConcurrentBucketHashMap&amp;lt;K,V&amp;gt; implementation, we pass a concurrentMapFactory to LongConcurrentMapKMerCounter's constructor.  You should use this factory to create your instance of ConcurrentMap via the apply method.  The Optional&amp;lt;Integer&amp;gt; parameter to this apply method is the initialCapacity.&lt;br /&gt;
&lt;br /&gt;
Note: anyone feeling the weight of a pandemic should feel free to pass Optional.empty() to the apply method.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IntArrayKMerCounter==&lt;br /&gt;
{{CodeToImplement|IntArrayKMerCounter|parse|kmer.lab.intarray}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
Like the String HashMap implementation, this implementation should be completed sequentially. However, unlike the previous implementations, we will be using an array instead of a map. This means that you must keep in mind what size to make the array in order to instantiate it. KMerUtils has a method which will calculate this number for you, but think about which one to use, why you would use it, and how that would differ from simply calculating n - k + 1 yourself. Think of using the int array as similar to using the map, but with ints as the key/value pairs. Use KMerUtils.toPackedInt() in order to convert the sequence into an int for our purposes.&lt;br /&gt;
&lt;br /&gt;
Hint: calculatePossibleKmers and calculateSumOfAllKmers are distinct and take different input parameters. calculateSumOfAllKmers performs the n - k + 1 calculation on a list of arrays of sequences and sums the total. calculatePossibleKmers only takes the length of the k-mer (k) as the input parameter, meaning it will calculate possibilities independent of the size of the sequence. Thus, the n - k + 1 calculation would yield a value ranging from 0 (inclusive) to the result of calculatePossibleKmers (exclusive).&lt;br /&gt;
&lt;br /&gt;
==AtomicIntegerArrayKMerCounter==&lt;br /&gt;
{{CodeToImplement|AtomicIntegerArrayKMerCounter|parse|kmer.lab.atomicintegerarray}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This implementation is simply the parallel equivalent of the int array implementation. To do so, you will be making use of Java’s atomic version of an array: an AtomicIntegerArray. Like before, you will be need to complete the parse method. You can choose to finish this method with a forall loop or a finish/async approach, which is completely up to you.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
==ConcurrentBucketHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|ConcurrentBucketHashMapKMerCounter|parse|kmer.lab.concurrentbuckethashmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will go a step beyond the previous implementations and divide entries into 1024 buckets. Although a different number of buckets can be used, we will simply stick to 1024. Every time an entry is added to the bucket dictionary, it will create a hash based on the entry's key and place the entry into one of the 1024 buckets. Because we would only need to search one bucket for an entry based on a key's hash, this mainly serves the purpose of cutting down on search time. The compute method of the ConcurrentBucketHashMap should automatically compute the correct bucket for the entry, so this implementation should closely resemble the previous ConcurrentHashMap implementations. When instantiating the ConcurrentBucketHashMap, we recommend making the key/value pairs a Long and an Integer.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
==ConcurrentBucketHashMap==&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;
For this section of the assignment you will create a thread safe data structure.  You have previously created a [[Iterator,_Collection,_and_Map#BucketsHashMap|non-thread safe Map]].  Now, with the help of multiple  [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html ReentrantReadWriteLocks] you will create a thread safe Map that both deals with contention and allows simultaneous gets. &lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ConcurrentBucketHashMap|constructor&amp;lt;br&amp;gt;getIndex&amp;lt;br&amp;gt;getBucket&amp;lt;br&amp;gt;getLock&amp;lt;br&amp;gt;getEntry&amp;lt;br&amp;gt;get&amp;lt;br&amp;gt;put&amp;lt;br&amp;gt;compute|kmer.lab.concurrentbuckethashmap}}&lt;br /&gt;
&lt;br /&gt;
===constructor===&lt;br /&gt;
{{Sequential|public ConcurrentBucketHashMap(int initialCapacity)}}&lt;br /&gt;
&lt;br /&gt;
initialCapacity can be treated as a hint of how many entries the Map can expect to receive.&lt;br /&gt;
&lt;br /&gt;
===private support methods===&lt;br /&gt;
{{Tip|Use HashUtils.toIndex(key,N)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private List&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; getBucket(Object key)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private ReadWriteLock getLock(Object key)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private static &amp;lt;K, V&amp;gt; Optional&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; getEntry(List&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; bucket, Object key)}}&lt;br /&gt;
&lt;br /&gt;
===public methods===&lt;br /&gt;
&lt;br /&gt;
Be sure to make each of these methods thread safe by acquiring the appropriate (read or write) lock for the appropriate bucket.&lt;br /&gt;
&lt;br /&gt;
Which method, [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock()] or [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#tryLock-- tryLock()], is the appropriate one to use for creating a thread safe data structure such as this?&lt;br /&gt;
&amp;lt;spoiler show=&amp;quot;spoiler&amp;quot; hide=&amp;quot;spoiler&amp;quot;&amp;gt;[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#lock-- lock()]&amp;lt;/spoiler&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever you acquire a lock, but sure to leverage [https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html try finally] and [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html#unlock-- unlock()] the lock within the finally block.&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|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- spec]&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|public V put(K key, V value)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#put-K-V- spec]&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|public V compute(K key, BiFunction&amp;lt;? super K, ? super V, ? extends V&amp;gt; remappingFunction)}}&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentMap.html#compute-K-java.util.function.BiFunction- spec]&lt;br /&gt;
&lt;br /&gt;
===advice===&lt;br /&gt;
&lt;br /&gt;
{{ warning | Do '''NOT''' call get() from compute().  It will result in a nonintuitive error.  getEntry(bucket, key) gives you everything you need. }}&lt;br /&gt;
&lt;br /&gt;
{{ warning | Do '''NOT''' call put() from compute() either.  Just do the work within the compute method. }}&lt;br /&gt;
&lt;br /&gt;
{{ tip | Be sure to make your ConcurrentBucketHashMap thread-safe.  If any code is going to access shared mutable data, it needs to do so in a thread-safe manner. }}&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
A simple example of how to use the many incantations of isolated:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;@ThreadSafe&lt;br /&gt;
public class IsolatedInteger {&lt;br /&gt;
    private int value;&lt;br /&gt;
&lt;br /&gt;
    public IsolatedInteger(int value) {&lt;br /&gt;
        this.value = value;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public int getValue() {&lt;br /&gt;
        return isolatedWithReturn(objectBased(readMode(this)), () -&amp;gt; {&lt;br /&gt;
            return value;&lt;br /&gt;
        });&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public void increment() {&lt;br /&gt;
        isolated(objectBased(this), () -&amp;gt; {&lt;br /&gt;
            value++;&lt;br /&gt;
        });&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
=Extra Credit=&lt;br /&gt;
==Open-ended K-mer Counter==&lt;br /&gt;
&lt;br /&gt;
This implementation is worth up to 4.5 extra credit points.&lt;br /&gt;
&lt;br /&gt;
It is intended as an opportunity to build a high-performing, thread-safe data structure.&lt;br /&gt;
&lt;br /&gt;
One possible path was outlined at a high level in class.  However, students are encouraged to think of alternate solutions.&lt;br /&gt;
&lt;br /&gt;
We have explicitly disallowed implementations akin to our matrix map reduce assignment, since we have already done the &amp;quot;give everyone their own data and combine in post&amp;quot; pattern to death.&lt;br /&gt;
&lt;br /&gt;
Partial credit is possible for solutions which demonstrate significant ingenuity.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|KMerTestSuite|kmer.lab}}&lt;br /&gt;
&lt;br /&gt;
=Rubric=&lt;br /&gt;
&lt;br /&gt;
Total points: 100&lt;br /&gt;
&lt;br /&gt;
* Correct ThresholdSlices (10)&lt;br /&gt;
* Correct and Parallel Long ConcurrentHashMap (25)&lt;br /&gt;
* Correct Int Array (15)&lt;br /&gt;
* Correct and Parallel AtomicIntegerArray (15)&lt;br /&gt;
* Correct and ThreadSafe ConcurrentBucketHashMap implementation (25)&lt;br /&gt;
* Clarity and efficiency (10)&lt;br /&gt;
&lt;br /&gt;
=Research=&lt;br /&gt;
[https://en.wikipedia.org/wiki/K-mer wikipedia article on k-mers]&lt;br /&gt;
&lt;br /&gt;
paper: [https://academic.oup.com/bioinformatics/article-lookup/doi/10.1093/bioinformatics/btr011 A fast, lock-free approach for efficient parallel counting of occurrences of k-mers] (Jellyfish)&lt;br /&gt;
&lt;br /&gt;
paper: [https://peerj.com/articles/cs-94/ Multiple comparative metagenomics using multiset k-mer counting]&lt;br /&gt;
&lt;br /&gt;
[http://homolog.us/Tutorials/index.php?p=3.7&amp;amp;s=2 Collection of approaches to k-mer counting]&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform Burrows–Wheeler transform]&lt;br /&gt;
&lt;br /&gt;
[http://biology.stackexchange.com/questions/45016/what-is-the-biological-significance-of-k-mer-counting Significance of k-mer Counting]&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=String_Map_K_Mer_Assignment&amp;diff=3663</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=3663"/>
		<updated>2022-04-06T19:11:21Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* ByteArrayRangeConcurrentHashMapKMerCounter */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&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 Concurrent==&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html ConcurrentHashMap]&lt;br /&gt;
&lt;br /&gt;
==KMerUtils==&lt;br /&gt;
The following methods should be useful as you build the assignment.&lt;br /&gt;
&lt;br /&gt;
Check out the Javadocs either [http://www.cse.wustl.edu/~cosgroved/courses/cse231/f17/apidocs/kmer/core/KMerUtils.html on the web], or in the comments in KMerUtils.java for further details on how they work.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===String toString(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static String toString(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	return new String(sequence, offset, kMerLength, StandardCharsets.UTF_8);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==KMerCounts==&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/KMerCount.html interface KMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/map/StringMapKMerCount.html class StringMapKMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/map/ByteArrayRangeMapKMerCount.html class ByteArrayRangeMapKMerCount]&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==StringHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|StringHashMapKMerCounter|parse|kmer.group.stringmap}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public KMerCount 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 MapKMerCount(which takes in a map), a class provided to you which does exactly what its name suggests. In order to implement this method, we recommend taking a look at the KMerUtils class and more specifically the toString method, which converts a given byte array into a String in order to make it compatible with the String HashMap.&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 KMerUtils.toString() method to create a string to use 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;
&amp;lt;youtube&amp;gt;Bwv3QMN0Mk4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==StringConcurrentHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|StringConcurrentHashMapKMerCounter|parse|kmer.group.stringmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount 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 atomic version of a HashMap: a ConcurrentHashMap. Like before, you will be need to complete the parse method. You can choose to finish this method with a forall loop or a finish/async approach, which is completely up to you.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;a9CK1BPzESM&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;4M9abyUyzxU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=ByteArrayRangeConcurrentHashMapKMerCounter=&lt;br /&gt;
{{CodeToImplement|ByteArrayRangeConcurrentHashMapKMerCounter|parse|kmer.group.bytearrayrangemap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will overlap greatly with your previous two implementations, but it will use ByteArrayRange instead of Strings and use the corresponding [[#KMerCounts |KMerCount]] implementation. Refer to the ByteArrayRange class in &amp;lt;code&amp;gt;kmer.core.bytearrayrange&amp;lt;/code&amp;gt; in order to get a better idea of how to implement this method and instantiate an instance of the class. The class closely resembles the Slice class you worked on earlier in the semester.&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|KMerWarmupTestSuite|kmer.group}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=String_Map_K_Mer_Assignment&amp;diff=3662</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=3662"/>
		<updated>2022-04-06T18:59:16Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* ByteArrayRangeConcurrentHashMapKMerCounter */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&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 Concurrent==&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html ConcurrentHashMap]&lt;br /&gt;
&lt;br /&gt;
==KMerUtils==&lt;br /&gt;
The following methods should be useful as you build the assignment.&lt;br /&gt;
&lt;br /&gt;
Check out the Javadocs either [http://www.cse.wustl.edu/~cosgroved/courses/cse231/f17/apidocs/kmer/core/KMerUtils.html on the web], or in the comments in KMerUtils.java for further details on how they work.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===String toString(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static String toString(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	return new String(sequence, offset, kMerLength, StandardCharsets.UTF_8);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==KMerCounts==&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/KMerCount.html interface KMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/map/StringMapKMerCount.html class StringMapKMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/map/ByteArrayRangeMapKMerCount.html class ByteArrayRangeMapKMerCount]&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==StringHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|StringHashMapKMerCounter|parse|kmer.group.stringmap}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public KMerCount 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 MapKMerCount(which takes in a map), a class provided to you which does exactly what its name suggests. In order to implement this method, we recommend taking a look at the KMerUtils class and more specifically the toString method, which converts a given byte array into a String in order to make it compatible with the String HashMap.&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 KMerUtils.toString() method to create a string to use 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;
&amp;lt;youtube&amp;gt;Bwv3QMN0Mk4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==StringConcurrentHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|StringConcurrentHashMapKMerCounter|parse|kmer.group.stringmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount 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 atomic version of a HashMap: a ConcurrentHashMap. Like before, you will be need to complete the parse method. You can choose to finish this method with a forall loop or a finish/async approach, which is completely up to you.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;a9CK1BPzESM&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;4M9abyUyzxU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=ByteArrayRangeConcurrentHashMapKMerCounter=&lt;br /&gt;
{{CodeToImplement|ByteArrayRangeConcurrentHashMapKMerCounter|parse|kmer.group.bytearrayrangemap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will overlap greatly with your previous two implementations, but it will use ByteArrayRange instead of Strings. Refer to the ByteArrayRange class in &amp;lt;code&amp;gt;kmer.core.bytearrayrange&amp;lt;/code&amp;gt; in order to get a better idea of how to implement this method and instantiate an instance of the class. The class closely resembles the Slice class you worked on earlier in the semester.&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|KMerWarmupTestSuite|kmer.group}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=String_Map_K_Mer_Assignment&amp;diff=3661</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=3661"/>
		<updated>2022-04-06T18:54:28Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* ByteArrayRangeConcurrentHashMapKMerCounter */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&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 Concurrent==&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html ConcurrentHashMap]&lt;br /&gt;
&lt;br /&gt;
==KMerUtils==&lt;br /&gt;
The following methods should be useful as you build the assignment.&lt;br /&gt;
&lt;br /&gt;
Check out the Javadocs either [http://www.cse.wustl.edu/~cosgroved/courses/cse231/f17/apidocs/kmer/core/KMerUtils.html on the web], or in the comments in KMerUtils.java for further details on how they work.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===String toString(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static String toString(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	return new String(sequence, offset, kMerLength, StandardCharsets.UTF_8);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==KMerCounts==&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/KMerCount.html interface KMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/map/StringMapKMerCount.html class StringMapKMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/map/ByteArrayRangeMapKMerCount.html class ByteArrayRangeMapKMerCount]&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==StringHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|StringHashMapKMerCounter|parse|kmer.group.stringmap}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public KMerCount 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 MapKMerCount(which takes in a map), a class provided to you which does exactly what its name suggests. In order to implement this method, we recommend taking a look at the KMerUtils class and more specifically the toString method, which converts a given byte array into a String in order to make it compatible with the String HashMap.&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 KMerUtils.toString() method to create a string to use 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;
&amp;lt;youtube&amp;gt;Bwv3QMN0Mk4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==StringConcurrentHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|StringConcurrentHashMapKMerCounter|parse|kmer.group.stringmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount 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 atomic version of a HashMap: a ConcurrentHashMap. Like before, you will be need to complete the parse method. You can choose to finish this method with a forall loop or a finish/async approach, which is completely up to you.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;a9CK1BPzESM&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;4M9abyUyzxU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=ByteArrayRangeConcurrentHashMapKMerCounter=&lt;br /&gt;
{{CodeToImplement|ByteArrayRangeConcurrentHashMapKMerCounter|parse|kmer.warmup.bytearrayrangemap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will overlap greatly with your previous two implementations, but it will use ByteArrayRange instead of Strings. Refer to the ByteArrayRange class in &amp;lt;code&amp;gt;kmer.core.bytearrayrange&amp;lt;/code&amp;gt; in order to get a better idea of how to implement this method and instantiate an instance of the class. The class closely resembles the Slice class you worked on earlier in the semester.&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|KMerWarmupTestSuite|kmer.group}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=String_Map_K_Mer_Assignment&amp;diff=3658</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=3658"/>
		<updated>2022-04-02T22:00:11Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* ByteArrayRangeConcurrentHashMapKMerCounter */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&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 Concurrent==&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html ConcurrentHashMap]&lt;br /&gt;
&lt;br /&gt;
==KMerUtils==&lt;br /&gt;
The following methods should be useful as you build the assignment.&lt;br /&gt;
&lt;br /&gt;
Check out the Javadocs either [http://www.cse.wustl.edu/~cosgroved/courses/cse231/f17/apidocs/kmer/core/KMerUtils.html on the web], or in the comments in KMerUtils.java for further details on how they work.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===String toString(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static String toString(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	return new String(sequence, offset, kMerLength, StandardCharsets.UTF_8);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==KMerCounts==&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/KMerCount.html interface KMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/map/StringMapKMerCount.html class StringMapKMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/map/ByteArrayRangeMapKMerCount.html class ByteArrayRangeMapKMerCount]&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==StringHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|StringHashMapKMerCounter|parse|kmer.group.stringmap}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public KMerCount 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 MapKMerCount(which takes in a map), a class provided to you which does exactly what its name suggests. In order to implement this method, we recommend taking a look at the KMerUtils class and more specifically the toString method, which converts a given byte array into a String in order to make it compatible with the String HashMap.&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 KMerUtils.toString() method to create a string to use 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;
&amp;lt;youtube&amp;gt;Bwv3QMN0Mk4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==StringConcurrentHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|StringConcurrentHashMapKMerCounter|parse|kmer.group.stringmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount 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 atomic version of a HashMap: a ConcurrentHashMap. Like before, you will be need to complete the parse method. You can choose to finish this method with a forall loop or a finish/async approach, which is completely up to you.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;a9CK1BPzESM&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;4M9abyUyzxU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=ByteArrayRangeConcurrentHashMapKMerCounter=&lt;br /&gt;
{{CodeToImplement|ByteArrayRangeConcurrentHashMapKMerCounter|parse|kmer.warmup.bytearrayrangemap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will overlap greatly with your previous two implementations, but it will use ByteArrayRange instead of Strings. Refer to the ByteArrayRange class &amp;lt;code&amp;gt;(kmer.core.bytearrayrange)&amp;lt;/code&amp;gt; in order to get a better idea of how to implement this method and instantiate an instance of the class. The class closely resembles the Slice class you worked on earlier in the semester.&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|KMerWarmupTestSuite|kmer.group}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=String_Map_K_Mer_Assignment&amp;diff=3652</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=3652"/>
		<updated>2022-04-01T05:57:51Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* StringConcurrentHashMapKMerCounter */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&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 Concurrent==&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html ConcurrentHashMap]&lt;br /&gt;
&lt;br /&gt;
==KMerUtils==&lt;br /&gt;
The following methods should be useful as you build the assignment.&lt;br /&gt;
&lt;br /&gt;
Check out the Javadocs either [http://www.cse.wustl.edu/~cosgroved/courses/cse231/f17/apidocs/kmer/core/KMerUtils.html on the web], or in the comments in KMerUtils.java for further details on how they work.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===String toString(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static String toString(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	return new String(sequence, offset, kMerLength, StandardCharsets.UTF_8);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==KMerCounts==&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/KMerCount.html interface KMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/map/StringMapKMerCount.html class StringMapKMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/map/ByteArrayRangeMapKMerCount.html class ByteArrayRangeMapKMerCount]&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==StringHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|StringHashMapKMerCounter|parse|kmer.group.stringmap}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public KMerCount 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 MapKMerCount(which takes in a map), a class provided to you which does exactly what its name suggests. In order to implement this method, we recommend taking a look at the KMerUtils class and more specifically the toString method, which converts a given byte array into a String in order to make it compatible with the String HashMap.&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 KMerUtils.toString() method to create a string to use 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;
&amp;lt;youtube&amp;gt;Bwv3QMN0Mk4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==StringConcurrentHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|StringConcurrentHashMapKMerCounter|parse|kmer.group.stringmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount 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 atomic version of a HashMap: a ConcurrentHashMap. Like before, you will be need to complete the parse method. You can choose to finish this method with a forall loop or a finish/async approach, which is completely up to you.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;a9CK1BPzESM&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;4M9abyUyzxU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=ByteArrayRangeConcurrentHashMapKMerCounter=&lt;br /&gt;
{{CodeToImplement|ByteArrayRangeConcurrentHashMapKMerCounter|parse|kmer.warmup.bytearrayrangemap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will overlap greatly with your previous two implementations, but it will use ByteArrayRange instead of Strings. Refer to the ByteArrayRange class in order to get a better idea of how to implement this method and instantiate an instance of the class. The class closely resembles the Slice class you worked on earlier in the semester.&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|KMerWarmupTestSuite|kmer.group}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=String_Map_K_Mer_Assignment&amp;diff=3651</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=3651"/>
		<updated>2022-04-01T05:57:36Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* StringHashMapKMerCounter */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&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 Concurrent==&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html ConcurrentHashMap]&lt;br /&gt;
&lt;br /&gt;
==KMerUtils==&lt;br /&gt;
The following methods should be useful as you build the assignment.&lt;br /&gt;
&lt;br /&gt;
Check out the Javadocs either [http://www.cse.wustl.edu/~cosgroved/courses/cse231/f17/apidocs/kmer/core/KMerUtils.html on the web], or in the comments in KMerUtils.java for further details on how they work.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===String toString(byte[] sequence, int offset, int kMerLength)===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public static String toString(byte[] sequence, int offset, int kMerLength) {&lt;br /&gt;
	return new String(sequence, offset, kMerLength, StandardCharsets.UTF_8);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==KMerCounts==&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/KMerCount.html interface KMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/map/StringMapKMerCount.html class StringMapKMerCount]&lt;br /&gt;
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/kmer/core/map/ByteArrayRangeMapKMerCount.html class ByteArrayRangeMapKMerCount]&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==StringHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|StringHashMapKMerCounter|parse|kmer.group.stringmap}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public KMerCount 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 MapKMerCount(which takes in a map), a class provided to you which does exactly what its name suggests. In order to implement this method, we recommend taking a look at the KMerUtils class and more specifically the toString method, which converts a given byte array into a String in order to make it compatible with the String HashMap.&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 KMerUtils.toString() method to create a string to use 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;
&amp;lt;youtube&amp;gt;Bwv3QMN0Mk4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==StringConcurrentHashMapKMerCounter==&lt;br /&gt;
{{CodeToImplement|StringConcurrentHashMapKMerCounter|parse|kmer.warmup.stringmap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount 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 atomic version of a HashMap: a ConcurrentHashMap. Like before, you will be need to complete the parse method. You can choose to finish this method with a forall loop or a finish/async approach, which is completely up to you.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;a9CK1BPzESM&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;4M9abyUyzxU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=ByteArrayRangeConcurrentHashMapKMerCounter=&lt;br /&gt;
{{CodeToImplement|ByteArrayRangeConcurrentHashMapKMerCounter|parse|kmer.warmup.bytearrayrangemap}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public KMerCount parse(List&amp;lt;byte[]&amp;gt; sequences, int k)}}&lt;br /&gt;
&lt;br /&gt;
This parallel implementation will overlap greatly with your previous two implementations, but it will use ByteArrayRange instead of Strings. Refer to the ByteArrayRange class in order to get a better idea of how to implement this method and instantiate an instance of the class. The class closely resembles the Slice class you worked on earlier in the semester.&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|KMerWarmupTestSuite|kmer.group}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Atomicity&amp;diff=3650</id>
		<title>Atomicity</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Atomicity&amp;diff=3650"/>
		<updated>2022-03-29T19:57:32Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* Code To Investigate: CheckThenActCourse */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Many [https://en.wikipedia.org/wiki/Race_condition race conditions] can be prevented by proper encapsulation avoiding check-then-write and read-then-modify-then-write in non-[https://en.wikipedia.org/wiki/Linearizability atomically].&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
Take a look at the [[Reference_Page#Synchronized|synchronized and lock sections of the reference page]]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/tutorial/essential/concurrency/index.html Concurrency Tutorial]&lt;br /&gt;
: [https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html Synchronized Methods]&lt;br /&gt;
: [https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html Intrinsic Locks]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentMap.html ConcurrentMap]&lt;br /&gt;
: [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentMap.html#compute-K-java.util.function.BiFunction- compute]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;4M9abyUyzxU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Lecture=&lt;br /&gt;
&amp;lt;youtube&amp;gt;0jISL5lT7TU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Song to Recall=&lt;br /&gt;
&lt;br /&gt;
''get then put is not atomic&amp;lt;br&amp;gt;call [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html#compute-K-java.util.function.BiFunction- compute]&amp;lt;br&amp;gt;call [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html#compute-K-java.util.function.BiFunction- compute]&amp;lt;br&amp;gt;use [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html ConcurrentHashMap]&amp;lt;br&amp;gt;use [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html ConcurrentHashMap]&amp;lt;br&amp;gt;or say shoot!&amp;lt;br&amp;gt;or say shoot!''&lt;br /&gt;
&lt;br /&gt;
=Exercise=&lt;br /&gt;
==SuspectWordCount==&lt;br /&gt;
&amp;lt;youtube&amp;gt;glpqcoTELi0&amp;lt;/youtube&amp;gt;&lt;br /&gt;
===Code To Fix: countWords===&lt;br /&gt;
{{Warning|Do NOT use synchronized here.  ConcurrentMaps are already thread safe.}}&lt;br /&gt;
&lt;br /&gt;
fix the code to remove the atomicity race.&lt;br /&gt;
{{CodeToDebug|SuspectWordCount|countWords|atomicity.wordcount.exercise}}&lt;br /&gt;
{{Parallel|public static Map&amp;lt;String, Integer&amp;gt; countWords(Iterable&amp;lt;String&amp;gt; words)}}&lt;br /&gt;
==StockPortfolio==&lt;br /&gt;
&amp;lt;youtube&amp;gt;z9IvYmvyIh0&amp;lt;/youtube&amp;gt;&lt;br /&gt;
===Code To Fix: transfer===&lt;br /&gt;
{{Warning|Do NOT use synchronized here.  ConcurrentMaps are already thread safe.}}&lt;br /&gt;
&lt;br /&gt;
For this part of the studio we are presented with broken code.  Despite using a thread-safe concurrent map:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;private final ConcurrentMap&amp;lt;String, Integer&amp;gt; map;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
it suffers from an atomicity race:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;private int transfer(String listingSymbol, int deltaShareCount) {&lt;br /&gt;
	Integer oldValue = this.map.get(listingSymbol);&lt;br /&gt;
	Integer newValue;&lt;br /&gt;
	if (oldValue != null) {&lt;br /&gt;
		newValue = oldValue + deltaShareCount;&lt;br /&gt;
	} else {&lt;br /&gt;
		newValue = deltaShareCount;&lt;br /&gt;
	}&lt;br /&gt;
	this.map.put(listingSymbol, newValue);&lt;br /&gt;
	return newValue;&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|StockPortfolio|transfer|atomicity.stockportfolio.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|private int transfer(String listingSymbol, int deltaShareCount)}}&lt;br /&gt;
&lt;br /&gt;
NOTE: do not be concerned about ConcurrentMap vs ConcurrentHashMap.  The supplier's get method will return an instance of a thread-safe map.  (It will, in fact, be an instance of ConcurrentHashMap.)&lt;br /&gt;
&lt;br /&gt;
==Course==&lt;br /&gt;
&amp;lt;youtube&amp;gt;gtcOC0RdnMA&amp;lt;/youtube&amp;gt;&lt;br /&gt;
===Code To Investigate: CheckThenActCourse===&lt;br /&gt;
{{Warning|Although the class below is technically thread-safe, it offers check-then-act usage which leads to atomicity races.}}&lt;br /&gt;
&lt;br /&gt;
{{CodeToInvestigate|CheckThenActCourse|isSpaceRemaining&amp;lt;br&amp;gt;add&amp;lt;br&amp;gt;drop|atomicity.course.demo}}&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public class CheckThenActCourse {&lt;br /&gt;
	private final Collection&amp;lt;Student&amp;gt; students;&lt;br /&gt;
	private final int limit;&lt;br /&gt;
&lt;br /&gt;
	public CheckThenActCourse(int limit, Supplier&amp;lt;Collection&amp;lt;Student&amp;gt;&amp;gt; collectionSupplier) {&lt;br /&gt;
		this.students = collectionSupplier.get();&lt;br /&gt;
		this.limit = limit;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public int getLimit() {&lt;br /&gt;
		return this.limit;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public boolean isSpaceRemaining() {&lt;br /&gt;
		synchronized (this.students) {&lt;br /&gt;
			return this.students.size() &amp;lt; this.limit;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public void add(Student student) {&lt;br /&gt;
		synchronized (this.students) {&lt;br /&gt;
			this.students.add(student);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public boolean drop(Student student) {&lt;br /&gt;
		synchronized (this.students) {&lt;br /&gt;
			return this.students.remove(student);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Code To Implement: Course===&lt;br /&gt;
We will build a thread-safe &amp;lt;code&amp;gt;Course&amp;lt;/code&amp;gt; class with methods which do not lead clients towards atomicity races.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|Course|addIfSpace&amp;lt;br&amp;gt;drop|atomicity.course.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|public boolean addIfSpace(Student student)}}&lt;br /&gt;
&lt;br /&gt;
Adds the student if there is space under the limit specified to the constructor and returns whether or not the add actually occurred.&lt;br /&gt;
&lt;br /&gt;
NOTE: Must be thread safe.&lt;br /&gt;
&lt;br /&gt;
{{ThreadSafe|public boolean drop(Student student)}}&lt;br /&gt;
&lt;br /&gt;
Removes the student from the course if the student was enrolled (the remove method returns just the status you need).&lt;br /&gt;
&lt;br /&gt;
NOTE: Must be thread safe.&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|_AtomicityTestSuite|atomicity}}&lt;br /&gt;
&lt;br /&gt;
:{{TestSuite|_WordCountAtomicityTestSuite|atomicity.wordcount.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_StockPortfolioAtomicityTestSuite|atomicity.stockportfolio.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|_CourseAtomicityTestSuite|atomicity.course.exercise}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=3647</id>
		<title>Scan</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=3647"/>
		<updated>2022-03-25T19:30:44Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* (Extra Credit) Work Efficient Scan */&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;
==Hillis-Steele Prefix Sum==&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;
&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;
==(Extra Credit) Work-efficient==&lt;br /&gt;
===Exclusive (Blelloch)===&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_2:_Work-efficient Blelloch Algorithm]&lt;br /&gt;
&lt;br /&gt;
[https://www.cs.cmu.edu/~guyb/papers/sc90.pdf Scan Primitives for Vector Computers]&lt;br /&gt;
&lt;br /&gt;
[https://www.cs.cmu.edu/~guyb/papers/Ble93.pdf Prefix Sums and Their Applications]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;mmYv3Haj6uc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Inclusive===&lt;br /&gt;
[[File:Prefix sum 16.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;
=Code To Use=&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#getSrcForPhase(int) getSrcForPhase(phaseIndex)]&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;
&lt;br /&gt;
[[PowersOf2Iterable]]&lt;br /&gt;
&lt;br /&gt;
==PhasableIntArrays==&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. 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;getDstForPhase(int phaseIndex)&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getSrcForPhase(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''' ||0||1||2||3||4&lt;br /&gt;
|-&lt;br /&gt;
|'''src'''||data||a||b||a||b&lt;br /&gt;
|-&lt;br /&gt;
|'''dst'''||a||b||a||b||a&lt;br /&gt;
|-&lt;br /&gt;
|||||||||||&lt;br /&gt;
|-&lt;br /&gt;
|'''bonus info: power of 2''' ||1|||2||4||8||16&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;
&lt;br /&gt;
==[[PowersOf2Iterable]]==&lt;br /&gt;
This [[PowersOf2Iterable|studio]] should come in handy here. Recall that we implemented a &amp;lt;code&amp;gt;PowersOfTwoLessThan&amp;lt;/code&amp;gt; class in this studio.&lt;br /&gt;
&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;for(int v : new PowersOfTwoLessThan(71)) {&lt;br /&gt;
    System.out.println(v);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==Sequential Sum Scanner==&lt;br /&gt;
{{CodeToImplement|SequentialSumScanner|sumScan|scan.exerise}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do NOT mutate the data parameter.  Return a new array which contains the sum scan of data.}}&lt;br /&gt;
&lt;br /&gt;
Implement the simple sequential scan algorithm with the dependencies depicted below:&lt;br /&gt;
&lt;br /&gt;
[[File:Sequential_scan.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Work: N&lt;br /&gt;
&lt;br /&gt;
CPL: N&lt;br /&gt;
&lt;br /&gt;
==Hillis and Steele Parallel Sum Scanner==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|StepEfficientParallelSumScanner|sumScan|scan.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
==(Extra Credit) Work Efficient Scan==&lt;br /&gt;
You may choose to implement either Inclusive or Exclusive Blelloch Scan. This is an in-place mutating scan algorithm, which means you can mutate the original data array.&lt;br /&gt;
&lt;br /&gt;
===Inclusive Scan or Blelloch Exclusive Scan===&lt;br /&gt;
{{CodeToImplement|WorkEfficientParallelMutatingSumScanner|IsInclusive|scan.challenge}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public boolean isInclusive()}}&lt;br /&gt;
: Here you can indicate whether you will implement inclusive or exclusive scan.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|WorkEfficientParallelMutatingSumScanner|sumScan|scan.challenge}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|void sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
=Visualization=&lt;br /&gt;
{{Viz|ScanApp|scan.viz|main}}&lt;br /&gt;
&lt;br /&gt;
[[File:Step_Efficient_Scan_Viz.png]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
===Required===&lt;br /&gt;
{{TestSuite|__ScanTestSuite|scan.exercise}}&lt;br /&gt;
===Optional Work Efficient===&lt;br /&gt;
{{TestSuite|_WorkEfficientScanTestSuite|scan.challenge}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=3646</id>
		<title>Scan</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=3646"/>
		<updated>2022-03-25T19:29:15Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* Blelloch Scan */&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;
==Hillis-Steele Prefix Sum==&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;
&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;
==(Extra Credit) Work-efficient==&lt;br /&gt;
===Exclusive (Blelloch)===&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_2:_Work-efficient Blelloch Algorithm]&lt;br /&gt;
&lt;br /&gt;
[https://www.cs.cmu.edu/~guyb/papers/sc90.pdf Scan Primitives for Vector Computers]&lt;br /&gt;
&lt;br /&gt;
[https://www.cs.cmu.edu/~guyb/papers/Ble93.pdf Prefix Sums and Their Applications]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;mmYv3Haj6uc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Inclusive===&lt;br /&gt;
[[File:Prefix sum 16.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;
=Code To Use=&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#getSrcForPhase(int) getSrcForPhase(phaseIndex)]&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;
&lt;br /&gt;
[[PowersOf2Iterable]]&lt;br /&gt;
&lt;br /&gt;
==PhasableIntArrays==&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. 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;getDstForPhase(int phaseIndex)&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getSrcForPhase(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''' ||0||1||2||3||4&lt;br /&gt;
|-&lt;br /&gt;
|'''src'''||data||a||b||a||b&lt;br /&gt;
|-&lt;br /&gt;
|'''dst'''||a||b||a||b||a&lt;br /&gt;
|-&lt;br /&gt;
|||||||||||&lt;br /&gt;
|-&lt;br /&gt;
|'''bonus info: power of 2''' ||1|||2||4||8||16&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;
&lt;br /&gt;
==[[PowersOf2Iterable]]==&lt;br /&gt;
This [[PowersOf2Iterable|studio]] should come in handy here. Recall that we implemented a &amp;lt;code&amp;gt;PowersOfTwoLessThan&amp;lt;/code&amp;gt; class in this studio.&lt;br /&gt;
&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;for(int v : new PowersOfTwoLessThan(71)) {&lt;br /&gt;
    System.out.println(v);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==Sequential Sum Scanner==&lt;br /&gt;
{{CodeToImplement|SequentialSumScanner|sumScan|scan.exerise}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do NOT mutate the data parameter.  Return a new array which contains the sum scan of data.}}&lt;br /&gt;
&lt;br /&gt;
Implement the simple sequential scan algorithm with the dependencies depicted below:&lt;br /&gt;
&lt;br /&gt;
[[File:Sequential_scan.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Work: N&lt;br /&gt;
&lt;br /&gt;
CPL: N&lt;br /&gt;
&lt;br /&gt;
==Hillis and Steele Parallel Sum Scanner==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|StepEfficientParallelSumScanner|sumScan|scan.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
==(Extra Credit) Work Efficient Scan==&lt;br /&gt;
You may choose to implement either Inclusive or Exclusive Blelloch Scan. This is a in-place mutating scan, which means you can mutate the data array.&lt;br /&gt;
&lt;br /&gt;
===Inclusive Scan or Blelloch Exclusive Scan===&lt;br /&gt;
{{CodeToImplement|WorkEfficientParallelMutatingSumScanner|IsInclusive|scan.challenge}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public boolean isInclusive()}}&lt;br /&gt;
: Here you can indicate whether you will implement inclusive or exclusive scan.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|WorkEfficientParallelMutatingSumScanner|sumScan|scan.challenge}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|void sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
=Visualization=&lt;br /&gt;
{{Viz|ScanApp|scan.viz|main}}&lt;br /&gt;
&lt;br /&gt;
[[File:Step_Efficient_Scan_Viz.png]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
===Required===&lt;br /&gt;
{{TestSuite|__ScanTestSuite|scan.exercise}}&lt;br /&gt;
===Optional Work Efficient===&lt;br /&gt;
{{TestSuite|_WorkEfficientScanTestSuite|scan.challenge}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=3645</id>
		<title>Scan</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=3645"/>
		<updated>2022-03-25T19:26:44Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* (Extra Credit) Work Efficient Scan */&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;
==Hillis-Steele Prefix Sum==&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;
&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;
==(Extra Credit) Work-efficient==&lt;br /&gt;
===Exclusive (Blelloch)===&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_2:_Work-efficient Blelloch Algorithm]&lt;br /&gt;
&lt;br /&gt;
[https://www.cs.cmu.edu/~guyb/papers/sc90.pdf Scan Primitives for Vector Computers]&lt;br /&gt;
&lt;br /&gt;
[https://www.cs.cmu.edu/~guyb/papers/Ble93.pdf Prefix Sums and Their Applications]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;mmYv3Haj6uc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Inclusive===&lt;br /&gt;
[[File:Prefix sum 16.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;
=Code To Use=&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#getSrcForPhase(int) getSrcForPhase(phaseIndex)]&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;
&lt;br /&gt;
[[PowersOf2Iterable]]&lt;br /&gt;
&lt;br /&gt;
==PhasableIntArrays==&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. 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;getDstForPhase(int phaseIndex)&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getSrcForPhase(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''' ||0||1||2||3||4&lt;br /&gt;
|-&lt;br /&gt;
|'''src'''||data||a||b||a||b&lt;br /&gt;
|-&lt;br /&gt;
|'''dst'''||a||b||a||b||a&lt;br /&gt;
|-&lt;br /&gt;
|||||||||||&lt;br /&gt;
|-&lt;br /&gt;
|'''bonus info: power of 2''' ||1|||2||4||8||16&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;
&lt;br /&gt;
==[[PowersOf2Iterable]]==&lt;br /&gt;
This [[PowersOf2Iterable|studio]] should come in handy here. Recall that we implemented a &amp;lt;code&amp;gt;PowersOfTwoLessThan&amp;lt;/code&amp;gt; class in this studio.&lt;br /&gt;
&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;for(int v : new PowersOfTwoLessThan(71)) {&lt;br /&gt;
    System.out.println(v);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==Sequential Sum Scanner==&lt;br /&gt;
{{CodeToImplement|SequentialSumScanner|sumScan|scan.exerise}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do NOT mutate the data parameter.  Return a new array which contains the sum scan of data.}}&lt;br /&gt;
&lt;br /&gt;
Implement the simple sequential scan algorithm with the dependencies depicted below:&lt;br /&gt;
&lt;br /&gt;
[[File:Sequential_scan.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Work: N&lt;br /&gt;
&lt;br /&gt;
CPL: N&lt;br /&gt;
&lt;br /&gt;
==Hillis and Steele Parallel Sum Scanner==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|StepEfficientParallelSumScanner|sumScan|scan.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
==(Extra Credit) Work Efficient Scan==&lt;br /&gt;
You may choose to implement either Inclusive or Exclusive Blelloch Scan. This is a in-place mutating scan, which means you can mutate the data array.&lt;br /&gt;
&lt;br /&gt;
===Blelloch Scan===&lt;br /&gt;
{{CodeToImplement|WorkEfficientParallelMutatingSumScanner|IsInclusive|scan.challenge}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public boolean isInclusive()}}&lt;br /&gt;
: Here you can indicate whether you will implement inclusive or exclusive scan.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|WorkEfficientParallelMutatingSumScanner|sumScan|scan.challenge}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|void sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
=Visualization=&lt;br /&gt;
{{Viz|ScanApp|scan.viz|main}}&lt;br /&gt;
&lt;br /&gt;
[[File:Step_Efficient_Scan_Viz.png]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
===Required===&lt;br /&gt;
{{TestSuite|__ScanTestSuite|scan.exercise}}&lt;br /&gt;
===Optional Work Efficient===&lt;br /&gt;
{{TestSuite|_WorkEfficientScanTestSuite|scan.challenge}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=3644</id>
		<title>Scan</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=3644"/>
		<updated>2022-03-25T18:26:24Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* PowersOf2Iterable */&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;
==Hillis-Steele Prefix Sum==&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;
&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;
==(Extra Credit) Work-efficient==&lt;br /&gt;
===Exclusive (Blelloch)===&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_2:_Work-efficient Blelloch Algorithm]&lt;br /&gt;
&lt;br /&gt;
[https://www.cs.cmu.edu/~guyb/papers/sc90.pdf Scan Primitives for Vector Computers]&lt;br /&gt;
&lt;br /&gt;
[https://www.cs.cmu.edu/~guyb/papers/Ble93.pdf Prefix Sums and Their Applications]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;mmYv3Haj6uc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Inclusive===&lt;br /&gt;
[[File:Prefix sum 16.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;
=Code To Use=&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#getSrcForPhase(int) getSrcForPhase(phaseIndex)]&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;
&lt;br /&gt;
[[PowersOf2Iterable]]&lt;br /&gt;
&lt;br /&gt;
==PhasableIntArrays==&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. 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;getDstForPhase(int phaseIndex)&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getSrcForPhase(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''' ||0||1||2||3||4&lt;br /&gt;
|-&lt;br /&gt;
|'''src'''||data||a||b||a||b&lt;br /&gt;
|-&lt;br /&gt;
|'''dst'''||a||b||a||b||a&lt;br /&gt;
|-&lt;br /&gt;
|||||||||||&lt;br /&gt;
|-&lt;br /&gt;
|'''bonus info: power of 2''' ||1|||2||4||8||16&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;
&lt;br /&gt;
==[[PowersOf2Iterable]]==&lt;br /&gt;
This [[PowersOf2Iterable|studio]] should come in handy here. Recall that we implemented a &amp;lt;code&amp;gt;PowersOfTwoLessThan&amp;lt;/code&amp;gt; class in this studio.&lt;br /&gt;
&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;for(int v : new PowersOfTwoLessThan(71)) {&lt;br /&gt;
    System.out.println(v);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==Sequential Sum Scanner==&lt;br /&gt;
{{CodeToImplement|SequentialSumScanner|sumScan|scan.exerise}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do NOT mutate the data parameter.  Return a new array which contains the sum scan of data.}}&lt;br /&gt;
&lt;br /&gt;
Implement the simple sequential scan algorithm with the dependencies depicted below:&lt;br /&gt;
&lt;br /&gt;
[[File:Sequential_scan.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Work: N&lt;br /&gt;
&lt;br /&gt;
CPL: N&lt;br /&gt;
&lt;br /&gt;
==Hillis and Steele Parallel Sum Scanner==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|StepEfficientParallelSumScanner|sumScan|scan.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
==(Extra Credit) Work Efficient Scan==&lt;br /&gt;
Choose one path.&lt;br /&gt;
===Inclusive (Blelloch)===&lt;br /&gt;
{{CodeToImplement|WorkEfficientParallelScan|sumScanExclusiveInPlace|scan.challenge}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public static void sumScanExclusiveInPlace(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
===Exclusive===&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|WorkEfficientInPlaceInclusiveSumScanner|sumScan|scan.challenge}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|void sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
=Visualization=&lt;br /&gt;
{{Viz|ScanApp|scan.viz|main}}&lt;br /&gt;
&lt;br /&gt;
[[File:Step_Efficient_Scan_Viz.png]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
===Required===&lt;br /&gt;
{{TestSuite|__ScanTestSuite|scan.exercise}}&lt;br /&gt;
===Optional Work Efficient===&lt;br /&gt;
{{TestSuite|_WorkEfficientScanTestSuite|scan.challenge}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=3643</id>
		<title>Scan</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=3643"/>
		<updated>2022-03-24T21:31:20Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* 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;
==Hillis-Steele Prefix Sum==&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;
&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;
==(Extra Credit) Work-efficient==&lt;br /&gt;
===Exclusive (Blelloch)===&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_2:_Work-efficient Blelloch Algorithm]&lt;br /&gt;
&lt;br /&gt;
[https://www.cs.cmu.edu/~guyb/papers/sc90.pdf Scan Primitives for Vector Computers]&lt;br /&gt;
&lt;br /&gt;
[https://www.cs.cmu.edu/~guyb/papers/Ble93.pdf Prefix Sums and Their Applications]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;mmYv3Haj6uc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Inclusive===&lt;br /&gt;
[[File:Prefix sum 16.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;
=Code To Use=&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#getSrcForPhase(int) getSrcForPhase(phaseIndex)]&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;
&lt;br /&gt;
[[PowersOf2Iterable]]&lt;br /&gt;
&lt;br /&gt;
==PhasableIntArrays==&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. 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;getDstForPhase(int phaseIndex)&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getSrcForPhase(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''' ||0||1||2||3||4&lt;br /&gt;
|-&lt;br /&gt;
|'''src'''||data||a||b||a||b&lt;br /&gt;
|-&lt;br /&gt;
|'''dst'''||a||b||a||b||a&lt;br /&gt;
|-&lt;br /&gt;
|||||||||||&lt;br /&gt;
|-&lt;br /&gt;
|'''bonus info: power of 2''' ||1|||2||4||8||16&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;
&lt;br /&gt;
==[[PowersOf2Iterable]]==&lt;br /&gt;
This [[PowersOf2Iterable|studio]] should come in handy here.&lt;br /&gt;
&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;for(int v : new PowersOfTwoLessThan(71)) {&lt;br /&gt;
    System.out.println(v);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==Sequential Sum Scanner==&lt;br /&gt;
{{CodeToImplement|SequentialSumScanner|sumScan|scan.exerise}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do NOT mutate the data parameter.  Return a new array which contains the sum scan of data.}}&lt;br /&gt;
&lt;br /&gt;
Implement the simple sequential scan algorithm with the dependencies depicted below:&lt;br /&gt;
&lt;br /&gt;
[[File:Sequential_scan.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Work: N&lt;br /&gt;
&lt;br /&gt;
CPL: N&lt;br /&gt;
&lt;br /&gt;
==Hillis and Steele Parallel Sum Scanner==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|StepEfficientParallelSumScanner|sumScan|scan.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
==(Extra Credit) Work Efficient Scan==&lt;br /&gt;
Choose one path.&lt;br /&gt;
===Inclusive (Blelloch)===&lt;br /&gt;
{{CodeToImplement|WorkEfficientParallelScan|sumScanExclusiveInPlace|scan.challenge}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public static void sumScanExclusiveInPlace(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
===Exclusive===&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|WorkEfficientInPlaceInclusiveSumScanner|sumScan|scan.challenge}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|void sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
=Visualization=&lt;br /&gt;
{{Viz|ScanApp|scan.viz|main}}&lt;br /&gt;
&lt;br /&gt;
[[File:Step_Efficient_Scan_Viz.png]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
===Required===&lt;br /&gt;
{{TestSuite|__ScanTestSuite|scan.exercise}}&lt;br /&gt;
===Optional Work Efficient===&lt;br /&gt;
{{TestSuite|_WorkEfficientScanTestSuite|scan.challenge}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=3642</id>
		<title>Scan</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=3642"/>
		<updated>2022-03-24T20:42:11Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* 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;
==Hillis-Steele Prefix Sum==&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;
&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;
==(Extra Credit) Work-efficient==&lt;br /&gt;
===Exclusive (Blelloch)===&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_2:_Work-efficient Blelloch Algorithm]&lt;br /&gt;
&lt;br /&gt;
[https://www.cs.cmu.edu/~guyb/papers/sc90.pdf Scan Primitives for Vector Computers]&lt;br /&gt;
&lt;br /&gt;
[https://www.cs.cmu.edu/~guyb/papers/Ble93.pdf Prefix Sums and Their Applications]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;mmYv3Haj6uc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Inclusive===&lt;br /&gt;
[[File:Prefix sum 16.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;
=Code To Use=&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#getSrcForPhase(int) getSrcForPhase(phaseIndex)]&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;
&lt;br /&gt;
[[PowersOf2Iterable]]&lt;br /&gt;
&lt;br /&gt;
==PhasableIntArrays==&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.&lt;br /&gt;
&lt;br /&gt;
The table below shows which array will be passed back for each offset.&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|'''phase''' ||0||1||2||3||4&lt;br /&gt;
|-&lt;br /&gt;
|'''src'''||data||a||b||a||b&lt;br /&gt;
|-&lt;br /&gt;
|'''dst'''||a||b||a||b||a&lt;br /&gt;
|-&lt;br /&gt;
|||||||||||&lt;br /&gt;
|-&lt;br /&gt;
|'''bonus info: power of 2''' ||1|||2||4||8||16&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;
&lt;br /&gt;
==[[PowersOf2Iterable]]==&lt;br /&gt;
This [[PowersOf2Iterable|studio]] should come in handy here.&lt;br /&gt;
&lt;br /&gt;
Example usage:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;for(int v : new PowersOfTwoLessThan(71)) {&lt;br /&gt;
    System.out.println(v);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==Sequential Sum Scanner==&lt;br /&gt;
{{CodeToImplement|SequentialSumScanner|sumScan|scan.exerise}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|Do NOT mutate the data parameter.  Return a new array which contains the sum scan of data.}}&lt;br /&gt;
&lt;br /&gt;
Implement the simple sequential scan algorithm with the dependencies depicted below:&lt;br /&gt;
&lt;br /&gt;
[[File:Sequential_scan.png|600px]]&lt;br /&gt;
&lt;br /&gt;
Work: N&lt;br /&gt;
&lt;br /&gt;
CPL: N&lt;br /&gt;
&lt;br /&gt;
==Hillis and Steele Parallel Sum Scanner==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|StepEfficientParallelSumScanner|sumScan|scan.exercise}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
==(Extra Credit) Work Efficient Scan==&lt;br /&gt;
Choose one path.&lt;br /&gt;
===Inclusive (Blelloch)===&lt;br /&gt;
{{CodeToImplement|WorkEfficientParallelScan|sumScanExclusiveInPlace|scan.challenge}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public static void sumScanExclusiveInPlace(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
===Exclusive===&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|WorkEfficientInPlaceInclusiveSumScanner|sumScan|scan.challenge}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|void sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
=Visualization=&lt;br /&gt;
{{Viz|ScanApp|scan.viz|main}}&lt;br /&gt;
&lt;br /&gt;
[[File:Step_Efficient_Scan_Viz.png]]&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
===Required===&lt;br /&gt;
{{TestSuite|__ScanTestSuite|scan.exercise}}&lt;br /&gt;
===Optional Work Efficient===&lt;br /&gt;
{{TestSuite|_WorkEfficientScanTestSuite|scan.challenge}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Matrix_MapReduce_Framework_Assignment&amp;diff=3641</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=3641"/>
		<updated>2022-03-24T20:36:03Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* MultiWrapMap */&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 [[Slices|Slices studio]] is conforming to spec.}}&lt;br /&gt;
&lt;br /&gt;
=Code To Use=&lt;br /&gt;
To allow our frameworks to work well with JDK8 Streams, we employ a couple of standard interfaces over creating our own custom ones.&lt;br /&gt;
&lt;br /&gt;
==BiConsumer==&lt;br /&gt;
We use the standard [https://docs.oracle.com/javase/8/docs/api/java/util/function/BiConsumer.html BiConsumer&amp;lt;T,U&amp;gt;] interface with an [https://docs.oracle.com/javase/8/docs/api/java/util/function/BiConsumer.html#accept-T-U- BiConsumer's accept(t,u)] method in the place of a mythical, custom MapContext&amp;lt;K,V&amp;gt; interface with an emit(key,value) method.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public interface BiConsumer&amp;lt;T, U&amp;gt; {&lt;br /&gt;
    // invoke accept with each key and value you wish to emit&lt;br /&gt;
    void accept(T t, U u);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Reducer&amp;lt;V,A,R&amp;gt;==&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/mapreduce/core/Reducer.html Reducer]&lt;br /&gt;
&lt;br /&gt;
check out the [[https://classes.engineering.wustl.edu/cse231/core/index.php?title=MapReduce_Reducer_Assignment#Background|Reducer Background]]&lt;br /&gt;
&lt;br /&gt;
==Range==&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;
==Ranges==&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;
&lt;br /&gt;
==HashUtils==&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/s20/apidocs/hash/studio/HashUtils.html#toIndex(java.lang.Object,int) toIndex(key,N)]&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|mapAndAccumulateAll&amp;lt;br&amp;gt;combineAndFinishAll|mapreduce.framework.lab.matrix}}&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 two 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 toIndex() 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;
===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[].class&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 a toIndex 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;
'''Spring 2022 Note:''' if you wish to use &amp;lt;code&amp;gt;join_void_fork_loop_with_index&amp;lt;/code&amp;gt; you will need to add the static import to the top of the file: &lt;br /&gt;
&lt;br /&gt;
 import static fj.FJ.join_void_fork_loop_with_index;&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;
===combineAndFinishAll===&lt;br /&gt;
{{Parallel|Map&amp;lt;K, R&amp;gt; combineAndFinishAll(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;
Hint: You can use the provided [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/util/MultiWrapMap.html MultiWrapMap] class to collect the output and return it as a valid output. (MultiWrapMap implements Map.) &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;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|_MatrixFrameworkTestSuite|mapreduce.framework.matrix.exercise}}&lt;br /&gt;
===MapAccumulateAll===&lt;br /&gt;
: {{TestSuite|MatrixMapAccumulateAllTestSuite|mapreduce.framework.matrix.exercise}}&lt;br /&gt;
&lt;br /&gt;
===CombineFinishAll===&lt;br /&gt;
{{TestSuite|MatrixCombineFinishAllTestSuite|mapreduce.framework.matrix.exercise}}&lt;br /&gt;
===Holistic===&lt;br /&gt;
{{TestSuite|MatrixHolisticTestSuite|mapreduce.framework.matrix.exercise}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Matrix_MapReduce_Framework_Assignment&amp;diff=3640</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=3640"/>
		<updated>2022-03-24T20:31:10Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* Parameterized Type Array Tip */&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 [[Slices|Slices studio]] is conforming to spec.}}&lt;br /&gt;
&lt;br /&gt;
=Code To Use=&lt;br /&gt;
To allow our frameworks to work well with JDK8 Streams, we employ a couple of standard interfaces over creating our own custom ones.&lt;br /&gt;
&lt;br /&gt;
==BiConsumer==&lt;br /&gt;
We use the standard [https://docs.oracle.com/javase/8/docs/api/java/util/function/BiConsumer.html BiConsumer&amp;lt;T,U&amp;gt;] interface with an [https://docs.oracle.com/javase/8/docs/api/java/util/function/BiConsumer.html#accept-T-U- BiConsumer's accept(t,u)] method in the place of a mythical, custom MapContext&amp;lt;K,V&amp;gt; interface with an emit(key,value) method.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public interface BiConsumer&amp;lt;T, U&amp;gt; {&lt;br /&gt;
    // invoke accept with each key and value you wish to emit&lt;br /&gt;
    void accept(T t, U u);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Reducer&amp;lt;V,A,R&amp;gt;==&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/mapreduce/core/Reducer.html Reducer]&lt;br /&gt;
&lt;br /&gt;
check out the [[https://classes.engineering.wustl.edu/cse231/core/index.php?title=MapReduce_Reducer_Assignment#Background|Reducer Background]]&lt;br /&gt;
&lt;br /&gt;
==Range==&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;
==Ranges==&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;
&lt;br /&gt;
==HashUtils==&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/s20/apidocs/hash/studio/HashUtils.html#toIndex(java.lang.Object,int) toIndex(key,N)]&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;]&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|mapAndAccumulateAll&amp;lt;br&amp;gt;combineAndFinishAll|mapreduce.framework.lab.matrix}}&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 two 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 toIndex() 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;
===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[].class&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 a toIndex 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;
'''Spring 2022 Note:''' if you wish to use &amp;lt;code&amp;gt;join_void_fork_loop_with_index&amp;lt;/code&amp;gt; you will need to add the static import to the top of the file: &lt;br /&gt;
&lt;br /&gt;
 import static fj.FJ.join_void_fork_loop_with_index;&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;
===combineAndFinishAll===&lt;br /&gt;
{{Parallel|Map&amp;lt;K, R&amp;gt; combineAndFinishAll(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;
Hint: You can use the provided [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/util/MultiWrapMap.html MultiWrapMap] class to collect the output and return it as a valid output. (MultiWrapMap implements Map.) &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;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|_MatrixFrameworkTestSuite|mapreduce.framework.matrix.exercise}}&lt;br /&gt;
===MapAccumulateAll===&lt;br /&gt;
: {{TestSuite|MatrixMapAccumulateAllTestSuite|mapreduce.framework.matrix.exercise}}&lt;br /&gt;
&lt;br /&gt;
===CombineFinishAll===&lt;br /&gt;
{{TestSuite|MatrixCombineFinishAllTestSuite|mapreduce.framework.matrix.exercise}}&lt;br /&gt;
===Holistic===&lt;br /&gt;
{{TestSuite|MatrixHolisticTestSuite|mapreduce.framework.matrix.exercise}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Matrix_MapReduce_Framework_Assignment&amp;diff=3632</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=3632"/>
		<updated>2022-03-22T21:03:55Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* combineAndFinishAll */&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;
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;
&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 [[Slices|Slices studio]] is conforming to spec.}}&lt;br /&gt;
&lt;br /&gt;
=Code To Use=&lt;br /&gt;
To allow our frameworks to work well with JDK8 Streams, we employ a couple of standard interfaces over creating our own custom ones.&lt;br /&gt;
&lt;br /&gt;
==BiConsumer==&lt;br /&gt;
We use the standard [https://docs.oracle.com/javase/8/docs/api/java/util/function/BiConsumer.html BiConsumer&amp;lt;T,U&amp;gt;] interface with an [https://docs.oracle.com/javase/8/docs/api/java/util/function/BiConsumer.html#accept-T-U- BiConsumer's accept(t,u)] method in the place of a mythical, custom MapContext&amp;lt;K,V&amp;gt; interface with an emit(key,value) method.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public interface BiConsumer&amp;lt;T, U&amp;gt; {&lt;br /&gt;
    // invoke accept with each key and value you wish to emit&lt;br /&gt;
    void accept(T t, U u);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Reducer&amp;lt;V,A,R&amp;gt;==&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/mapreduce/core/Reducer.html Reducer]&lt;br /&gt;
&lt;br /&gt;
check out the [[https://classes.engineering.wustl.edu/cse231/core/index.php?title=MapReduce_Reducer_Assignment#Background|Reducer Background]]&lt;br /&gt;
&lt;br /&gt;
==Range==&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;
==Ranges==&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;
&lt;br /&gt;
==HashUtils==&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/s20/apidocs/hash/studio/HashUtils.html#toIndex(java.lang.Object,int) toIndex(key,N)]&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;]&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|mapAndAccumulateAll&amp;lt;br&amp;gt;combineAndFinishAll|mapreduce.framework.lab.matrix}}&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 two 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 toIndex() 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;
===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[].class&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 a toIndex 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;
'''Spring 2022 Note:''' if you wish to use &amp;lt;code&amp;gt;join_void_fork_loop_with_index&amp;lt;/code&amp;gt; you will need to add the static import to the top of the file: &lt;br /&gt;
&lt;br /&gt;
 import static fj.FJ.join_void_fork_loop_with_index;&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;
===combineAndFinishAll===&lt;br /&gt;
{{Parallel|Map&amp;lt;K, R&amp;gt; combineAndFinishAll(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;
Hint: You can use the provided [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/edu/wustl/cse231s/util/MultiWrapMap.html MultiWrapMap] class to collect the output and return it as a valid output. (MultiWrapMap implements Map.) &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;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|_MatrixFrameworkTestSuite|mapreduce.framework.matrix.exercise}}&lt;br /&gt;
===MapAccumulateAll===&lt;br /&gt;
: {{TestSuite|MatrixMapAccumulateAllTestSuite|mapreduce.framework.matrix.exercise}}&lt;br /&gt;
&lt;br /&gt;
===CombineFinishAll===&lt;br /&gt;
{{TestSuite|MatrixCombineFinishAllTestSuite|mapreduce.framework.matrix.exercise}}&lt;br /&gt;
===Holistic===&lt;br /&gt;
{{TestSuite|MatrixHolisticTestSuite|mapreduce.framework.matrix.exercise}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Matrix_MapReduce_Framework_Assignment&amp;diff=3631</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=3631"/>
		<updated>2022-03-22T20:51:30Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* Mistakes To Avoid */&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;
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;
&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 [[Slices|Slices studio]] is conforming to spec.}}&lt;br /&gt;
&lt;br /&gt;
=Code To Use=&lt;br /&gt;
To allow our frameworks to work well with JDK8 Streams, we employ a couple of standard interfaces over creating our own custom ones.&lt;br /&gt;
&lt;br /&gt;
==BiConsumer==&lt;br /&gt;
We use the standard [https://docs.oracle.com/javase/8/docs/api/java/util/function/BiConsumer.html BiConsumer&amp;lt;T,U&amp;gt;] interface with an [https://docs.oracle.com/javase/8/docs/api/java/util/function/BiConsumer.html#accept-T-U- BiConsumer's accept(t,u)] method in the place of a mythical, custom MapContext&amp;lt;K,V&amp;gt; interface with an emit(key,value) method.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public interface BiConsumer&amp;lt;T, U&amp;gt; {&lt;br /&gt;
    // invoke accept with each key and value you wish to emit&lt;br /&gt;
    void accept(T t, U u);&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Reducer&amp;lt;V,A,R&amp;gt;==&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/mapreduce/core/Reducer.html Reducer]&lt;br /&gt;
&lt;br /&gt;
check out the [[https://classes.engineering.wustl.edu/cse231/core/index.php?title=MapReduce_Reducer_Assignment#Background|Reducer Background]]&lt;br /&gt;
&lt;br /&gt;
==Range==&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;
==Ranges==&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;
&lt;br /&gt;
==HashUtils==&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/s20/apidocs/hash/studio/HashUtils.html#toIndex(java.lang.Object,int) toIndex(key,N)]&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;]&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|mapAndAccumulateAll&amp;lt;br&amp;gt;combineAndFinishAll|mapreduce.framework.lab.matrix}}&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 two 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 toIndex() 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;
===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[].class&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 a toIndex 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;
'''Spring 2022 Note:''' if you wish to use &amp;lt;code&amp;gt;join_void_fork_loop_with_index&amp;lt;/code&amp;gt; you will need to add the static import to the top of the file: &lt;br /&gt;
&lt;br /&gt;
 import static fj.FJ.join_void_fork_loop_with_index;&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;
===combineAndFinishAll===&lt;br /&gt;
{{Parallel|Map&amp;lt;K, R&amp;gt; combineAndFinishAll(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;
Hint: You can use the provided MultiWrapMap class to return the final row as a valid output.&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;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|_MatrixFrameworkTestSuite|mapreduce.framework.matrix.exercise}}&lt;br /&gt;
===MapAccumulateAll===&lt;br /&gt;
: {{TestSuite|MatrixMapAccumulateAllTestSuite|mapreduce.framework.matrix.exercise}}&lt;br /&gt;
&lt;br /&gt;
===CombineFinishAll===&lt;br /&gt;
{{TestSuite|MatrixCombineFinishAllTestSuite|mapreduce.framework.matrix.exercise}}&lt;br /&gt;
===Holistic===&lt;br /&gt;
{{TestSuite|MatrixHolisticTestSuite|mapreduce.framework.matrix.exercise}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Cholera_MapReduce_Application&amp;diff=3630</id>
		<title>Cholera MapReduce Application</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Cholera_MapReduce_Application&amp;diff=3630"/>
		<updated>2022-03-22T20:48:34Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* getValueRepresentation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:John_Snow_memorial_and_pub.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
=Motivation=&lt;br /&gt;
[https://en.wikipedia.org/wiki/Epidemiology Epidemiology is the important study of &amp;quot;why certain people are getting ill.&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
We get a chance to make sense of the data in a relatively open-ended studio.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;TLpzHHbFrHY&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;1jlsyucUwpo&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;9NVT6iZP2qg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Imagine you are [https://en.wikipedia.org/wiki/John_Snow a physician] in [https://en.wikipedia.org/wiki/1854_Broad_Street_cholera_outbreak 1854 London in the midst of a cholera outbreak].  Your theory that contaminated water is the cause meets resistance from the medical establishment which holds that it is spread via the air.&lt;br /&gt;
&lt;br /&gt;
Imagine further that your friend [https://en.wikipedia.org/wiki/Ada_Lovelace Ada] has left you access to computing.&lt;br /&gt;
&lt;br /&gt;
=Code To Use=&lt;br /&gt;
We have made the data from [https://www1.udel.edu/johnmack/frec682/cholera/cholera2.html this GIS Analysis] available via methods:&lt;br /&gt;
&lt;br /&gt;
* [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/SohoCholeraOutbreak1854.html#getDeaths() SohoCholeraOutbreak1854.getDeaths()]&amp;lt;br/&amp;gt;source: [https://web.archive.org/web/20160822002253/http://www1.udel.edu/johnmack/frec682/cholera/deaths.txt deaths.txt]&lt;br /&gt;
* [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/WaterPump.html WaterPump.values()]&amp;lt;br/&amp;gt;source: [https://web.archive.org/web/20160822002256/http://www1.udel.edu/johnmack/frec682/cholera/pumps.txt pumps.txt]&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/Location.html class Location]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/Location.html#getDistanceTo-mapreduce.apps.cholera.core.Location- double getDistanceTo( Location other )]&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/CholeraDeath.html class CholeraDeath]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/CholeraDeath.html#getLocation-- Location getLocation()]&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/WaterPump.html enum WaterPump]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/WaterPump.html#getLocation-- Location getLocation()]&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/SohoCholeraOutbreak1854.html class SohoCholeraOutbreak1854]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/SohoCholeraOutbreak1854.html#getDeaths-- &amp;lt;nowiki&amp;gt;static CholeraDeath[] getDeaths()&amp;lt;/nowiki&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
In order to provide a more open-ended assignment, we have abstracted out different aspects of the studio. In particular, &amp;lt;code&amp;gt;getValueRepresentation()&amp;lt;/code&amp;gt;  allows you to specify whether your CholeraApp sees high or low numbers as suspects. For example, if you return &amp;lt;code&amp;gt;ValueRepresentation.HIGH_NUMBERS_SUSPECT&amp;lt;/code&amp;gt;, you are indicating that a higher number is more likely to indicate that the water pump is the source of the cholera outbreak.  This will aid the [[#Visualization]] in presenting your findings. &lt;br /&gt;
&lt;br /&gt;
We have anticipated two basic approaches to this problem (with a variation^2 on one of the approaches). You may choose to implement an integer-based or a double-based CholeraApp. If you take a different approach than one we have anticipated, that is fine.  Get it checked out by an instructor.&lt;br /&gt;
&lt;br /&gt;
==CholeraUtils==&lt;br /&gt;
&lt;br /&gt;
===createApp===&lt;br /&gt;
return a new CholeraApp. Depending on which approach you plan to implement, it will be either &amp;lt;code&amp;gt;IntegerCholeraApp()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;DoubleCholeraApp()&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IntegerCholeraApp or DoubleCholeraApp==&lt;br /&gt;
{{CodeToImplement|IntegerCholeraApp or DoubleCholeraApp|getValueRepresentation&amp;lt;br&amp;gt;createMapper&amp;lt;br&amp;gt;createCollector|mapreduce.apps.cholera.exercise}}&lt;br /&gt;
&lt;br /&gt;
===getValueRepresentation===&lt;br /&gt;
{{Sequential|public static ValueRepresentation getValueRepresentation()}}&lt;br /&gt;
&lt;br /&gt;
return one of the three values of the [https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html enum] &amp;lt;code&amp;gt;ValueRepresentation&amp;lt;/code&amp;gt; to aid the visualization and testing. You can access the specific enum constant by typing &amp;lt;code&amp;gt; ValueRepresentation. &amp;lt;/code&amp;gt; .&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public enum ValueRepresentation {&lt;br /&gt;
	HIGH_NUMBERS_SUSPECT, LOW_NUMBERS_SUSPECT, LOW_NUMBERS_SUSPECT_SQUARED&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===createMapper===&lt;br /&gt;
{{Sequential|public static Mapper&amp;lt;CholeraDeath, WaterPump, Number&amp;gt; createMapper()}}&lt;br /&gt;
&lt;br /&gt;
We have implemented WaterPump as an enum.  You can access all the enum constants of any enum [https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html via its values() method]. For example:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;for (WaterPump pump : WaterPump.values()) {&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you need inspirations on how to implement a Mapper, revisit past assignments such as [https://classes.engineering.wustl.edu/cse231/core/index.php?title=Int_Sum_MapReduce_Apps_Studio#Card_Mapper CardMapper and WordCountMapper], which implements the Mapper&amp;lt;E, K, V&amp;gt; interface. Or you can directly tunnel into the Mapper&amp;lt;E, K, V&amp;gt; interface to see what method you need to implement.&lt;br /&gt;
&lt;br /&gt;
===createReducer===&lt;br /&gt;
Depending on your approach, you may be able to reuse one of your existing Reducers.  Otherwise, implement one to go with your Mapper.&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public static Reducer&amp;lt;? extends Number, ?, ? extends Number&amp;gt; createReducer()}}&lt;br /&gt;
&lt;br /&gt;
note: if you decide to build your own Reducer, the following will likely be a fine charateristics method&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;	@Override&lt;br /&gt;
	public Set&amp;lt;Characteristics&amp;gt; characteristics() {&lt;br /&gt;
		return EnumSet.noneOf(Characteristics.class);&lt;br /&gt;
	}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===OPTIONAL getThresholdIfApplicable===&lt;br /&gt;
We have noted that some students have been going with a threshold approach.  In order to support this in the visualization and the test (and to minimize the chances of interfering with students who have already started) we have added a separate class file CholeraThreshold.  You need not concern yourself with this file if you are going with a threshold-less strategy.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|CholeraApp|getThresholdIfApplicable|mapreduce.apps.cholera.studio}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public static double getThresholdIfApplicable()}}&lt;br /&gt;
&lt;br /&gt;
Return the threshold if you are using one in your Mapper.  Otherwise return [https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#NaN Double.NaN].&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;	public static double getThresholdIfApplicable() {&lt;br /&gt;
		final boolean IS_THRESHOLD_APPLICABLE = false;&lt;br /&gt;
		if (IS_THRESHOLD_APPLICABLE) {&lt;br /&gt;
			throw new NotYetImplementedException();&lt;br /&gt;
		} else {&lt;br /&gt;
			return Double.NaN;&lt;br /&gt;
		}&lt;br /&gt;
	}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Visualization==&lt;br /&gt;
Original Map Drawn By John Snow:&lt;br /&gt;
&lt;br /&gt;
[[File:Snow-cholera-map-1.jpg||400px]]&lt;br /&gt;
&lt;br /&gt;
Our Visualization App:&lt;br /&gt;
&lt;br /&gt;
{{Viz|CholeraOutbreakVisualizationApp|mapreduce.apps.cholera.viz}}&lt;br /&gt;
[[File:CholeraOutbreak.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Correctness==&lt;br /&gt;
If you have chosen to go a different route than the ones we anticipated, do not worry about passing the test suite.  Demo your work to an instructor in class and we can discuss its fitness.&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|CholeraStudioTestSuite|mapreduce}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Mutual_Friends_MapReduce_Application&amp;diff=3629</id>
		<title>Mutual Friends MapReduce Application</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Mutual_Friends_MapReduce_Application&amp;diff=3629"/>
		<updated>2022-03-22T20:43:30Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* MutualFriendsMapper */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Finding mutual friends is a compelling MapReduce App and has a nice write up on the web.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
[https://web.archive.org/web/20201109035737/http://stevekrenzel.com/finding-friends-with-mapreduce Steve Krenzel Finding Friends]&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
==Account==&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/mapreduce/apps/friends/core/Account.html class Account]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/s20/apidocs/mapreduce/apps/friends/core/Account.html#getId() getId()]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/mapreduce/apps/friends/core/Account.html#getFriendIds() getFriendIds()]&lt;br /&gt;
&lt;br /&gt;
==AccountId==&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/s20/apidocs/mapreduce/apps/friends/core/AccountId.html class AccountId]&lt;br /&gt;
&lt;br /&gt;
==SetIntersector&amp;lt;E&amp;gt;==&lt;br /&gt;
class [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/mapreduce/apps/friends/core/SetIntersector.html  SetIntersector&amp;lt;E&amp;gt;]&lt;br /&gt;
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/mapreduce/apps/friends/core/SetIntersector.html#accept(java.util.Set)) accept(friendIds)]&lt;br /&gt;
&lt;br /&gt;
: [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/mapreduce/apps/friends/core/SetIntersector.html#currentIntersection() currentIntersection()]&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==MutualFriendsMapper==&lt;br /&gt;
{{CodeToImplement|MutualFriendsMapper|map|mapreduce.apps.friends.exercise}}&lt;br /&gt;
{{Sequential|public void map(Account account, BiConsumer&amp;lt;OrderedPair&amp;lt;AccountId&amp;gt;, Set&amp;lt;AccountId&amp;gt;&amp;gt; keyValuePairConsumer)}}&lt;br /&gt;
&lt;br /&gt;
The only method you will need to alter is the map method. In this method, you will need to map every combination of the account holder to their friends. In order to do this, create ordered pairs of the given account’s ID and the IDs of the account holder’s friends. For each ordered pair, you feed the pair along with the full set of IDs of the account holder's friends into the keyValuePairConsumer.&lt;br /&gt;
&lt;br /&gt;
Hint: check out the methods in the Account class for help.&lt;br /&gt;
&lt;br /&gt;
==MutualFriendsReducer==&lt;br /&gt;
{{CodeToImplement|MutualFriendsReducer|reduce|mapreduce.apps.friends.exercise}}&lt;br /&gt;
{{Sequential|public Set&amp;lt;AccountId&amp;gt; reduce(List&amp;lt;Set&amp;lt;AccountId&amp;gt;&amp;gt; container)}}&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/mapreduce/apps/friends/core/SetIntersector.html SetIntersector] can be used to calculate the set intersection.  Invoke &amp;lt;code&amp;gt;accept(set)&amp;lt;/code&amp;gt; for each set in the &amp;lt;container&amp;gt; parameter, then invoke &amp;lt;code&amp;gt;currentIntersection()&amp;lt;/code&amp;gt; to get the result.&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|_MutualFriendsTestSuite|mapreduce.apps.friends.exercise}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Cholera_MapReduce_Application&amp;diff=3628</id>
		<title>Cholera MapReduce Application</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Cholera_MapReduce_Application&amp;diff=3628"/>
		<updated>2022-03-22T20:33:28Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* Code To Implement */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:John_Snow_memorial_and_pub.jpg|thumb]]&lt;br /&gt;
&lt;br /&gt;
=Motivation=&lt;br /&gt;
[https://en.wikipedia.org/wiki/Epidemiology Epidemiology is the important study of &amp;quot;why certain people are getting ill.&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
We get a chance to make sense of the data in a relatively open-ended studio.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;TLpzHHbFrHY&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;1jlsyucUwpo&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;9NVT6iZP2qg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Imagine you are [https://en.wikipedia.org/wiki/John_Snow a physician] in [https://en.wikipedia.org/wiki/1854_Broad_Street_cholera_outbreak 1854 London in the midst of a cholera outbreak].  Your theory that contaminated water is the cause meets resistance from the medical establishment which holds that it is spread via the air.&lt;br /&gt;
&lt;br /&gt;
Imagine further that your friend [https://en.wikipedia.org/wiki/Ada_Lovelace Ada] has left you access to computing.&lt;br /&gt;
&lt;br /&gt;
=Code To Use=&lt;br /&gt;
We have made the data from [https://www1.udel.edu/johnmack/frec682/cholera/cholera2.html this GIS Analysis] available via methods:&lt;br /&gt;
&lt;br /&gt;
* [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/SohoCholeraOutbreak1854.html#getDeaths() SohoCholeraOutbreak1854.getDeaths()]&amp;lt;br/&amp;gt;source: [https://web.archive.org/web/20160822002253/http://www1.udel.edu/johnmack/frec682/cholera/deaths.txt deaths.txt]&lt;br /&gt;
* [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/WaterPump.html WaterPump.values()]&amp;lt;br/&amp;gt;source: [https://web.archive.org/web/20160822002256/http://www1.udel.edu/johnmack/frec682/cholera/pumps.txt pumps.txt]&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/Location.html class Location]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/Location.html#getDistanceTo-mapreduce.apps.cholera.core.Location- double getDistanceTo( Location other )]&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/CholeraDeath.html class CholeraDeath]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/CholeraDeath.html#getLocation-- Location getLocation()]&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/WaterPump.html enum WaterPump]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/WaterPump.html#getLocation-- Location getLocation()]&lt;br /&gt;
&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/SohoCholeraOutbreak1854.html class SohoCholeraOutbreak1854]&lt;br /&gt;
: [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/mapreduce/apps/cholera/core/SohoCholeraOutbreak1854.html#getDeaths-- &amp;lt;nowiki&amp;gt;static CholeraDeath[] getDeaths()&amp;lt;/nowiki&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
In order to provide a more open-ended assignment, we have abstracted out different aspects of the studio. In particular, &amp;lt;code&amp;gt;getValueRepresentation()&amp;lt;/code&amp;gt;  allows you to specify whether your CholeraApp sees high or low numbers as suspects. For example, if you return &amp;lt;code&amp;gt;ValueRepresentation.HIGH_NUMBERS_SUSPECT&amp;lt;/code&amp;gt;, you are indicating that a higher number is more likely to indicate that the water pump is the source of the cholera outbreak.  This will aid the [[#Visualization]] in presenting your findings. &lt;br /&gt;
&lt;br /&gt;
We have anticipated two basic approaches to this problem (with a variation^2 on one of the approaches). You may choose to implement an integer-based or a double-based CholeraApp. If you take a different approach than one we have anticipated, that is fine.  Get it checked out by an instructor.&lt;br /&gt;
&lt;br /&gt;
==CholeraUtils==&lt;br /&gt;
&lt;br /&gt;
===createApp===&lt;br /&gt;
return a new CholeraApp. Depending on which approach you plan to implement, it will be either &amp;lt;code&amp;gt;IntegerCholeraApp()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;DoubleCholeraApp()&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IntegerCholeraApp or DoubleCholeraApp==&lt;br /&gt;
{{CodeToImplement|IntegerCholeraApp or DoubleCholeraApp|getValueRepresentation&amp;lt;br&amp;gt;createMapper&amp;lt;br&amp;gt;createCollector|mapreduce.apps.cholera.exercise}}&lt;br /&gt;
&lt;br /&gt;
===getValueRepresentation===&lt;br /&gt;
{{Sequential|public static ValueRepresentation getValueRepresentation()}}&lt;br /&gt;
&lt;br /&gt;
return one of the three values of the [https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html enum] &amp;lt;code&amp;gt;ValueRepresentation&amp;lt;/code&amp;gt; to aid the visualization and testing. You can access the specific enum constant by typing &amp;lt;code&amp;gt; ValueRepresentation. &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;public enum ValueRepresentation {&lt;br /&gt;
	HIGH_NUMBERS_SUSPECT, LOW_NUMBERS_SUSPECT, LOW_NUMBERS_SUSPECT_SQUARED&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===createMapper===&lt;br /&gt;
{{Sequential|public static Mapper&amp;lt;CholeraDeath, WaterPump, Number&amp;gt; createMapper()}}&lt;br /&gt;
&lt;br /&gt;
We have implemented WaterPump as an enum.  You can access all the enum constants of any enum [https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html via its values() method]. For example:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;for (WaterPump pump : WaterPump.values()) {&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you need inspirations on how to implement a Mapper, revisit past assignments such as [https://classes.engineering.wustl.edu/cse231/core/index.php?title=Int_Sum_MapReduce_Apps_Studio#Card_Mapper CardMapper and WordCountMapper], which implements the Mapper&amp;lt;E, K, V&amp;gt; interface. Or you can directly tunnel into the Mapper&amp;lt;E, K, V&amp;gt; interface to see what method you need to implement.&lt;br /&gt;
&lt;br /&gt;
===createReducer===&lt;br /&gt;
Depending on your approach, you may be able to reuse one of your existing Reducers.  Otherwise, implement one to go with your Mapper.&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public static Reducer&amp;lt;? extends Number, ?, ? extends Number&amp;gt; createReducer()}}&lt;br /&gt;
&lt;br /&gt;
note: if you decide to build your own Reducer, the following will likely be a fine charateristics method&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;	@Override&lt;br /&gt;
	public Set&amp;lt;Characteristics&amp;gt; characteristics() {&lt;br /&gt;
		return EnumSet.noneOf(Characteristics.class);&lt;br /&gt;
	}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===OPTIONAL getThresholdIfApplicable===&lt;br /&gt;
We have noted that some students have been going with a threshold approach.  In order to support this in the visualization and the test (and to minimize the chances of interfering with students who have already started) we have added a separate class file CholeraThreshold.  You need not concern yourself with this file if you are going with a threshold-less strategy.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|CholeraApp|getThresholdIfApplicable|mapreduce.apps.cholera.studio}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public static double getThresholdIfApplicable()}}&lt;br /&gt;
&lt;br /&gt;
Return the threshold if you are using one in your Mapper.  Otherwise return [https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#NaN Double.NaN].&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;	public static double getThresholdIfApplicable() {&lt;br /&gt;
		final boolean IS_THRESHOLD_APPLICABLE = false;&lt;br /&gt;
		if (IS_THRESHOLD_APPLICABLE) {&lt;br /&gt;
			throw new NotYetImplementedException();&lt;br /&gt;
		} else {&lt;br /&gt;
			return Double.NaN;&lt;br /&gt;
		}&lt;br /&gt;
	}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Visualization==&lt;br /&gt;
Original Map Drawn By John Snow:&lt;br /&gt;
&lt;br /&gt;
[[File:Snow-cholera-map-1.jpg||400px]]&lt;br /&gt;
&lt;br /&gt;
Our Visualization App:&lt;br /&gt;
&lt;br /&gt;
{{Viz|CholeraOutbreakVisualizationApp|mapreduce.apps.cholera.viz}}&lt;br /&gt;
[[File:CholeraOutbreak.png|600px]]&lt;br /&gt;
&lt;br /&gt;
==Correctness==&lt;br /&gt;
If you have chosen to go a different route than the ones we anticipated, do not worry about passing the test suite.  Demo your work to an instructor in class and we can discuss its fitness.&lt;br /&gt;
&lt;br /&gt;
{{TestSuite|CholeraStudioTestSuite|mapreduce}}&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Windbag_MapReduce_Application&amp;diff=3627</id>
		<title>Windbag MapReduce Application</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Windbag_MapReduce_Application&amp;diff=3627"/>
		<updated>2022-03-22T18:26:33Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* WindbagMapper */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
A simple Mapper and two simple Collectors reinforce our understanding of how pieces of the MapReduce puzzle can be reused.  Further, we will reduce the value type of Boolean to both an Integer and a Double in two different Collectors.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
A windbag is [https://www.merriam-webster.com/dictionary/windbag an exhaustively talkative person].  In this studio we will try to find who is the biggest windbag in [https://en.wikipedia.org/wiki/Hamlet Hamlet].&lt;br /&gt;
&lt;br /&gt;
[http://shakespeare-online.com/plays/hamlet_1_3.html Polonius' Advice To Laertes]&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==WindbagMapper==&lt;br /&gt;
{{CodeToImplement|WindbagMapper|map|mapreduce.apps.windbag.studio}}&lt;br /&gt;
&lt;br /&gt;
Note: the WindBagMapper is constructed with an [https://docs.oracle.com/javase/8/docs/api/java/util/function/IntPredicate.html IntPredicate].  This IntPredicate's test method should be invoked to determine if a [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/s20/apidocs/mapreduce/apps/play/core/PlayLine.html PlayLine]'s word count qualifies it as a windbag line. You can investigate the PlayLine class to see what information it contains.&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public void map(PlayLine item, BiConsumer&amp;lt;PlayRole, Boolean&amp;gt; keyValuePairConsumer)}}&lt;br /&gt;
&lt;br /&gt;
==TrueCountClassicReducer==&lt;br /&gt;
This Collector reduces to the number of emitted trues.&lt;br /&gt;
&lt;br /&gt;
For example, if a total of eight (8) values were emitted for a particular key and three (3) of them were true:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;[false, '''true''', false, false, '''true''', '''true''', false, false]&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
then the reduced value would be &amp;lt;code&amp;gt;3&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|TrueCountClassicReducer|reduce|mapreduce.apps.windbag.exercise}}&lt;br /&gt;
{{Sequential|public Integer reduce(List&amp;lt;Boolean&amp;gt; container)}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|This class inherits from ClassicReducer.  Be sure to have completed that studio first.}}&lt;br /&gt;
&lt;br /&gt;
==TruePortionClassicReducer==&lt;br /&gt;
This Collector reduces to the portion of emitted trues.&lt;br /&gt;
&lt;br /&gt;
For example, if a total of eight (8) values were emitted for a particular key and three (3) of them were true:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;[false, '''true''', false, false, '''true''', '''true''', false, false]&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
then the reduced value would be &amp;lt;code&amp;gt;0.375&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|TruePortionClassicReducer|reduce|mapreduce.apps.windbag.exercise}}&lt;br /&gt;
{{Sequential|public Double reduce(List&amp;lt;Boolean&amp;gt; container)}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|This class inherits from ClassicReducer.  Be sure to have completed that studio first.}}&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|WindbagStudioTestSuite|mapreduce}}&lt;br /&gt;
&lt;br /&gt;
==Output==&lt;br /&gt;
&lt;br /&gt;
{{Performance|WindbagApp|mapreduce.apps.windbag.client}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
           COUNT&lt;br /&gt;
===========================&lt;br /&gt;
      HAMLET: 27&lt;br /&gt;
        KING: 14&lt;br /&gt;
    POLONIUS:  7&lt;br /&gt;
     HORATIO:  5&lt;br /&gt;
     OPHELIA:  2&lt;br /&gt;
       GHOST:  2&lt;br /&gt;
FIRST PLAYER:  2&lt;br /&gt;
     LAERTES:  1&lt;br /&gt;
 ROSENCRANTZ:  1&lt;br /&gt;
   VOLTEMAND:  1&lt;br /&gt;
 PLAYER KING:  1&lt;br /&gt;
   GENTLEMAN:  1&lt;br /&gt;
PLAYER QUEEN:  1&lt;br /&gt;
       QUEEN:  1&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
          PORTION&lt;br /&gt;
===========================&lt;br /&gt;
   VOLTEMAND: 1.000000&lt;br /&gt;
   GENTLEMAN: 0.333333&lt;br /&gt;
 PLAYER KING: 0.250000&lt;br /&gt;
FIRST PLAYER: 0.250000&lt;br /&gt;
PLAYER QUEEN: 0.200000&lt;br /&gt;
       GHOST: 0.142857&lt;br /&gt;
        KING: 0.137255&lt;br /&gt;
    POLONIUS: 0.081395&lt;br /&gt;
      HAMLET: 0.075419&lt;br /&gt;
     HORATIO: 0.046729&lt;br /&gt;
     OPHELIA: 0.034483&lt;br /&gt;
 ROSENCRANTZ: 0.022222&lt;br /&gt;
     LAERTES: 0.016129&lt;br /&gt;
       QUEEN: 0.014493&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Windbag_MapReduce_Application&amp;diff=3626</id>
		<title>Windbag MapReduce Application</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Windbag_MapReduce_Application&amp;diff=3626"/>
		<updated>2022-03-22T18:19:34Z</updated>

		<summary type="html">&lt;p&gt;AngelaChen: /* WindbagMapper */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
A simple Mapper and two simple Collectors reinforce our understanding of how pieces of the MapReduce puzzle can be reused.  Further, we will reduce the value type of Boolean to both an Integer and a Double in two different Collectors.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
A windbag is [https://www.merriam-webster.com/dictionary/windbag an exhaustively talkative person].  In this studio we will try to find who is the biggest windbag in [https://en.wikipedia.org/wiki/Hamlet Hamlet].&lt;br /&gt;
&lt;br /&gt;
[http://shakespeare-online.com/plays/hamlet_1_3.html Polonius' Advice To Laertes]&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==WindbagMapper==&lt;br /&gt;
{{CodeToImplement|WindbagMapper|map|mapreduce.apps.windbag.studio}}&lt;br /&gt;
&lt;br /&gt;
Note: the WindBagMapper is constructed with an [https://docs.oracle.com/javase/8/docs/api/java/util/function/IntPredicate.html IntPredicate].  This IntPredicate's test method should be invoked to determine if a [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse231/s20/apidocs/mapreduce/apps/play/core/PlayLine.html PlayLine]'s word count qualifies it as a windbag line.&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public void map(PlayLine item, BiConsumer&amp;lt;PlayRole, Boolean&amp;gt; keyValuePairConsumer)}}&lt;br /&gt;
&lt;br /&gt;
==TrueCountClassicReducer==&lt;br /&gt;
This Collector reduces to the number of emitted trues.&lt;br /&gt;
&lt;br /&gt;
For example, if a total of eight (8) values were emitted for a particular key and three (3) of them were true:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;[false, '''true''', false, false, '''true''', '''true''', false, false]&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
then the reduced value would be &amp;lt;code&amp;gt;3&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|TrueCountClassicReducer|reduce|mapreduce.apps.windbag.exercise}}&lt;br /&gt;
{{Sequential|public Integer reduce(List&amp;lt;Boolean&amp;gt; container)}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|This class inherits from ClassicReducer.  Be sure to have completed that studio first.}}&lt;br /&gt;
&lt;br /&gt;
==TruePortionClassicReducer==&lt;br /&gt;
This Collector reduces to the portion of emitted trues.&lt;br /&gt;
&lt;br /&gt;
For example, if a total of eight (8) values were emitted for a particular key and three (3) of them were true:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;[false, '''true''', false, false, '''true''', '''true''', false, false]&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
then the reduced value would be &amp;lt;code&amp;gt;0.375&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|TruePortionClassicReducer|reduce|mapreduce.apps.windbag.exercise}}&lt;br /&gt;
{{Sequential|public Double reduce(List&amp;lt;Boolean&amp;gt; container)}}&lt;br /&gt;
&lt;br /&gt;
{{Warning|This class inherits from ClassicReducer.  Be sure to have completed that studio first.}}&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|WindbagStudioTestSuite|mapreduce}}&lt;br /&gt;
&lt;br /&gt;
==Output==&lt;br /&gt;
&lt;br /&gt;
{{Performance|WindbagApp|mapreduce.apps.windbag.client}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
           COUNT&lt;br /&gt;
===========================&lt;br /&gt;
      HAMLET: 27&lt;br /&gt;
        KING: 14&lt;br /&gt;
    POLONIUS:  7&lt;br /&gt;
     HORATIO:  5&lt;br /&gt;
     OPHELIA:  2&lt;br /&gt;
       GHOST:  2&lt;br /&gt;
FIRST PLAYER:  2&lt;br /&gt;
     LAERTES:  1&lt;br /&gt;
 ROSENCRANTZ:  1&lt;br /&gt;
   VOLTEMAND:  1&lt;br /&gt;
 PLAYER KING:  1&lt;br /&gt;
   GENTLEMAN:  1&lt;br /&gt;
PLAYER QUEEN:  1&lt;br /&gt;
       QUEEN:  1&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
          PORTION&lt;br /&gt;
===========================&lt;br /&gt;
   VOLTEMAND: 1.000000&lt;br /&gt;
   GENTLEMAN: 0.333333&lt;br /&gt;
 PLAYER KING: 0.250000&lt;br /&gt;
FIRST PLAYER: 0.250000&lt;br /&gt;
PLAYER QUEEN: 0.200000&lt;br /&gt;
       GHOST: 0.142857&lt;br /&gt;
        KING: 0.137255&lt;br /&gt;
    POLONIUS: 0.081395&lt;br /&gt;
      HAMLET: 0.075419&lt;br /&gt;
     HORATIO: 0.046729&lt;br /&gt;
     OPHELIA: 0.034483&lt;br /&gt;
 ROSENCRANTZ: 0.022222&lt;br /&gt;
     LAERTES: 0.016129&lt;br /&gt;
       QUEEN: 0.014493&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>AngelaChen</name></author>
		
	</entry>
</feed>