Lecture3

Report
Lecture 3
Tuesday, February 10, 2015
[With the help of free online resources]
Divide and Conquer
Divide and Conquer?
• Split the entire range into smaller manageable parts.
• Solve each part separately.
• Combine the results to get result for the original larger problem.
Array of size 
Divide
among 
cores
and
solve
/
/
/
/
/
/
Array of size 

  = 0 +
 cos
=1
/


/
/
/
Merge
Divide and Conquer
Recursive Divide and Conquer?
• Doing Divide and Conquer recursively.
• i.e., First divide into two parts, then divide each of those two parts into two more smaller
parts and so on until reach a small enough size.
• cilk_for is also implemented as a recursive divide and conquer.
• Splits the loop range in a divide and conquer way
Array of size 
Split
into
2 parts


Array of size 2

Array of size 4
log 
Array of size 2

Array of size
Array of size 4

4
Array of size

4
………
Array of
size 2
Array of
size 2
Array of
size 2
Array of
size 2
Array of
size 2
Array of
size 2
Array of
size 2
Array of
size 2
……… /2
of them
Computing sum of  numbers
// Sequential sum of n numbers
// Parallel sum of n numbers
function sum((1. . ))
function sum((1. . ))
if ( = 1) return (1)
if ( = 1) return (1)
else
else

2
x = sum((1. . ))


2
x = cilk_spawn sum((1. . ))

y = sum((2 + 1. . ))
y = sum((2 + 1. . ))
return ( + )
cilk_sync
return ( + )

+ log 

   : 1 = ()
:  :  = 
Homework: Implement it and show the performance.
Computing sum of 8 numbers
Merge Sort:
Sort n numbers recursively using
divide and conquer.
 , ,  // ℎ   [ … ]
1.  ( < ) ℎ
3. (, , )
4. (,  + 1, )
2.  = ( + ) / 2
5. (, , , )
(, , ) //sort the elements in A[p…r]
1.   <  ℎ
2. cilk_spawn  , , 
4. ,  + 1, 
5. = ( + ) / 2
6. , , , 
7.
Picture: Wikipedia
Divide and Conquer
Fibonacci number:
In mathematics, the Fibonacci numbers or Fibonacci sequence are the
numbers in the following integer sequence
1 1 2 3 5 8 13 21 ….
int fib(int n)
F(n) = F(n - 1) + F (n - 2) int fib(int n)
{
{
}
if (n < 2) return n;
int x = cilk_spawn fib(n-1);
int y = fib(n-2);
cilk_sync;
return x + y;
if (n < 2) return n;
int x = fib(n-1);
int y = fib(n-2);
return x + y;
}
Matrix-multiplication
Iterative-MM ( Z, X, Y ) // X, Y, Z are n × n matrices,
// where n is a positive integer
1. for i ← 1 to n do
3.
Z[ i ][ j ] ← 0
4.
for k ← 1 to n do
2.
for j ← 1 to n do
5.
Z[ i ][ j ] ← Z[ i ][ j ] + (X[ i ][ k ] * Y[ k ][ j ])
Par-Iterative-MM ( Z, X, Y ) // X, Y, Z are n × n matrices,
// where n is a positive integer
1. parallel for i ← 1 to n do
3.
Z[ i ][ j ] ← 0
4.
for k ← 1 to n do
2.
parallel for j ← 1 to n do
5.
Z[ i ][ j ] ← Z[ i ][ j ] + X[ i ][ k ] ⋅ Y[ k ][ j ]
Source: http://www.mathsisfun.com/algebra/matrix-multiplying.html
http://algebra.nipissingu.ca/tutorials/matrices.html
Links for c/c++ tutorial
• https://www.youtube.com/watch?v=rk2fK2IIiiQ
• https://www.youtube.com/watch?v=Rub-JsjMhWY
• https://www.youtube.com/watch?v=S3t-5UtvDN0
• https://www.youtube.com/watch?v=KNFv4DZ4Mp4
• https://www.youtube.com/watch?v=c5gg9F8h8Fw&list=PLfVsf4Bjg79
CZ5kHTiQHcm-l2q8j06ofd (probably by this time you know all)
• Source: google!
Group Homework1
[1] . Teach yourself c/c++
• Watch these online videos and other resources freely available in Google
and write a 10 page group report on c/c++ basic data types,
for loops, input, output, arrays and metrics with dynamic allocation,
and function.
•
•
•
•
•
https://www.youtube.com/watch?v=rk2fK2IIiiQ
https://www.youtube.com/watch?v=Rub-JsjMhWY
https://www.youtube.com/watch?v=S3t-5UtvDN0
https://www.youtube.com/watch?v=KNFv4DZ4Mp4
https://www.youtube.com/watch?v=c5gg9F8h8Fw&list=PLfVsf4Bjg79CZ5kHTiQHcml2q8j06ofd (probably by this time you know all)
[2]. Write all the codes discussed in the class so far. Compile them and generate
output. Vary n and take the time using the cilktime function.
Report the change in running time with n.
Fix n to the largest and vary # of cores and report the running time.

similar documents