Download - Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Transcript
Page 1: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Algorithms: Sorting

. Rand Sort

. Compare-and-exchange

. Merge Sort

. Quick Sort

. Odd-even merge sort

. Bitonic merge sort

Page 2: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Rank Sort

for(i=0 ; i<n ; i++){ x=0; for(j=0 ; j<n ; j++) if( a[i]>a[j]) x++; b[x] = a[i];}

O(n2)

Page 3: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Rank Sort

Using n processors

forall(i=0 ; i<n ; i++){ x=0; for(j=0 ; j<n ; j++) if(a[i] > a[j] ) x++: b[x] = a[i];}

O(n)

Page 4: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Rank Sort

Using n2 processors O(1)

Page 5: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Rank Sort

Page 6: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Compare-and-Exchange

if(A > B) { temp = A; A = B; B = temp;}

Page 7: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Compare-and-Exchange

Process P1send(&A, P2);recv(&A, P2);

Process P2recv(&A, P1);if(A > B){ send(&B, P1); B = A;}else send(&A, P1);

Page 8: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Compare-and-Exchange

Process P1send(&A, P2);recv(&B, P2);if(A > B) A = B;

Process P2recv(&A, P1);send(&B, P1);if(A > B) B = A;

Page 9: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Compare-and-Exchange

Data partitioning

Page 10: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Compare-and-Exchange

Data partitioning

Page 11: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Bubble Sort and Odd-even Transposition Sort

Page 12: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Bubble Sort and Odd-even Transposition Sort

for(i=n-1 ; i > 0 ; i++) for(j=0 ; j < i ; j++){ k = j+1; if( a[j] < a[k]) { temp = a[j]; a[i] = a[k]; a[k] = temp; } }

O(n2)

Page 13: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Bubble Sort and Odd-even Transposition Sort

Parallel Code

Page 14: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Bubble Sort and Odd-even Transposition Sort

Page 15: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Bubble Sort and Odd-even Transposition Sort

Pi,i=0,2,4,6,...,n-2(even)

recv(&A, Pi+1);

send(&B, Pi+1);

if(A > B) B = A;

Pi,i=1,3,5,..,n-1(odd)

send(&A,Pi-1);

recv(&B, Pi-1);

if(A > B) A = B;

Page 16: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Bubble Sort and Odd-even Transposition Sort

Pi,i=1,3,5,...,n-3(odd)

send(&A, Pi+1);

recv(&B, Pi+1);

if(A > B) A = B;

Pi,i=2,4,6,...,n-2(even)

recv(&A, Pi-1);

send(&B, Pi-1);

if(A > B) B = A;

Page 17: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Bubble Sort and Odd-even Transposition Sort

Pi,i=0,2,4,...,n-1(odd)send(&A, Pi-1);recv(&B, Pi-1);if(A > B) A=B;if(i<=n-3){ send(&A, Pi+1); recv(&B, Pi+1); if(A < B) A=B;}

Pi,i=0,2,4,...,n-2(even)recv(&A, Pi+1);send(&B, Pi+1);if(A > B) B = A;if(i >= 2){ recv(&A, Pi-1); send(&B, Pi-1); if(A > B) B=A;}

Page 18: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Bubble Sort and Odd-even Transposition Sort

Two-Dimension Sorting

Page 19: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Bubble Sort and Odd-even Transposition Sort

Odd Phase, the following actios are done:

Each row of numbers is sorted independently, in alternative directions:

Even rows: The smallest number of each column is placed at rightmost end

and largest number at the leftmost end

Odd rows: The smallest number of each column is placed at the leftmost end

and the largest number at the rightmost end.

In even phase, the following actions are done:

Each column of numbers is sorted independently, placing the smallest number of each column at the leftmost end and the largest number at the rightmost end.

)1(log nn

Page 20: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Bubble Sort and Odd-even Transposition Sort

Page 21: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Bubble Sort and Odd-even Transposition Sort

Page 22: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Merge Sort

Page 23: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Merge Sort

Communication (division phase)

datastartup tnt )2/(

datastartup tnt )4/(

datastartup tnt )8/(

Communication at each stepProcessor communication

P0->P4

P0->P2;P4->P6

P0->P1;P2->P3;P4->P5;P6->P7

Page 24: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Merge Sort

Communication (merge phase)

datastartup tnt )8/(

datastartup tnt )4/(

datastartup tnt )2/(

Communication at each stepProcessor communication

P0->P4

P0->P2;P4->P6

P0->P1;P2->P3;P4->P5;P6->P7

Page 25: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Merge Sort

Communication

datastartupcomm

datastartupdatastartupdatastartupcomm

nttpt

tnttnttntt

2)(log2

....))8/()4/()2/((2

Page 26: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Merge Sort

Computation

1compt

3compt

7compt P0

P0;P4

P0;P2;P4;P6

p

i

icompt

log

1

)12(

The parallel computational time complexity is O(p) using p processorsand one number in each processor

Page 27: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Quick Sort

quicksort(list, start, end)

{

if(start < end)

partition(list, start, end pivot);

quicksort(list, start, pivot-1);

quicksort(list, pivot-1, end);

}

Page 28: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Quick Sort

Page 29: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Quick Sort

Page 30: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Quick Sort

Computation

nnnnntcomp 2....8/4/2/

Communication

datastartup

datastartupdatastartupdatastartupcomm

nttp

tnttnttntt

)(log

...))8/(())4/(())2/((

Page 31: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Quick Sort

Implementation

Page 32: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Quick Sort on Hypercube

Complete list in one processor

1st step: 000 -> 100 (numbers greater than a pivot, say p1)

2nd step: 000 -> 010 (numbers greater than a pivot, say p2)

100 -> 110 (numbers greater than a pivot, say p3)

3rd step: 000 -> 001 (numbers greater than a pivot, say p4)

010 -> 011 (numbers greater than a pivot, say p5)

100 -> 101 (numbers greater than a pviot, say p6)

110 -> 111 (numbers greater than a pivot, say p7)

Page 33: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Quick Sort on Hypercube

Page 34: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Quick Sort on Hypercube

Number initially distributed across all processors

1. one processor(say P0) selects (or computers) a suitable pivot and broadcast this to all others in the cube

2. The processors in the lower subcube send their numbers, which are greater than the pivot, to their partner processor in the upper subcube. The processors in the upper subcube send their numbers, which are equal to or less than the pivot, to their partner processor in the lower cube.

3. Each processor concatenates the list received with what remains of its own list.

Page 35: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Quick Sort on Hypercube

Page 36: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Quick Sort on Hypercube

Page 37: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Quick Sort on Hypercube

1. Each processor sorts its list sequentially.

2. one processor(say P0) selects (or computers) a suitable pivot

and broadcast this to all others in the cube

3. The processors in the lower subcube send their numbers, which are

greater than the pivot, to their partner processor in the upper subcube.

The processors in the upper subcube sned their numbers, which are equal

to or less than the pivot, to their partner processor in the lower cube.

4. Each processor merger the list received with its own to obtain a sorted list.

Page 38: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Quick Sort on Hypercube

Page 39: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Quick Sort on Hypercube

Computation-Pivot Selection O(1) : the (n/2p)th number

Communication-Pivot broadcast

Computation-Data split if the numbers are sorted and there are x numbers, the split operation can be done in logx steps. (same as binary search)

Communication-Data from split

Computation-Data Merge to merge two sorted lists into one sorted list requires x steps if the biggest list has x numbers

)(2

)1(

2

)1()(

1

0

datastartup

d

i

ttdd

ddid

datastartup tx

t2

Page 40: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Odd-even Merge Sort

1. The elements with odd indices of each sequence-that is, A1, A3,A5,…,An-1, and B1, B3,B5,…,Bn-1---are merged into one sorted list, C1, C2, C3,…,Cn

2. The elements with even indices of each sequence---that is A2,A4,A6,…,An, and B2,B4,B6,…,Bn-2---are merged into one sorted list, D1, D2, D3,…,Dn.

3. The final sorted list, E1, E2,…,E2n, is obtained by the following: E2i=min{Ci+1, Di} E2i+1=max{Ci+1,Di} for 1<=i<=n-1

Page 41: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Odd-even Merge Sort

Page 42: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Odd-even Merge Sort

Page 43: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Bitonic Merge Sort

Bitonic sequence

A0<A1<A2<A3<…,Ai-1<Ai>Ai+1>Ai+2>…..>An

3,5,8,9,7,4,2,1

Page 44: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Bitonic Merge Sort

Page 45: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Bitonic Merge Sort

Page 46: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Bitonic Merge Sort

Page 47: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Bitonic Merge Sort

Phase 1(step1) Covert pairs of numbers into increasing/decreasing sequences

and hence into 4-bit bitonic sequences

Phase 2(step2/3) Split each 4-bit bitonic sequence into two 2-bit bitonic

sequences, higher sequence at center.

Sort each 4-bit bitonic sequence increasing/decreasing

sequences and merge into 8-bit bitonic sequence.

Phase 3(step4/5/6) Sort 8-bit bitonic sequence

Page 48: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Bitonic Merge Sort

Page 49: Algorithms: Sorting. Rand Sort. Compare-and-exchange. Merge Sort. Quick Sort. Odd-even merge sort. Bitonic merge sort.

Bitonic Merge Sort