CO322: DS & A
Parallel Computing
Dhammika Elkaduwe
Department of Computer Engineering
Faculty of Engineering
University of Peradeniya
Dhammika Elkaduwe CO322: DS & A Parallel Computing
Why?
World is parallel!
Your phone has few cores in it!!
Can we make use of more than one processing element?
Depends on the algorithm
I For some algorithms parallel processes are useless
I For some others you can make use of them
Dhammika Elkaduwe CO322: DS & A Parallel Computing
Why?
World is parallel!
Your phone has few cores in it!!
Can we make use of more than one processing element?
Depends on the algorithm
I For some algorithms parallel processes are useless
I For some others you can make use of them
Dhammika Elkaduwe CO322: DS & A Parallel Computing
Example algorithm: Bubble sort Java code
public void bubble_sort(int [] data) {
int i,j;
for(i=0; i < data.length; i++) {
for(j = data.length-1; j > i; j--) {
if(data[j] < data[j-1]) {
int tmp = data[j];
data[j] = data[j-1];
data[j-1] = tmp;
}
}
}
}
Can you make this implementation parallel?
No. Every loop depends on the previous one(s)!
Dhammika Elkaduwe CO322: DS & A Parallel Computing
Example algorithm: Bubble sort Java code
public void bubble_sort(int [] data) {
int i,j;
for(i=0; i < data.length; i++) {
for(j = data.length-1; j > i; j--) {
if(data[j] < data[j-1]) {
int tmp = data[j];
data[j] = data[j-1];
data[j-1] = tmp;
}
}
}
}
Can you make this implementation parallel?
No. Every loop depends on the previous one(s)!
Dhammika Elkaduwe CO322: DS & A Parallel Computing
Example algorithm: Matrix multiplication
For two matrices Aam and Bmb , if C = A × B then
m
P
Cij = Aik ∗ Bkj
k=1
static int [][] multiply(int [][]A, int [][]B) {
/* implement me!
* What is/are the preconditions? */
}
Dhammika Elkaduwe CO322: DS & A Parallel Computing
Example algorithm: Matrix multiplication Java code
static int [][] multiply(int [][]A, int [][]B) {
int ai = A.length, aj=A[0].length, bi=B.length,
bj=B[0].length;
int C[][] = new int [ai][bj];
int i, j, k, sum;
if(aj != bj) /* may be panic */ return C;
for(i=0; i < ai; i++) {
for(j=0; j < bj; j++) {
for(k=0, sum=0; k < aj; k++) sum += (A[i][k] *
B[k][j]);
C[i][j] = sum;
}/* for{i */ } /* for(j= */
return C;
}
What is the run-time complexity? (Aa×n , Bn×b )
Can you make it parallel? Why?
Dhammika Elkaduwe CO322: DS & A Parallel Computing
Example algorithm: Odd-Even sort
Basic idea:
I Similar to bubble sort
I Can be implemented in parallel systems (you tell me why :))
I Compare all odd ones with adjacent element, repeat for even
elements.
I Repeat until array is sorted.
Dhammika Elkaduwe CO322: DS & A Parallel Computing
Example algorithm: Odd-Even sort Java code
static void oddEvenSort(int [] data) {
boolean sorted = false;
while(!sorted) {
sorted = true;
for(int i=0; i< data.length-1; i +=2) {
I What is the
if(data[i] > data[i+1]) { worst time
swap(data, i, i+1); complexity?
sorted = false; I What is the
} /* if */ } /* for */ best time
complexity?
for(int i=1; i< data.length-1; i +=2) {
if(data[i] > data[i+1]) { I Can use run
swap(data, i, i+1); this parallel?
sorted = false; Why?
} /* if */ } /* for */
}
}
Dhammika Elkaduwe CO322: DS & A Parallel Computing
Exercises
1. Out of bubble, insert, selection, quick and merge sort which
ones can be implemented to run parallelly?
2. Can you implement matrix multiplication on parallel system?
2.1 if so, what is the run time complexity on a system with 2 CUP
cores for Aa×n , Bn×b ?
2.2 What if I have m > n number of CPUs? will it help?
Dhammika Elkaduwe CO322: DS & A Parallel Computing
Take home point
I Parallel systems depends on algorithms
I Some algorithms can make use of parallel cores while other
cannot
I Many cores does not make sense without good algorithms
I Increase the speed by overlapping executions
Dhammika Elkaduwe CO322: DS & A Parallel Computing