-
Notifications
You must be signed in to change notification settings - Fork 0
/
fib.cc
68 lines (55 loc) · 1.62 KB
/
fib.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
*
* Copyright (C) 2013 Virginia Tech
* See the file license.txt for copying permission.
*
* Justus Calvin
* Department of Chemistry, Virginia Tech
*
* fib.cc
* Sep 20, 2013
*
*/
#include <iostream>
#include <world/world.h>
long serial_fib(long n) {
if( n<2 )
return n;
else
return serial_fib(n - 1) + serial_fib(n - 2);
}
long sum(long left, long right) {
return left + right;
}
madness::Future<long> task_fib(madness::World* world,
long n, const long cutoff) {
if(n < cutoff ) {
return madness::Future<long>(serial_fib(n));
} else {
madness::Future<long> x =
world->taskq.add(& task_fib, world, n - 1,
cutoff, madness::TaskAttributes::hipri());
madness::Future<long> y = task_fib(world, n - 2, cutoff);
return world->taskq.add(& sum, x, y,
madness::TaskAttributes::hipri());
}
}
long nth = 46;
int main(int argc, char** argv) {
madness::World& world = madness::initialize(argc,argv);
// Serial calculation
const double serial_start = madness::wall_time();
long fib1 = serial_fib(nth);
const double serial_time = madness::wall_time() - serial_start;
std::cout << nth << "-th Fibonacci is " << fib1 << "\n";
std::cout << "Serial time = " << serial_time << "\n";
// Parallel calculation
const double parallel_start = madness::wall_time();
madness::Future<long> fib2 = task_fib(& world, nth, 30);
fib2.get();
const double parallel_time = madness::wall_time() - parallel_start;
std::cout << "Parallel time = " << parallel_time
<< "\nSpeedup = " << serial_time / parallel_time << "\n";
madness::finalize();
return 0;
}