Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 CS 584

2 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)

3 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.

4 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.)

5 Compare-Exchange

6 Compare-Split

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

8 Sorting Network

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

10 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

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

12 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

13 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.

14 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.

15 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

16

17 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

18 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?

19 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?

20 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

21 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)

22 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.

23 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.

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

25 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

26 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.

27 Rearranging a Bitonic Sequence

28 Bitonic Merging Network

29 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

30 Creating a Bitonic Sequence

31 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

32 Bitonic Merge on Hypercube

33 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

34 Bitonic Sort Stages

35 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.


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

Similar presentations


Ads by Google