Docsity

Parallel Sorting Algorithms: Unlimited, Fixed, and Scalable Approaches, Study notes of Computer Science

Partial preview of the text

Download Parallel Sorting Algorithms: Unlimited, Fixed, and Scalable Approaches and more Study notes Computer Science in PDF only on Docsity!

Parallel Sorting

Three solutions to sorting in parallel:• Unlimited parallelism — similar to bubble sort• Fixed parallelism — 26 threads, one per letter• Scalable parallelism — Batcher’s Bitonic Sort

Using Unlimited Parallelism Dog^

Cat

Art

Eel

Bus

Fun

Cat

Dog

Art

Eel

Bus

Fun

Cat

Art

Dog

Bus

Eel

Fun

Art

Cat

Bus

Dog

Eel

Fun

Art

Bus

Cat

Dog

Eel

Fun

Swaping Pairs

void

swap_pairs(int start) {

forall(i

in(start...n

stride

by

{ rec

temp; bool

done

= true;

if

(strcmp(L

[i].x, L

[i+1].x)

>^

0)^

temp

=^

L[i];

L[i]

=^

L[i+1];

L[i+1]

= temp; done

=^

false;

} if

(!start) continue

= !(&&/done);

Fixed Parallelism

Cog

Cat

Art

Cel

Bus

Bun

counts['C']++counts['C']++

counts['A']++counts['C']++

counts['B']++counts['B']++

counts['A']

Art

counts['B'] Bus

Bun

counts['C'] Cog

Cat

Cel

Art

Bun

Bus

Cat

Cel

Cog

Art

Bun

Bus

Cat

Cel

Cog

Local Sorting

rec[]

localSort(int index, int

myCount)

rec

temp[] = new rec[myCount]; int

j^

for

(int i = 0; i < n

; i++)

if^

(letRank(L

[i].x[0])

index)

temp[j++] = L

[i];

alphabetizeInPlace(temp, myCount);return

temp;

Scalable Parallelism

Batcher’s Setup

rec L

[n]; int t

int m

=^

log2(t

int size

n^

/^

t;

typedef

struct

{ char x[MAXLEN];

int

home;

key;

key BufK

[m

][size

];

bool free'[m

]^

= false, ready'[m

];

Assume

L^ localized into equal contiguous segments

Assume

BufK

localized by first index

Batcher’s Thread Outline

forall

(index

in(0..t

-1))

{

rec

myL[size

]^ = localize(L

),^

inputCopy[size

];

key

Kn[][]

=^

localize(BufK

),^

K[size

];

for

(int

i^

=^ 0;

i^

<^ size

;^ i++)

{

K[i].x

=^

myL[i].x;

K[i].home

=^

localToGobal(myL,

i,

0);

} alphabetizeInPlace(K,

size

,^ bit(index,

/*

up

or

down

*/);

....

/*

main

loop

*/

for

(int

i^

=^ 0;

i^

<^ size

;^ i++)

inputCopy[i]

=^

L[K[i].home];

barrier;for

(int

i^

=^ 0; i

<^

size

;^ i++)

myL[i]

=^

inputCopy[i];

}

Batcher’s Merge

void merge(int index, int d, int p, key

Kn[][],

key

K[])

{ for (int i = 0;

i < size

; i++) {

bool want;int cmp;cmp = strcmp(Kn[index][i].x, K[i].x);if (bit(index, d) == bit(index, p))want = (cmp

0);

elsewant = (cmp

< 0);

if (want)K[i] = Kn[i];} }^