-
Notifications
You must be signed in to change notification settings - Fork 1
/
main2.1.cpp
59 lines (48 loc) · 1.36 KB
/
main2.1.cpp
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
#include <iostream>
#include "taskruntime2.h"
long serial_fib(const long n) {
if (n < 2)
return n;
return serial_fib(n - 1) + serial_fib(n - 2);
}
struct FibContinuation : public silk::demo_runtime_2::task {
long* const sum;
long x, y;
FibContinuation(long* sum_) : sum(sum_) {
}
task* execute() override {
*sum = x + y;
return nullptr;
}
};
struct FibTask : public silk::demo_runtime_2::task {
long n;
long* sum;
FibTask(long n_, long* sum_) : n(n_), sum(sum_) {
}
task* execute() override {
if (n<14) {
*sum = serial_fib(n);
return nullptr;
} else {
FibContinuation& c = *new(allocate_continuation()) FibContinuation(sum);
FibTask& a = *new(c.allocate_child()) FibTask(n - 2, &c.x);
n = n - 1;
sum = &c.y;
recycle_as_child_of(c);
c.set_ref_count(2);
spawn(a);
return this;
}
}
};
int main() {
silk::init_pool(silk::demo_runtime_2::schedule, silk::demo_runtime_2::makeuwcontext);
const auto start = std::chrono::high_resolution_clock::now();
long sum;
silk::demo_runtime_2::spawn( *new FibTask(45, &sum) );
silk::join_main_thread_2_pool(silk::demo_runtime_2::schedule);
const auto end = std::chrono::high_resolution_clock::now();
std::cout << "result: " << sum << " time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << std::endl;
return 0;
}