CSE332: Data Abstractions Lecture 20: Parallel Prefix, Pack, and Sorting Dan Grossman Spring 2012.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Section 5: More Parallel Algorithms
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2010.
CSE 332 Data Abstractions: Introduction to Parallelism and Concurrency Kate Deibel Summer 2012 August 1, 2012CSE 332 Data Abstractions, Summer
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
CS 206 Introduction to Computer Science II 04 / 27 / 2009 Instructor: Michael Eckmann.
A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 3 Parallel Prefix, Pack, and Sorting Dan Grossman Last Updated: November.
CSE332: Data Abstractions Lecture 20: Parallel Prefix and Parallel Sorting Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 21: Parallel Prefix and Parallel Sorting Tyler Robison Summer
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 3 Parallel Prefix, Pack, and Sorting Steve Wolfman, based on work by Dan.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
CSE373: Data Structures & Algorithms Lecture 27: Parallel Reductions, Maps, and Algorithm Analysis Nicki Dell Spring 2014.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
Chapter 4, Part II Sorting Algorithms. 2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
CSE373: Data Structures & Algorithms Lecture 22: Parallel Reductions, Maps, and Algorithm Analysis Kevin Quinn Fall 2015.
A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 3 Parallel Prefix, Pack, and Sorting Steve Wolfman, based on work by Dan.
Parallel Prefix, Pack, and Sorting. Outline Done: –Simple ways to use parallelism for counting, summing, finding –Analysis of running time and implications.
Mergesort example: Merge as we return from recursive calls Merge Divide 1 element 829.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
CSE332: Data Abstractions Lecture 7: AVL Trees
Prof. U V THETE Dept. of Computer Science YMA
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Data Structures: Disjoint Sets, Segment Trees, Fenwick Trees
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
CS 162 Intro to Programming II
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
Quicksort "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Design and Analysis of Algorithms
Instructor: Lilian de Greef Quarter: Summer 2017
Parallel Sorting Algorithms
Quicksort 1.
CS 3343: Analysis of Algorithms
CSE373: Data Structures & Algorithms Lecture 27: Parallel Reductions, Maps, and Algorithm Analysis Catie Baker Spring 2015.
Complexity Present sorting methods. Binary search. Other measures.
i206: Lecture 14: Heaps, Graphs intro.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
Unit-2 Divide and Conquer
Ch 7: Quicksort Ming-Te Chi
Data Structures Review Session
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
ITEC 2620M Introduction to Data Structures
C++ Plus Data Structures
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
Parallel Sorting Algorithms
24 Searching and Sorting.
Sub-Quadratic Sorting Algorithms
CSE373: Data Structure & Algorithms Lecture 21: Comparison Sorting
Quicksort.
ITEC 2620M Introduction to Data Structures
CSE373: Data Structures & Algorithms Implementing Union-Find
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Algorithms CSCI 235, Spring 2019 Lecture 20 Order Statistics II
Quicksort.
Searching/Sorting/Searching
Algorithms Recurrences.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
The Selection Problem.
CSE 332: Parallel Algorithms
Design and Analysis of Algorithms
CSCE 3110 Data Structures & Algorithm Analysis
CS203 Lecture 15.
Divide and Conquer Merge sort and quick sort Binary search
Quicksort.
Presentation transcript:

CSE332: Data Abstractions Lecture 20: Parallel Prefix, Pack, and Sorting Dan Grossman Spring 2012

CSE332: Data Abstractions Outline Done: Simple ways to use parallelism for counting, summing, finding Analysis of running time and implications of Amdahl’s Law Now: Clever ways to parallelize more than is intuitively possible Parallel prefix: This “key trick” typically underlies surprising parallelization Enables other things like packs Parallel sorting: quicksort (not in place) and mergesort Easy to get a little parallelism With cleverness can get a lot Spring 2012 CSE332: Data Abstractions

The prefix-sum problem Given int[] input, produce int[] output where output[i] is the sum of input[0]+input[1]+…+input[i] Sequential can be a CSE142 exam problem: int[] prefix_sum(int[] input){ int[] output = new int[input.length]; output[0] = input[0]; for(int i=1; i < input.length; i++) output[i] = output[i-1]+input[i]; return output; } Does not seem parallelizable Work: O(n), Span: O(n) This algorithm is sequential, but a different algorithm has Work: O(n), Span: O(log n) Spring 2012 CSE332: Data Abstractions

CSE332: Data Abstractions Parallel prefix-sum The parallel-prefix algorithm does two passes Each pass has O(n) work and O(log n) span So in total there is O(n) work and O(log n) span So like with array summing, parallelism is n/log n An exponential speedup First pass builds a tree bottom-up: the “up” pass Second pass traverses the tree top-down: the “down” pass Spring 2012 CSE332: Data Abstractions

CSE332: Data Abstractions Local bragging Historical note: Original algorithm due to R. Ladner and M. Fischer at UW in 1977 Richard Ladner joined the UW faculty in 1971 and hasn’t left 1968? 1973? recent Spring 2012 CSE332: Data Abstractions

CSE332: Data Abstractions Example range 0,8 sum fromleft 76 range 0,4 sum fromleft range 4,8 sum fromleft 36 40 range 0,2 sum fromleft range 2,4 sum fromleft range 4,6 sum fromleft range 6,8 sum fromleft 10 26 30 10 r 0,1 s f r 1,2 s f r 2,3 s f r 3,4 s f r 4,5 s f r 5,6 s f r 6,7 s f r 7,8 s f 6 4 16 10 16 14 2 8 input 6 4 16 10 16 14 2 8 output Spring 2012 CSE332: Data Abstractions

CSE332: Data Abstractions Example range 0,8 sum fromleft 76 range 0,4 sum fromleft range 4,8 sum fromleft 36 40 36 range 0,2 sum fromleft range 2,4 sum fromleft range 4,6 sum fromleft range 6,8 sum fromleft 10 26 30 10 10 36 66 r 0,1 s f r 1,2 s f r 2,3 s f r 3,4 s f r 4,5 s f r 5,6 s f r 6,7 s f r 7,8 s f 6 4 16 10 16 14 2 8 6 10 26 36 52 66 68 input 6 4 16 10 16 14 2 8 output 6 10 26 36 52 66 68 76 Spring 2012 CSE332: Data Abstractions

CSE332: Data Abstractions The algorithm, part 1 Up: Build a binary tree where Root has sum of the range [x,y) If a node has sum of [lo,hi) and hi>lo, Left child has sum of [lo,middle) Right child has sum of [middle,hi) A leaf has sum of [i,i+1), i.e., input[i] This is an easy fork-join computation: combine results by actually building a binary tree with all the range-sums Tree built bottom-up in parallel Could be more clever with an array like with heaps Analysis: O(n) work, O(log n) span Spring 2012 CSE332: Data Abstractions

CSE332: Data Abstractions The algorithm, part 2 Down: Pass down a value fromLeft Root given a fromLeft of 0 Node takes its fromLeft value and Passes its left child the same fromLeft Passes its right child its fromLeft plus its left child’s sum (as stored in part 1) At the leaf for array position i, output[i]=fromLeft+input[i] This is an easy fork-join computation: traverse the tree built in step 1 and produce no result Leaves assign to output Invariant: fromLeft is sum of elements left of the node’s range Analysis: O(n) work, O(log n) span Spring 2012 CSE332: Data Abstractions

CSE332: Data Abstractions Sequential cut-off Adding a sequential cut-off is easy as always: Up: just a sum, have leaf node hold the sum of a range Down: output[lo] = fromLeft + input[lo]; for(i=lo+1; i < hi; i++) output[i] = output[i-1] + input[i] Spring 2012 CSE332: Data Abstractions

Parallel prefix, generalized Just as sum-array was the simplest example of a common pattern, prefix-sum illustrates a pattern that arises in many, many problems Minimum, maximum of all elements to the left of i Is there an element to the left of i satisfying some property? Count of elements to the left of i satisfying some property This last one is perfect for an efficient parallel pack… Perfect for building on top of the “parallel prefix trick” We did an inclusive sum, but exclusive is just as easy Spring 2012 CSE332: Data Abstractions

CSE332: Data Abstractions Pack [Non-standard terminology] Given an array input, produce an array output containing only elements such that f(elt) is true Example: input [17, 4, 6, 8, 11, 5, 13, 19, 0, 24] f: is elt > 10 output [17, 11, 13, 19, 24] Parallelizable? Finding elements for the output is easy But getting them in the right place seems hard Spring 2012 CSE332: Data Abstractions

Parallel prefix to the rescue Parallel map to compute a bit-vector for true elements input [17, 4, 6, 8, 11, 5, 13, 19, 0, 24] bits [1, 0, 0, 0, 1, 0, 1, 1, 0, 1] Parallel-prefix sum on the bit-vector bitsum [1, 1, 1, 1, 2, 2, 3, 4, 4, 5] Parallel map to produce the output output [17, 11, 13, 19, 24] output = new array of size bitsum[n-1] FORALL(i=0; i < input.length; i++){ if(bits[i]==1) output[bitsum[i]-1] = input[i]; } Spring 2012 CSE332: Data Abstractions

CSE332: Data Abstractions Pack comments First two steps can be combined into one pass Just using a different base case for the prefix sum No effect on asymptotic complexity Can also combine third step into the down pass of the prefix sum Again no effect on asymptotic complexity Analysis: O(n) work, O(log n) span 2 or 3 passes, but 3 is a constant Parallelized packs will help us parallelize quicksort… Spring 2012 CSE332: Data Abstractions

CSE332: Data Abstractions Quicksort review Recall quicksort was sequential, in-place, expected time O(n log n) Best / expected case work Pick a pivot element O(1) Partition all the data into: O(n) The elements less than the pivot The pivot The elements greater than the pivot Recursively sort A and C 2T(n/2) How should we parallelize this? Spring 2012 CSE332: Data Abstractions

CSE332: Data Abstractions Quicksort Best / expected case work Pick a pivot element O(1) Partition all the data into: O(n) The elements less than the pivot The pivot The elements greater than the pivot Recursively sort A and C 2T(n/2) Easy: Do the two recursive calls in parallel Work: unchanged of course O(n log n) Span: now T(n) = O(n) + 1T(n/2) = O(n) So parallelism (i.e., work / span) is O(log n) Spring 2012 CSE332: Data Abstractions

CSE332: Data Abstractions Doing better O(log n) speed-up with an infinite number of processors is okay, but a bit underwhelming Sort 109 elements 30 times faster Google searches strongly suggest quicksort cannot do better because the partition cannot be parallelized The Internet has been known to be wrong  But we need auxiliary storage (no longer in place) In practice, constant factors may make it not worth it, but remember Amdahl’s Law Already have everything we need to parallelize the partition… Spring 2012 CSE332: Data Abstractions

Parallel partition (not in place) Partition all the data into: The elements less than the pivot The pivot The elements greater than the pivot This is just two packs! We know a pack is O(n) work, O(log n) span Pack elements less than pivot into left side of aux array Pack elements greater than pivot into right size of aux array Put pivot between them and recursively sort With a little more cleverness, can do both packs at once but no effect on asymptotic complexity With O(log n) span for partition, the total span for quicksort is T(n) = O(log n) + 1T(n/2) = O(log2 n) Spring 2012 CSE332: Data Abstractions

CSE332: Data Abstractions Example Step 1: pick pivot as median of three 8 1 4 9 3 5 2 7 6 Steps 2a and 2c (combinable): pack less than, then pack greater than into a second array Fancy parallel prefix to pull this off not shown 1 4 3 5 2 1 4 3 5 2 6 8 9 7 Step 3: Two recursive sorts in parallel Can sort back into original array (like in mergesort) Spring 2012 CSE332: Data Abstractions

CSE332: Data Abstractions Now mergesort Recall mergesort: sequential, not-in-place, worst-case O(n log n) Sort left half and right half 2T(n/2) Merge results O(n) Just like quicksort, doing the two recursive sorts in parallel changes the recurrence for the span to T(n) = O(n) + 1T(n/2) = O(n) Again, parallelism is O(log n) To do better, need to parallelize the merge The trick won’t use parallel prefix this time Spring 2012 CSE332: Data Abstractions

Parallelizing the merge Need to merge two sorted subarrays (may not have the same size) 1 4 8 9 2 3 5 6 7 Idea: Suppose the larger subarray has n elements. In parallel: Merge the first n/2 elements of the larger half with the “appropriate” elements of the smaller half Merge the second n/2 elements of the larger half with the rest of the smaller half Spring 2012 CSE332: Data Abstractions

Parallelizing the merge 4 6 8 9 1 2 3 5 7 Spring 2012 CSE332: Data Abstractions

Parallelizing the merge 4 6 8 9 1 2 3 5 7 Get median of bigger half: O(1) to compute middle index Spring 2012 CSE332: Data Abstractions

Parallelizing the merge 4 6 8 9 1 2 3 5 7 Get median of bigger half: O(1) to compute middle index Find how to split the smaller half at the same value as the left-half split: O(log n) to do binary search on the sorted small half Spring 2012 CSE332: Data Abstractions

Parallelizing the merge 4 6 8 9 1 2 3 5 7 Get median of bigger half: O(1) to compute middle index Find how to split the smaller half at the same value as the left-half split: O(log n) to do binary search on the sorted small half Size of two sub-merges conceptually splits output array: O(1) Spring 2012 CSE332: Data Abstractions

Parallelizing the merge 4 6 8 9 1 2 3 5 7 1 2 3 4 5 6 7 8 9 lo hi Get median of bigger half: O(1) to compute middle index Find how to split the smaller half at the same value as the left-half split: O(log n) to do binary search on the sorted small half Size of two sub-merges conceptually splits output array: O(1) Do two submerges in parallel Spring 2012 CSE332: Data Abstractions

CSE332: Data Abstractions The Recursion 4 6 8 9 1 2 3 5 7 4 6 8 9 1 2 3 5 7 When we do each merge in parallel, we split the bigger one in half and use binary search to split the smaller one Spring 2012 CSE332: Data Abstractions

Analysis Sequential recurrence for mergesort: T(n) = 2T(n/2) + O(n) which is O(n log n) Doing the two recursive calls in parallel but a sequential merge: Work: same as sequential Span: T(n)=1T(n/2)+O(n) which is O(n) Parallel merge makes work and span harder to compute Each merge step does an extra O(log n) binary search to find how to split the smaller subarray To merge n elements total, do two smaller merges of possibly different sizes But worst-case split is (1/4)n and (3/4)n When subarrays same size and “smaller” splits “all” / “none” Spring 2012 CSE332: Data Abstractions

CSE332: Data Abstractions Analysis continued For just a parallel merge of n elements: Work is T(n) = T(3n/4) + T(n/4) + O(log n) which is O(n) Span is T(n) = T(3n/4) + O(log n), which is O(log2 n) (neither bound is immediately obvious, but “trust me”) So for mergesort with parallel merge overall: Work is T(n) = 2T(n/2) + O(n), which is O(n log n) Span is T(n) = 1T(n/2) + O(log2 n), which is O(log3 n) So parallelism (work / span) is O(n / log2 n) Not quite as good as quicksort’s O(n / log n) But worst-case guarantee And as always this is just the asymptotic result Spring 2012 CSE332: Data Abstractions