The method that magically creates parallelism is

Info icon This preview shows pages 18–21. Sign up to view the full content.

• The method that “magically” creates parallelism is called fork instead of start . • The method that starts executing when a new thread begins is called compute instead of run . Recall these methods can also be called normally. (The method join is still called join .) The small additions involve starting the computation with a call to ForkJoinPool.commonPool().invoke . These are just some details because the library is not built into the Java language , so we have to do a little extra to initialize the library and start using it. Here is all you really need to know: • The entire program should have exactly one ForkJoinPool , so it makes sense to store it in a static field and use it for all the parallel algorithms in your program. The library’s commonPool static method returns such an object for you. CPEN 221 – Fall 2016
Image of page 18

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Fork-Join Parallelism 19 • Inside a subclass of RecursiveAction , you use fork , compute , and join just like we previously used start , run , and join . But outside such classes, you cannot use these methods or the library may not work correctly. To “get the parallelism started,” you instead use the invoke method of the ForkJoinPool . You pass to invoke a subclass of RecursiveAction and that object’s compute method will be called. Basically, use invoke once to start the algorithm and then fork or compute for the recursive calls. Conversely, do not use invoke from “inside” the library, i.e., in compute or any helper methods it uses. We will present one final version of our array-summing program to demonstrate one more class of the ForkJoin Framework that you should use as a matter of style. The RecursiveAction class is best only when the subcomputations do not produce a re- sult, whereas in our example they do: the sum of the range. It is quite common not to produce a result, for example a parallel program that increments every element of an array. So far, the way we have “returned” results is via a field, which we called ans . Instead, we can subclass RecursiveTask instead of RecursiveAction . However, RecursiveTask is a generic class with one type parameter: the type of value that compute should return. Here is the full version of the code using this more convenient and less error-prone class, followed by an explanation: Final, better version import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveTask; class SumArray extends RecursiveTask<Integer> { static int SEQUENTIAL_THRESHOLD = 1000 ; int lo; int hi; int [] arr; SumArray ( int [] a, int l, int h) { lo=l; hi=h; arr=a; } public Integer compute () { if (hi - lo <= SEQUENTIAL_THRESHOLD) { int ans = 0 ; for ( int i=lo; i < hi; ++i) ans += arr[i]; return ans; CPEN 221 – Fall 2016
Image of page 19
Fork-Join Parallelism 20 } else { SumArray left = new SumArray (arr,lo,(hi+lo)/ 2 ); SumArray right = new SumArray (arr,(hi+lo)/ 2 ,hi); left. fork (); int rightAns = right. compute (); int leftAns = left. join (); return leftAns + rightAns; } } } class Main { static int sumArray ( int [] array) { return ForkJoinPool. commonPool (). invoke ( new SumArray (array, 0 ,array. length )); } } Here are the differences from the version that subclasses RecursiveAction : compute returns an object of whatever (object) type we want. Because
Image of page 20

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 21
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '17
  • satish
  • fork-join parallelism, SumThread

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern