The term join is common in different programming

Info icon This preview shows pages 5–8. Sign up to view the full content.

is better style. The term “join” is common in different programming languages and libraries, though honestly it is not the most descriptive English word for the concept. It is common to combine the joining for-loop and the result-combining for-loop. Un- derstanding why this is still correct helps understand the join primitive. So far we have suggested writing code like this in our sum method: for ( int i= 0 ; i < 4 ; i++) ts[i]. join (); for ( int i= 0 ; i < 4 ; i++) ans += ts[i]. ans ; return ans; There is nothing wrong with the code above, but the following is also correct: for ( int i= 0 ; i < 4 ; i++) { ts[i]. join (); CPEN 221 – Fall 2016
Image of page 5

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

Fork-Join Parallelism 6 ans += ts[i]. ans ; } return ans; Here we do not wait for all the helper threads to finish before we start producing the final answer. But we still ensure that the main thread does not access a helper thread’s ans field until at least that helper thread has terminated. There is one last Java-specific detail we need when using the join method defined in java.lang.Thread . It turns out this method can throw a java.lang.InterruptedException so a method calling join will not compile unless it catches this exception or declares that it might be thrown. For concurrent programming, it may be bad style to ignore this exception, but for basic parallel programming like we are doing, this exception is a nui- sance and will not occur. So we will say no more about it. Also the ForkJoin framework we will use soon has a different join method that does not throw exceptions. Here, then, is a complete and correct program. 2 There is no change to the SumThread class. This example shows many of the key con- cepts of fork-join parallelism, but we will explain why it is poor style and can lead to suboptimal performance. Then we will discuss better approaches. class SumThread extends java. lang . Thread { int lo; // fields for communicating inputs int hi; int [] arr; int ans = 0 ; // for communicating result SumThread ( int [] a, int l, int h) { lo=l; hi=h; arr=a; } public void run () { // overriding, must have this type for ( int i=lo; i<hi; i++) ans += arr[i]; } } class C { static int sum ( int [] arr) throws java. lang . InterruptedException { int len = arr. length ; 2 Technically, for very large arrays, i*len might be too large and we should declare one of these vari- ables to have type long . We will ignore this detail throughout our readings to avoid distractions, but long is a wiser choice when dealing with arrays that may be very large. CPEN 221 – Fall 2016
Image of page 6
Fork-Join Parallelism 7 int ans = 0 ; SumThread[] ts = new SumThread[ 4 ]; for ( int i= 0 ; i < 4 ; i++) { ts[i] = new SumThread (arr,(i*len)/ 4 ,((i +1 )*len)/ 4 ); ts[i]. start (); } for ( int i= 0 ; i < 4 ; i++) { ts[i]. join (); ans += ts[i]. ans ; } return ans; } 3 Why Not To Use One Thread Per Processor Having presented a basic parallel algorithm, we will argue that the approach the algo- rithm takes is poor style and likely to lead to unnecessary inefficiency. Do not despair: the concepts we have learned like creating threads and using join will remain useful — and it was best to explain them using a too-simple approach. Moreover, many parallel programs are written in pretty much exactly this style, often because libraries (such as the ForkJoin framework) are unavailable. Fortunately, such libraries are now available
Image of page 7

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

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