Experiment with function objects and the Java Fork/Join Framework for parallel computing.
Complete the assigned reading for the next session, according to the course schedule.
Science! For the rest of this homework you’ll write a little code, run several experiments, and write up a brief lab report.
We encourage you to work in pairs for this homework. We have not set up pair repositories, so just choose one person’s machine for the project and do your work there. (Consistently using one machine will also reduce the sources of variation in your experimental results.)
Complete the following tasks in the project ForkJoinIntro
that you checked out in class.
Get started with the fork/join framework.
Open the class useless.Main
. Run the class as a Java application; it will probably give you an error. Read the big comment block at the top of the file that describes how to run programs that use the fork/join framework. Follow those instructions and get help if the program still won’t run. (Eclipse may hide, or fold, this comment for us. Click the + symbol next to NOTE WELL
to see the whole comment.)
(This extra step is because the framework hasn’t been included in Java 6. When Java 7 is released, this step will no longer be necessary.
Incrementor
class so the program correctly prints The answer is 42
. Visualizing the processors It can be fun to visualize how the fork-join code uses additional processors. The Resource Monitor program in Windows 7 can help with that.
Resource Monitor
in the listCPU
tab.
A useful experiment, part 1. Next you’ll conduct two experiments with the code in the useful
package. For this first experiment, you’ll vary the size of the array
useful.Main
, using your new knowledge from task a. useful.Main
and useful.ArraySummer
. It’s based on the reading for today. Make sure you understand what the code is doing. In particular:
ArraySummer
is a function object that recursively creates more function objects.ArraySummer
’s compute()
method. Review Grossman’s Beginner’s Introduction to the Java ForkJoin Framework and ask questions if ArraySummer
doesn’t make sense to you. StopWatch
in useful.Main
to collect timing information.
A useful experiment, part 2. Continuing with the useful
package, next you’ll vary the cut-off point where the ArraySummer
switches from parallel recursive tasks, to old-school sequential code.
SEQUENTIAL_THRESHOLD
in ArraySummer
, carefully noting the threshold and the time reported. Be sure to investigate thresholds near 1 (fully parallel) and near the array size (fully sequential). As before, you’ll make a table and graph of these for your lab report (see step 3.i below).
Programming Interlude. The provided mergesort
package contains a sequential implementation of merge sort in the class SequentialMergeSorter
. The package also provides the skeletons for a Main
class and a parallel merge sorter.
Before you can conduct the last two experiments, you need to complete ParallelMergeSorter
. To do this:
mergesort.Main
and SequentialMergeSorter
.compute()
method in ArraySummer.
compute()
method in ParallelMergeSorter
by combining the sorting algorithm from SequentialMergeSorter
’s sort()
method, with the fork/join technique and sequential threshold used in ArraySummer
. mergesort.Main
.
Build your test fixture. To conduct the last two experiments, you also need to write some code to execute and time the tests. Use the timing code in useful.Main
as a model, but carefully note the following differences:
StopWatch
objects, one that times sequential tests and one that times parallel tests.
Inside your TEST_ITERATIONS
loop, you should generate a new random array and make a copy of it each iteration, like the code at the beginning of mergesort.Main
’s main()
method does.
Why?!? Because the sorters mutate the array that they are sorting. If we want unique timing data for each test iteration, we need a new test array.
StopWatch
objects can be restarted after they are stopped. So, you’ll want to start the appropriate stop watch immediately before sequential sorting and stop it immediately after. Then do the same with the other stop watch and sorter. Make sure you don’t have a stop watch running while generating and copying the new random array (unless you want to add a third StopWatch
object for that purpose). Lab Report. Prepare a brief report summarizing the experiments you performed. For each experiment,
Reports will be graded for accuracy and professionalism, including proper use of English.
Remember, in all your code:
Turn in your programming work by committing it to your SVN repository. If you worked in pairs, only one of you needs to commit the work, but be sure to put both your names in the comments of the files you edit.
Bring a hard copy of your lab report to your next class session.