25
Parallel Algorithms - sorting Fernando Silva DCC-FCUP (Some slides are based on those from the book “Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers, 2nd ed.” de B. Wilkinson) Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 1 / 25

Parallel Algorithms - sorting

  • Upload
    vothuan

  • View
    245

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Parallel Algorithms - sorting

Parallel Algorithms - sorting

Fernando Silva

DCC-FCUP

(Some slides are based on those from the book “Parallel Programming Techniques &Applications Using Networked Workstations & Parallel Computers, 2nd ed.” de B.Wilkinson)

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 1 / 25

Page 2: Parallel Algorithms - sorting

Sorting in Parallel

Why?it is a frequent operation in many applications

Goal?sorting a sequence of values in increasing order using n processors

Potential speedup?best sequential algorithm has complexity O(n log n)the best we can aim with a parallel algorithm, using n processors is:optimal complexity of a parallel sorting algorithm: O(n log n)/n =

O(log n)

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 2 / 25

Page 3: Parallel Algorithms - sorting

Compare-and-swap with message exchange (1/2)

Sequential sorting requires the comparison of values and swapping in thepositions they occupy in the sequence. And, if it is in parallel? And, if thememory is distributed?version 1:

P1 send A to P2, this compares B with A and sends to P1 themin(A, B).

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 3 / 25

Page 4: Parallel Algorithms - sorting

Compare-and-swap with message exchange (2/2)

version 2:P1 sends A to P2; P2 sends B to P1; P1 does A = min(A, B) and P2does B = max(A, B).

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 4 / 25

Page 5: Parallel Algorithms - sorting

Data partition

Version 1:n numbers and p processorsn/p numbers assigned to each processor.

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 5 / 25

Page 6: Parallel Algorithms - sorting

Merging two sub-lists

version 2:

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 6 / 25

Page 7: Parallel Algorithms - sorting

Bubble Sort

compares two consecutive values at a time and swaps them if they areout of order.greater values are being moved towards the end of the list.number of comparisons and swaps:

∑n−1i=1 i = n(n−1)

2

which corresponds to a time complexity O(n2).

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 7 / 25

Page 8: Parallel Algorithms - sorting

Bubble-sort example

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 8 / 25

Page 9: Parallel Algorithms - sorting

Parallel Bubble-sort

The idea is to run multiple iterations in parallel.

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 9 / 25

Page 10: Parallel Algorithms - sorting

Odd-Even with transposition (1/2)

it is a variant of the bubble-sortoperates in two alternate phases:

Phase-even:I even processes exchange values with right neighbors.

Phase-odd:I odd processes exchange values with right neighbors.

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 10 / 25

Page 11: Parallel Algorithms - sorting

Odd-Even with transposition (2/2)

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 11 / 25

Page 12: Parallel Algorithms - sorting

Parallel algorithm - Odd-Even with transposition

void ODD-EVEN-PAR(n){

id = process labelfor (i= 1; i<= n; i++) {

if (i is odd)compare-and-exchange-min(id+1);

elsecompare-and-exchange-max(id-1);

if (i is even)compare-and-exchange-min(id+1);

elsecompare-and-exchange-max(id-1);

}}

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 12 / 25

Page 13: Parallel Algorithms - sorting

Mergesort (1/2)

Example of a divide-and-conquer algorithmSorting method to sort a vector; first subdivides it in two parts,applies again the same method to each part and when they are bothsorted (2 sorted vectors/lists) with m and n elements, they aremerged to produce a sorted vector that contains m + n elements ofthe initial vector.The average complexidade is O(n log n).

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 13 / 25

Page 14: Parallel Algorithms - sorting

Parallel Mergesort (2/2)

Using a strategy to assign work to processors organized in a tree.

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 14 / 25

Page 15: Parallel Algorithms - sorting

Parallel Quicksort

Using a strategy for work-assignment in a tree-fashion.

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 15 / 25

Page 16: Parallel Algorithms - sorting

Difficulties with the allocation of processesorganized in a tree

the initial division starts with just one process, which is limitating.the search tree of quicksort is not, in general, balancedselecting the pivot is very important for efficiecy

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 16 / 25

Page 17: Parallel Algorithms - sorting

Odd-Even mergesort

complexity: O(log 2n)merging the two lists a1, a2, . . . , an and b1, b2, . . . , bn, where n is apower of 2.

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 17 / 25

Page 18: Parallel Algorithms - sorting

Odd-Even mergesort

Apply recursively odd-even merge:

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 18 / 25

Page 19: Parallel Algorithms - sorting

Bitonic Sort (1/7)

complexity: O(log 2n)a sequence is bitonic if it contains two sequences, one increasing andone decreasing, i.e.a1 < a2 < . . . < ai−1 < ai > ai+1 > ai+2 > . . . > anfor some i such that (0 ≤ i ≤ n)a sequence is bitonic if the property described is attained by a circularrotation to the right of its elements.Examples:

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 19 / 25

Page 20: Parallel Algorithms - sorting

Bitonic Sort (2/7)

Special characteristic of bitonic sequences:if we do a compare-and-exchange operation with elements ai andai+n/2, for all i , in a sequence of size n,we obtain two bitonic sequences in which all the values in onesequence are smaller then the values of the other.Example: start with sequence 3, 5, 8, 9, 7, 4, 2, 1 and we obtain:

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 20 / 25

Page 21: Parallel Algorithms - sorting

Bitonic Sort (3/7)

the compare-and-exchange operation moves smaller values to the leftand greater values to the right.given a bitonic sequence, if we apply recursively these operations weget a sorted sequence.

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 21 / 25

Page 22: Parallel Algorithms - sorting

Bitonic Sort example (4/7)

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 22 / 25

Page 23: Parallel Algorithms - sorting

Bitonic Sort (5/7)

To sort an unsorted sequencemerge sequences in larger bitonic sequences, starting with adjacentpairs, alternating monotonicity.in the end, the bitonic sequence becomes sorted in a uniqueincreasing sequence.

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 23 / 25

Page 24: Parallel Algorithms - sorting

Bitonic Sort (6/7)

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 24 / 25

Page 25: Parallel Algorithms - sorting

Bitonic Sort (7/7)

Unsorted sequence ⇒ bitonic sequence ⇒ sorted sequence.

Fernando Silva (DCC-FCUP) Parallel Algorithms - sorting 25 / 25