In practice the cutoffs are usually larger with

Info icon This preview shows pages 15–17. Sign up to view the full content.

View Full Document Right Arrow Icon
In practice, the cutoffs are usually larger, with numbers between 500 and 5000 being typical. It is often worth doing some quick calculations to understand the benefits of things like cutoffs. Suppose we are summing an array with 2 30 elements. Without a cutoff, we would use 2 31 - 1, (i.e., two billion) threads. With a cutoff of 1000, we would use approximately 2 21 (i.e., 2 million) threads since the last 10 levels of the recursion would be eliminated. Computing 1 - 2 21 / 2 31 , we see we have eliminated 99 . 9 % of the threads. Use cutoffs! Our second improvement may seem anticlimactic compared to cutoffs because it only reduces the number of threads by an additional factor of two. Nonetheless, it is worth seeing for efficiency especially because the ForkJoin Framework in the next section per- forms poorly if you do not do this optimization “by hand.” The key is to notice that all threads that create two helper threads are not doing much work themselves: they di- vide the work in half, give it to two helpers, wait for them to finish, and add the results. Rather than having all these threads wait around, it is more efficient to create one helper thread to do half the work and have the thread do the other half itself . Modifying our code to do this is easy since we can just call the run method directly, which recall is just an ordinary method call unlike the “magic” start method. class SumThread extends java. lang . Thread { static int SEQUENTIAL_CUTOFF = 1000 ; int lo; // fields for communicating inputs int hi; int [] arr; int ans = 0 ; // for communicating result SumThread ( int [] a, int l, int h) { arr=a; lo=l; hi=h; } CPEN 221 – Fall 2016
Image of page 15

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

View Full Document Right Arrow Icon
Fork-Join Parallelism 16 public void run (){ if (hi - lo < SEQUENTIAL_CUTOFF) { for ( int i=lo; i < hi; i++) ans += arr[i]; } else { SumThread left = new SumThread (arr,lo,(hi+lo)/ 2 ); SumThread right= new SumThread (arr,(hi+lo)/ 2 ,hi); left. start (); right. run (); left. join (); ans = left. ans + right. ans ; } } } int sum ( int [] arr) { SumThread t = new SumThread (arr, 0 ,arr. length ); t. run (); return t. ans ; } Notice how the code above creates two SumThread objects, but only creates one helper thread with left.start() . It then does the right half of the work itself by calling right.run() . There is only one call to join because only one helper thread was cre- ated. The order here is still essential so that the two halves of the work are done in parallel. Creating a SumThread object for the right half and then calling run rather than creating a thread may seem odd, but it keeps the code from getting more compli- cated and still conveys the idea of dividing the work into two similar parts that are done in parallel. Unfortunately, even with these optimizations, the code above will run poorly in practice, especially if given a large array. The implementation of Java’s threads is not engineered for threads that do such a small amount of work as adding 1000 numbers: it takes much longer just to create, start running, and dispose of a thread. The space overhead may also be prohibitive. In particular, it is not uncommon for a Java implementation to pre- allocate the maximum amount of space it allows for the call-stack, which might be 2MB or more. So creating thousands of threads could use gigabytes of space. Hence we will
Image of page 16
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.

{[ 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