sorting algorithms lesson 4. · disim - universit`a dell’aquila sorting algorithms lesson 4....

68
DISIM - Universit ` a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica, L’Aquila DISIM, L’Aquila, 01.04.2019 [email protected] http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Upload: others

Post on 21-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Sorting AlgorithmsLesson 4.

Adriano FESTA

Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,L’Aquila

DISIM, L’Aquila, 01.04.2019

[email protected]

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 2: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Topic Overview

Issues in Sorting on Parallel Computers

Sorting Networks

Bubble Sort and its Variants

Quicksort

Bucket and Sample Sort

Other Sorting Algorithms

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 3: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Sorting: Overview

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

Sorting can be comparison-based or noncomparison-based.

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

The lower bound on any comparison-based sort of n numbers isΘ(n log n).

We focus here on comparison-based sorting algorithms.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 4: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Iterative Sorting

Bubble sort

Every element is compared to any other to get the bigger

Then we repeat the procedure with the remaining n− 1 elements.

Cost: O(n2) comparisons

Other similar approaches: insertion sort, shell sort

Quick sort

Dividet et impera algorithm

We choose a pivot, we partition the set in two dividing theelements in smaller and bigger than the pivot

The pivot is in the right place, we repeat the procedure with theremaining two groups of elements.

Cost: O(n2) in the worst case, O(n log2 n) in average

Other similar approaches: merge sort

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 5: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Sorting: Basics

What is a parallel sorted sequence? Where are the input and outputlists stored?

We assume that the input and output lists are distributed.

The (output) sorted list is partitioned with the property that eachpartitioned list is sorted and each element in processor Pi’s list isless than that in Pj’s list if i < j.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 6: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Sorting: Parallel Compare Exchange Operation

aj

pi

aj,ai

pj pi

ai

Step 1 Step 2 Step 3

pj pi pj

ai,aj ma minimummin

A parallel compare-exchange operation. Processes Pi and Pj sendtheir elements to each other. Process Pi keeps minai ,aj, and Pj keeps

maxai ,aj.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 7: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Sorting: Basics

What is the parallel counterpart to a sequential comparator?

If each processor has one element, the compare exchangeoperation stores the smaller element at the processor with smallerid. This can be done in ts + tw time.

If we have more than one element per processor, we call thisoperation a compare-split. Assume each of two processors haven/p elements.

After the compare-split operation, the smaller n/p elements are atprocessor Pi and the larger n/p elements at Pj , where i < j.

The communication cost for a compare-split operation is(ts + tw n/p).

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 8: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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 4

pi

pi

pj

pj

Step 3

pi pj

Step 1

pi pj

A compare-split operation. Each process sends its block of size n/p to the otherprocess. Each process merges the received block with its own block and retains

only the appropriate half of the merged block. In this example, process Pi

retains the smaller elements and process Pj retains the larger elements.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 9: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Sorting Networks

Networks of comparators designed specifically for sorting.

A comparator is a device with two inputs x and y and two outputsx ′ and y ′. For an increasing comparator , x ′ = minx , y andy ′ = maxx , y; and vice-versa.

We denote an increasing comparator by ⊕ and a decreasingcomparator by .

The speed of the network is proportional to its depth.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 10: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Sorting Networks: Comparators

(a)

(b)

x

y

x

y

y

x

x2

y2

y

x

x3

y3 y3output

x3output

y2output

x2output

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

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 11: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Sorting Networks

Columns of comparators

Input

wir

es

Outp

ut

wir

es

Inte

rconnec

tion n

etw

ork

A typical sorting network. Every sorting network is made up of a seriesof columns, and each column contains a number of comparators

connected in parallel.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 12: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Sorting Networks: Bitonic Sort

A bitonic sorting network sorts n elements in Θ(log2 n) time.

A bitonic sequence has two tones – increasing and decreasing, orvice versa. Any cyclic rotation of such networks is also consideredbitonic.

〈1, 2, 4, 7, 6, 0〉 is a bitonic sequence, because it first increases andthen decreases. 〈8, 9, 2, 1, 0, 4〉 is another bitonic sequence,because it is a cyclic shift of 〈0, 4, 8, 9, 2, 1〉.The kernel of the network is the rearrangement of a bitonicsequence into a sorted sequence.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 13: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Sorting Networks: Bitonic Sort

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

Consider the following subsequences of s:

s1 = 〈mina0,an/2,mina1,an/2+1, . . . ,minan/2−1,an−1〉s2 = 〈maxa0,an/2,maxa1,an/2+1, . . . ,maxan/2−1,an−1〉

(1)

Note that s1 and s2 are both bitonic and each element of s1 is lessthat every element in s2.

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

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 14: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Sorting Networks: Bitonic Sort

Originalsequence 3 5 8 9 10 12 14 20 95 90 60 40 35 23 18 01st Split 3 5 8 9 10 12 14 0 95 90 60 40 35 23 18 202nd Split 3 5 8 0 10 12 14 9 35 23 18 20 95 90 60 403rd Split 3 0 8 5 10 9 14 12 18 20 35 23 60 40 95 904th 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.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 15: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Sorting Networks: Bitonic Sort

We can easily build a sorting network to implement this bitonicmerge algorithm.

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 byBM[n].

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 16: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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 are numbered0, 1 . . . ,n− 1, and the binary representation of these numbers is shown. Each

column of comparators is drawn separately; the entire figure represents a⊕BM[16] bitonic merging network. The network takes a bitonic sequence and

outputs it in sorted order.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 17: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Sorting Networks: Bitonic Sort

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

We must first build a single bitonic sequence from the givensequence.

A sequence of length 2 is a bitonic sequence.

A bitonic sequence of length 4 can be built by sorting the first twoelements using ⊕BM[2] and next two, using BM[2].

This process can be repeated to generate larger bitonicsequences.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 18: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Sorting Networks: Bitonic Sort

BM[2]

BM[2]

BM[2]

BM[2]

BM[2]

BM[2]

BM[2]

BM[2]

BM[16]

BM[4]

BM[4]

BM[4]

BM[4]

BM[8]

BM[8]

0001

0100

0101

0000

0010

0011

0110

0111

1000

1001

1010

1011

1100

1101

1110

1111

Wires

A schematic representation of a network that converts an inputsequence into a bitonic sequence. In this example, ⊕BM[k] and

BM[k] denote bitonic merging networks of input size k that use ⊕ and comparators, respectively. The last merging network (⊕BM[16]) sorts

the input. In this example, n = 16.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 19: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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 of 16unordered numbers into a bitonic sequence.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 20: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Sorting Networks: Bitonic Sort

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

Each stage of the network contains n/2 comparators. A serialimplementation of the network would have complexity Θ(n log2 n).

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 21: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Mapping Bitonic Sort to Hypercubes

Consider the case of one item per processor. The questionbecomes one of how the wires in the bitonic network should bemapped to the hypercube interconnect.

Note from our earlier examples that the compare-exchangeoperation is performed between two wires only if their labels differin exactly one bit!

This implies a direct mapping of wires to processors. Allcommunication is nearest neighbor!

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 22: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Mapping Bitonic Sort to Hypercubes

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.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 23: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Mapping Bitonic Sort to Hypercubes

1110

11001101

1011

10011000

1010

01100101

0111

0100001100100001

1111

0000

Stage 3

1

1

1

1

1

1

1

1

2,1

2,1

2,1

2,1

3,2,1

3,2,1

4,3,2,1

Processors

Stage 4Stage 2Stage 1

Communication characteristics of bitonic sort on a hypercube. Duringeach stage of the algorithm, processes communicate along the

dimensions shown.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 24: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Mapping Bitonic Sort to Hypercubes

1. procedure BITONIC SORT(label,d)2. begin3. for i := 0 to d − 1 do4. for j := i downto 0 do5. if (i + 1)st bit of label 6= jth bit of label then6. comp exchange max(j);7. else8. comp exchange min(j);9. end BITONIC SORT

Parallel formulation of bitonic sort on a hypercube with n = 2d processes.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 25: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Mapping Bitonic Sort to Hypercubes

During each step of the algorithm, every process performs acompare-exchange operation (single nearest neighborcommunication of one word).

Since each step takes Theta(1) time, the parallel time is

TP = Θ(log2 n) (2)

This algorithm is cost optimal w.r.t. its serial counterpart, but notw.r.t. the best sorting algorithm.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 26: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Mapping Bitonic Sort to Meshes

The connectivity of a mesh is lower than that of a hypercube, sowe must expect some overhead in this mapping.

Consider the row-major shuffled mapping of wires to processors.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 27: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Mapping Bitonic Sort to Meshes

1101

1001 1010

0100

1000 1011

1100 1110 1111

0101 0110 0111

0000 0001 0010 0011

1110

1001

0010

1010

1101 1100

0000 0001 0011

0111 0110 0101 0100

1000 1011

1111

1000

1110

1100

1111

0000 0001 0100 0101

0010 0011 0110 0111

1001 1101

1010 1011

(a) (b) (c)

Different ways of mapping the input wires of the bitonic sortingnetwork to a mesh of processes: (a) row-major mapping, (b) row-major

snakelike mapping, and (c) row-major shuffled mapping.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 28: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Mapping Bitonic Sort to Meshes

Step 1 Step 2 Step 3 Step 4

Stage 4

The last stage of the bitonic sort algorithm for n = 16 on a mesh, usingthe row-major shuffled mapping. During each step, process pairscompare-exchange their elements. Arrows indicate the pairs of

processes that perform compare-exchange operations.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 29: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Mapping Bitonic Sort to Meshes

In the row-major shuffled mapping, wires that differ at the i th

least-significant bit are mapped onto mesh processes that are2b(i−1)/2c communication links away.

The total amount of communication performed by each process is∑log ni=1

∑ij=1 2b(j−1)/2c ≈ 7

√n, or Θ(

√n). The total computation

performed by each process is Θ(log2 n).

The parallel runtime is:

TP =

comparisons︷ ︸︸ ︷Θ(log2 n) +

communication︷ ︸︸ ︷Θ(√

n).

This is not cost optimal.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 30: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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 replaced by acompare-split operation.

We can effectively view the bitonic network as having(1 + log p)(log p)/2 steps.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 31: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Block of Elements Per Processor: Hypercube

Initially the processes sort their n/p elements (using merge sort) intime Θ((n/p) log(n/p)) and then perform Θ(log2 p) compare-splitsteps.

The parallel run time of this formulation is

TP =

local sort︷ ︸︸ ︷Θ

(np

lognp

)+

comparisons︷ ︸︸ ︷Θ

(np

log2 p)

+

communication︷ ︸︸ ︷Θ

(np

log2 p).

Comparing to an optimal sort, the algorithm can efficiently use upto p = Θ(2

√log n) processes.

The isoefficiency function due to both communication and extrawork is Θ(plog p log2 p).

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 32: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Block of Elements Per Processor: Mesh

The parallel runtime in this case is given by:

TP =

local sort︷ ︸︸ ︷Θ

(np

lognp

)+

comparisons︷ ︸︸ ︷Θ

(np

log2 p)

+

communication︷ ︸︸ ︷Θ

(n√p

)

This formulation can efficiently use up to p = Θ(log2 n) processes.

The isoefficiency function is Θ(2√

p√p).

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 33: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Performance of Parallel Bitonic Sort

The performance of parallel formulations of bitonic sort for n elementson p processes. (Let’s keep in mind that E = S/p = TS/pTp and

isoefficiency function W = f (p))

Maximum Number of Corresponding IsoefficiencyArchitecture Processes for E = Θ(1) Parallel Run Time Function

Hypercube Θ(2√

log n) Θ(n/(2√

log n) log n) Θ(plog p log2 p)

Mesh Θ(log2 n) Θ(n/ log n) Θ(2√

p√p)Ring Θ(log n) Θ(n) Θ(2pp)

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 34: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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. begin3. for i := n− 1 downto 1 do4. for j := 1 to i do5. compare-exchange(aj ,aj+1);6. end BUBBLE SORT

Sequential bubble sort algorithm.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 35: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 36: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Odd-Even Transposition

1. procedure ODD-EVEN(n)2. begin3. for i := 1 to n do4. begin5. if i is odd then6. for j := 0 to n/2− 1 do7. compare-exchange(a2j+1,a2j+2);8. if i is even then9. for j := 1 to n/2− 1 do10. compare-exchange(a2j ,a2j+1);11. end for12. end ODD-EVEN

Sequential odd-even transposition sort algorithm.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 37: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 38: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 39: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Parallel Odd-Even Transposition

Consider the one item per processor case.

There are n iterations, in each iteration, each processor does onecompare-exchange.

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

This is cost optimal with respect to the base serial algorithm but notthe optimal one.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 40: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Parallel Odd-Even Transposition

1. procedure ODD-EVEN PAR(n)2. begin3. id := process’s label4. for i := 1 to n do5. begin6. if i is odd then7. if id is odd then8. compare-exchange min(id + 1);9. else10. compare-exchange max(id − 1);11. if i is even then12. if id is even then13. compare-exchange min(id + 1);14. else15. compare-exchange max(id − 1);16. end for17. end ODD-EVEN PAR

Parallel formulation of odd-even transposition.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 41: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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︷ ︸︸ ︷Θ

(np

lognp

)+

comparisons︷ ︸︸ ︷Θ(n) +

communication︷ ︸︸ ︷Θ(n).

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 42: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Parallel Odd-Even Transposition

The parallel formulation is cost-optimal for p = O(log n).

The isoefficiency function of this parallel formulation is Θ(p 2p).

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 43: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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-eventransposition sort.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 44: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Parallel Shellsort

Initially, each process sorts its block of n/p elements internally.

Each process is now paired with its corresponding process in thereverse order of the array. That is, process Pi , where i < p/2, ispaired with process Pp−i−1.

A compare-split operation is performed.

The processes are split into two groups of size p/2 each and theprocess repeated in each group.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 45: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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 an eight-processarray.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 46: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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, eachrequiring time Θ(n/p).

The parallel run time of the algorithm is:

TP =

local sort︷ ︸︸ ︷Θ

(np

lognp

)+

first phase︷ ︸︸ ︷Θ

(np

log p)

+

second phase︷ ︸︸ ︷Θ

(lnp

). (3)

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 47: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Quicksort

Quicksort is one of the most common sorting algorithms forsequential computers because of its simplicity, low overhead, andoptimal 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.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 48: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Quicksort

1. procedure QUICKSORT (A,q, r)2. begin3. if q < r then4. begin5. x := A[q];6. s := q;7. for i := q + 1 to r do8. if A[i] ≤ x then9. begin10. s := s + 1;11. swap(A[s],A[i]);12. end if13. swap(A[q],A[s]);14. QUICKSORT (A,q, s);15. QUICKSORT (A, s + 1, r);16. end if17. end QUICKSORT

The sequential quicksort algorithm.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 49: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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 size n = 8.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 50: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Quicksort

The performance of quicksort depends critically on the quality ofthe 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).

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 51: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Parallelizing Quicksort

Lets start with recursive desomposition – the list is partitionedserially and each of the subproblems is handled by a differentprocessor.

The time for this algorithm is lower-bounded by Ω(n)!

Can we parallelize the partitioning step – in particular, if we canuse n processors to partition a list of length n around a pivot inO(1) time, we have a winner.

This is difficult to do on real machines, though.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 52: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Parallelizing Quicksort: PRAM Formulation

We assume a CRCW (concurrent read, concurrent write) PRAMwith concurrent writes resulting in an arbitrary write succeeding.

The formulation works by creating pools of processors. Everyprocessor is assigned to the same pool initially and has oneelement.

Each processor attempts to write its element to a commonlocation (for the pool).

Each processor tries to read back the location. If the value readback is greater than the processor’s value, it assigns itself to the‘left’ pool, else, it assigns itself to the ‘right’ pool.

Each pool performs this operation recursively.

Note that the algorithm generates a tree of pivots. The depth ofthe tree is the expected parallel runtime. The average value isO(log n).

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 53: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Parallelizing Quicksort: PRAM Formulation

3

5

3

7

1

2

4

8

A binary tree generated by the execution of the quicksort algorithm.Each level of the tree represents a different array-partitioning iteration.

If pivot selection is optimal, then the height of the tree is Θ(log n),which is also the number of iterations.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 54: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Parallelizing Quicksort: PRAM Formulation

1 2 3 4 5 6 7 8

1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8

2

67

35 8

2 3 6 7

3 7

8

1 2 3 4 5 6 7 8

[4] 54

[1] 33

[6] 33

[5] 82

[2] 21

[3] 13 [7] 40

[8] 72

54 82 40 72132133 33(a)

(b)

(c)

1

5

(f)

(d) (e)

2

6

1

5

8 2

6

3 1

5

8

7

leftchild

rightchild

leftchild

rightchild

leftchild

rightchild

1

root = 4

The execution of the PRAM algorithm on the array shown in (a).

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 55: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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 known toall processors.

Each processor partitions its list into two, say Li and Ui , based onthe selected pivot.

All of the Li lists are merged and all of the Ui lists are mergedseparately.

The set of processors is partitioned into two (in proportion of thesize of lists L and U). The process is recursively applied to each ofthe lists.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 56: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Shared Address Space Formulation

after globalrearrangement

pivot=7

pivot selection

after localrearrangement

Fir

st S

tep

after localrearrangement

Fo

urt

h S

tep

pivot=5 pivot=17

after localrearrangement

after globalrearrangement

after localrearrangement

after globalrearrangement

Sec

on

d S

tep

pivot selection

Th

ird

Ste

p

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

$P_0$ $P_1$ $P_2$ $P_3$ $P_4$

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

$P_0$ $P_1$ $P_2$ $P_3$ $P_4$

10 9 8 12 11 13 17 1514 16

$P_2$ $P_3$

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

$P_0$ $P_1$ $P_2$ $P_3$ $P_4$

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

$P_0$ $P_1$ $P_2$ $P_3$ $P_4$

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

$P_0$ $P_2$ $P_3$ $P_4$

$P_0$ $P_2$ $P_3$ $P_4$

$P_1$

$P_1$

$P_0$ $P_2$ $P_3$ $P_4$$P_1$

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 57: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Parallelizing Quicksort: Shared Address Space Formulation

The only thing we have not described is the global reorganization(merging) of local lists to form L and U.

The problem is one of determining the right location for eachelement in the merged list.

Each processor computes the number of elements locally lessthan and greater than pivot.

It computes two sum-scans to determine the starting location forits elements in the merged L and U lists.

Once it knows the starting locations, it can write its elements safely.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 58: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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 and broadcastthe pivot; (ii) locally rearrange the array assigned to each process;(iii) determine the locations in the globally rearranged array thatthe local elements will go to; and (iv) perform the globalrearrangement.

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

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 59: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Parallelizing Quicksort: Shared Address Space Formulation

The process recurses until there are p lists, at which point, the listsare sorted locally.

Therefore, the total parallel time is:

TP =

local sort︷ ︸︸ ︷Θ

(np

lognp

)+

array splits︷ ︸︸ ︷Θ

(np

log p)

+ Θ(log2 p). (4)

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

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 60: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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 paired with 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 (Li), andother greater (Ui) than the pivot.

A processor in the low half of the machine sends its list Ui to thepaired processor in the other half. The paired processor sends its listLi .

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

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 61: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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 broadcasting thepivot element, Θ(n/p) for splitting the locally assigned portion ofthe array, Θ(n/p) for exchange and local reorganization.

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

It is important to remember that the reorganization of elements isa bandwidth sensitive operation.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 62: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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 buckets canbe expected to have roughly identical number of elements.

Elements in the buckets are locally sorted.

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

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 63: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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 is responsiblefor.

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 a singleall-to-all personalized communication.

Each processor sorts all the elements it receives.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 64: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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 m blocksof size n/m each, and sorts each block by using quicksort.

From each sorted block it chooses m− 1 evenly spaced elements.

The m(m− 1) elements selected from all the blocks represent thesample used to determine the buckets.

This scheme guarantees that the number of elements ending up ineach bucket is less than 2n/m.

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 65: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Parallel Bucket and Sample Sort

Initial element

distribution

Local sort &

sample selection

Global splitter

selection

Final element

assignment

Sample combining

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

$P_0$ $P_1$ $P_2$

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

$P_0$ $P_1$ $P_2$

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

$P_0$ $P_1$ $P_2$

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

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 66: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

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-all broadcastoperation.

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

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 67: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Parallel Bucket and Sample Sort: Analysis

The internal sort of n/p elements requires time Θ((n/p) log(n/p)),and the selection of p − 1 sample elements requires time Θ(p).

The time for an all-to-all broadcast is Θ(p2), the time to internallysort the p(p − 1) sample elements is Θ(p2 log p), and selectingp − 1 evenly spaced splitters takes time Θ(p).

Each process can insert these p− 1 splitters in its local sorted blockof size n/p by performing p − 1 binary searches in timeΘ(p log(n/p)).

The time for reorganization of the elements is O(n/p).

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms

Page 68: Sorting Algorithms Lesson 4. · DISIM - Universit`a dell’Aquila Sorting Algorithms Lesson 4. Adriano FESTA Dipartimento di Ingegneria e Scienze dell’Informazione e Matematica,

DISIM - Universita dell’Aquila

Parallel Bucket and Sample Sort: Analysis

The total time is given by:

TP =

local sort︷ ︸︸ ︷Θ

(np

lognp

)+

sort sample︷ ︸︸ ︷Θ(

p2 log p)

+

block partition︷ ︸︸ ︷Θ

(p log

np

)+

communication︷ ︸︸ ︷Θ(n/p).

(5)

The isoefficiency of the formulation is Θ(p3 log p).

http://adrianofesta.altervista.org/ A. FESTA, Parallel Programming Platforms