Fortunately such libraries are now available on many

Info icon This preview shows pages 7–9. Sign up to view the full content.

View Full Document Right Arrow Icon
the ForkJoin framework) are unavailable. Fortunately, such libraries are now available on many platforms. The problem with the previous approach was dividing the work into exactly 4 pieces. This approach assumes there are 4 processors available to do the work (no other code needs them) and that each processor is given approximately the same amount of work. Sometimes these assumptions may hold, but it would be better to use algorithms that do not rely on such brittle assumptions. The rest of this section explains in more detail why these assumptions are unlikely to hold and some partial solutions. Different computers have different numbers of processors We want parallel programs that effectively use the processors available to them. Using exactly 4 threads is a horrible approach. If 8 processors are available, half of them will sit idle and our program will be no faster than with 4 processors. If 3 processors are available, our 4-thread program will take approximately twice as long as with 4 proces- sors. If 3 processors are available and we rewrite our program to use 3 threads, then we will use resources effectively and the result will only be about 33% slower than when we had 4 processors and 4 threads. (We will take 1/3 as much time as the sequential version compared to 1/4 as much time. And 1/3 is 33% slower than 1/4.) But we do CPEN 221 – Fall 2016
Image of page 7

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

View Full Document Right Arrow Icon
Fork-Join Parallelism 8 not want to have to edit our code every time we run it on a computer with a different number of processors. A natural solution is a core software-engineering principle you should already know: Do not use constants where a variable is appropriate. Our sum method can take as a parameter the number of threads to use, leaving it to some other part of the program to decide the number. (There are Java library methods to ask for the number of proces- sors on the computer, for example, but we argue next that using that number is often unwise.) It would look like this: static int sum ( int [] arr, int numThreads) throws java. lang . InterruptedException { int len = arr. length ; int ans = 0 ; SumThread[] ts = new SumThread[numThreads]; for ( int i= 0 ; i < numThreads; i++) { ts[i] = new SumThread (arr,(i*len)/numThreads,((i +1 )*len)/numThreads); ts[i]. start (); } for ( int i= 0 ; i < numThreads; i++) { ts[i]. join (); ans += ts[i]. ans ; } return ans; } Note that you need to be careful with integer division not to introduce rounding errors when dividing the work. The processors available to part of the code can change The second dubious assumption made so far is that every processor is available to the code we are writing. But some processors may be needed by other programs or even other parts of the same program. We have parallelism after all — maybe the caller to sum is already part of some outer parallel algorithm. The operating system can reassign processors at any time, even when we are in the middle of summing array elements. It is fine to assume that the underlying Java implementation will try to use the available processors effectively, but we should not assume 4 or even numThreads processors will be available from the beginning to the end of running our parallel algorithm.
Image of page 8
Image of page 9
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