CS 584. Sorting n One of the most common operations n Definition: –Arrange an unordered collection of elements into a monotonically increasing or decreasing.

Slides:



Advertisements
Similar presentations
Parallel Sorting Sathish Vadhiyar. Sorting  Sorting n keys over p processors  Sort and move the keys to the appropriate processor so that every key.
Advertisements

1 Parallel Parentheses Matching Plus Some Applications.
ALGORITMOS DE ORDENACIÓN EN PARALELO
Lecture 7-2 : Distributed Algorithms for Sorting Courtesy : Michael J. Quinn, Parallel Programming in C with MPI and OpenMP (chapter 14)
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Quick-Sort     29  9.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
High Performance Comparison-Based Sorting Algorithm on Many-Core GPUs Xiaochun Ye, Dongrui Fan, Wei Lin, Nan Yuan, and Paolo Ienne Key Laboratory of Computer.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M
1 Tuesday, November 14, 2006 “UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever.
Chapter 10 in textbook. Sorting Algorithms
Sorting Algorithms CS 524 – High-Performance Computing.
1 Friday, November 17, 2006 “In the confrontation between the stream and the rock, the stream always wins, not through strength but by perseverance.” -H.
1 Lecture 11 Sorting Parallel Computing Fall 2008.
CSCI 4440 / 8446 Parallel Computing Three Sorting Algorithms.
Design of parallel algorithms Sorting J. Porras. Problem Rearrange numbers (x 1,...,x n ) into ascending order ? What is your intuitive approach –Take.
Sorting Algorithms: Topic Overview
CS 584. Sorting n One of the most common operations n Definition: –Arrange an unordered collection of elements into a monotonically increasing or decreasing.
Sorting Algorithms Ananth Grama, Anshul Gupta, George Karypis, and Vipin Kumar To accompany the text ``Introduction to Parallel Computing'', Addison Wesley,
Sorting Chapter 10. Chapter 10: Sorting2 Chapter Objectives To learn how to use the standard sorting methods in the Java API To learn how to implement.
CSCI-455/552 Introduction to High Performance Computing Lecture 22.
1 Sorting Algorithms - Rearranging a list of numbers into increasing (strictly non-decreasing) order. ITCS4145/5145, Parallel Programming B. Wilkinson.
Parallel Programming in C with MPI and OpenMP
Lecture 12: Parallel Sorting Shantanu Dutt ECE Dept. UIC.
1 Parallel Sorting Algorithms. 2 Potential Speedup O(nlogn) optimal sequential sorting algorithm Best we can expect based upon a sequential sorting algorithm.
Adaptive Parallel Sorting Algorithms in STAPL Olga Tkachyshyn, Gabriel Tanase, Nancy M. Amato
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Outline  introduction  Sorting Networks  Bubble Sort and its Variants 2.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Sorting Chapter 10. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
Sorting Fun1 Chapter 4: Sorting     29  9.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Sorting CS 110: Data Structures and Algorithms First Semester,
1. 2 Sorting Algorithms - rearranging a list of numbers into increasing (strictly nondecreasing) order.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Lecture 4 Sorting Networks. Comparator comparator.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd ed., by B. Wilkinson & M
“Sorting networks and their applications”, AFIPS Proc. of 1968 Spring Joint Computer Conference, Vol. 32, pp
Ananth Grama, Anshul Gupta, George Karypis, and Vipin Kumar
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Data Structures and Algorithms in Parallel Computing Lecture 8.
Non-Linear Algebra Problems Sathish Vadhiyar SERC IISc.
Parallel Programming - Sorting David Monismith CS599 Notes are primarily based upon Introduction to Parallel Programming, Second Edition by Grama, Gupta,
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
Unit-8 Sorting Algorithms Prepared By:-H.M.PATEL.
Sorting: Parallel Compare Exchange Operation A parallel compare-exchange operation. Processes P i and P j send their elements to each other. Process P.
Divide and Conquer Algorithms Sathish Vadhiyar. Introduction  One of the important parallel algorithm models  The idea is to decompose the problem into.
Advanced Sorting 7 2  9 4   2   4   7
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Sorting.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
CMU : Internet Search Technologies
Quick-Sort 9/12/2018 3:26 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Radish-Sort 11/11/ :01 AM Quick-Sort     2 9  9
Advanced Sorting Methods: Shellsort
Bitonic Sorting and Its Circuit Design
Unit-2 Divide and Conquer
Bitonic and Merging sorting networks
Parallel Sorting Algorithms
Quick-Sort 2/25/2019 2:22 AM Quick-Sort     2
Quick-Sort 4/8/ :20 AM Quick-Sort     2 9  9
Sorting Algorithms - Rearranging a list of numbers into increasing (strictly non-decreasing) order. Sorting number is important in applications as it can.
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
CS203 Lecture 15.
Presentation transcript:

CS 584

Sorting n One of the most common operations n Definition: –Arrange an unordered collection of elements into a monotonically increasing or decreasing order. n Two categories of sorting –internal (fits in memory) –external (uses auxiliary storage)

Sorting Algorithms n Comparison based –compare-exchange –O(n log n) n Noncomparison based –Uses known properties of the elements –O(n) - bucket sort etc.

Parallel Sorting Issues n Input and Output sequence storage –Where? –Local to one processor or distributed n Comparisons –How compare elements on different nodes n # of elements per processor –One (compare-exchange --> comm.) –Multiple (compare-split --> comm.)

Compare-Exchange

Compare-Split

Sorting Networks n Specialized hardware for sorting –based on comparator xyxyxyxy max{x,y} min{x,y} max{x,y}

Sorting Network

Parallel Sorting Algorithms n Merge Sort n Quick Sort n Bitonic Sort n Others …

Merge Sort n Simplest parallel sorting algorithm? n Steps –Distribute the elements –Everybody sort their own sequence –Merge the lists n Problem –How to merge the lists

Quicksort n Simple, low overhead n O(n log n) n Divide and conquer n Divide recursively into smaller subsequences.

Quicksort n n elements stored in A[1…n] n Divide –Divide a sequence into two parts –A[q…r] becomes A[q…s] and A[s+1…r] –make all elements of A[q…s] smaller than or equal to all elements of A[s+1…r] n Conquer –Recursively apply Quicksort

Quicksort n Partition the sequence A[q…r] by picking a pivot. n Performance is greatly affected by the choice of the pivot. n If we pick a bad pivot, we end up with a O(n 2 ) algorithm.

Parallelizing Quicksort n Task parallelism –At each step of the algorithm 2 recursive calls are made. –Farm out one of the recursive calls to another processor. n Problems –The work of partitioning is done by one processor.

Parallelizing Quicksort n Consider domain decomposition. n Hypercube –a d dimensional hypercube can be split into two (d-1) dimensional hypercubes such that each processor in one cube is connected to one in the other cube. n If all processors know the pivot, neighbors split their respective lists and all elements larger than the pivot are distributed to one subcube and smaller elements are distributed to the other subcube

Parallelizing Quicksort n After we go through each dimension, if n>p the numbers are not totally sorted. –Why? n Each processor then sorts their own sublist using a sequential quicksort. n Pivot selection is particularly important –Bad pivots eliminate some processors

Pivot Selection n Random selection –During the i th split one of the processors in each subcube picks a random element from its list and broadcasts to others. n Problem –What if a bad pivot is selected at first?

Pivot Selection n Median selection –If the distribution is uniform then each processor's list is a representative sample thus the median is representative n Problem –Is the distribution really uniform? –Can we assume that a single processor's list has the same distribution as the full list?

Procedure HypercubeQuickSort(B) sort B using sequential quicksort for I = 1 to d Select pivot and broadcast or receive pivot partition B into B 1 and B 2 such that B 1 <= pivot < B 2 if i th bit of iproc is zero then send B 2 to neighbor along i th dimension C = subsequence received along i th dimension Merge B 1 and C into B else send B 2 to neighbor along C = subsequence received along i th dimension Merge B 2 and C into B endif endfor

Analysis n Iterations = log 2 p n Select a pivot = O(n) –keep sublist sorted n Broadcast pivot = O(log 2 p) n Split the sequence –split own sequence = O(log n/p) –exchange blocks with neighbor = O(n/p) –merge blocks = O(n/p)

Analysis n Quicksort appears very scalable n Depends heavily on the pivot n Easy to parallelize n Hypercube sorting algorithms depend on the ability to map a hypercube onto the node communication architecture.

Bitonic Sort n Key operation: –rearrange a bitonic sequence to ordered n Bitonic Sequence –sequence of elements –sequence of elements n There exists i such that is monotonically increasing and is monotonically decreasing or n There exists a cyclic shift of indicies such that the above is satisfied.

Bitonic Sequences n n –First it increases then decreases – i = 3 n n –Consider a cyclic shift –i will equal 2 or 3

Rearranging a Bitonic Sequence n Let s = n Let s = –a n/2 is the beginning of the decreasing seq. n Let s 1 = n Let s 1 = n Let s 2 = n Let s 2 = n In sequence s 1 there is an element b i = min{a i, a n/2+i } –all elements before b i are from increasing –all elements after b i are from decreasing n Sequence s 2 has a similar point n Sequences s 1 and s 2 are bitonic

Rearranging a Bitonic Sequence n Every element of s 1 is smaller than every element of s 2 n Thus, we have reduced the problem of rearranging a bitonic sequence of size n to rearranging two bitonic sequences of size n/2 then concatenating the sequences.

Rearranging a Bitonic Sequence

Bitonic Merging Network

What about unordered lists? n To use the bitonic merge for n items, we must first have a bitonic sequence of n items. n Two elements form a bitonic sequence n Any unsorted sequence is a concatenation of bitonic sequences of size 2 n Merge those into larger bitonic sequences until we end up with a bitonic sequence of size n

Creating a Bitonic Sequence

Mapping onto a hypercube n One element per processor n Start with the sorting network maps n Each wire represents a processor n Map processors to wires to minimize the distance traveled during exchange

Bitonic Merge on Hypercube

Bitonic Sort Procedure BitonicSort for i = 0 to d -1 for j = i downto 0 if (i + 1) st bit of iproc <> j th bit of iproc comp_exchange_max(j, item) else comp_exchange_min(j, item) endif endfor comp_exchange_max and comp_exchange_min compare and exchange the item with the neighbor on the j th dimension

Bitonic Sort Stages

Assignment n Pick 16 random integers n Draw the Bitonic Sort network n Step through the Bitonic sort network to produce a sorted list of integers. n Explain how the if statement in the Bitonic sort algorithm works.