Because int is not an object type we write integer

Info icon This preview shows pages 20–22. Sign up to view the full content.

returns an object of whatever (object) type we want. Because int is not an object type, we write Integer instead. Java implicitly converts an int to/from an Integer in all the places it is needed in this program. • The join method also returns the value returned by compute in the thread that is being joined to. Therefore, we do not need an ans field: we write int leftAns = left.join() to get the value that the thread bound to left returned from its compute method. • The invoke method works similarly when passed a subclass of a RecursiveTask class: It returns the value of the compute method that is called. • The class declaration of SumArray indicates that it extends RecursiveTask<Integer> . More generally, always put the same type between the angle brackets as the return type of compute . • Note that because Java expressions are always evaluated left-to-right, we could replace the last 3 lines of the else-branch in compute with just return right.compute() + left.join(); . But this is rather subtle — left.join() + right.compute() would destroy the parallelism — so we do not advise writ- ing this shorter but less clear code. If you are familiar with Java’s generic types, this use of them should not be particularly perplexing. The library is also using static overloading for the invoke method. But as users of the library, it suffices just to follow the pattern in the example above. CPEN 221 – Fall 2016
Image of page 20

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

Fork-Join Parallelism 21 Why is there RecursiveAction and RecursiveTask ? The former is better style when there really is nothing to return; otherwise RecursiveTask is better. Both are actually implemented in terms of the same superclass inside the library. You can use that class directly, but it is less convenient. Given the convenience of not needing a field for returning values, why not also provide the convenience of not needing fields to pass arguments ( arr , lo , and hi )? That would be nice, but there is no particularly pleasant way to write such a library in Java. The ForkJoin framework is just a library; it cannot make any changes/extensions to the Java language. It uses some advanced Java features to be as convenient as it can be. 6 Reductions and Maps It may seem that given all the work we did to implement something as conceptually simple as summing an array that fork/join programming is too complicated. To the con- trary, it turns out that many, many problems can be solved very much like we solved this one. Just like regular for-loops took some getting used to when you started pro- gramming but now you can recognize exactly what kind of loop you need for all sorts of problems, divide-and-conquer parallelism often follows a small number of patterns. Once you know the patterns, most of your programs are largely the same. For example, here are several problems for which efficient parallel algorithms look al- most identical to summing an array: • Count how many array elements satisfy some property (e.g., how many elements are the number 42).
Image of page 21
Image of page 22
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