Using the ForkJoin Framework
Using the ForkJoin Framework
First and foremost, read Dan's intro to the framework; reading and following its directions can save a fair amount of anguish later on. The steps listed in this html page are in part a summary of Dan's text, though some is added and some is left out.
There are alternatives to the steps below, but it'll save you a headache to follow through these the first time, even if they differ from the way you normally do things in eclipse.
- Download the jar here
- Outside of Eclipse, create a new directory in which you want to place your forkjoin project; place the jar there
- In Eclipse, create a new project, but choose the 'Create Project from Existing Source' option while doing so; choose the directory into which you have just placed the jar file
- Now for some settings: go to Project->Properties and under "Java Compiler" check "Enable project specific settings", and make sure everything is set to Java 1.6 (if you don't have 1.6 installed, you'll need to do so before proceeding)
- Still in Project->Properties, go to "Java Build Path", then Source, choose "Add Folder", and select the directory you added above.
- In your new project, create a new 'Source Folder', if one doesn't show up already
- Create/add your class to the source folder; make sure it has a main method:
- Your class should extend RecursiveTask or RecursiveAction
- Once in your program, and only once, before you start parallelizing, you need to startup the forkjoin pool. It can be done in main, or you can just have it as a public static variable: static final ForkJoinPool fjPool=new ForkJoinPool();
- In main(), you start up the entire process via fjPool.invoke(new ForkJoinTemplate(a,0,num)), or whatever its arguments are
- You need a particular run configuration for this whole thing to work: Create a new run configuration and under arguments, in VM Arguments, put the following: -Xbootclasspath/p:jsr166.jar
Troubleshooting
I ran into a number of possibilities setting this set up the first time, so allocate a bit of extra time to get it up and running. If you run into problems:
- Did you create your project from 'existing source'? There are other ways to make sure the jar is correctly referenced, but creating from existing source is probably the easiest. Getting a 'Security Exception' when you try to run the program is one sign of this happening
- 'NoClassDefFoundError' or something similar when you try to run may mean that the 'VM Argument' isn't set correctly; it must be exactly '-Xbootclasspath/p:jsr166.jar'
- Out of memory, stack overflow, etc.: It's running, but runs out of memory (as you may have divined from the error message); up the cutoff, cut down on the size of the input, etc. Another possibility is that it's not switching to your sequential case properly; try checking that code