Difference between revisions of "Threads and Executors"
Line 2: | Line 2: | ||
=Background= | =Background= | ||
+ | |||
+ | ==Tutorial== | ||
+ | [http://winterbe.com/posts/2015/04/07/java8-concurrency-tutorial-thread-executor-examples/ Java 8 Concurrency Tutorial: Threads and Executors] | ||
+ | |||
==Quicksort== | ==Quicksort== | ||
<youtube>SLauY6PpjW4</youtube> | <youtube>SLauY6PpjW4</youtube> | ||
Line 12: | Line 16: | ||
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/s18/apidocs/sort/core/quick/PivotLocation.html#getRightSidesLowerInclusive-- getRightSidesLowerInclusive()] | :[https://www.cse.wustl.edu/~cosgroved/courses/cse231/s18/apidocs/sort/core/quick/PivotLocation.html#getRightSidesLowerInclusive-- getRightSidesLowerInclusive()] | ||
− | =Where To Start= | + | ==Where To Start== |
Habanero Java Library (HJlib) is the product of the [https://wiki.rice.edu/confluence/display/HABANERO/Habanero+Extreme+Scale+Software+Research+Project Habanero Extreme Scale Software Research Laboratory]. It builds on the [http://x10-lang.org/ X10 programming language]. | Habanero Java Library (HJlib) is the product of the [https://wiki.rice.edu/confluence/display/HABANERO/Habanero+Extreme+Scale+Software+Research+Project Habanero Extreme Scale Software Research Laboratory]. It builds on the [http://x10-lang.org/ X10 programming language]. | ||
Line 18: | Line 22: | ||
In this assignment we will remove the training wheels for a moment to get some experience with some core Java parallel features Threads and Executors. | In this assignment we will remove the training wheels for a moment to get some experience with some core Java parallel features Threads and Executors. | ||
− | |||
− | |||
− | |||
=(Optional But Recommended) Join All Warm Up= | =(Optional But Recommended) Join All Warm Up= |
Revision as of 09:14, 8 February 2018
Contents
Motivation
Background
Tutorial
Java 8 Concurrency Tutorial: Threads and Executors
Quicksort
Javadocs
Where To Start
Habanero Java Library (HJlib) is the product of the Habanero Extreme Scale Software Research Laboratory. It builds on the X10 programming language.
HJlib provides some parallel features via static methods, most notably async and finish which handle a lot of the details of starting and joining tasks. We in CSE231 have thinly wrapped these methods for stylistic reasons as well as to afford more easily testing student code.
In this assignment we will remove the training wheels for a moment to get some experience with some core Java parallel features Threads and Executors.
(Optional But Recommended) Join All Warm Up
OPTIONAL
source: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html
"Iterators are weakly consistent, returning elements reflecting the state of the queue at some point at or since the creation of the iterator."
JoinAllTestSuite
ThreadsEventually joinAllInQueueViaPoll
Completing this optional warm up will help you when you implement #XQuicksort.
Threads
Javadocs
class SimpleThreadFactory
Video
implement Thread newThread(Runnable target)
Create and return a new thread with the target Runnable parameter you are passed.
Do *NOT* start this thread.
Certainly, do *NOT* run this thread.
Do not pass Go. Do not collect $200.
To repeat: just create a new Thread with the target Runnable and return it.
TAgeSum
implement int sumUpperLowerSplit(int[] ages, ThreadFactory threadFactory)
You will need use the passed in ThreadFactory to create a new thread or two (at your preference), start any threads you create, and join them.
Think about where you need to start and join any Threads to ensure both correctness and an appropriate amount of parallelism.
Executors
Javadocs
Videos
Executor submit and Future get
Mistake To Avoid: Do NOT call shutdown
Each method you write will be passed an executor. You need not (and should NOT) invoke shutdown(). That is the responsibility of whoever created the executor. For example, a JUnit test would do this if it was appropriate (i.e. it created an executor and was not using the common pool).
XNucleobaseCount
This part of the assignment should be very familiar as it is to a large degree implementing the Nucleobase Counting assignment with Executors instead of Habanero. It also adds a divide and conquer implementation of nucleobase counting.
class: | XNucleobaseCount.java | |
methods: | countLowerUpperSplit countNWaySplit countDivideAndConquer countDivideAndConquerKernel |
|
package: | tnx.lab.executor | |
source folder: | student/src/main/java |
Tip:Use countRangeSequential() from Nucleobase_Counting |
lower upper split
method: countLowerUpperSplit
(parallel implementation required)
n-way split
method: countNWaySplit
(parallel implementation required)
Please feel free to use the provided SliceUtils createNSlices(byte[] data, int numSlices). Then use the Java for each loop to iterate over the slices to create your tasks.
divide-and-conquer
method: countDivideAndConquer
(parallel implementation required)
This method should just get things going by invoking the countDivideAndConquerKernel on the entire chromosome array.
method: countDivideAndConquerKernel
(parallel implementation required)
NOTE: When you get down below the threshold and convert from parallel to sequential execution, do NOT feel compelled to build a sequential divide and conquer. Just invoke countRangeSequential on the remaining range. It is not like divide and conquer gives you a performance benefit in counting like it would for sorting.
XQuicksort
class: | XQuicksort.java | |
methods: | sequentialQuicksort sequentialQuicksortKernel parallelQuicksort parallelQuicksortKernel |
|
package: | tnx.lab.executor | |
source folder: | student/src/main/java |
Quicksort is an oldie but a goodie. First developed in 1959 and published in 1961 it is still the go to sorting algorithm today. The JDK8 implementation of Arrays.sort(array) is a DualPivotQuicksort.
Quicksort is also amenable to parallelism. Once the partitioning is done, both sides of the pivot can be sorted completely independently in parallel. This lends itself very nicely to the X10 family of languages as you can freely async as you divide and conquer, then join all of the tasks by wrapping it all in a single call to finish.
In this assignment you will mimic this behavior by submitting tasks to an executor, tracking the returned futures in in a ConcurrentLinkedQueue, then invoking get on all of those futures to mimic the single finish.
NOTE: ConcurrentLinkQueue's iterators are weakly consistent. Do the #Join All Warm Up to gain experience with handling this issue.
Videos
sequential
Warning:Unlike in Prof. Sarkar's videos which use inclusive maximums, in CSE 231 we use exclusive maximums to avoid having to subtract 1 all of the time. |
method: sequentialQuicksort
(sequential implementation only)
method: sequentialQuicksortKernel
(sequential implementation only)
parallel
method: parallelQuicksort
(parallel implementation required)
Make sure to use a thread safe data structure like ConcurrentLinkedQueue and NOT an unsafe data structure like LinkedList.
method: parallelQuicksortKernel
(parallel implementation required)
(Optional) Parallel Partition Fun
While perhaps a bit more complicated, and beyond the scope of this class, the partitioning step can also be done in parallel with scan.
class: | ParallelPartitioner.java | |
methods: | partitionRange | |
package: | sort.fun.quick | |
source folder: | student/src/main/java |
method: partitionRange
(parallel implementation required)
Testing Your Solution
Correctness
class: | ThreadsAndExecutorsTestSuite.java | |
package: | tnx.lab | |
source folder: | testing/src/test/java |
Sub test suites have been created so you can test portions of the lab separately.
Performance
class: | NucleobaseCountTiming.java | |
package: | tnx.lab.executor | |
source folder: | src/main/java |
class: | QuicksortTiming.java | |
package: | tnx.lab.executor | |
source folder: | src/main/java |
Rubric
As always, please make sure to cite your work appropriately.
Total points: 100
SimpleThreadFactory subtotal: 5
- Correct newThread (5)
TAgeSum subtotal: 10
- Correct sumUpperLowerSplit (10)
XNucleobaseCount subtotal: 40
- Correct count2WaySplit (10)
- Correct countNWaySplit (15)
- Correct countDivideAndConquer and countDivideAndConquerKernel (15)
XQuicksort subtotal: 35
- Correct sequentialQuicksort and sequentialQuicksortKernel (10)
- Correct parallelQuicksort and parallelQuicksortKernel (25)
Whole project:
- Clarity and efficiency (10)
Pledge, Acknowledgments, Citations
As always, fill out the Pledge, Acknowledgments, and Citations file: lab3-pledge-acknowledgments-citations.txt