Find the maximum or minimum element of an array given

Info icon This preview shows pages 21–24. Sign up to view the full content.

View Full Document Right Arrow Icon
• Find the maximum or minimum element of an array. • Given an array of strings, compute the sum (or max, or min) of all their lengths. • Find the left-most array index that has an element satisfying some property. Compared to summing an array, all that changes is the base case for the recursion and how we combine results. For example, to find the index of the leftmost 42 in an array of length n , we can do the following (where a final result of n means the array does not hold a 42): • For the base case, return lo if arr[lo] holds 42 and n otherwise. • To combine results, return the smaller number. CPEN 221 – Fall 2016
Image of page 21

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

View Full Document Right Arrow Icon
Fork-Join Parallelism 22 Implement one or two of these problems to convince yourself they are not any harder than what we have already done. Or come up with additional problems that can be solved the same way. Problems that have this form are so common that there is a name for them: reductions , which you can remember by realizing that we take a collection of data items (in an array) and reduce the information down to a single result. As we have seen, the way reductions can work in parallel is to compute answers for the two halves recursively and in parallel and then merge these to produce a result. However, we should be clear that not every problem over an array of data can be solved with a simple parallel reduction . To avoid getting into arcane problems, let us describe a general situation. Suppose you have sequential code like this: interface BinaryOperation<T> { T m (T x, T y); } class C<T> { T fold (T[] arr, BinaryOperation<T> binop, T initialValue) { T ans = initialValue; for ( int i= 0 ; i < arr. length ; ++i) ans = binop. m (ans,arr[i]); return ans; } } The name fold is conventional for this sort of algorithm. The idea is to start with initialValue and keep updating the “answer so far” by applying some binary function m to the current answer and the next element of the array. Without any additional information about what m computes, this algorithm cannot be effectively parallelized since we cannot process arr[i] until we know the answer from the first i-1 iterations of the for-loop. For a more humorous example of a procedure that cannot be sped up given additional resources: 9 women cannot make a baby in 1 month. So what do we have to know about the BinaryOperation above in order to use a paral- lel reduction? It turns out all we need is that the operation is {associative , meaning for all a , b , and c , m ( a , m ( b , c )) is the same as m ( m ( a , b ) , c ) . Our array-summing algorithm is correct because a + ( b + c ) = ( a + b ) + c . Our find-the-leftmost-index-holding 42 algorithm is correct because min is also an associative operator. CPEN 221 – Fall 2016
Image of page 22
Fork-Join Parallelism 23 Because reductions using associative operators are so common, we could write one generic algorithm that took the operator, and what to do for a base case, as arguments.
Image of page 23

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

View Full Document Right Arrow Icon
Image of page 24
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