So creating thousands of threads could use gigabytes

Info icon This preview shows pages 16–19. Sign up to view the full content.

or more. So creating thousands of threads could use gigabytes of space. Hence we will switch to the library described in the next section for parallel programming. CPEN 221 – Fall 2016
Image of page 16

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

Fork-Join Parallelism 17 5 The Java ForkJoin Framework Java 7 (and higher) includes classes in the java.util.concurrent package designed exactly for the kind of fine-grained fork-join parallel computing these notes use. In ad- dition to supporting lightweight threads (which the library calls ForkJoinTasks) that are small enough that even a million of them should not overwhelm the system, the imple- mentation includes a scheduler and run-time system with provably optimal expected- time guarantees. Similar libraries for other languages include Intel’s Thread Building Blocks, Microsoft’s Task Parallel Library for C#, and others. The core ideas and imple- mentation techniques go back much further to the Cilk language , an extension of C developed since 1994. This section describes just a few practical details and library specifics. Compared to Java threads, the core ideas are all the same, but some of the method names and inter- faces are different — in places more complicated and in others simpler. Naturally, we give a full example (actually two) for summing an array of numbers. The actual library contains many other useful features and classes, but we will use only the primitives related to forking and joining, implementing anything else we need ourselves. We first show a full program (minus a main method) that is as much as possible like the version we wrote using Java threads. We show a version using a sequential cut-off and only one helper thread at each recursive subdivision though removing these important improvements would be easy. After discussing this version, we show a second version that uses Java’s generic types and a different library class. This second version is better style, but easier to understand after the first version. First version: inferior style import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveAction; class SumArray extends RecursiveAction { static int SEQUENTIAL_THRESHOLD = 1000 ; int lo; int hi; int [] arr; int ans = 0 ; SumArray ( int [] a, int l, int h) { lo=l; hi=h; arr=a; } protected void compute () { CPEN 221 – Fall 2016
Image of page 17
Fork-Join Parallelism 18 if (hi - lo <= SEQUENTIAL_THRESHOLD) { for ( int i=lo; i < hi; ++i) ans += arr[i]; } else { SumArray left = new SumArray (arr,lo,(hi+lo)/ 2 ); SumArray right = new SumArray (arr,(hi+lo)/ 2 ,hi); left. fork (); right. compute (); left. join (); ans = left. ans + right. ans ; } } } class Main { static int sumArray ( int [] array) { SumArray t = new SumArray (array, 0 ,array. length ); ForkJoinPool. commonPool (). invoke (t); return t. ans ; } } While there are many differences compared to using Java’s threads, the overall struc- ture of the algorithm should look similar. Furthermore, most of the changes are just different names for classes and methods: • Subclass java.util.concurrent.RecursiveAction instead of java.lang.Thread .
Image of page 18

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

Image of page 19
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