fib_task_region.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include <boost/config.hpp>
  6. #if ! defined BOOST_NO_CXX11_DECLTYPE
  7. #define BOOST_RESULT_OF_USE_DECLTYPE
  8. #endif
  9. #define BOOST_THREAD_VERSION 4
  10. #define BOOST_THREAD_PROVIDES_EXECUTORS
  11. #include <boost/thread/experimental/task_region.hpp>
  12. #include <iostream>
  13. #if ! defined BOOST_NO_CXX11_RANGE_BASED_FOR && ! defined BOOST_NO_CXX11_LAMBDAS
  14. int fib_task_region(int n)
  15. {
  16. using boost::experimental::parallel::task_region;
  17. using boost::experimental::parallel::task_region_handle;
  18. if (n == 0) return 0;
  19. if (n == 1) return 1;
  20. int n1;
  21. int n2;
  22. task_region([&](task_region_handle& trh)
  23. {
  24. trh.run([&]
  25. {
  26. n1 = fib_task_region(n - 1);
  27. });
  28. n2 = fib_task_region(n - 2);
  29. });
  30. return n1 + n2;
  31. }
  32. #if defined BOOST_THREAD_PROVIDES_EXECUTORS
  33. template <class Ex>
  34. int fib_task_region_gen( Ex& ex, int n)
  35. {
  36. using boost::experimental::parallel::task_region;
  37. using boost::experimental::parallel::task_region_handle_gen;
  38. if (n == 0) return 0;
  39. if (n == 1) return 1;
  40. int n1;
  41. int n2;
  42. task_region(ex, [&](task_region_handle_gen<Ex>& trh)
  43. {
  44. trh.run([&]
  45. {
  46. n1 = fib_task_region(n - 1);
  47. });
  48. n2 = fib_task_region(n - 2);
  49. });
  50. return n1 + n2;
  51. }
  52. #endif
  53. int main()
  54. {
  55. for (int i = 0; i<10; ++i) {
  56. std::cout << fib_task_region(i) << " ";
  57. }
  58. std::cout << std::endl;
  59. #if defined BOOST_THREAD_PROVIDES_EXECUTORS
  60. boost::basic_thread_pool tp;
  61. for (int i = 0; i<10; ++i) {
  62. std::cout << fib_task_region_gen(tp,i) << " ";
  63. }
  64. std::cout << std::endl;
  65. #endif
  66. return 0;
  67. }
  68. #else
  69. int main()
  70. {
  71. return 0;
  72. }
  73. #endif