Chapter9 SortingAlgorithms...• Bubble sort is difficult to parallelize since the algorithm has no...

45
Chapter 9 Sorting Algorithms A. Grama, A. Gupta, G. Karypis, and V. Kumar To accompany the text “Introduction to Parallel Computing”, Addison Wesley, 2003.

Transcript of Chapter9 SortingAlgorithms...• Bubble sort is difficult to parallelize since the algorithm has no...

  • Chapter 9Sorting Algorithms

    A. Grama, A. Gupta, G. Karypis, and V. Kumar

    To accompany the text “Introduction to Parallel Computing”,Addison Wesley, 2003.

  • Topic Overview

    • Issues in Sorting on Parallel Computers

    • Sorting Networks

    • Bubble Sort and its Variants

    • Quicksort

    • Bucket and Sample Sort

  • Sorting: Basics

    • One of the most commonly used and well-studied kernels.

    • The fundamental operation of comparison-based sorting iscompare-exchange.

    • The lower bound on any comparison-based sort of n numbersis Θ(n log n) on a serial computer.

    • In case of parallel sorting, the sorted list is partitioned among anumber of processors, such that (1) each sublist is sorted (2) fori < j, each element in processor Pi’s sublist is less than those inPj’s sublist.

  • Sorting: Parallel Compare Exchange Operation

    Step 1 Step 2 Step 3

    ai aj

    Pi PiPi PjPj Pj

    max{ai, aj}min{ai, aj}aj, aiai, aj

    A parallel compare-exchange operation (each process isresponsible for one element). Processes Pi and Pj send theirelements to each other. Process Pi keeps min{ai, aj}, and Pj

    keeps max{ai, aj}.

  • Sorting: Parallel Compare Split Operation

    861 11 13

    1 2 6 7 82 6 9 12 13

    1311

    1 6 9 12 137 8 1110 10

    11861 13 2 97 1210

    87

    1 86

    211 1

    2 97 12

    2 97 12

    10

    10

    Step 2

    9 12 131110

    Step 4Step 3

    Step 1

    Pi

    Pi Pi

    PiPj

    Pj Pj

    Pj

    A compare-split operation (each process is responsible for ablock of elements). Each process sends all its elements to

    another process. Each process merges the received block withits own block and retains only the appropriate half of the

    merged block. In this example, process Pi retains the smallerelements and process Pj retains the larger elements.

  • Comparators

    A comparator is a device with two inputs x and y and twooutputs x′ and y′.

    (a)

    (b)

    x

    x

    x

    x

    y

    y

    y

    y

    x′ = min{x, y}

    y′ = max{x, y}

    x′ = max{x, y}

    y′ = min{x, y}

    x′ = min{x, y}

    y′ = max{x, y}

    x′ = max{x, y}

    y′ = min{x, y}

    A schematic representation of comparators: (a) an increasingcomparator ⊕ , and (b) a decreasing comparator ⊖.

  • Sorting Networks

    • Networks of comparators designed specifically for sorting.

    • Every sorting network is made up of a series of columns, whereeach column contains a number of comparators connectedin parallel.

    • The speed of the network is proportional to the number ofcolumns.

  • Bitonic Sequence

    • A bitonic sequence has two tones – increasing anddecreasing, or vice versa. Any cyclic rotation of such networksis also considered bitonic.

    • 〈1, 2, 4, 7, 6, 0〉 is a bitonic sequence, because it first increasesand then decreases. 〈8, 9, 2, 1, 0, 4〉 is another bitonic sequence,because it is a cyclic shift of 〈0, 4, 8, 9, 2, 1〉.

  • Sorting Networks: Bitonic Sort

    • A bitonic sorting network rearranges a bitonic sequence into asorted sequence.

    • Let s = 〈a0, a1, . . . , an−1〉 be a bitonic sequence such that a0 ≤a1 ≤ . . . ≤ an/2−1 and an/2 ≥ an/2+1 ≥ . . . ≥ an−1.

    • Consider the following subsequences of s:

    s1 = 〈min{a0, an/2},min{a1, an/2+1}, . . . ,min{an/2−1, an−1}〉s2 = 〈max{a0, an/2},max{a1, an/2+1}, . . . ,max{an/2−1, an−1}〉

    (1)

    • Note that s1 and s2 are both bitonic and each element of s1 isless that every element in s2.

    • We can apply the procedure recursively on s1 and s2 to get thesorted sequence.

  • Sorting Networks: Bitonic SortOriginal

    sequence 3 5 8 9 10 12 14 20 95 90 60 40 35 23 18 0

    1st Split 3 5 8 9 10 12 14 0 95 90 60 40 35 23 18 20

    2nd Split 3 5 8 0 10 12 14 9 35 23 18 20 95 90 60 40

    3rd Split 3 0 8 5 10 9 14 12 18 20 35 23 60 40 95 90

    4th Split 0 3 5 8 9 10 12 14 18 20 23 35 40 60 90 95

    Merging a 16-element bitonic sequence through a series of log 16bitonic splits.

  • Sorting Networks: Bitonic Sort

    • We can build a sorting network to implement this bitonic mergealgorithm.

    • Such a network is called a bitonic merging network .

    • The network contains log n columns. Each column contains n/2comparators and performs one step of the bitonic merge.

    • We denote a bitonic merging network with n inputs by ⊕BM[n].

    • Replacing the ⊕ comparators by ⊖ comparators results in adecreasing output sequence; such a network is denoted by⊖BM[n].

  • Sorting Networks: Bitonic Sort

    18

    23

    35

    40

    60

    90

    95

    20

    14

    12

    10

    9

    8

    5

    3

    95

    90

    60

    40

    35

    23

    20

    18

    14

    12

    10

    9

    8

    5

    3

    0

    90

    95

    40

    60

    23

    35

    20

    18

    12

    14

    9

    10

    5

    8

    0

    3

    40

    90

    60

    95

    20

    18

    23

    35

    9

    0

    12

    10

    0

    5

    8

    33

    5

    10

    14

    8

    9

    12

    14

    0

    95

    90

    40

    35

    23

    20

    18

    60

    0011

    1100

    1110

    1101

    1011

    1010

    1001

    1000

    0111

    0110

    0101

    0100

    0010

    0001

    0000

    Wires

    1111

    A bitonic merging network for n = 16. The input wires arenumbered 0, 1 . . . , n− 1, and the binary representation of these

    numbers is shown. Each column of comparators is drawnseparately; the entire figure represents a ⊕BM[16] bitonic

    merging network. The network takes a bitonic sequence andoutputs it in sorted order.

  • Sorting Networks: Bitonic Sort

    How do we sort an unsorted sequence using a bitonic merge?

    • We must first build a bitonic sequence from the givensequence, by an iterative procedure:

    – Note that a sequence of length 2 is a bitonic sequence.– Note also that a bitonic sequence of length 4 can be builtby sorting the first two elements using ⊕BM[2] and next two,using ⊖BM[2].

    – And so on...

  • Sorting Networks: Bitonic Sort

    23

    90

    60

    40

    0

    3

    8

    12

    14

    20

    10

    9

    5

    0

    18

    23

    35

    40

    60

    90

    95

    20

    14

    12

    10

    9

    8

    18

    95

    5

    3

    18

    95

    35

    23

    40

    60

    90

    0

    12

    14

    8

    3

    5

    9

    20

    10

    18

    95

    35

    23

    40

    60

    0

    90

    14

    12

    8

    3

    9

    5

    20

    10

    35

    1000

    1111

    1110

    1101

    1100

    1011

    1010

    1001

    0111

    0110

    0101

    0100

    0011

    0010

    0001

    0000

    Wires

    The comparator network that transforms an input sequence of16 unordered numbers into a bitonic sequence.

  • Sorting Networks: Bitonic Sort

    • The depth of the network is Θ(log2 n).

    • Each stage of the network contains n/2 comparators. Aserial implementation of the network would have complexityΘ(n log2 n).

  • Mapping Bitonic Sort to Hypercubes (one item per

    processor)

    Step 2

    Step 4Step 3

    Step 1

    1101

    1100

    1010

    1110

    1111

    1011

    0000

    0001

    0101

    0100 0110

    0011

    0111

    1000

    1001

    0010

    0100 1100

    1101

    1110

    0000

    0001

    0101

    0010

    0110

    0111

    0011

    1001

    1000

    1010

    1011

    1111

    1100

    1101

    1010

    0100

    0000

    0001

    0101

    0010

    0110

    0111

    0011

    1000

    1001 1011

    1111

    1110

    1100

    1101

    0100

    0000

    0001

    0101

    0010

    0110

    0111

    1000

    1001 1011

    1111

    1110

    0011

    1010

    Communication during the last stage of bitonic sort. Each wire ismapped to a hypercube process; each connection represents a

    compare-exchange between processes.

  • Block of Elements Per Processor

    • Each process is assigned a block of n/p elements.

    • The first step is a local sort of the local block.

    • Each subsequent compare-exchange operation is replacedby a compare-split operation.

  • Block of Elements Per Processor: Hypercube

    • Initially the processes sort their n/p elements (using merge sort)in time Θ((n/p) log(n/p)) and then perform Θ(log2 p) compare-split steps.

    • The parallel run time of this formulation is

    TP =

    local sort︷ ︸︸ ︷

    Θ

    (n

    plog

    n

    p

    )

    +

    comparisons︷ ︸︸ ︷

    Θ

    (n

    plog2 p

    )

    +

    communication︷ ︸︸ ︷

    Θ

    (n

    plog2 p

    )

    .

  • Bubble Sort and its Variants

    The sequential bubble sort algorithm compares and exchangesadjacent elements in the sequence to be sorted:

    1. procedure BUBBLE SORT(n)

    2. begin

    3. for i := n − 1 downto 1 do

    4. for j := 1 to i do

    5. compare-exchange(aj, aj+1);

    6. end BUBBLE SORT

    Sequential bubble sort algorithm.

  • Bubble Sort and its Variants

    • The complexity of bubble sort is Θ(n2).

    • Bubble sort is difficult to parallelize since the algorithm has noconcurrency.

    • A simple variant, though, uncovers the concurrency.

  • Odd-Even Transposition

    1. procedure ODD-EVEN(n)

    2. begin

    3. for i := 1 to n do

    4. begin

    5. if i is odd then

    6. for j := 0 to n/2 − 1 do

    7. compare-exchange(a2j+1, a2j+2);

    8. if i is even then

    9. for j := 1 to n/2 − 1 do

    10. compare-exchange(a2j, a2j+1);

    11. end for

    12. end ODD-EVEN

    Sequential odd-even transposition sort algorithm.

  • Odd-Even Transposition

    3 3 4 5 6 8

    Phase 6 (even)

    2 1

    53 2 8 6 4

    2 3 8 5 6 4

    5 48 62 3

    3 5 8 4 6

    3 5 4 8 6

    3 4 5 6 8

    Phase 1 (odd)

    Unsorted

    Phase 2 (even)

    Phase 3 (odd)

    Phase 4 (even)

    Phase 5 (odd)

    3 1

    13

    3 1

    12

    2

    2

    3

    3

    3

    1

    1

    Sorted

    3 3 4 5 6 8

    3 3 4 5 6 8

    1 2

    1 2

    Phase 8 (even)

    Phase 7 (odd)

    Sorting n = 8 elements, using the odd-even transposition sortalgorithm. During each phase, n = 8 elements are compared.

  • Odd-Even Transposition

    • After n phases of odd-even exchanges, the sequence is sorted.

    • Each phase of the algorithm (either odd or even) requires Θ(n)comparisons.

    • Serial complexity is Θ(n2).

  • Parallel Odd-Even Transposition

    • Consider the one item per processor case.

    • There are n iterations, in each iteration, each processor doesone compare-exchange.

    • The parallel run time of this formulation is Θ(n).

    • This is cost optimal with respect to the base serial algorithm butnot the optimal one.

  • Parallel Odd-Even Transposition

    1. procedure ODD-EVEN PAR(n)

    2. begin

    3. id := process’s label

    4. for i := 1 to n do

    5. begin

    6. if i is odd then

    7. if id is odd then

    8. compare-exchange min(id + 1);

    9. else

    10. compare-exchange max(id − 1);

    11. if i is even then

    12. if id is even then

    13. compare-exchange min(id + 1);

    14. else

    15. compare-exchange max(id − 1);

    16. end for

    17. end ODD-EVEN PAR

    Parallel formulation of odd-even transposition.

  • Parallel Odd-Even Transposition

    • Consider a block of n/p elements per processor.

    • The first step is a local sort.

    • In each subsequent step, the compare exchange operation isreplaced by the compare split operation.

    • The parallel run time of the formulation is

    TP =

    local sort︷ ︸︸ ︷

    Θ

    (n

    plog

    n

    p

    )

    +

    comparisons︷ ︸︸ ︷

    Θ(n) +

    communication︷ ︸︸ ︷

    Θ(n).

  • Shellsort

    • Let n be the number of elements to be sorted and p be thenumber of processes.

    • During the first phase, processes that are far away from eachother in the array compare-split their elements.

    • During the second phase, the algorithm switches to an odd-even transposition sort.

  • Parallel Shellsort

    0 123 4 5 6 7

    0 123 4 5 6 7

    0 123 4 5 6 7

    An example of the first phase of parallel shellsort on aneight-process array.

  • Parallel Shellsort

    • Each process performs d = log p compare-split operations.

    • With O(p) bisection width, the each communication can beperformed in time Θ(n/p) for a total time of Θ((n log p)/p).

    • In the second phase, l odd and even phases are performed,each requiring time Θ(n/p).

    • The parallel run time of the algorithm is:

    TP =

    local sort︷ ︸︸ ︷

    Θ

    (n

    plog

    n

    p

    )

    +

    first phase︷ ︸︸ ︷

    Θ

    (n

    plog p

    )

    +

    second phase︷ ︸︸ ︷

    Θ

    (

    ln

    p

    )

    . (2)

  • Quicksort

    • Quicksort is one of the most common sorting algorithms forsequential computers because of its simplicity, low overhead,and optimal average complexity.

    • Quicksort selects one of the entries in the sequence to be thepivot and divides the sequence into two – one with all elementsless than the pivot and other greater.

    • The process is recursively applied to each of the sublists.

  • Quicksort

    1 2 3 3 4 5 8 7

    1 2 3 3 4 5 7 8

    3 2 1 5 8 4 3 7(a)

    (b)

    (c)

    (d)

    (e)

    1 2 3 5 8 4 3 7

    1 2 3 3 4 5 7 8

    Final position

    Pivot

    Example of the quicksort algorithm sorting a sequence of sizen = 8.

  • Quicksort

    • The performance of quicksort depends critically on the qualityof the pivot.

    • In the best case, the pivot divides the list in such a way that thelarger of the two lists does not have more than αn elements (forsome constant α).

    • In this case, the complexity of quicksort is O(n log n).

  • Parallelizing Quicksort: Shared Address Space

    Formulation

    • Consider a list of size n equally divided across p processors.

    • A pivot is selected by one of the processors and made knownto all processors.

    • Each processor partitions its list into two, say Si and Li, basedon the selected pivot.

    • All of the Si lists are merged and all of the Li lists are mergedseparately.

    • The set of processors is partitioned into two (in proportion of thesize of lists S and L). The process is recursively applied to eachof the lists.

  • Shared Address Space Formulation

    after globalrearrangement

    pivot=7

    pivot selection

    after localrearrangement

    Firs

    t Ste

    p

    after localrearrangement

    Four

    th S

    tep

    pivot=5 pivot=17

    after localrearrangement

    after globalrearrangement

    after localrearrangement

    after globalrearrangement

    Seco

    nd S

    tep

    pivot selection

    Thi

    rd S

    tep

    pivot selection

    pivot=11

    Solution

    7 2 1 6 3 4 5 18 13 17 14 20 10 15 9 19 16 12 11 8

    1 313 1014 920177 418 152 196 1116 12 5 8

    314 9207 15 1962 18 13 1 17 10 4 16 5 1112 8

    10 9 8 12 11 13 17 1514 16

    7 2 1 6 3 4 5 18 13 17 14 20 10 15 9 19 16 12 11 8

    2 6 3 4 571 13 20 10 15 9 19 16 12 11 81714 18

    21 13 17143 4 5 67 10 15 9 16 12 11 8 18 20 19

    10 9 8 12 11 13 17 1514 16

    21 13 17143 4 5 67 10 15 9 16 12 11 8 18 20 19

    21 3 4 5 6 7 18 19 2013 17 15 9 12 111410 8 16

    21 3 4 5 6 7 18 19 2013 14 15 16 179 108 11 12

    P0

    P0

    P0

    P0

    P0

    P0

    P0

    P1

    P1

    P1

    P1

    P1

    P1

    P1

    P2

    P2

    P2

    P2

    P2

    P2

    P2

    P2

    P3

    P3

    P3

    P3

    P3

    P3

    P3

    P3

    P4

    P4

    P4

    P4

    P4

    P4

    P4

  • Parallelizing Quicksort: Shared Address Space

    Formulation

    • How to globally merge the local lists (S0, L0, S1, L1, . . .) to formS and L?

    • Each processor needs to determine the right location for itselements in the merged list.

    • Each processor first counts the number of elements locally lessthan and greater than pivot.

    • It then computes two sum-scans to determine the startinglocation for its elements in the merged S and L lists.

    • Once it knows the starting locations, it can write its elementssafely.

  • Parallelizing Quicksort: Shared Address Space

    Formulation

    pivot=7

    pivot selection

    after localrearrangement

    after globalrearrangement

    1 313 1014 920177 418 152 196 1116 12 5 8

    314 9207 15 1962 18 13 1 17 10 4 16 5 1112 8

    2 1 6 3 4 5 18 13 17 14 20 10 15 9 19 16 12 11 870 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    2 1 1 2 1 2 3 3 2 3

    0 2 5 8 100 2 3 4 6 7 13

    P0

    P0

    P1

    P1

    P2

    P2

    P3

    P3

    P4

    P4

    |Si| |Li|

    Prefix SumPrefix Sum

    Efficient global rearrangement of the array.

  • Parallelizing Quicksort: Shared Address Space

    Formulation

    • The parallel time depends on the split and merge time, and thequality of the pivot.

    • The latter is an issue independent of parallelism, so we focus onthe first aspect, assuming ideal pivot selection.

    • The algorithm executes in four steps: (i) determine andbroadcast the pivot; (ii) locally rearrange the array assignedto each process; (iii) determine the locations in the globallyrearranged array that the local elements will go to; and (iv)perform the global rearrangement.

    • The first step takes time Θ(log p), the second, Θ(n/p), the third,Θ(log p), and the fourth, Θ(n/p).

    • The overall complexity of splitting an n-element array isΘ(n/p)+Θ(log p).

  • Parallelizing Quicksort: Shared Address Space

    Formulation

    • The process recurses until there are p lists, at which point, thelists are sorted locally.

    • Therefore, the total parallel time is:

    TP =

    local sort︷ ︸︸ ︷

    Θ

    (n

    plog

    n

    p

    )

    +

    array splits︷ ︸︸ ︷

    Θ

    (n

    plog p

    )

    +Θ(log2 p). (3)

    • The corresponding isoefficiency is Θ(p log2 p) due to broadcastand scan operations.

  • Parallelizing Quicksort: Message Passing Formulation

    • A simple message passing formulation is based on the recursivehalving of the machine.

    • Assume that each processor in the lower half of a p processorensemble is pairedwith a corresponding processor in the upperhalf.

    • A designated processor selects and broadcasts the pivot.

    • Each processor splits its local list into two lists, one less (Si), andother greater (Li) than the pivot.

    • A processor in the low half of the machine sends its list Li to thepaired processor in the other half. The paired processor sendsits list Si.

    • It is easy to see that after this step, all elements less than thepivot are in the low half of the machine and all elementsgreater than the pivot are in the high half.

  • Parallelizing Quicksort: Message Passing Formulation

    • The above process is recursed until each processor has its ownlocal list, which is sorted locally.

    • The time for a single reorganization is Θ(log p) for broadcastingthe pivot element, Θ(n/p) for splitting the locally assignedportion of the array, Θ(n/p) for exchange and localreorganization.

    • We note that this time is identical to that of the correspondingshared address space formulation.

    • It is important to remember that the reorganization of elementsis a bandwidth sensitive operation.

  • Bucket and Sample Sort

    • In Bucket sort, the range [a, b] of input numbers is divided into mequal sized intervals, called buckets.

    • Each element is placed in its appropriate bucket.

    • If the numbers are uniformly divided in the range, the bucketscan be expected to have roughly identical number ofelements.

    • Elements in the buckets are locally sorted.

    • The run time of this algorithm is Θ(n log(n/m)).

  • Parallel Bucket Sort

    • Parallelizing bucket sort is relatively simple. We can select m =p.

    • In this case, each processor has a range of values it isresponsible for.

    • Each processor runs through its local list and assigns each of itselements to the appropriate processor.

    • The elements are sent to the destination processors using asingle all-to-all personalized communication.

    • Each processor sorts all the elements it receives.

  • Parallel Bucket and Sample Sort

    • The critical aspect of the above algorithm is one of assigningranges to processors. This is done by suitable splitter selection.

    • The splitter selection method divides the n elements into mblocks of size n/m each, and sorts each block by usingquicksort.

    • From each sorted block it chooses m − 1 evenly spacedelements.

    • The m(m − 1) elements selected from all the blocks representthe sample used to determine the buckets.

    • This scheme guarantees that the number of elements endingup in each bucket is less than 2n/m.

  • Parallel Bucket and Sample Sort

    Initial element distribution

    Local sort &sample selection

    Global splitter selection

    Final elementassignment

    Sample combining

    113 1014 201718 2 6722 24 3 191615 23 4 11 12 5 8219

    1 2 13 181714 22 3 6 9 2410 15 20 21 4 5 8 11 12 16 23197

    7 17 9 20 8 16

    7 8 9 16 17 20

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 20 21 22 23 2419

    P0

    P0

    P0

    P1

    P1

    P1

    P2

    P2

    P2

    An example of the execution of sample sort on an array with 24elements on three processes.

  • Parallel Bucket and Sample Sort

    • The splitter selection scheme can itself be parallelized.

    • Each processor generates the p− 1 local splitters in parallel.

    • All processors share their splitters using a single all-to-allbroadcast operation.

    • Each processor sorts the p(p−1) elements it receives and selectsp− 1 uniformly spaces splitters from them.