Download PowerPoint 簡報

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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)
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)
Rank Sort
Using n2 processors
O(1)
Rank Sort
Compare-and-Exchange
if(A > B) {
temp = A;
A = B;
B = temp;
}
Compare-and-Exchange
Process P1
send(&A, P2);
recv(&A, P2);
Process P2
recv(&A, P1);
if(A > B){
send(&B, P1);
B = A;
}else
send(&A, P1);
Compare-and-Exchange
Process P1
send(&A, P2);
recv(&B, P2);
if(A > B) A = B;
Process P2
recv(&A, P1);
send(&B, P1);
if(A > B) B = A;
Compare-and-Exchange
Data partitioning
Compare-and-Exchange
Data partitioning
Bubble Sort and Odd-even Transposition 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)
Bubble Sort and Odd-even Transposition Sort
Parallel Code
Bubble Sort and Odd-even Transposition Sort
Bubble Sort and Odd-even Transposition Sort
Pi,i=0,2,4,6,...,n-2(even)
Pi,i=1,3,5,..,n-1(odd)
recv(&A, Pi+1);
send(&A,Pi-1);
send(&B, Pi+1);
recv(&B, Pi-1);
if(A > B) B = A;
if(A > B) A = B;
Bubble Sort and Odd-even Transposition Sort
Pi,i=1,3,5,...,n-3(odd)
Pi,i=2,4,6,...,n-2(even)
send(&A, Pi+1);
recv(&A, Pi-1);
recv(&B, Pi+1);
send(&B, Pi-1);
if(A > B) A = B;
if(A > B) B = A;
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;
}
Bubble Sort and Odd-even Transposition Sort
Two-Dimension Sorting
Bubble Sort and Odd-even Transposition Sort
Odd Phase, the following actios are done:
n (log n  1)
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.
Bubble Sort and Odd-even Transposition Sort
Bubble Sort and Odd-even Transposition Sort
Merge Sort
Merge Sort
Communication (division phase)
Communication at each step
Processor communication
t startup ( n / 2)t data
P0->P4
t startup ( n / 4)t data
P0->P2;P4->P6
t startup(n / 8)t data
P0->P1;P2->P3;P4->P5;P6->P7
Merge Sort
Communication (merge phase)
Communication at each step
Processor communication
t startup(n / 8)t data
P0->P1;P2->P3;P4->P5;P6->P7
t startup ( n / 4)t data
P0->P2;P4->P6
t startup ( n / 2)t data
P0->P4
Merge Sort
Communication
tcomm  2(t startup  (n / 2)tdata  t startup  (n / 4)tdata  tstartup  (n / 8)tdata  ....)
tcomm  2(log p)t startup  2ntdata
Merge Sort
Computation
The parallel computational time complexity is O(p) using p processors
and one number in each processor
t comp  1
P0;P2;P4;P6
t comp  3
P0;P4
t comp  7
P0
log p
tcomp   (2i  1)
i 1
Quick Sort
quicksort(list, start, end)
{
if(start < end)
partition(list, start, end pivot);
quicksort(list, start, pivot-1);
quicksort(list, pivot-1, end);
}
Quick Sort
Quick Sort
Quick Sort
Computation
tcomp  n  n / 2  n / 4  n / 8  ....  2n
Communication
tcomm  (t startup  (n / 2)tdata )  (t startup  (n / 4)tdata )  (t startup  (n / 8)tdata )  ...
 (log p)t startup  ntdata
Quick Sort
Implementation
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)
Quick Sort on Hypercube
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.
Quick Sort on Hypercube
Quick Sort on Hypercube
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.
Quick Sort on Hypercube
Quick Sort on Hypercube
Computation-Pivot Selection
O(1) : the (n/2p)th number
d 1
 (d  i ) 
i 0
Communication-Pivot broadcast
d ( d  1)
2
d ( d  1)
(t startup  t data )
2
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
t startup 
x
t data
2
Computation-Data Merge
to merge two sorted lists into one sorted list requires x steps
if the biggest list has x numbers
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
Odd-even Merge Sort
Odd-even 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
Bitonic Merge Sort
Bitonic 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
Bitonic Merge Sort
Bitonic Merge Sort