Docsity

Sorting - Parallel Computing - Lecture Slides, Slides of Parallel Computing and Programming

Partial preview of the text

Download Sorting - Parallel Computing - Lecture Slides and more Slides Parallel Computing and Programming in PDF only on Docsity!

Parallel Sorting

Topics for Today

  • Introduction
  • Sorting networks and Batcher’s bitonic sort
  • Other parallel sorting methods —radix sort —sample sort —histogram sort —parallel sort with exact splitters 2

4

Parallel Sorting Basics

  • Where are the input and output lists stored? —we assume that both input and output lists are distributed
  • What is a parallel sorted sequence? —sequence partitioned among the processors —each processor’s sub-sequence is sorted —all in^ Pj's^ sub-sequence < all in^ Pk's^ sub-sequence if^ j < k - the best process numbering can depend on network topology

5

When partitioning is one element per process

1. Processes P

j

and P

k

send their elements to each other

Each process now has both elements

2. Process P

j

keeps min(a

j

,a

k

), and P

k

keeps max(a

j

, a

k

Pj Pk aj ak

Element-wise Parallel Compare-Exchange

Pj Pk aj, ak ak, aj Pj Pk min(aj, ak) max(ak, aj)

[communication step]

[comparison step]

7

Basic Analysis

  • Assumptions — P i and P j are neighbors — communication channels are bi-directional
  • Elementwise compare-exchange: 1 element per processor — time =^ ts + tw
  • Bulk compare-split: n/p elements per processor — after compare-split on pair of processors P i and P j ,^ i^ <^ j - smaller^ n/p^ elements are at processor^ Pi - larger^ n/p^ elements at^ Pj — time =^ ts+ twn/p - merge in O(n/p) time, as long as partial lists are sorted

8

Sorting Network

  • Network of comparators designed for sorting
  • Comparator : two inputs x and y; two outputs x' and y’ —types - increasing (denoted^ ⊕): x' = min(x,y) and y' = max(x,y) - decreasing (denoted Ө) : x' = max(x,y) and y' = min(x,y)
  • Sorting network speed is proportional to its depth x min(x,y) y max(x,y)

x max(x,y) y min(x,y)

10

Example: Bitonic Sorting Network

  • Bitonic sequence —two parts: increasing and decreasing - 〈1,2,4,7,6,0〉: first increases and then decreases (or vice versa) —cyclic rotation of a bitonic sequence is also considered bitonic - 〈8,9,2,1,0,4〉: cyclic rotation of^ 〈0,4,8,9,2,1〉
  • Bitonic sorting network —sorts n elements in Θ(log 2 n) time —network kernel: rearrange a bitonic sequence into a sorted one

11

Bitonic Split (Batcher, 1968)

  • Let^ s =^ 〈 a 0 ,a 1 ,…,an-1 〉^ be a bitonic sequence — a 0 ≤^ a 1 ≤^ ···^ ≤^ an/2-1 , and — an/2 ≥^ an/2+1 ≥^ ···^ ≥^ an-
  • Consider the following subsequences of^ s

s

1

= 〈min( a

0

, a

n/

),min( a

1

, a

n/2+

), … ,min( a

n/2-

, a

n-

s

2

= 〈max( a

0

, a

n/

),max( a

1

, a

n/2+

), … ,max( a

n/2-

, a

n-

  • Sequence properties — s 1 and^ s 2 are both bitonic —∀ x ∀ y x^ ∈^ s 1 , y^ ∈^ s 2 , x < y
  • Apply recursively on^ s 1 and^ s 2 to produce a sorted sequence
  • Works for any bitonic sequence, even if^ the increasing and

decreasing parts are different lengths

Splitting Bitonic Sequences - I

min max^13

Sequence properties

s 1 and s 2 are both bitonic ∀ x ∀ y x ∈ s 1 , y ∈ s 2 , x < y

Splitting Bitonic Sequences - I

min max^14

Sequence properties

s 1 and s 2 are both bitonic ∀ x ∀ y x ∈ s 1 , y ∈ s 2 , x < y

Splitting Bitonic Sequences - I

min max^16

Sequence properties

s 1 and s 2 are both bitonic ∀ x ∀ y x ∈ s 1 , y ∈ s 2 , x < y

Splitting Bitonic Sequences - II

min max^17

Sequence properties

s 1 and s 2 are both bitonic ∀ x ∀ y x ∈ s 1 , y ∈ s 2 , x < y

19

Sorting via Bitonic Merging Network

  • Sorting network can implement bitonic merge algorithm — bitonic merging network
  • Network structure —log 2 n^ columns —each column - n/2 comparators - performs one step of the bitonic merge
  • Bitonic merging network with^ n^ inputs:^ ⊕ BM[n] —yields increasing output sequence
  • Replacing^ ⊕^ comparators by^ Ө^ comparators:^ Ө BM[n] —yields decreasing output sequence

20 Bitonic Merging Network, ⊕ BM[16]

  • Input: bitonic sequence — (^) input wires are numbered 0,1,…, n - 1 (shown in binary)
  • Output: sequence in sorted order
  • Each column of comparators is drawn separately