1 | // Copyright (C) 2012 Vicente Botet |
2 | // |
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
5 | |
6 | #include <boost/config.hpp> |
7 | #if ! defined BOOST_NO_CXX11_DECLTYPE |
8 | #define BOOST_RESULT_OF_USE_DECLTYPE |
9 | #endif |
10 | |
11 | #define BOOST_THREAD_VERSION 4 |
12 | #define BOOST_THREAD_PROVIDES_EXECUTORS |
13 | |
14 | #include <boost/thread/experimental/task_region.hpp> |
15 | #include <iostream> |
16 | |
17 | #if ! defined BOOST_NO_CXX11_RANGE_BASED_FOR && ! defined BOOST_NO_CXX11_LAMBDAS |
18 | |
19 | int fib_task_region(int n) |
20 | { |
21 | using boost::experimental::parallel::task_region; |
22 | using boost::experimental::parallel::task_region_handle; |
23 | |
24 | if (n == 0) return 0; |
25 | if (n == 1) return 1; |
26 | |
27 | int n1; |
28 | int n2; |
29 | |
30 | task_region(f: [&](task_region_handle& trh) |
31 | { |
32 | trh.run(f: [&] |
33 | { |
34 | n1 = fib_task_region(n: n - 1); |
35 | }); |
36 | |
37 | n2 = fib_task_region(n: n - 2); |
38 | }); |
39 | |
40 | return n1 + n2; |
41 | } |
42 | |
43 | #if defined BOOST_THREAD_PROVIDES_EXECUTORS |
44 | template <class Ex> |
45 | int fib_task_region_gen( Ex& ex, int n) |
46 | { |
47 | using boost::experimental::parallel::task_region; |
48 | using boost::experimental::parallel::task_region_handle_gen; |
49 | |
50 | if (n == 0) return 0; |
51 | if (n == 1) return 1; |
52 | |
53 | int n1; |
54 | int n2; |
55 | |
56 | task_region(ex, [&](task_region_handle_gen<Ex>& trh) |
57 | { |
58 | trh.run([&] |
59 | { |
60 | n1 = fib_task_region(n: n - 1); |
61 | }); |
62 | |
63 | n2 = fib_task_region(n: n - 2); |
64 | }); |
65 | |
66 | return n1 + n2; |
67 | } |
68 | #endif |
69 | |
70 | int main() |
71 | { |
72 | for (int i = 0; i<10; ++i) { |
73 | std::cout << fib_task_region(n: i) << " " ; |
74 | } |
75 | std::cout << std::endl; |
76 | |
77 | #if defined BOOST_THREAD_PROVIDES_EXECUTORS |
78 | boost::basic_thread_pool tp; |
79 | for (int i = 0; i<10; ++i) { |
80 | std::cout << fib_task_region_gen(ex&: tp,n: i) << " " ; |
81 | } |
82 | std::cout << std::endl; |
83 | #endif |
84 | return 0; |
85 | } |
86 | #else |
87 | int main() |
88 | { |
89 | return 0; |
90 | } |
91 | #endif |
92 | |