### Lecture3

```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
CZ5kHTiQHcm-l2q8j06ofd (probably by this time you know all)
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.
•
•
•
•
•