Difference between revisions of "Iced Cakes Pipeline"

From CSE231 Wiki
Jump to navigation Jump to search
Line 52: Line 52:
  
 
=Code To Implement=
 
=Code To Implement=
 +
==Required Code==
 
{{CodeToImplement|CakePipeline|mixBakeAndIceCakes|pipeline.cake.studio}}
 
{{CodeToImplement|CakePipeline|mixBakeAndIceCakes|pipeline.cake.studio}}
  
 
{{Parallel|public static IcedCake[] mixBakeAndIceCakes(Mixer mixer, Baker baker, Icer icer, int cakeCount)}}
 
{{Parallel|public static IcedCake[] mixBakeAndIceCakes(Mixer mixer, Baker baker, Icer icer, int cakeCount)}}
 +
 +
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! icer.ice
 +
 +
==Optional Challenge==
 +
{{CodeToImplement|TwoOvenPipeline|mixBakeAndIceCakes|pipeline.cake.challenge}}
 +
 +
{{Parallel|public static IcedCake[] mixBakeAndIceCakes(Mixer mixer, Baker baker1, Baker baker2, Icer icer, int cakeCount)}}
  
 
=Testing Your Solution=
 
=Testing Your Solution=

Revision as of 16:44, 15 June 2018

Motivation

Pipelines can increase throughput when processing a stream of data. We will gain additional experience with Phasers by building a software pipeline.

Backgroud

Code To Use

Cakes

class Mixer

mix(int cakeIndex)

class Baker

bake(int cakeIndex, MixedIngredients mixedIngredients)

class Icer

ice(int cakeIndex, BakedCake bakedCake)


Looping

Java For Loop

Phasers

class Phaser (Guide to the Java Phaser)

register
arrive
awaitAdvance use via PhaserUtils.awaitAdvanceForPhase(phaser, phase)

PhaserUtils

awaitAdvanceForPhase

If you are on or past the phase you want to await, then calling phaser.awaitAdvance() directly is fine. If you might be on a prior phase, then invoke PhaserUtils.awaitAdvanceForPhase(phaser, phase). If in doubt, invoking awaitAdvanceForPhase is safer.

	public static int awaitAdvanceForPhase(Phaser phaser, int phase) {
		return awaitAdvanceForPhase(phaser, phase, () -> Thread.yield());
	}

	public static int awaitAdvanceForPhase(Phaser phaser, int phase, Runnable runnable) {
		while (true) {
			int currentPhase = phaser.awaitAdvance(phase);
			if (currentPhase < 0 || currentPhase > phase) {
				return currentPhase;
			} else {
				if (runnable != null) {
					runnable.run();
				}
			}
		}
	}

Questions To Ask Yourself

  1. What are my tasks?
  2. What work does each task need to do?
  3. What, if anything, does each task depend upon? That is: what does each task have to wait for before it may proceed?

Code To Implement

Required Code

class: CakePipeline.java Java.png
methods: mixBakeAndIceCakes
package: pipeline.cake.studio
source folder: student/src/main/java

method: public static IcedCake[] mixBakeAndIceCakes(Mixer mixer, Baker baker, Icer icer, int cakeCount) Parallel.svg (parallel implementation required)

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! icer.ice

Optional Challenge

class: TwoOvenPipeline.java Java.png
methods: mixBakeAndIceCakes
package: pipeline.cake.challenge
source folder: student/src/main/java

method: public static IcedCake[] mixBakeAndIceCakes(Mixer mixer, Baker baker1, Baker baker2, Icer icer, int cakeCount) Parallel.svg (parallel implementation required)

Testing Your Solution

Visualization

class: CakePipelineVizApp.java VIZ
package: pipeline.cake.viz
source folder: student/src//java

CakePipelineGIF.gif

Correctness

class: CakePipelineTestSuite.java Junit.png
package: pipeline.cake.studio
source folder: testing/src/test/java

When you are passing the tests and your visualization looks good, demo it to an instructor.