Parallel Algorithms Unit 3 by Dr. Choudhary Ravi Singh
Parallel Algorithms Unit 3 by Dr. Choudhary Ravi Singh
The key component of these sorting networks is a comparator. A comparator is a device with
two input values x and y and two outputs x1 and y1. For an increasing comparator
x1=min(x,y) and y1=max(x,y).
x x1=min(x,y)
Comparator
y y1=max(x,y)
x + x1=min(x,y)
y + y1=max(x,y
A sorting network consists of two items: comparators and wires. Each wire carries with it a
value, and each comparator has two input wires, and two output wires. When two values
enter a comparator, the comparator emits the lower value from the top wire, and the higher
value from the bottom wire. A network of wires and comparators that will correctly sort all
possible inputs into ascending order is called a sorting network.
A sorting network consists of two items: comparators and wires. Each wire carries with it a
value, and each comparator has two input wires, and two output wires. When two values
enter a comparator, the comparator emits the lower value from the top wire, and the higher
value from the bottom wire. A network of wires and comparators that will correctly sort all
possible inputs into ascending order is called a sorting network.
The full operation of a simple sorting network is shown below. It is easy to see why this
sorting network will correctly sort the inputs; note that the first four comparators will "sink"
the largest value to the bottom and "float" the smallest value to the top. The final comparator
simply sorts out the middle two wires.
A simple sorting network consisting of four wires
and five connectors
A sorting network constructed recursively that first A sorting network constructed recursively that first sorts
sinks the largest value to the bottom and then sorts the first n wires, and then inserts the remaining value.
the remaining wires. Based on bubble sort Based on insertion sort
The structures of these two sorting networks are very similar. A construction of the two
different variants, which collapses together comparators that can be performed
simultaneously shows that, in fact, they are identical.
Depth= 2n-3=2*6-3=9
It will return largest of n value. It will return largest of n-1 values and so on.
Efficient Networks:
The insertion network has a large depth of O(n) making it impractical. There are simple
networks which achieve depth O((log n)2) (hence size O(n (log n)2)), such as Batcher odd–
even mergesort, bitonic sort, Shell sort, and the Pairwise sorting network. These networks are
often used in practice.
t=0
t=1
t=2
1. n=length(A)
2. for(t=0 to n/2 -2) { // n/2-1 times Even-Odd comparison are
performed
3. for(i=0 to n/2-1) {
4. Compare-Exchange(A[2i], A[2i+1]) } // Even Comparison
5. For(j=1 to n/2-1) {
6. Compare-Exchange(A[2j-1], A[2j]) } } // Odd Comparison
Algorithm Even-Odd Transposition using Merge Split Procedure:
1. Divide n element sequence among p processor. Each of p processor has n/p element in
its subsequence.
2. Apply Quick Sort on each subsequence in parallel.
3. Now each even numbered processor Pi merges the two sorted subsequences Si and Si
1
+1 into a sorted sequence Si . Then all odd numbered processors performed the same
process. This process is continuous until we get desired output.
Example:
P0 P1 P2 P3
{8,2,5} {10,1,7} {3,12,6} {11,4,9} Each processor applies Quick Sort on its subse
Subsequence
Even index
// Apply processors
Even perform merge operation
transposition
(P1 , P2)
//Even index
Apply processors
Even perform merge operation
transposition
1. position[0,1,2,…….,n-1], sorted[0,1,2,------n-1]
2. spawn Pi, j where 0<=i<=n-1 and 0<=j<=n-1
3. for all Pi, j 0<=i<=n-1 and 0<=j<=n-1 // determine position[i] for
element A[i]
4. position[i]=0;
5. if{ A[i]>A[j] or (A[i]=A[j]) and i>j) }then
6. position[i]=1
7. end if
8. end for
9. for all Pi, 0 where 1<=i<=n-1 // Inserting elements in sorted array from
array A
10. do sorted [position[i]]=A[i]
11. end for
12. stop
Example:
1. Distribute the given elements evenly among p processors and sort the subsequences
using QuickSort.
2. Now p sorted subsequences are merged pairwise, simultaneously using the procedure
CRCW Merge. The resulting subsequences are again merged pairwise and the process
continues until one sorted sequence of length n is obtained.
Example:
1 1 1 1 1 1 1 1
A 1 =A1 ,P1 =P1 A 2 =A2 ,P2 =P2 A 3 =A3 ,P3 =P3 A 4 =A4 ,P4 =P4
A13={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
Algorithm:
3, 5, 8, 9, 7, 4, 2, 1
3, 5, 8, 9, 7, 4, 2, 1
3 5 8 9 10 12 14 20 95 90 60 40 35 23 18 0
16 14 34 10 12 32 25 20 38 36 26 13 8 40 24 28
First four elements are in increasing order and next four are in
Bitonic Bitonic Bitonic
10 10 34 16
12 12 32 20
in decreasing order
32 32 25 25
25 25 20 32
20 20 12 34
38 36 13 40
36 38 26 38
26 26 36 36
decreasing order
13 13 38 28
8 8 40 26
40 40 28 24
24 28 24 13
28 24 8 8
The entire array a[ ] can be sorted in increasing order by applying following procedure.
Let array a={10,12,14,16,20,25,32,34,8,13,24,26,28,36,38,40}
Array
a= 10 12 14 16 20 25 32 34 8 13 24 26 28 36 38 40
ae[] ao[]
10 14 20 32 8 24 28 38 12 16 25 34 13 26 36 40
ae[] ao[] ao[]
10 20 8 28 14 32 24 38 12 16 25 34 13 26 36 40
ae[] ao[] ao[] ao[]
10 8 20 28 14 32 24 38 12 16 25 34 13 26 36 40
merge ao[] ao[]
10 8 20 28 14 32 24 38 12 16 25 34 13 26 36 40
swap 1 and 2 if required ao[] ao[]
8 10 20 28 14 32 24 38 12 16 25 34 13 26 36 40
ae[] ao[] ao[]
8 10 20 28 14 24 32 38 12 16 25 34 13 26 36 40
merge ao[]
8 10 20 28 14 24 32 38 12 16 25 34 13 26 36 40
swap 1 and 2 if
required ao[]
8 10 20 28 14 24 32 38 12 16 25 34 13 26 36 40
first merge and swap 1 and 2 if required ao[]
8 10 14 20 24 28 32 38 12 25 13 36 16 34 26 40
ae[] ao[]
8 10 14 20 24 28 32 38 12 13 16 26 25 36 34 40
ae[] ao[] ao[]
8 10 14 20 24 28 32 38 12 16 13 26 25 36 34 40
first merge and swap
1 and 2 if required ao[]
8 10 14 20 24 28 32 38 12 13 16 26 25 36 34 40
ae[] ao[]
8 10 14 20 24 28 32 38 12 13 16 26 25 34 36 40
merge
8 10 14 20 24 28 32 38 12 13 16 26 25 34 36 40
swap 1 and 2 if
required
8 10 14 20 24 28 32 38 12 13 16 26 25 34 36 40
8 10 14 20 24 28 32 38 12 13 16 26 25 36 34 40
merge
8 10 12 13 14 16 20 24 25 26 28 32 34 36 38 40
swap 1 and 2 if required
a= 8 10 12 13 14 16 20 24 25 26 28 32 34 36 38 40
Parallel Merging:
The problem of merging can be defined as:
Let A={a1,a2,….ar} and B={b1,b2,……bs} be two sorted sequence in ascending order. We
merge A and B into one sorted sequence C={c1, c2,…………cr+s}, also sorted in ascending
order such that each element of sequence C belongs to either A or B.
Example:
Let A={1,2,3,4,5} and B={0,6,8,9,10}
rank(1,A,B)=1 // number of elements less than 1 in set A and set B.
rank(2,A,B)=2, rank(3,A,B)=3, rank(4,A,B)=4, rank(5,A,B)=5, rank(0,A,B)=0
rank(6,A,B)=6, rank(8,A,B)=7, rank(9,A,B)=8, rank(10,A,B)=9
Now we insert these elements into one sorted sequence C in ascending order, at their ranked
positions.
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 8 9 10
Merged array C
Example:
Using CREW/EREW merge procedure merge the following two subsequences
A={2,3,4,6,11,12,13,15,16,20,22,24} and
B={1,5,7,8,9,10,14,17,18,19,21,23} and consider number of processors are p=3
//First we divide these n elements among p processors such that each processors has n/p
distinct elements. After that we sort p subsequences in ascending order and then we apply
ranked procedure for merging these subsequences in parallel as described below.
p1 p2 p3
A1={2,3,4,6,11,12,13,15} A2={16,20,22,24, 1,5,7,8} A3={9,10,14,17,18,19,21,23}
sort A1 in ascending order sort A2 in ascending order sort A3 in ascending order
A1={2,3,4,6,11,12,13,15} A2={1,5,7,8,16,20,22,24} A3={9,10,14,17,18,19,21,23}
rank(2,A1,A2,A3)=1 rank(16,A1,A2,A3)=15 rank(9,A1,A2,A3)=8
rank(3,A1,A2,A3)=2 rank(20,A1,A2,A3)=19 rank(10,A1,A2,A3)=9
rank(4,A1,A2,A3)=3 rank(22,A1,A2,A3)=21 rank(14,A1,A2,A3)=13
rank(6,A1,A2,A3)=5 rank(24,A1,A2,A3)=23 rank(17,A1,A2,A3)=16
rank(11,A1,A2,A3)=10 rank(1,A1,A2,A3)=0 rank(18,A1,A2,A3)=17
rank(12,A1,A2,A3)=11 rank(5,A1,A2,A3)=4 rank(19,A1,A2,A3)=18
rank(13,A1,A2,A3)=12 rank(7,A1,A2,A3)=6 rank(21,A1,A2,A3)=20
rank(15,A1,A2,A3)=14 rank(8,A1,A2,A3)=7 rank(23,A1,A2,A3)=22
now we insert above values in array C at their ranked positions
C={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}
Finding Median of Two Sorted Sequences:
The median of two sorted sequences A and B is a pair (ax, by) where ax belongs to A and by
belongs to B.
The median pair (ax, by) satisfies the following properties:
1. Either ax or by is larger than {ceiling (m/2) -1} elements and smaller than floor
(m/2) elements.
2. If ax satisfies property 1 then
a. Either by is the largest element in B smaller than or equal to ax.
b. Or by is the smallest element in B larger than or equal to ax.
3. if by satisfies property 1 then
a. Either ax is the largest element in A smaller than or equal to by.
b. Or ax is the smallest element in A larger than or equal to by.
Algorithm:
Let us consider A(a1,a2,……….,ar) and B=(b1,b2,………………..,bs)
Median(A,B)
1. na // total number of elements in set A
2. nb // total number of elements in set B
3. lowa // low index value of set A
4. higha // high index value of set A
5. lowb // low index value of set B
6. highb // high index value of set B
7. set lowa=1, lowb=1, higha=r, highb=s, na=r, nb=s
8. while na>1 and nb>1{
9. u=lowa+ceiling{(higha-lowa-1)/2}
10. v= lowb+ceiling{(highb-lowb-1)/2}
11. w=min{floor(na/2),floor(nb/2)}
12. na=na-w
13. nb=nb-w
14. if au>=bv //element au is greater than or equal to element bv
15. {
16. higha=higha-w, lowb=lowb+w}
17. Else{ highb=highb-w, lowa=lowa+w}} //end of while loop.
18. return index value of (au-1,au,au+1) x (bv-1,bv,bv+1) satisfying properties of median pair.
Example:
Problem
Defined tasks
partition
Tasks
communiction
communication
Agglomerated tasks
agglomeration
Processor 1 Processors 2
Algorithm:
Let us consider a 2 dimensional mesh having n rows and m columns.
for i=1,..., 2(log n) + 1 do sequentially
if (i is odd)
then SORT_ALL_ROWS_SNAKELIKE // ( Figure, Phase 1,3,5)
else SORT_ALL_COLUMNS //( Figure, Phase 2,4)
Example:
Illustrate the operations of Shearsort algorithm on the following mesh.
// here number of rows n=4, so for loop is continue from i=1 to 2(log4) +1 // log4=2
// if i is odd then we sort odd rows from right to left ( ) and even rows from left
to right ( ).
// if i is even then we sort all columns from top to bottom.