Sorting Algorithms. - rearranging a list of numbers into increasing (or decreasing) order. Potential Speedup

Similar documents
Algorithms and Applications

Parallel Sorting Algorithms

Lecture 3: Sorting 1

Parallel Systems Course: Chapter VIII. Sorting Algorithms. Kumar Chapter 9. Jan Lemeire ETRO Dept. Fall Parallel Sorting

Parallel Systems Course: Chapter VIII. Sorting Algorithms. Kumar Chapter 9. Jan Lemeire ETRO Dept. November Parallel Sorting

Lecture 8 Parallel Algorithms II

Sorting Algorithms. Ananth Grama, Anshul Gupta, George Karypis, and Vipin Kumar

CSC 447: Parallel Programming for Multi- Core and Cluster Systems

Parallel Sorting Algorithms

Sorting (Chapter 9) Alexandre David B2-206

CSC630/CSC730 Parallel & Distributed Computing

Introduction to Parallel Computing

Sorting (Chapter 9) Alexandre David B2-206

Parallel Sorting. Sathish Vadhiyar

Parallel quicksort algorithms with isoefficiency analysis. Parallel quicksort algorithmswith isoefficiency analysis p. 1

CS256 Applied Theory of Computation

Distributed Sorting. Chapter Array & Mesh

Sorting Algorithms. + Analysis of the Sorting Algorithms

CS475 Parallel Programming

Information Coding / Computer Graphics, ISY, LiTH

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

COMP Data Structures

Quick Sort. CSE Data Structures May 15, 2002

Divide and Conquer Sorting Algorithms and Noncomparison-based

Mesh Models (Chapter 8)

Partitioning and Divide-and- Conquer Strategies

Conquer Strategies. Partitioning and Divide-and- Partitioning Strategies + + Using separate send()s and recv()s. Slave

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Classic Data Structures Introduction UNIT I

Sorting Algorithms. Slides used during lecture of 8/11/2013 (D. Roose) Adapted from slides by

O(n): printing a list of n items to the screen, looking at each item once.

CHAPTER 5 Pipelined Computations

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Sorting (I) Hwansoo Han

DATA STRUCTURES/UNIT 3

Lecture 9: Group Communication Operations. Shantanu Dutt ECE Dept. UIC

Prefix Computation and Sorting in Dual-Cube

Pipelined Computations

S O R T I N G Sorting a list of elements implemented as an array. In all algorithms of this handout the sorting of elements is in ascending order

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

Unit-2 Divide and conquer 2016

4. Sorting and Order-Statistics

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

Chapter 7 Sorting. Terminology. Selection Sort

Sorting and Selection

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)?

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

S1) It's another form of peak finder problem that we discussed in class, We exploit the idea used in binary search.

Lesson 1 4. Prefix Sum Definitions. Scans. Parallel Scans. A Naive Parallel Scans

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

Sorting. Task Description. Selection Sort. Should we worry about speed?

Chapter 11 Image Processing

Numerical Algorithms

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Question 7.11 Show how heapsort processes the input:

Load Balancing and Termination Detection

Sorting. CSC 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation CSC Picture

CS61BL. Lecture 5: Graphs Sorting

Sorting. Bringing Order to the World

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

Multiple-choice (35 pt.)

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

II (Sorting and) Order Statistics

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4.

Dense Matrix Algorithms

SORTING AND SELECTION

Introduction. Sorting. Definitions and Terminology: Program efficiency. Sorting Algorithm Analysis. 13. Sorting. 13. Sorting.

SORTING, SETS, AND SELECTION

Cpt S 122 Data Structures. Sorting

1 a = [ 5, 1, 6, 2, 4, 3 ] 4 f o r j i n r a n g e ( i + 1, l e n ( a ) 1) : 3 min = i

CS 310 Advanced Data Structures and Algorithms

Interconnection networks

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used

Data Partitioning. Figure 1-31: Communication Topologies. Regular Partitions

Contents. Preface xvii Acknowledgments. CHAPTER 1 Introduction to Parallel Computing 1. CHAPTER 2 Parallel Programming Platforms 11

UNIT 7. SEARCH, SORT AND MERGE

John Mellor-Crummey Department of Computer Science Rice University

Programming II (CS300)

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

Copyright 2012 by Pearson Education, Inc. All Rights Reserved.

Lecture Notes on Quicksort

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

CHAPTER 7 Iris Hui-Ru Jiang Fall 2008

Synchronous Computations

Overview of Sorting Algorithms

Lecture 5. Applications: N-body simulation, sorting, stencil methods

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

The divide and conquer strategy has three basic parts. For a given problem of size n,

Sorting. Dr. Baldassano Yu s Elite Education

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

Efficient Prefix Computation on Faulty Hypercubes

Dr. Amotz Bar-Noy s Compendium of Algorithms Problems. Problems, Hints, and Solutions

Principles of Algorithm Design

Transcription:

Sorting Algorithms - rearranging a list of numbers into increasing (or decreasing) order. Potential Speedup The worst-case time complexity of mergesort and the average time complexity of quicksort are both Ο(nlogn), where there are n numbers. Ο(nlogn) is, in fact, optimal for any sequential sorting algorithm without using any special properties of the numbers. Hence, the best parallel time complexity we can expect based upon a sequential sorting algorithm but using n processors is O(nlogn ) Optimal parallel time complexity = ----------------------- = O( logn ) n A Ο(logn) sorting algorithm with n processors has been demonstrated by Leighton (1984) based upon an algorithm by Ajtai, Komlós, and Szemerédi (1983), but the constant hidden in the order notation is extremely large. An Ο(logn) sorting algorithm is also described by Leighton (1994) for an n-processor hypercube using random operations. Akl (1985) describes 20 different parallel sorting algorithms, several of which achieve the lower bound for a particular interconnection network. But, in general, a realistic Ο(logn) algorithm with n processors is a goal that will not be easy to achieve. It may be that the number of processors will be greater than n. 241

Rank Sort The number of numbers that are smaller than each selected number is counted. This count provides the position of the selected number in the list; that is, its rank in the list. Suppose there are n numbers stored in an array, a[0] a[n-1]. First a[0] is read and compared with each of the other numbers, a[1] a[n-1], recording the number of numbers less than a[0]. Suppose this number is x. This is the index of the location in the final sorted list. The number a[0] is copied into the final sorted list b[0] b[n-1], at location b[x]. Actions repeated with the other numbers. Overall sequential sorting time complexity of Ο(n 2 ) (not exactly a good sequential sorting algorithm!). Sequential Code for (i = 0; i < n; i++) { /* for each number */ x = 0; for (j = 0; j < n; j++) /* count number of nos less than it */ if (a[i] > a[j]) x++; b[x] = a[i]; /* copy number into correct place */ } (This code will fail if duplicates exist in the sequence of numbers.) 242

Parallel Code Using n Processors One processor allocated to one of the numbers Processor finds the final index of one numbers in Ο(n) steps. With all processors operating in parallel, the parallel time complexity Ο(n). In forall notation, the code would look like forall (i = 0; i < n; i++) { /* for each number in parallel*/ x = 0; for (j = 0; j < n; j++) /* count number of nos less than it */ if (a[i] > a[j]) x++; b[x] = a[i]; /* copy number into correct place */ } The parallel time complexity, Ο(n), is better than any sequential sorting algorithm. We can do even better if we have more processors. 243

Using n 2 Processors Comparing one selected number with each of the other numbers in the list can be performed using multiple processors: a[i] a[0] a[i] a[n-1] Compare Increment counter, x b[x] = a[i] Figure 9.1 Finding the rank in parallel. n 1 processors are used to find the rank of one number With n numbers, (n 1)n processors or (almost) n 2 processors needed. A single counter is needed for each number. Incrementing the counter is done sequentially and requires a maximum of n steps. Total number of steps would be given by 1 + n. 244

Reduction in Number of Steps A tree structure could be used to reduce the number of steps involved in incrementing the counter: a[i] a[0] a[i] a[1] a[i] a[2] a[i] a[3] Compare 0/1 0/1 0/1 0/1 Add Add 0/1/2 0/1/2 Tree Add 0/1/2/3/4 Figure 9.2 Parallelizing the rank computation. Leads to an Ο(logn) algorithm with n 2 processors for sorting numbers. The actual processor efficiency of this method is relatively low. 245

Parallel Rank Sort Conclusions Rank sort can sort: in Ο(n) with n processors or in Ο(logn) using n 2 processors. In practical applications, using n 2 processors will be prohibitive. Theoretically possible to reduce the time complexity to Ο(1) by considering all the increment operations as happening in parallel since they are independent of each other. Ο(1) is, of course, the lower bound for any problem. 246

Message Passing Parallel Rank Sort Master-Slave Approach Requires shared access to the list of numbers. Master process responds to request for numbers from slaves. Algorithm better for shared memory Master a[] b[] Read numbers Place selected number Slaves Figure 9.3 slaves. Rank sort using a master and 247

Compare-and-Exchange Sorting Algorithms Compare and Exchange Form the basis of several, if not most, classical sequential sorting algorithms. Two numbers, say A and B, are compared. If A > B, A and B are exchanged, i.e.: if (A > B) { temp = A; A = B; B = temp; } 248

Message-Passing Compare and Exchange One simple way of implementing the compare and exchange is for P 1 to send A to P 2, which then compares A and B and sends back B to P 1 if A is larger than B (otherwise it sends back A to P 1 ): Sequence of steps A P 1 1 Send(A) P 2 B If A > B send(b) else send(a) 2 Compare If A > B load A else load B 3 Figure 9.4 Compare and exchange on a message-passing system Version 1. Code: Process P 1 send(&a, P 2 ); recv(&a, P 2 ); Process P 2 recv(&a, P 1 ); if (A > B) { send(&b, P 1 ); B = A; } else send(&a, P 1 ); 249

Alternative Message Passing Method For P 1 to send A to P 2 and P 2 to send B to P 1. Then both processes perform compare operations. P 1 keeps the larger of A and B and P 2 keeps the smaller of A and B: A P 1 1 Send(A) P 2 B Send(B) If A > B load B 3 Compare 2 Compare If A > B load A 3 Figure 9.5 Compare and exchange on a message-passing system Version 2. Code: Process P 1 send(&a, P 2 ); recv(&b, P 2 ); if (A > B) A = B; Process P 2 recv(&a, P 1 ); send(&b, P 1 ); if (A > B) B = A; Process P 1 performs the send() first and process P 2 performs the recv() first to avoid deadlock. Alternatively, both P 1 and P 2 could perform send() first if locally blocking (asynchronous) sends are used and sufficient buffering is guaranteed to exist - not safe message passing. 250

Note on Precision of Duplicated Computations Previous code assumes that the if condition, A > B, will return the same Boolean answer in both processors. Different processors operating at different precision could conceivably produce different answers if real numbers are being compared. This situation applies to anywhere computations are duplicated in different processors to reduce message passing, or to make the code SPMD. 251

Data Partitioning Suppose there are p processors and n numbers. A list of n/p numbers would be assigned to each processor: P 1 P 2 Original numbers Final numbers 88 50 28 25 43 42 28 25 88 50 28 25 98 80 43 42 Merge 98 88 80 50 43 42 28 25 Keep higher numbers Return lower numbers Figure 9.6 Merging two sublists Version 1. 252

Keep lower numbers (final numbers) 98 88 80 50 43 42 28 25 P 1 P 2 Original Merge numbers Merge 98 80 43 42 88 50 28 25 Original numbers 98 80 43 42 88 50 28 25 Figure 9.7 Merging two sublists Version 2. 98 88 80 50 43 42 28 25 Keep higher numbers (final numbers) 253

Bubble Sort The largest number is first moved to the very end of the list by a series of compares and exchanges, starting at the opposite end. The actions are repeated for each number. The larger numbers move ( bubble ) toward one end: Original sequence: 4 2 7 8 5 1 3 6 4 2 7 8 5 1 3 6 2 4 7 8 5 1 3 6 Phase 1 Place largest number 2 4 7 8 5 1 3 6 2 4 7 8 5 1 3 6 2 4 7 5 8 1 3 6 2 4 7 5 1 8 3 6 2 4 7 5 1 3 8 6 2 4 7 5 1 3 6 8 2 4 7 5 1 3 6 8 Phase 2 Place next largest number 2 4 7 5 1 3 6 8 2 4 5 7 1 3 6 8 2 4 5 1 7 3 6 8 2 4 5 1 3 7 6 8 2 4 5 1 3 6 7 8 Phase 3 2 4 5 1 3 6 7 8 Time Figure 9.8 Steps in bubble sort. 254

With numbers held in array a[]: Sequential Code for (i = n - 1; i > 0; i--) for (j = 0; j < i; j++) { k = j + 1; if (a[j] > a[k]) { temp = a[j]; a[j] = a[k]; a[k] = temp; } } Time Complexity n 1 Number of compare and exchange operations = i = n ------------------- ( n 1) 2 i = 1 which indicates a time complexity of Ο(n 2 ) given that a single compare-and-exchange operation has a constant complexity, Ο(1). 255

Parallel Bubble Sort The bubbling action of one iteration could start before the previous iteration has finished so long as it does not overtake the previous bubbling action: Phase 1 1 Phase 2 1 2 1 Time Phase 3 2 1 3 2 1 Phase 4 3 2 1 4 3 2 1 Figure 9.9 Overlapping bubble sort actions in a pipeline. 256

Odd-Even (Transposition) Sort Variation of bubble sort. Operates in two alternating phases, an even phase and an odd phase. Even phase Even-numbered processes exchange numbers with their right neighbor. Odd phase Odd-numbered processes exchange numbers with their right neighbor. Time Step 0 1 2 3 4 5 6 7 P 0 P 1 P 2 P 3 P 4 P 5 P 6 P 7 4 2 7 8 5 1 3 6 2 4 7 8 1 5 3 6 2 4 7 1 8 3 5 6 2 4 1 7 3 8 5 6 2 1 4 3 7 5 8 6 1 2 3 4 5 7 6 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 Figure 9.10 Odd-even transposition sort sorting eight numbers. 257

Odd-Even Transposition Sort Code Even Phase P i, i = 0, 2, 4,, n 2 (even) P i, i = 1, 3, 5,, n 1 (odd) recv(&a, P i+1 ); send(&a, P i-1 ); /* even phase */ send(&b, P i+1 ); recv(&b, P i-1 ); if (A > B) B = A; if (A > B) A = B; /* exchange */ where the number stored in P i (even) is B and the number stored in P i (odd) is A. Odd Phase P i, i = 1, 3, 5,, n 3 (odd) P i, i = 2, 4, 6,, n 2 (even) send(&a, P i+1 ); recv(&a, P i-1 ); /* odd phase */ recv(&b, P i+1 ); send(&b, P i-1 ); if (A > B) A = B; if (A > B) B = A; /* exchange */ Combined P i, i = 1, 3, 5,, n 3 (odd) P i, i = 0, 2, 4,, n 2 (even) send(&a, P i-1 ); recv(&a, P i+1 ); /* even phase */ recv(&b, P i-1 ); send(&b, P i+1 ); if (A > B) A = B; if (A > B) B = A; if (i <= n-3) { if (i >= 2) { /* odd phase */ send(&a, P i+1 ); recv(&a, P i-1 ); recv(&b, P i+1 ) send(&b, P i-1 ); if (A > B) A = B; if (A > B) B = A; } } 258

Two-Dimensional Sorting If the numbers are mapped onto a mesh, other distinct possibilities exist for sorting the numbers. The layout of a sorted sequence on a mesh could be row by row or snakelike. In a snakelike sorted list, the numbers are arranged in nondecreasing order: Smallest number Largest number Figure 9.11 Snakelike sorted list. 259

Shearsort Requires n( logn + 1) steps for n numbers on a n n mesh. Odd phase Each row of numbers is sorted independently, in alternative directions: Even rows The smallest number of each column is placed at the rightmost end and largest number at the leftmost end. Odd rows The smallest number of each column is placed at the leftmost end and the largest number at the rightmost end. Even phase Each column of numbers is sorted independently, placing the smallest number of each column at the top and the largest number at the bottom. After logn + 1 phases, the numbers are sorted with a snakelike placement in the mesh. Note the alternating directions of the row sorting phase, which matches the final snakelike layout. 4 14 8 2 10 3 13 16 7 15 1 5 12 6 11 9 (a) Original placement of numbers 2 4 8 14 16 13 10 3 1 5 7 15 12 11 9 6 (b) Phase 1 Row sort 1 4 7 3 2 5 8 6 12 11 9 14 16 13 10 15 (c) Phase 2 Column sort 1 3 4 7 8 6 5 2 9 11 12 14 16 15 13 10 1 3 4 2 8 6 5 7 9 11 12 10 16 15 13 14 1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13 (d) Phase 3 Row sort (e) Phase 4 Column sort (f) Final phase Row sort Figure 9.12 Shearsort. 260

Using Transposition For algorithms that alternate between acting within rows and acting within columns, we can be limited to rows by transposing the array of data points between each phase. A transpose operation causes the elements in each column to be in positions in a row. The transpose operation is placed between the row operations and column operations: (a) Operations between elements in rows (b) Transpose operation (c) Operations between elements in rows (originally columns) Figure 9.13 Using the transpose operation to maintain operations in rows. The transposition can be achieved with n( n 1) communications or Ο(n) communications. A single all-to-all routine could be reduce this. 261

Mergesort The unsorted list is first divided into half. Each half is again divided into two. This is continued until individual numbers are obtained. Then pairs of numbers are combined (merged) into sorted list of two numbers. Pairs of these lists of four numbers are merged into sorted lists of eight numbers. This is continued until the one fully sorted list is obtained. Unsorted list 4 2 7 8 5 1 3 6 P 0 Divide list 4 2 7 8 5 1 3 6 P 0 P 4 4 2 7 8 5 1 3 6 P 0 P 2 P 4 P 6 4 2 7 8 5 1 3 6 P 0 P 1 P 2 P 3 P 4 P 5 P 6 P 7 2 4 7 8 1 5 3 6 P 0 P 2 P 4 P 6 Merge 2 4 7 8 1 3 5 6 P 0 P 4 1 2 3 4 5 6 7 8 Sorted list P 0 Process allocation Figure 9.14 Mergesort using tree allocation of processes. 262

Sequential time complexity is Ο(nlogn). Analysis There are 2 log n steps in the parallel version but each step may need to perform more than one basic operation, depending upon the number of numbers being processed. Communication In the division phase, communication only takes place as follows: Communication at each step Processor communication t startup + (n/2)t data P 0 P 4 t startup + (n/4)t data P 0 P 2 ; P 4 P 6 t startup + (n/8)t data P 0 P 1 ; P 2 P 3 ; P 4 P 5 ; P 6 P 7. with log p steps, given p processors. In the merge phase, the reverse communications take place: t startup + (n/8)t data P 0 P 1 ; P 2 P 3 ; P 4 P 5 ; P 6 P 7 t startup + (n/4)t data P 0 P 2 ; P 4 P 6 t startup + (n/2)t data P 0 P 4. again log p steps. This leads to the communication time being or: t comm = 2(t startup + (n/2)t data + t startup + (n/4)t data + t startup + (n/8)t data + ) t comm 2(log p)t startup + 2nt data 263

Computation Computations only occurs in merging the sublists. Merging can be done by stepping through each list, moving the smallest found into the final list first. It takes 2n 1 steps in the worst case to merge two sorted lists each of n numbers into one sorted list in this manner. Therefore, the computation consists of Hence: t comp = 1 P 0 ; P 2 ; P 4 ; P 6 t comp = 3 P 0 ; P 2 t comp = 7 P 0. log p t comp = ( 2 i 1) i = 1 The parallel computational time complexity is Ο(p) using p processors and one number in each processor. As with all sorting algorithms, normally we would partition the list into groups, one group of numbers for each processor. 264

Quicksort Sequential time complexity of Ο(nlogn). The question to answer is whether a parallel version can achieve the time complexity of Ο(logn) with n processors. Quicksort sorts a list of numbers by first dividing the list into two sublists, as in mergesort. All the numbers in one sublist are arranged to be smaller than all the numbers in the other sublist. Achieved by first selecting one number, called a pivot, against which every other number is compared. If the number is less than the pivot, it is placed in one sublist. Otherwise, it is placed in the other sublist. By repeating the procedure sufficiently, we are left with sublists of one number each. With proper ordering of the sublists, a sorted list is obtained. Sequential Code Suppose an array list[] holds the list of numbers and pivot is the index in the array of the final position of the pivot: quicksort(list, start, end) { if (start < end) { partition(list, start, end, pivot) quicksort(list, start, pivot-1); /* recursively call on sublists*/ quicksort(list, pivot+1, end); } } Partition() moves numbers in the list between start to end so that those less than the pivot are before the pivot and those equal or greater than the pivot are after the pivot. The pivot is in its final position of the sorted list. 265

Parallelizing Quicksort Pivot Unsorted list 4 2 7 8 5 1 3 6 P 0 3 2 1 4 5 7 8 6 P 0 P 4 2 1 3 4 5 7 8 6 P 0 P 2 P 4 P 6 1 2 3 6 7 8 P 0 P 1 P 6 P 7 Sorted list Figure 9.15 Quicksort using tree allocation of processes. Process allocation 266

With the pivot being withheld Pivot Unsorted list 4 2 7 8 5 1 3 6 4 3 2 1 5 7 8 6 3 5 1 2 7 8 6 1 7 2 6 8 2 6 8 Sorted list Figure 9.16 Pivots Quicksort showing pivot withheld in processes. 267

Analysis Fundamental problem with all of these tree constructions initial division is done by a single processor, which will seriously limit the speed. Suppose the pivot selection is ideal and each division creates two sublists of equal size. Computation First, one processor operates upon n numbers. Then two processors each operate upon n/2 numbers. Then four processors each operate upon n/4 numbers, and so on: t comp = n + n/2 + n/4 + n/8 + 2n Communication Communication also occurs in a similar fashion as for mergesort: t comm = (t startup + (n/2)t data ) + (t startup + (n/4)t data ) + (t startup + (n/8)t data ) + (log p)t startup + nt data The major difference between quicksort and mergesort is that the tree in quicksort will not, in general, be perfectly balanced The selection of the pivot is very important to make quicksort operate fast. 268

Work Pool Implementation First, the work pool holds the initial unsorted list, which is given to the first processor. This processor divides the list into two parts. One part is returned to the work pool to be given to another processor, while the other part is operated upon again. Work pool Sublists Request sublist Return sublist Slave processes Figure 9.17 quicksort. Work pool implementation of 269

Quicksort on a Hypercube Complete List Placed in One Processor The list can be divided into two parts by using a pivot determined by the processor, with one part sent to the adjacent node in the highest dimension. Then the two nodes can repeat the process, dividing their lists into two parts using locally selected pivots. One part is sent to a node in the next highest dimension. This process is continued for logd steps for a d-dimensional hypercube. Node Node 1st step: 000 001 (numbers greater than a pivot, say p 1 ) 2nd step: 000 010 (numbers greater than a pivot, say p 2 ) 001 011 (numbers greater than a pivot, say p 3 ) 3rd step: 000 100 (numbers greater than a pivot, say p 4 ) 001 101 (numbers greater than a pivot, say p 5 ) 010 110 (numbers greater than a pivot, say p 6 ) 011 111 (numbers greater than a pivot, say p 7 ) 270

(a) Phase 1 000 001 010 011 100 101 110 111 p 1 > p 1 (b) Phase 2 000 001 010 011 100 101 110 111 p 2 > p 2 p 3 > p 3 (c) Phase 3 000 001 010 011 100 101 110 111 p 4 > p 4 p 5 > p 5 p 6 > p 6 p 7 > p 7 Figure 9.18 Hypercube quicksort algorithm when the numbers are originally in node 000. 271

Numbers Initially Distributed across All Processors Suppose the unsorted numbers are initially distributed across the nodes in an equitable fashion but not in any special order. A 2 d -node hypercube (d-dimensional hypercube) is composed of two smaller 2 d 1 -node hypercubes, which are interconnected with links between pairs of nodes in each cube in the dth dimension. This feature can be used in a direct extension of the quicksort algorithm to a hypercube as follows. Steps 1. One processor (say P 0 ) selects (or computes) a suitable pivot and broadcasts this to all others in the cube. 2. The processors in the lower subcube send their numbers, which are greater than the pivot, to their partner processor in the upper subcube. The processors in the upper subcube send their numbers, which are equal to or less than the pivot, to their partner processor in the lower cube. 3. Each processor concatenates the list received with what remains of its own list. Given a d-dimensional hypercube, after these steps the numbers in the lower (d 1)- dimensional subcube will all be equal to or less than the pivot and all the numbers in the upper (d 1)-dimensional hypecube will be greater than the pivot. Steps 2 and 3 are now repeated recursively on the two (d 1)-dimensional subcubes. One process in each subcube computes a pivot for its subcube and broadcasts it throughout its subcube. These actions terminate after log d recursive phases. Suppose the hypercube has three dimensions. Now the numbers in the processor 000 will be smaller than the numbers in processor 001, which will be smaller than the numbers in processor 010, and so on. 272

Broadcast pivot, p 1 (a) Phase 1 000 001 010 011 100 101 110 111 p 1 > p 1 Broadcast pivot, p 2 Broadcast pivot, p 3 (b) Phase 2 000 001 010 011 100 101 110 111 p 2 > p 2 p 3 > p 3 Broadcast pivot, p 4 Broadcast pivot, p 5 Broadcast pivot, p 6 Broadcast pivot, p 7 (c) Phase 3 000 001 010 011 100 101 110 111 p 4 > p 4 p 5 > p 5 p 6 > p 6 p 7 > p 7 Figure 9.19 Hypercube quicksort algorithm when numbers are distributed among nodes. 273

Communication Patterns in Hypercube 110 111 (a) Phase 1 communication 010 011 100 101 000 001 110 111 (b) Phase 2 communication 010 011 100 101 000 001 110 111 (c) Phase 3 communication 010 011 100 000 001 101 Figure 9.20 Hypercube quicksort communication. 274

Pivot Selection A poor pivot selection could result in most of the numbers being allocated to a small part of the hypercube, leaving the rest idle. This is most deleterious in the first split. In the sequential quicksort algorithm, often the pivot is simply chosen to be the first number in the list, which could be obtained in a single step or with Ο(1) time complexity. One approach take a sample of a numbers from the list, compute the mean value, and select the median as the pivot. The numbers sampled would need to be sorted at least halfway through to find the median. We might choose a simple bubble sort, which can be terminated when the median is reached. 275

Hyperquicksort Sorts the numbers at each stage to maintain sorted numbers in each processor. Not only does this simplify selecting the pivots, it eliminates the final sorting operation. Steps 1. Each processor sorts its list sequentially. 2. One processor (say P 0 ) selects (or computes) a suitable pivot and broadcasts this pivot to all others in the cube. 3. The processors in the lower subcube send their numbers, which are greater than the pivot, to their partner processor in the upper subcube. The processors in the upper subcube send their numbers, which are equal to or less than the pivot, to their partner processor in the lower cube. 4. Each processor merges the list received with its own to obtain a sorted list. Steps 2, 3, and 4 are repeated (d phases in all for a d-dimensional hypercube). 276

Broadcast pivot, p1 (a) Phase 1 000 001 011 010 110 111 101 100 p1 Broadcast pivot, p2 Broadcast pivot, p3 > p1 (b) Phase 2 000 001 011 010 110 111 101 100 p2 > p2 p3 > p3 Broadcast pivot, p4 Broadcast pivot, p5 Broadcast pivot, p6 Broadcast pivot, p7 (c) Phase 3 000 001 011 010 110 111 101 100 p4 > p4 p5 > p5 p6 > p6 p7 > p7 Figure 9.21 Quicksort hypercube algorithm with Gray code ordering. 277

Analysis Suppose n numbers and p processors in a d-dimensional hypercube so that 2 d = p. Initially, the numbers are distributed so that each processor has n/p numbers. Afterward, the number of numbers at each processor will vary. Let the number be x. (Ideally of course, x = n/p throughout.) The algorithm calls for d phases. After the initial sorting step requiring Ο(n/plog n/p), each phase has pivot selection, pivot broadcast, a data split, data communication, and data merge. Computation Pivot Selection With a sorted list, pivot selection can be done in one step, O(1), if there always were n/p numbers. In the more general case, the time complexity will be higher. Communication Pivot Broadcast d( d 1) ------------------- ( t 2 startup + t data ) Computation Data Split If the numbers are sorted and there are x numbers, the split operation can be done in log x steps. Communication Data from Split t startup x + --t 2 data Computation Data Merge To merge two sorted lists into one sorted list requires x steps if the biggest list has x numbers. Total The total time is given by the sum of the individual communication times and computation times. Pivot broadcast is the most expensive part of the algorithm. 278

Based upon odd-even merge algorithm. Odd-Even Mergesort Odd-Even Merge Algorithm Will merge two sorted lists into one sorted list, and this is used recursively to build up larger and larger sorted lists. Given two sorted lists a 1, a 2, a 3,, a n and b 1, b 2, b 3,, b n (where n is a power of 2), the following actions are performed: 1. The elements with odd indices of each sequence that is, a 1, a 3, a 5,, a n 1, and b 1, b 3, b 5,, b n 1 are merged into one sorted list, c 1, c 2, c 3,, c n. 2. The elements with even indices of each sequence that is, a 2, a 4, a 6,, a n, and b 2, b 4, b 6,, b n are merged into one sorted list, d 1, d 2,, d n. 3. The final sorted list, e 1, e 2,, e 2n, is obtained by the following: e 2i = min{c i+1, d i } e 2i+1 = max{c i+1, d i } for 1 i n 1. Essentially the odd and even index lists are interleaved, and pairs of odd/even elements are interchanged to move the larger toward one end, if necessary. The first number is given by e 1 = c 1 (since this will be the smallest of first elements of each list, a 1 or b 1 ) and the last number by e 2n = d n (since this will be the largest of last elements of each list, a n or b n ). 279

Sorted lists Even indices Odd indices a[] b[] 2 4 5 8 1 3 6 7 Merge Merge c[] 1 2 5 6 d[] 3 4 7 8 Compare and exchange Final sorted list e[] 1 2 3 4 5 6 7 8 Figure 9.22 sorted lists. Odd-even merging of two 280

b n n 1 Even mergesort Compare and exchange c 2n 2n 1 c 2n 2 b 4 3 b 2 1 a n a n 1 a 4 3 a 2 1 Odd mergesort c 7 6 c 5 4 c 3 2 1 Figure 9.23 Odd-even mergesort. 281

Bitonic Mergesort The basis of bitonic mergesort is the bitonic sequence. Bitonic Sequence A monotonic increasing sequence is a sequence of increasing numbers. A bitonic sequence has two sequences, one increasing and one decreasing. Formally, a bitonic sequence is a sequence of numbers, a 0, a 1, a 2, a 3,, a n 2, a n 1, which monotonically increases in value, reaches a single maximum, and then monotonically decreases in value; e.g., a 0 < a 1 < a 2, a 3,, a i 1 < a i > a i+1,, a n 2 > a n 1 for some value of i (0 i < n). A sequence is also bitonic if the preceding can be achieved by shifting the numbers cyclically (left or right). Value a 0, a 1, a 2, a 3, a n 2, a n 1 (a) Single maximum a 0, a 1, a 2, a 3, a n 2, a n 1 (b) Single maximum and single minimum Figure 9.24 Bitonic sequences. 282

Special Characteristic of Bitonic Sequences If we perform a compare-and-exchange operation on a i with a i+n/2 for all i (0 i < n/2), where there are n numbers in the sequence, we get two bitonic sequences, where the numbers in one sequence are all less than the numbers in the other sequence. Example Starting with the bitonic sequence 3, 5, 8, 9, 7, 4, 2, 1 we get the sequences shown below Bitonic sequence Compare and exchange 3 5 8 9 7 4 2 1 3 4 2 1 7 5 8 9 Bitonic sequence Bitonic sequence Figure 9.25 Creating two bitonic sequences from one bitonic sequence. 283

The compare-and-exchange operation moves the smaller numbers of each pair to the left sequence and the larger numbers of the pair to the right sequence. Given a bitonic sequence, recursively performing compare-and-exchange operations to subsequences will sort the list. Eventually, we obtain bitonic sequences consisting of one number each and a fully sorted list. Unsorted numbers 3 5 8 9 7 4 2 1 Compare and exchange 3 4 2 1 7 5 8 9 2 1 3 4 7 5 8 9 1 2 3 4 5 7 8 9 Sorted list Figure 9.26 Sorting a bitonic sequence. 284

Sorting To sort an unordered sequence, sequences are merged into larger bitonic sequences, starting with pairs of adjacent numbers. By a compare-and-exchange operation, pairs of adjacent numbers are formed into increasing sequences and decreasing sequences, pairs of which form a bitonic sequence of twice the size of each of the original sequences. By repeating this process, bitonic sequences of larger and larger lengths are obtained. In the final step, a single bitonic sequence is sorted into a single increasing sequence. Unsorted numbers Bitonic sorting operation Direction of increasing numbers Figure 9.27 Sorted list Bitonic mergesort. 285

Bitonic Mergesort Example Sorting eight numbers. The basic compare-and-exchange operation is given by a box, with an arrow indicating which output is the larger number of the operation: Compare and exchange a i with a i+n/2 (n numbers) Step 8 3 4 7 9 2 1 5 = bitonic list [Fig. 9.24 (a) or (b)] 1 n = 2 a i with a i+1 Form bitonic lists of four numbers 2 3 8 7 4 2 9 5 1 n = 4 a i with a i+2 Form bitonic list of eight numbers 3 3 4 7 8 5 9 2 1 Split n = 2 a i with a i+1 3 4 7 8 9 5 2 1 Sort 4 n = 8 a i with a i+4 Sort bitonic list 3 4 2 1 9 5 7 8 Split 5 n = 4 a i with a i+2 Compare and exchange 2 1 3 4 7 5 9 8 Split 6 n = 2 a i with a i+1 Lower Higher 1 2 3 4 5 7 8 9 Sort Figure 9.28 Bitonic mergesort on eight numbers. 286

Phases The six steps (for eight numbers) are divided into three phases: Phase 1 (Step 1) Phase 2 (Steps 2/3) Convert pairs of numbers into increasing/decreasing sequences and hence into 4-bit bitonic sequences. Split each 4-bit bitonic sequence into two 2-bit bitonic sequences, higher sequences at center. Sort each 4-bit bitonic sequence increasing/decreasing sequences and merge into 8-bit bitonic sequence. Phase 3 (Steps 4/5/6) Sort 8-bit bitonic sequence (as in Figure 9.27). Number of Steps In general, with n = 2 k, there are k phases, each of 1, 2, 3,, k steps. Hence the total number of steps is given by k Steps i k( k + 1) logn( logn + 1) = = -------------------- = ------------------------------------ = Ο ( log 2 n ) 2 2 i = 1 287

P R O B L E M S Scientific/Numerical 9-1. Modify the rank sort code given in Section 9.1.3: for (i = 0; i < n; i++) { /* for each number */ x = 0; for (j = 0; j < n; j++) /* count number of nos less than it */ if (a[i] > a[j]) x++; b[x] = a[i]; /* copy number into correct place */ } to cope with duplicates in the sequence of numbers (i.e., for it to sort in nondecreasing order). It is not necessary to maintain the order of identical numbers in the sorted list. 9-2. Rewrite the compare-and-exchange code given in Section to eliminate the message passing when exchanges are not needed. 9-3. Implement both methods of compare-and-exchange operations described in Section (i.e., Version 1 and Version 2), operating upon blocks of four numbers. Measure the performance of the methods. Devise an experiment to show that it is possible to obtain erroneous results on a heterogeneous system with computers having different arithmetic precisions (if available). 9-4. Determine the processor efficiency of the Ο(log n) algorithm based upon rank sort described in Section 9.1.3 for sorting numbers. 288

9-5. Fox et al. (1988) present a method of compare and exchange in which individual numbers are exchanged between the processors, say P 0 and P 1. Suppose each processor has four numbers in its group. Processor P 0 sends the largest number in its group to processor P 1 while processor P 1 sends the smallest number of its group to the processor P 0. Each processor inserts the received number into its group, so that both still have n/p numbers. The actions are repeated with the new largest and smallest numbers. The algorithm is most conveniently implemented using a queue with a pointer to the top or bottom, as illustrated in Figure 9.29. The algorithm can terminate when the insertions would be at the top and bottom of the groups, as shown. The maximum number of communication steps would be when all the left list were initially larger than all of the numbers in the other group (a maximum of x steps with x numbers in each group). Write a parallel program to implement Fox s method and evaluate this method compared to the methods described in the text. Step 1 88 50 28 25 98 80 43 42 Step 2 50 42 28 25 98 88 80 43 Step 3 43 42 28 25 98 88 80 50 Terminates when insertions at top/bottom of lists Figure 9.29 Compare-and-exchange algorithm for Problem 9-5. 289

9-6. The following is an attempt to code the odd-even transposition sort of Section 9.2.2 as one SPMD program. Process P i evenprocess = (i % 2 == 0); evenphase = 1; for (step = 0; step < n; step++, evenphase =!evenphase){ if ((evenphase && evenprocess) (!evenphase) &&!evenprocess)) { send(&a, P i+1 ); recv(&x, P i+1 ); if (x < a) a = x; /* keep smaller number */ } else { send(&a, P i-1 ); recv(&x, P i-1 ); if (x > a) a = x; /* keep larger number */ } } Determine whether this code is correct and, if not, correct any mistakes. 9-7. The odd-even transposition sort of Section 9.2.2 has been described with the assumption that the number of numbers and processors is even. What changes to the code would be necessary to allow an odd number of numbers and processors? 9-8. Show that there are n( n 1) communications for a transpose operation on a n n array, as stated in Section. 9-9. Write a parallel program to implement shearsort. 9-10. Write a parallel program to find the kth smallest number of a set of numbers. Use a parallel version of quicksort but apply only to the set of numbers containing the kth smallest number. See Cormen, Leiserson, and Rivest (1990) for further discussion on the problem. 290

9-11. There have been many suggestions that are supposed to improve the performance of sequential quicksort (see, for example, Wainwright, 1985 and references contained therein). The following are two suggestions that have appeared in the literature: 1. Rather than selecting the first number as the pivot, use the median of a group of three numbers picked randomly ( median-of-three technique). 2. The initial set of numbers is divided into two parts using a first number as the pivot. While the numbers are being compared with the pivot, the sum of the numbers in each of two parts is computed. From these sums, the mean of each part is computed. The mean of each part is used as the pivot for this part in the next partitioning stage. The process of computing two means is done in subsequent stages for the next pivots. This algorithm is called the meansort algorithm. Determine empirically the prospects for parallelizing these methods. 9-12. Analyze the hypercube quicksort algorithm in Section 9.2.6, in which processors do not maintain sorted numbers at each stage. 9-13. Draw the exchanges of numbers for a four-dimensional hypercube using the parallel hypercube described in Section 9.2.6 that leaves the results in embedded ring order. Hence, determine a general algorithm to handle any sized hypercube. 9-14. Draw the compare-and-exchange circuit configuration for the odd-even mergesort algorithm described in Section 9.2.7 to sort 16 numbers. Sort the following sequence by hand using the odd-even mergesort algorithm: 12 2 11 4 9 1 10 15 5 7 14 3 8 13 6 16 9-15. Repeat Problem 9-14 for bitonic mergesort. 9-16. Compare Batcher s odd-even mergesort algorithm (Section ) and his bitonic mergesort algorithm (Section ) and assess their relative advantages for parallel implementation on a message-passing multicomputer. 9-17. Prove that the time complexity of odd-even mergesort is Ο(log 2 n) with n processors. 291

Real Life 9-18. Fred has a deck of 52 playing cards that have been thoroughly shuffled. He has asked you to determine several things related to reordering them: 1. What modification to bitonic mergesort will be needed to sort the cards, given that there are four suits (spades, hearts, clubs and hearts)? 2. How many friends will Fred have to invite over (and feed) to carry out a modified bitonic mergesort in parallel and how many steps will each friend have to carry out? 9-19. One way to find matching entries in two files is first to sort the contents of the files and then step through the files comparing entries. Analyze this method compared to simply comparing each entry of one file with each entry of the second file without sorting. Write a parallel program to find matching entries of two files by first sorting the files. 9-20. Sorting algorithms can create any permutation of an input sequence by simply numbering the input elements in the required order and sorting on these numbers. Write a parallel program that randomizes the contents of a document file for security purposes by first numbering the words in the file using a random number generator and then sorting on these numbers. (Refer to Chapter 3 for details of random numbers.) Is this a good method of encoding a file for security purposes? Write a parallel program that restores the document file to its original state. 292