93
Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto Superior T´ ecnico November 29, 2012 CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 1 / 27

Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

  • Upload
    others

  • View
    16

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Sort

Parallel and Distributed Computing

Department of Computer Science and Engineering (DEI)Instituto Superior Tecnico

November 29, 2012

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 1 / 27

Page 2: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Outline

Parallel Sort

Hyperquicksort

PSRS, Parallel Sorting by Regular Sampling

Odd-Even Transposition Sort

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 2 / 27

Page 3: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Sorting Problem

Sorting Problem

Given an unordered sequence, obtain an ordered one through permutationsof the elements in the sequence.

Typically the value being sorted (key) is part of record with additionalvalues (satellite data).

Efficiency of sorting is particularly important as it is used as part of manyalgorithms.

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 3 / 27

Page 4: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Sorting Algorithms

Name Average Worst Memory Stable

Bubble sort O(n2) O(n2) O(1) Yes

Selection sort O(n2) O(n2) O(1) No

Insertion sort O(n2) O(n2) O(1) Yes

Merge sort O(n log n) O(n log n) O(n) Yes

Heapsort O(n log n) O(n log n) O(1) No

Quicksort O(n log n) O(n2) O(n log n) No

Smaller hidden constants in Quicksort make it popular.

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 4 / 27

Page 5: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Sorting Algorithms

Name Average Worst Memory Stable

Bubble sort O(n2) O(n2) O(1) Yes

Selection sort O(n2) O(n2) O(1) No

Insertion sort O(n2) O(n2) O(1) Yes

Merge sort O(n log n) O(n log n) O(n) Yes

Heapsort O(n log n) O(n log n) O(1) No

Quicksort O(n log n) O(n2) O(n log n) No

Smaller hidden constants in Quicksort make it popular.

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 4 / 27

Page 6: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Quicksort

procedure quicksort(array, left, right)

if right > left

select a pivot index (e.g. pivotIndex := left)

pivotNewIndex := partition(array, left, right, pivotIndex)

quicksort(array, left, pivotNewIndex - 1)

quicksort(array, pivotNewIndex + 1, right)

function partition(array, left, right, pivotIndex)

pivotValue := array[pivotIndex]

swap array[pivotIndex] and array[right] // Move pivot to end

storeIndex := left

for i from left to right - 1

if array[i] <= pivotValue

swap array[i] and array[storeIndex]

storeIndex := storeIndex + 1

// Move pivot to its final place

swap array[storeIndex] and array[right]

return storeIndex

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 5 / 27

Page 7: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Quicksort

procedure quicksort(array, left, right)

if right > left

select a pivot index (e.g. pivotIndex := left)

pivotNewIndex := partition(array, left, right, pivotIndex)

quicksort(array, left, pivotNewIndex - 1)

quicksort(array, pivotNewIndex + 1, right)

function partition(array, left, right, pivotIndex)

pivotValue := array[pivotIndex]

swap array[pivotIndex] and array[right] // Move pivot to end

storeIndex := left

for i from left to right - 1

if array[i] <= pivotValue

swap array[i] and array[storeIndex]

storeIndex := storeIndex + 1

// Move pivot to its final place

swap array[storeIndex] and array[right]

return storeIndex

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 5 / 27

Page 8: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Quicksort

18 15 66 10 23 64 11

23 64 11 18 15 66

35

15

23 23 18 66 15

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 6 / 27

Page 9: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Quicksort

1815 66 10 23 64 11

23 64 11 18 15 66

35

15

23 23 18 66 15

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 6 / 27

Page 10: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Quicksort

1815 66 10 23 64 11

23 64 11 18 15 66

35

15

23 23 18 66 15

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 6 / 27

Page 11: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Quicksort

1815 66 10 23 64 11

23 64 11 18 15 66

35

15

23 23 18 66 15

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 6 / 27

Page 12: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Quicksort

1815 66 10 23 64 11

23 64 11 18 15 66

35

15

23 23 18 66 15

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 6 / 27

Page 13: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Quicksort

1815 6610 23 64 11

23 64 11 18 15 66

35

15

23 23 18 66 15

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 6 / 27

Page 14: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Quicksort

1815 6610 23 64 11

23 64 11 18 15 66

35

15

23 23 18 66 15

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 6 / 27

Page 15: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Quicksort

1815 6610 23 64 11

23 64 11 18 15 66

35

15

23 23 18 66 15

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 6 / 27

Page 16: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Quicksort

1815 6610 23 6411

23 64 11 18 15 66

35

15

23 23 18 66 15

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 6 / 27

Page 17: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Quicksort

1815 6610 23 6411

23 64 11 18 15 66

35

15

23 23 18 66 15

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 6 / 27

Page 18: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Quicksort

1815 6610 23 6411

15 10 11 23 64 66

35

35

23 23 18 66 15

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 6 / 27

Page 19: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Quicksort

1815 6610 23 6411

1510 11 2364 66

35

35

23 23 18 66 15

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 6 / 27

Page 20: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Quicksort

1815 6610 23 6411

1510 11 23 64 66

35

35

10 11 64 66 35

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 6 / 27

Page 21: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Quicksort

1815 6610 23 6411

1510 11 23 64 66

35

35

10 11 64 66 35

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 6 / 27

Page 22: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Quicksort

1815 6610 23 6411

1510 11 23 64 66

35

35

10 11 64 66 35

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 6 / 27

Page 23: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Quicksort

1 one process broadcast initial pivot to allprocesses

2 each process in the upper half swaps with apartner in the lower half

3 recurse on each half

4 swap among partners in each half

5 each process uses quicksort on localelements

P0

P1

P2

P3

Unsorted List

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 7 / 27

Page 24: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Quicksort

1 one process broadcast initial pivot to allprocesses

2 each process in the upper half swaps with apartner in the lower half

3 recurse on each half

4 swap among partners in each half

5 each process uses quicksort on localelements

P0

P1

P2

P3

< >

< >

< >

< >

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 7 / 27

Page 25: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Quicksort

1 one process broadcast initial pivot to allprocesses

2 each process in the upper half swaps with apartner in the lower half

3 recurse on each half

4 swap among partners in each half

5 each process uses quicksort on localelements

P0

P1

P2

P3

Lower Half

Upper Half

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 7 / 27

Page 26: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Quicksort

1 one process broadcast initial pivot to allprocesses

2 each process in the upper half swaps with apartner in the lower half

3 recurse on each half

4 swap among partners in each half

5 each process uses quicksort on localelements

P0

P1

P2

P3

< >

< >

< >

< >

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 7 / 27

Page 27: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Quicksort

1 one process broadcast initial pivot to allprocesses

2 each process in the upper half swaps with apartner in the lower half

3 recurse on each half

4 swap among partners in each half

5 each process uses quicksort on localelements

P0

P1

P2

P3

1st quarter

2nd quarter

3rd quarter

4th quarter

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 7 / 27

Page 28: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Hyperquicksort

Limitation of parallel quicksort: poor balancing of list sizes.

Hyperquicksort: sort elements before broadcasting pivot.

1 sort elements in each process

2 select median as pivot element and broadcast it

3 each process in the upper half swaps with a partner in the lower half

4 recurse on each half

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 8 / 27

Page 29: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Hyperquicksort

11 50 53 95 36 67 86 44 35 16 81 1 44 23 15 5P2 P3

97 48 16 8 66 96 17 49 58 76 54 39 82 47 65 51P0 P1

11 50 53 9536 67 8644 3516 811 4423155P2 P3

9748168 66 9617 49 58 765439 8247 6551P0 P1

11

50 53 95

36

67 86

44 3516

81

1 4423155

P2 P397

48168

66 96

17

49 58 7654

39

82

47

6551

P0 P1

11

50 53 95

36

67 86

44 3516

81

1 4423155

P2 P397

48168

66 96

17

49 58 7654

39

82

47

6551

P0 P1

17

67

11

50 53 95

36

86

44 3516

81

1 4423155

P2 P397

48168

66 9649 58 7654

39

82

47

6551

P0 P1

17

67

11

50 53 95

36

86

443516

81

1 4423155

P2 P397

48168

66 9649 58 7654

39

82

47

6551

P0 P1

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 9 / 27

Page 30: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Hyperquicksort

11 50 53 95 36 67 86 44 35 16 81 1 44 23 15 5P2 P3

97 48 16 8 66 96 17 49 58 76 54 39 82 47 65 51P0 P1

11 50 53 9536 67 8644 3516 811 4423155P2 P3

9748168 66 9617 49 58 765439 8247 6551P0 P1

11

50 53 95

36

67 86

44 3516

81

1 4423155

P2 P397

48168

66 96

17

49 58 7654

39

82

47

6551

P0 P1

11

50 53 95

36

67 86

44 3516

81

1 4423155

P2 P397

48168

66 96

17

49 58 7654

39

82

47

6551

P0 P1

17

67

11

50 53 95

36

86

44 3516

81

1 4423155

P2 P397

48168

66 9649 58 7654

39

82

47

6551

P0 P1

17

67

11

50 53 95

36

86

443516

81

1 4423155

P2 P397

48168

66 9649 58 7654

39

82

47

6551

P0 P1

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 9 / 27

Page 31: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Hyperquicksort

11 50 53 95 36 67 86 44 35 16 81 1 44 23 15 5P2 P3

97 48 16 8 66 96 17 49 58 76 54 39 82 47 65 51P0 P1

11 50 53 9536 67 8644 3516 811 4423155P2 P3

9748168 66 9617 49 58 765439 8247 6551P0 P1

11

50 53 95

36

67 86

44 3516

81

1 4423155

P2 P397

48168

66 96

17

49 58 7654

39

82

47

6551

P0 P1

11

50 53 95

36

67 86

44 3516

81

1 4423155

P2 P397

48168

66 96

17

49 58 7654

39

82

47

6551

P0 P1

17

67

11

50 53 95

36

86

44 3516

81

1 4423155

P2 P397

48168

66 9649 58 7654

39

82

47

6551

P0 P1

17

67

11

50 53 95

36

86

443516

81

1 4423155

P2 P397

48168

66 9649 58 7654

39

82

47

6551

P0 P1

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 9 / 27

Page 32: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Hyperquicksort

11 50 53 95 36 67 86 44 35 16 81 1 44 23 15 5P2 P3

97 48 16 8 66 96 17 49 58 76 54 39 82 47 65 51P0 P1

11 50 53 9536 67 8644 3516 811 4423155P2 P3

9748168 66 9617 49 58 765439 8247 6551P0 P1

11

50 53 95

36

67 86

44 3516

81

1 4423155

P2 P397

48168

66 96

17

49 58 7654

39

82

47

6551

P0 P1

11

50 53 95

36

67 86

44 3516

81

1 4423155

P2 P397

48168

66 96

17

49 58 7654

39

82

47

6551

P0 P1

17

67

11

50 53 95

36

86

44 3516

81

1 4423155

P2 P397

48168

66 9649 58 7654

39

82

47

6551

P0 P1

17

67

11

50 53 95

36

86

443516

81

1 4423155

P2 P397

48168

66 9649 58 7654

39

82

47

6551

P0 P1

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 9 / 27

Page 33: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Hyperquicksort

11 50 53 95 36 67 86 44 35 16 81 1 44 23 15 5P2 P3

97 48 16 8 66 96 17 49 58 76 54 39 82 47 65 51P0 P1

11 50 53 9536 67 8644 3516 811 4423155P2 P3

9748168 66 9617 49 58 765439 8247 6551P0 P1

11

50 53 95

36

67 86

44 3516

81

1 4423155

P2 P397

48168

66 96

17

49 58 7654

39

82

47

6551

P0 P1

11

50 53 95

36

67 86

44 3516

81

1 4423155

P2 P397

48168

66 96

17

49 58 7654

39

82

47

6551

P0 P1

17

67

11

50 53 95

36

86

44 3516

81

1 4423155

P2 P397

48168

66 9649 58 7654

39

82

47

6551

P0 P1

17

67

11

50 53 95

36

86

443516

81

1 4423155

P2 P397

48168

66 9649 58 7654

39

82

47

6551

P0 P1

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 9 / 27

Page 34: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Hyperquicksort

11 50 53 95 36 67 86 44 35 16 81 1 44 23 15 5P2 P3

97 48 16 8 66 96 17 49 58 76 54 39 82 47 65 51P0 P1

11 50 53 9536 67 8644 3516 811 4423155P2 P3

9748168 66 9617 49 58 765439 8247 6551P0 P1

11

50 53 95

36

67 86

44 3516

81

1 4423155

P2 P397

48168

66 96

17

49 58 7654

39

82

47

6551

P0 P1

11

50 53 95

36

67 86

44 3516

81

1 4423155

P2 P397

48168

66 96

17

49 58 7654

39

82

47

6551

P0 P1

17

67

11

50 53 95

36

86

44 3516

81

1 4423155

P2 P397

48168

66 9649 58 7654

39

82

47

6551

P0 P1

17

67

11

50 53 95

36

86

443516

81

1 4423155

P2 P397

48168

66 9649 58 7654

39

82

47

6551

P0 P1

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 9 / 27

Page 35: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Complexity Analysis of Hyperquicksort

1 sort elements in each process

2 select median as pivot element and broadcast it

3 each process in the upper half swaps with a partner in the lower half

4 recurse on each half

Computation complexity:

initial quicksort step:

O(np log np )

remaining sorts: O(np )

Communication time:

pivot broadcast: O(log p)

array exchange: O(np )

Number of iterations: log p

Total time: O(np log n) n� p

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 10 / 27

Page 36: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Complexity Analysis of Hyperquicksort

1 sort elements in each process

2 select median as pivot element and broadcast it

3 each process in the upper half swaps with a partner in the lower half

4 recurse on each half

Computation complexity:

initial quicksort step: O(np log np )

remaining sorts:

O(np )

Communication time:

pivot broadcast: O(log p)

array exchange: O(np )

Number of iterations: log p

Total time: O(np log n) n� p

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 10 / 27

Page 37: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Complexity Analysis of Hyperquicksort

1 sort elements in each process

2 select median as pivot element and broadcast it

3 each process in the upper half swaps with a partner in the lower half

4 recurse on each half

Computation complexity:

initial quicksort step: O(np log np )

remaining sorts: O(np )

Communication time:

pivot broadcast:

O(log p)

array exchange: O(np )

Number of iterations: log p

Total time: O(np log n) n� p

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 10 / 27

Page 38: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Complexity Analysis of Hyperquicksort

1 sort elements in each process

2 select median as pivot element and broadcast it

3 each process in the upper half swaps with a partner in the lower half

4 recurse on each half

Computation complexity:

initial quicksort step: O(np log np )

remaining sorts: O(np )

Communication time:

pivot broadcast: O(log p)

array exchange:

O(np )

Number of iterations: log p

Total time: O(np log n) n� p

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 10 / 27

Page 39: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Complexity Analysis of Hyperquicksort

1 sort elements in each process

2 select median as pivot element and broadcast it

3 each process in the upper half swaps with a partner in the lower half

4 recurse on each half

Computation complexity:

initial quicksort step: O(np log np )

remaining sorts: O(np )

Communication time:

pivot broadcast: O(log p)

array exchange: O(np )

Number of iterations:

log p

Total time: O(np log n) n� p

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 10 / 27

Page 40: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Complexity Analysis of Hyperquicksort

1 sort elements in each process

2 select median as pivot element and broadcast it

3 each process in the upper half swaps with a partner in the lower half

4 recurse on each half

Computation complexity:

initial quicksort step: O(np log np )

remaining sorts: O(np )

Communication time:

pivot broadcast: O(log p)

array exchange: O(np )

Number of iterations: log p

Total time: O(np log n) n� p

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 10 / 27

Page 41: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Isoefficiency Analysis of Hyperquicksort

Isoefficiency analysis: T (n, 1) ≥ CT0(n, p)

(T (n, 1) sequential time; T0(n, p) parallel overhead)

Sequential time complexity: T (n, 1) = O(n log n)

Parallel overhead dominated by exchanges: O( np log p)

T0(n, p) = p × O(n

plog p) = O(n log p)

n log n ≥ Cn log p ⇒ n ≥ pC

Scalability function: M(f (p))/p

M(n) = n ⇒ M(pC )

p= pC−1

⇒ Scalability is only good for C ≤ 2.

C =ε(n, p)

1− ε(n, p)≤ 2⇒ ε(n, p) ≤ 2

3

Cannot maintain high efficiency!

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 11 / 27

Page 42: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Isoefficiency Analysis of Hyperquicksort

Isoefficiency analysis: T (n, 1) ≥ CT0(n, p)

(T (n, 1) sequential time; T0(n, p) parallel overhead)

Sequential time complexity: T (n, 1) = O(n log n)

Parallel overhead dominated by exchanges: O( np log p)

T0(n, p) = p × O(n

plog p) = O(n log p)

n log n ≥ Cn log p ⇒ n ≥ pC

Scalability function: M(f (p))/p

M(n) = n ⇒ M(pC )

p= pC−1

⇒ Scalability is only good for C ≤ 2.

C =ε(n, p)

1− ε(n, p)≤ 2⇒ ε(n, p) ≤ 2

3

Cannot maintain high efficiency!

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 11 / 27

Page 43: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Isoefficiency Analysis of Hyperquicksort

Isoefficiency analysis: T (n, 1) ≥ CT0(n, p)

(T (n, 1) sequential time; T0(n, p) parallel overhead)

Sequential time complexity: T (n, 1) = O(n log n)

Parallel overhead dominated by exchanges: O( np log p)

T0(n, p) = p × O(n

plog p) = O(n log p)

n log n ≥ Cn log p ⇒ n ≥ pC

Scalability function: M(f (p))/p

M(n) = n ⇒ M(pC )

p= pC−1

⇒ Scalability is only good for C ≤ 2.

C =ε(n, p)

1− ε(n, p)≤ 2⇒ ε(n, p) ≤ 2

3

Cannot maintain high efficiency!

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 11 / 27

Page 44: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Isoefficiency Analysis of Hyperquicksort

Isoefficiency analysis: T (n, 1) ≥ CT0(n, p)

(T (n, 1) sequential time; T0(n, p) parallel overhead)

Sequential time complexity: T (n, 1) = O(n log n)

Parallel overhead dominated by exchanges: O( np log p)

T0(n, p) = p × O(n

plog p) = O(n log p)

n log n ≥ Cn log p ⇒ n ≥ pC

Scalability function: M(f (p))/p

M(n) = n ⇒ M(pC )

p= pC−1

⇒ Scalability is only good for C ≤ 2.

C =ε(n, p)

1− ε(n, p)≤ 2⇒ ε(n, p) ≤ 2

3

Cannot maintain high efficiency!

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 11 / 27

Page 45: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Isoefficiency Analysis of Hyperquicksort

Isoefficiency analysis: T (n, 1) ≥ CT0(n, p)

(T (n, 1) sequential time; T0(n, p) parallel overhead)

Sequential time complexity: T (n, 1) = O(n log n)

Parallel overhead dominated by exchanges: O( np log p)

T0(n, p) = p × O(n

plog p) = O(n log p)

n log n ≥ Cn log p ⇒ n ≥ pC

Scalability function: M(f (p))/p

M(n) = n ⇒ M(pC )

p= pC−1

⇒ Scalability is only good for C ≤ 2.

C =ε(n, p)

1− ε(n, p)≤ 2⇒ ε(n, p) ≤ 2

3

Cannot maintain high efficiency!CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 11 / 27

Page 46: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Limitations on the Scalability of Hyperquicksort

analysis assumes lists remain balanced

as p increases, each processor’s share of list decreases

hence, as p increases, likelihood of lists becoming unbalancedincreases

unbalanced lists lowers efficiency

A better solution is to get sample values from all processes beforechoosing median.

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 12 / 27

Page 47: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Sorting by Regular Sampling

Parallel Sorting by Regular Sampling, PSRS:

each process sorts its share of elements

each process selects regular samples of sorted list

one process gathers and sorts samples, chooses pivot values fromsorted sample list, and broadcasts these pivot values

each process partitions its list into p pieces, using pivot values

each process sends partitions to other processes

each process merges its partitions

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 13 / 27

Page 48: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Sorting by Regular Sampling

53 97 84 58 32 27 33 72P215 46 48 6 93 39 72 91 36 69 40 89 61 97 12 21P0 P114 54 20

53 9784583227 33 72P215 46 486 9339 72 91 36 6940 8961 9712 21P0 P114 54 20

33 726 39 72 40 6912 20 33 726 39 7240 6912 20

53 9784583227 33 72P215 46 486 9339 72 91 36 6940 8961 9712 21P0 P114 54 2033 693333 6969

53 978458 72P2156 36 6940 8961 97P0 P114 5446 483912 21 9372 913227 3320

53 978458 72P2156 36 6940 8961 97P0 P114 5446 483912 21 9372 913227 3320

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 14 / 27

Page 49: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Sorting by Regular Sampling

53 97 84 58 32 27 33 72P215 46 48 6 93 39 72 91 36 69 40 89 61 97 12 21P0 P114 54 20

53 9784583227 33 72P215 46 486 9339 72 91 36 6940 8961 9712 21P0 P114 54 20

33 726 39 72 40 6912 20 33 726 39 7240 6912 20

53 9784583227 33 72P215 46 486 9339 72 91 36 6940 8961 9712 21P0 P114 54 2033 693333 6969

53 978458 72P2156 36 6940 8961 97P0 P114 5446 483912 21 9372 913227 3320

53 978458 72P2156 36 6940 8961 97P0 P114 5446 483912 21 9372 913227 3320

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 14 / 27

Page 50: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Sorting by Regular Sampling

53 97 84 58 32 27 33 72P215 46 48 6 93 39 72 91 36 69 40 89 61 97 12 21P0 P114 54 20

53 9784583227 33 72P215 46 486 9339 72 91 36 6940 8961 9712 21P0 P114 54 20

33 726 39 72 40 6912 20 33 726 39 7240 6912 20

53 9784583227 33 72P215 46 486 9339 72 91 36 6940 8961 9712 21P0 P114 54 2033 693333 6969

53 978458 72P2156 36 6940 8961 97P0 P114 5446 483912 21 9372 913227 3320

53 978458 72P2156 36 6940 8961 97P0 P114 5446 483912 21 9372 913227 3320

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 14 / 27

Page 51: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Sorting by Regular Sampling

53 97 84 58 32 27 33 72P215 46 48 6 93 39 72 91 36 69 40 89 61 97 12 21P0 P114 54 20

53 9784583227 33 72P215 46 486 9339 72 91 36 6940 8961 9712 21P0 P114 54 20

33 726 39 72 40 6912 20 33 726 39 7240 6912 20

53 9784583227 33 72P215 46 486 9339 72 91 36 6940 8961 9712 21P0 P114 54 2033 693333 6969

53 978458 72P2156 36 6940 8961 97P0 P114 5446 483912 21 9372 913227 3320

53 978458 72P2156 36 6940 8961 97P0 P114 5446 483912 21 9372 913227 3320

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 14 / 27

Page 52: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Sorting by Regular Sampling

53 97 84 58 32 27 33 72P215 46 48 6 93 39 72 91 36 69 40 89 61 97 12 21P0 P114 54 20

53 9784583227 33 72P215 46 486 9339 72 91 36 6940 8961 9712 21P0 P114 54 20

33 726 39 72 40 6912 20 33 726 39 7240 6912 20

53 9784583227 33 72P215 46 486 9339 72 91 36 6940 8961 9712 21P0 P114 54 2033 693333 6969

53 978458 72P2156 36 6940 8961 97P0 P114 5446 483912 21 9372 913227 3320

53 978458 72P2156 36 6940 8961 97P0 P114 5446 483912 21 9372 913227 3320

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 14 / 27

Page 53: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Sorting by Regular Sampling

53 97 84 58 32 27 33 72P215 46 48 6 93 39 72 91 36 69 40 89 61 97 12 21P0 P114 54 20

53 9784583227 33 72P215 46 486 9339 72 91 36 6940 8961 9712 21P0 P114 54 20

33 726 39 72 40 6912 20 33 726 39 7240 6912 20

53 9784583227 33 72P215 46 486 9339 72 91 36 6940 8961 9712 21P0 P114 54 2033 693333 6969

53 978458 72P2156 36 6940 8961 97P0 P114 5446 483912 21 9372 913227 3320

53 978458 72P2156 36 6940 8961 97P0 P114 5446 483912 21 9372 913227 3320

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 14 / 27

Page 54: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Complexity Analysis of PSRS

Computation complexity:

initial quicksort step:

O(np log np )

sorting samples: O(p2 log p)

merging subarrays: O(np log p)

Communication time:

gather samples: O(p log p)

pivot broadcast: O(p log p)

array exchange: O(np )

Total time: O(np log n)

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 15 / 27

Page 55: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Complexity Analysis of PSRS

Computation complexity:

initial quicksort step: O(np log np )

sorting samples:

O(p2 log p)

merging subarrays: O(np log p)

Communication time:

gather samples: O(p log p)

pivot broadcast: O(p log p)

array exchange: O(np )

Total time: O(np log n)

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 15 / 27

Page 56: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Complexity Analysis of PSRS

Computation complexity:

initial quicksort step: O(np log np )

sorting samples: O(p2 log p)

merging subarrays:

O(np log p)

Communication time:

gather samples: O(p log p)

pivot broadcast: O(p log p)

array exchange: O(np )

Total time: O(np log n)

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 15 / 27

Page 57: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Complexity Analysis of PSRS

Computation complexity:

initial quicksort step: O(np log np )

sorting samples: O(p2 log p)

merging subarrays: O(np log p)

Communication time:

gather samples:

O(p log p)

pivot broadcast: O(p log p)

array exchange: O(np )

Total time: O(np log n)

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 15 / 27

Page 58: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Complexity Analysis of PSRS

Computation complexity:

initial quicksort step: O(np log np )

sorting samples: O(p2 log p)

merging subarrays: O(np log p)

Communication time:

gather samples: O(p log p)

pivot broadcast:

O(p log p)

array exchange: O(np )

Total time: O(np log n)

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 15 / 27

Page 59: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Complexity Analysis of PSRS

Computation complexity:

initial quicksort step: O(np log np )

sorting samples: O(p2 log p)

merging subarrays: O(np log p)

Communication time:

gather samples: O(p log p)

pivot broadcast: O(p log p)

array exchange:

O(np )

Total time: O(np log n)

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 15 / 27

Page 60: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Complexity Analysis of PSRS

Computation complexity:

initial quicksort step: O(np log np )

sorting samples: O(p2 log p)

merging subarrays: O(np log p)

Communication time:

gather samples: O(p log p)

pivot broadcast: O(p log p)

array exchange: O(np )

Total time: O(np log n)

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 15 / 27

Page 61: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Isoefficiency Analysis of PSRS

Isoefficiency analysis: T (n, 1) ≥ CT0(n, p)

Sequential time complexity: T (n, 1) = O(n log n)

Parallel overhead:communication dominated by exchanges: O( n

p )

redundant computation of last merge: O( np log p)

T0(n, p) = p × O(n

plog p) = O(n log p)

n log n ≥ Cn log p ⇒ n ≥ pC

Scalability function: M(f (p))/p

M(n) = n ⇒ M(pC )

p= pC−1

⇒ Same scalability as hyperquicksort.

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 16 / 27

Page 62: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Comparison of Parallel QuickSorting Algorithms

Three parallel algorithms based on quicksort.

Keeping list sizes balanced:

Parallel quicksort: poor

Hyperquicksort: better

PSRS algorithm: excellent

Average number of times each key moved:

Parallel quicksort and hyperquicksort: log p2

PSRS algorithm: p−1p

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 17 / 27

Page 63: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Odd-Even Transposition Sort

Based on Bubble Sort

Consists of two distinct phases:

Even phase

compare and swaps are executed on pairs (a[0],a[1]); (a[2],a[3]);(a[4],a[5]);...

Odd phase

compare and swaps are executed on pairs (a[1],a[2]); (a[3],a[4]);(a[5],a[6]);...

Sorting is complete after at most n phases

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 18 / 27

Page 64: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Odd-Even Transposition Sort Example

18 15 22 10 23 11 odd phase

15 18 10 22 11 23even phase

15 10 18 11 22 23 odd phase

10 15 11 18 22 23even phase

10 11 15 18 22 23 odd phase

10 11 15 18 22 23even phase

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 19 / 27

Page 65: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Odd-Even Transposition Sort Example

18 15 22 10 23 11 odd phase

15 18 10 22 11 23even phase

15 10 18 11 22 23 odd phase

10 15 11 18 22 23even phase

10 11 15 18 22 23 odd phase

10 11 15 18 22 23even phase

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 19 / 27

Page 66: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Odd-Even Transposition Sort Example

18 15 22 10 23 11 odd phase

15 18 10 22 11 23even phase

15 10 18 11 22 23 odd phase

10 15 11 18 22 23even phase

10 11 15 18 22 23 odd phase

10 11 15 18 22 23even phase

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 19 / 27

Page 67: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Odd-Even Transposition Sort Example

18 15 22 10 23 11 odd phase

15 18 10 22 11 23even phase

15 10 18 11 22 23 odd phase

10 15 11 18 22 23even phase

10 11 15 18 22 23 odd phase

10 11 15 18 22 23even phase

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 19 / 27

Page 68: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Odd-Even Transposition Sort Example

18 15 22 10 23 11 odd phase

15 18 10 22 11 23even phase

15 10 18 11 22 23 odd phase

10 15 11 18 22 23even phase

10 11 15 18 22 23 odd phase

10 11 15 18 22 23even phase

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 19 / 27

Page 69: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Odd-Even Transposition Sort Example

18 15 22 10 23 11 odd phase

15 18 10 22 11 23even phase

15 10 18 11 22 23 odd phase

10 15 11 18 22 23even phase

10 11 15 18 22 23 odd phase

10 11 15 18 22 23even phase

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 19 / 27

Page 70: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Odd-Even Transposition Sort Program

void Odd_even_sort(int a[], int n)

{

int phase, i;

for (phase = 0; phase < n; phase++)

if (phase % 2 == 0) /* even phase */

for (i = 1; i < n; i += 2)

if (a[i-1] > a[i])

swap(&a[i], &a[i-1]);

else /* odd phase */

for (i = 1; i < n-1; i += 2)

if (a[i] > a[i+1])

swap(&a[i], &a[i+1]);

}

Complexity of sequencial algorihtm: O(n2)

Can we do better in the parallel algorithm?

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 20 / 27

Page 71: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Odd-Even Transposition Sort Program

void Odd_even_sort(int a[], int n)

{

int phase, i;

for (phase = 0; phase < n; phase++)

if (phase % 2 == 0) /* even phase */

for (i = 1; i < n; i += 2)

if (a[i-1] > a[i])

swap(&a[i], &a[i-1]);

else /* odd phase */

for (i = 1; i < n-1; i += 2)

if (a[i] > a[i+1])

swap(&a[i], &a[i+1]);

}

Complexity of sequencial algorihtm:

O(n2)

Can we do better in the parallel algorithm?

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 20 / 27

Page 72: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Odd-Even Transposition Sort Program

void Odd_even_sort(int a[], int n)

{

int phase, i;

for (phase = 0; phase < n; phase++)

if (phase % 2 == 0) /* even phase */

for (i = 1; i < n; i += 2)

if (a[i-1] > a[i])

swap(&a[i], &a[i-1]);

else /* odd phase */

for (i = 1; i < n-1; i += 2)

if (a[i] > a[i+1])

swap(&a[i], &a[i+1]);

}

Complexity of sequencial algorihtm: O(n2)

Can we do better in the parallel algorithm?

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 20 / 27

Page 73: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Odd-Even Transposition Sort

Partitioning:

Primitive task is to determine value of a[i ] at the end of each phase

Communication:

phase j-1

phase j

phase j+1

Aglomeration and Mapping:Distibute values of array through processors

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 21 / 27

Page 74: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Odd-Even Transposition Sort

Partitioning:Primitive task is to determine value of a[i ] at the end of each phase

Communication:

phase j-1

phase j

phase j+1

Aglomeration and Mapping:Distibute values of array through processors

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 21 / 27

Page 75: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Odd-Even Transposition Sort

Partitioning:Primitive task is to determine value of a[i ] at the end of each phase

Communication:

phase j-1

phase j

phase j+1

Aglomeration and Mapping:Distibute values of array through processors

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 21 / 27

Page 76: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Odd-Even Transposition Sort

Partitioning:Primitive task is to determine value of a[i ] at the end of each phase

Communication:

phase j-1

phase j

phase j+1

Aglomeration and Mapping:Distibute values of array through processors

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 21 / 27

Page 77: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Odd-Even Transposition Sort

Partitioning:Primitive task is to determine value of a[i ] at the end of each phase

Communication:

phase j-1

phase j

phase j+1

Aglomeration and Mapping:

Distibute values of array through processors

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 21 / 27

Page 78: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Odd-Even Transposition Sort

Partitioning:Primitive task is to determine value of a[i ] at the end of each phase

Communication:

phase j-1

phase j

phase j+1

Aglomeration and Mapping:Distibute values of array through processors

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 21 / 27

Page 79: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Odd-Even Transposition Sort Example

15, 11, 9, 16 3, 14, 8, 7 4, 6, 12, 10 5, 2, 13, 1Start

P0 P1 P2 P3

9, 11, 15, 16 3, 7, 8, 14 4, 6, 10, 12 1, 2, 5, 13After local sort

3, 7, 8, 9 11, 14, 15, 16 1, 2, 4, 5 6, 10, 12, 13After phase 0

3, 7, 8, 9 1, 2, 4, 5 11, 14, 15, 16 6, 10, 12, 13After phase 1

1, 2, 3, 4 5, 7, 8, 9 6, 10, 11, 12 13, 14, 15, 16After phase 2

1, 2, 3, 4 5, 6, 7, 8 9, 10, 11, 12 13, 14, 15, 16After phase 3

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 22 / 27

Page 80: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Odd-Even Transposition Sort Example

15, 11, 9, 16 3, 14, 8, 7 4, 6, 12, 10 5, 2, 13, 1Start

P0 P1 P2 P3

9, 11, 15, 16 3, 7, 8, 14 4, 6, 10, 12 1, 2, 5, 13After local sort

3, 7, 8, 9 11, 14, 15, 16 1, 2, 4, 5 6, 10, 12, 13After phase 0

3, 7, 8, 9 1, 2, 4, 5 11, 14, 15, 16 6, 10, 12, 13After phase 1

1, 2, 3, 4 5, 7, 8, 9 6, 10, 11, 12 13, 14, 15, 16After phase 2

1, 2, 3, 4 5, 6, 7, 8 9, 10, 11, 12 13, 14, 15, 16After phase 3

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 22 / 27

Page 81: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Odd-Even Transposition Sort Example

15, 11, 9, 16 3, 14, 8, 7 4, 6, 12, 10 5, 2, 13, 1Start

P0 P1 P2 P3

9, 11, 15, 16 3, 7, 8, 14 4, 6, 10, 12 1, 2, 5, 13After local sort

3, 7, 8, 9 11, 14, 15, 16 1, 2, 4, 5 6, 10, 12, 13After phase 0

3, 7, 8, 9 1, 2, 4, 5 11, 14, 15, 16 6, 10, 12, 13After phase 1

1, 2, 3, 4 5, 7, 8, 9 6, 10, 11, 12 13, 14, 15, 16After phase 2

1, 2, 3, 4 5, 6, 7, 8 9, 10, 11, 12 13, 14, 15, 16After phase 3

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 22 / 27

Page 82: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Odd-Even Transposition Sort Example

15, 11, 9, 16 3, 14, 8, 7 4, 6, 12, 10 5, 2, 13, 1Start

P0 P1 P2 P3

9, 11, 15, 16 3, 7, 8, 14 4, 6, 10, 12 1, 2, 5, 13After local sort

3, 7, 8, 9 11, 14, 15, 16 1, 2, 4, 5 6, 10, 12, 13After phase 0

3, 7, 8, 9 1, 2, 4, 5 11, 14, 15, 16 6, 10, 12, 13After phase 1

1, 2, 3, 4 5, 7, 8, 9 6, 10, 11, 12 13, 14, 15, 16After phase 2

1, 2, 3, 4 5, 6, 7, 8 9, 10, 11, 12 13, 14, 15, 16After phase 3

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 22 / 27

Page 83: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Odd-Even Transposition Sort Example

15, 11, 9, 16 3, 14, 8, 7 4, 6, 12, 10 5, 2, 13, 1Start

P0 P1 P2 P3

9, 11, 15, 16 3, 7, 8, 14 4, 6, 10, 12 1, 2, 5, 13After local sort

3, 7, 8, 9 11, 14, 15, 16 1, 2, 4, 5 6, 10, 12, 13After phase 0

3, 7, 8, 9 1, 2, 4, 5 11, 14, 15, 16 6, 10, 12, 13After phase 1

1, 2, 3, 4 5, 7, 8, 9 6, 10, 11, 12 13, 14, 15, 16After phase 2

1, 2, 3, 4 5, 6, 7, 8 9, 10, 11, 12 13, 14, 15, 16After phase 3

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 22 / 27

Page 84: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Parallel Odd-Even Transposition Sort Example

15, 11, 9, 16 3, 14, 8, 7 4, 6, 12, 10 5, 2, 13, 1Start

P0 P1 P2 P3

9, 11, 15, 16 3, 7, 8, 14 4, 6, 10, 12 1, 2, 5, 13After local sort

3, 7, 8, 9 11, 14, 15, 16 1, 2, 4, 5 6, 10, 12, 13After phase 0

3, 7, 8, 9 1, 2, 4, 5 11, 14, 15, 16 6, 10, 12, 13After phase 1

1, 2, 3, 4 5, 7, 8, 9 6, 10, 11, 12 13, 14, 15, 16After phase 2

1, 2, 3, 4 5, 6, 7, 8 9, 10, 11, 12 13, 14, 15, 16After phase 3

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 22 / 27

Page 85: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Complexity Analysis of Odd-Even Sort

1 sort elements in each process

2 send/receive values to/from partner process

3 if rank of process is smaller than rank of partner then keep smallervalues

4 otherwise keep larger values

Computational complexity:

initial quicksort:

O(np log np )

sorting smaller/larger values in each phase:

O(np )

Communication time:

Sending/receving values in each phase:

O(np )

Number of phases: p

Total time: O(np log n + n)

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 23 / 27

Page 86: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Complexity Analysis of Odd-Even Sort

1 sort elements in each process

2 send/receive values to/from partner process

3 if rank of process is smaller than rank of partner then keep smallervalues

4 otherwise keep larger values

Computational complexity:

initial quicksort: O(np log np )

sorting smaller/larger values in each phase:

O(np )

Communication time:

Sending/receving values in each phase:

O(np )

Number of phases: p

Total time: O(np log n + n)

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 23 / 27

Page 87: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Complexity Analysis of Odd-Even Sort

1 sort elements in each process

2 send/receive values to/from partner process

3 if rank of process is smaller than rank of partner then keep smallervalues

4 otherwise keep larger values

Computational complexity:

initial quicksort: O(np log np )

sorting smaller/larger values in each phase: O(np )

Communication time:

Sending/receving values in each phase:

O(np )

Number of phases: p

Total time: O(np log n + n)

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 23 / 27

Page 88: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Complexity Analysis of Odd-Even Sort

1 sort elements in each process

2 send/receive values to/from partner process

3 if rank of process is smaller than rank of partner then keep smallervalues

4 otherwise keep larger values

Computational complexity:

initial quicksort: O(np log np )

sorting smaller/larger values in each phase: O(np )

Communication time:

Sending/receving values in each phase: O(np )

Number of phases:

p

Total time: O(np log n + n)

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 23 / 27

Page 89: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Complexity Analysis of Odd-Even Sort

1 sort elements in each process

2 send/receive values to/from partner process

3 if rank of process is smaller than rank of partner then keep smallervalues

4 otherwise keep larger values

Computational complexity:

initial quicksort: O(np log np )

sorting smaller/larger values in each phase: O(np )

Communication time:

Sending/receving values in each phase: O(np )

Number of phases: p

Total time: O(np log n + n)

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 23 / 27

Page 90: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Isoefficiency Analysis of Odd-Even Sort

Isoefficiency analysis: T (n, 1) ≥ CT0(n, p)

Sequential time complexity: T (n, 1) = O(n log n)

Parallel overhead dominated by exchanges: O(n)

T0(n, p) = p × O(n) = O(pn)

n log n ≥ Cpn ⇒ n ≥ eCp

Scalability function: M(f (p))/p

M(n) = n ⇒ M(eCp)

p=

eCp

p

⇒ Poor scalability.

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 24 / 27

Page 91: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Comparison of Parallel Sorting Algorithms

HyperquicksortTotal time: O(np log n)

Scalability function: pC−1

⇒ Scalability is only good for C ≤ 2.

PSRSTotal time: O(np log n)

Scalability function: pC−1

⇒ Same scalability as hyperquicksort.

Odd-Even SortTotal time: O(np log n + n)

Scalability function: eCp

p⇒ Poor scalability.

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 25 / 27

Page 92: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Review

Parallel Sort

Hyperquicksort

PSRS, Parallel Sorting by Regular Sampling

Odd-Even Transposition Sort

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 26 / 27

Page 93: Parallel Sort - GitHub Pagesparallelcomp.github.io/parallelsort.pdf · Parallel Sort Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto

Next Class

Efficient parallelization of numerical algorithms

CPD (DEI / IST) Parallel and Distributed Computing – 21 2012-11-29 27 / 27