Transcript 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
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.