You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

371 lines
14 KiB

  1. /*
  2. Copyright 2005-2013 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks.
  4. Threading Building Blocks is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public License
  6. version 2 as published by the Free Software Foundation.
  7. Threading Building Blocks is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Threading Building Blocks; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. As a special exception, you may use this file as part of a free software
  15. library without restriction. Specifically, if other files instantiate
  16. templates or use macros or inline functions from this file, or you compile
  17. this file and link it with other files to produce an executable, this
  18. file does not by itself cause the resulting executable to be covered by
  19. the GNU General Public License. This exception does not however
  20. invalidate any other reasons why the executable file might be covered by
  21. the GNU General Public License.
  22. */
  23. #ifndef __TBB_parallel_invoke_H
  24. #define __TBB_parallel_invoke_H
  25. #include "task.h"
  26. namespace tbb {
  27. #if !__TBB_TASK_GROUP_CONTEXT
  28. /** Dummy to avoid cluttering the bulk of the header with enormous amount of ifdefs. **/
  29. struct task_group_context {};
  30. #endif /* __TBB_TASK_GROUP_CONTEXT */
  31. //! @cond INTERNAL
  32. namespace internal {
  33. // Simple task object, executing user method
  34. template<typename function>
  35. class function_invoker : public task{
  36. public:
  37. function_invoker(const function& _function) : my_function(_function) {}
  38. private:
  39. const function &my_function;
  40. /*override*/
  41. task* execute()
  42. {
  43. my_function();
  44. return NULL;
  45. }
  46. };
  47. // The class spawns two or three child tasks
  48. template <size_t N, typename function1, typename function2, typename function3>
  49. class spawner : public task {
  50. private:
  51. const function1& my_func1;
  52. const function2& my_func2;
  53. const function3& my_func3;
  54. bool is_recycled;
  55. task* execute (){
  56. if(is_recycled){
  57. return NULL;
  58. }else{
  59. __TBB_ASSERT(N==2 || N==3, "Number of arguments passed to spawner is wrong");
  60. set_ref_count(N);
  61. recycle_as_safe_continuation();
  62. internal::function_invoker<function2>* invoker2 = new (allocate_child()) internal::function_invoker<function2>(my_func2);
  63. __TBB_ASSERT(invoker2, "Child task allocation failed");
  64. spawn(*invoker2);
  65. size_t n = N; // To prevent compiler warnings
  66. if (n>2) {
  67. internal::function_invoker<function3>* invoker3 = new (allocate_child()) internal::function_invoker<function3>(my_func3);
  68. __TBB_ASSERT(invoker3, "Child task allocation failed");
  69. spawn(*invoker3);
  70. }
  71. my_func1();
  72. is_recycled = true;
  73. return NULL;
  74. }
  75. } // execute
  76. public:
  77. spawner(const function1& _func1, const function2& _func2, const function3& _func3) : my_func1(_func1), my_func2(_func2), my_func3(_func3), is_recycled(false) {}
  78. };
  79. // Creates and spawns child tasks
  80. class parallel_invoke_helper : public empty_task {
  81. public:
  82. // Dummy functor class
  83. class parallel_invoke_noop {
  84. public:
  85. void operator() () const {}
  86. };
  87. // Creates a helper object with user-defined number of children expected
  88. parallel_invoke_helper(int number_of_children)
  89. {
  90. set_ref_count(number_of_children + 1);
  91. }
  92. // Adds child task and spawns it
  93. template <typename function>
  94. void add_child (const function &_func)
  95. {
  96. internal::function_invoker<function>* invoker = new (allocate_child()) internal::function_invoker<function>(_func);
  97. __TBB_ASSERT(invoker, "Child task allocation failed");
  98. spawn(*invoker);
  99. }
  100. // Adds a task with multiple child tasks and spawns it
  101. // two arguments
  102. template <typename function1, typename function2>
  103. void add_children (const function1& _func1, const function2& _func2)
  104. {
  105. // The third argument is dummy, it is ignored actually.
  106. parallel_invoke_noop noop;
  107. internal::spawner<2, function1, function2, parallel_invoke_noop>& sub_root = *new(allocate_child())internal::spawner<2, function1, function2, parallel_invoke_noop>(_func1, _func2, noop);
  108. spawn(sub_root);
  109. }
  110. // three arguments
  111. template <typename function1, typename function2, typename function3>
  112. void add_children (const function1& _func1, const function2& _func2, const function3& _func3)
  113. {
  114. internal::spawner<3, function1, function2, function3>& sub_root = *new(allocate_child())internal::spawner<3, function1, function2, function3>(_func1, _func2, _func3);
  115. spawn(sub_root);
  116. }
  117. // Waits for all child tasks
  118. template <typename F0>
  119. void run_and_finish(const F0& f0)
  120. {
  121. internal::function_invoker<F0>* invoker = new (allocate_child()) internal::function_invoker<F0>(f0);
  122. __TBB_ASSERT(invoker, "Child task allocation failed");
  123. spawn_and_wait_for_all(*invoker);
  124. }
  125. };
  126. // The class destroys root if exception occurred as well as in normal case
  127. class parallel_invoke_cleaner: internal::no_copy {
  128. public:
  129. #if __TBB_TASK_GROUP_CONTEXT
  130. parallel_invoke_cleaner(int number_of_children, tbb::task_group_context& context)
  131. : root(*new(task::allocate_root(context)) internal::parallel_invoke_helper(number_of_children))
  132. #else
  133. parallel_invoke_cleaner(int number_of_children, tbb::task_group_context&)
  134. : root(*new(task::allocate_root()) internal::parallel_invoke_helper(number_of_children))
  135. #endif /* !__TBB_TASK_GROUP_CONTEXT */
  136. {}
  137. ~parallel_invoke_cleaner(){
  138. root.destroy(root);
  139. }
  140. internal::parallel_invoke_helper& root;
  141. };
  142. } // namespace internal
  143. //! @endcond
  144. /** \name parallel_invoke
  145. **/
  146. //@{
  147. //! Executes a list of tasks in parallel and waits for all tasks to complete.
  148. /** @ingroup algorithms */
  149. // parallel_invoke with user-defined context
  150. // two arguments
  151. template<typename F0, typename F1 >
  152. void parallel_invoke(const F0& f0, const F1& f1, tbb::task_group_context& context) {
  153. internal::parallel_invoke_cleaner cleaner(2, context);
  154. internal::parallel_invoke_helper& root = cleaner.root;
  155. root.add_child(f1);
  156. root.run_and_finish(f0);
  157. }
  158. // three arguments
  159. template<typename F0, typename F1, typename F2 >
  160. void parallel_invoke(const F0& f0, const F1& f1, const F2& f2, tbb::task_group_context& context) {
  161. internal::parallel_invoke_cleaner cleaner(3, context);
  162. internal::parallel_invoke_helper& root = cleaner.root;
  163. root.add_child(f2);
  164. root.add_child(f1);
  165. root.run_and_finish(f0);
  166. }
  167. // four arguments
  168. template<typename F0, typename F1, typename F2, typename F3>
  169. void parallel_invoke(const F0& f0, const F1& f1, const F2& f2, const F3& f3,
  170. tbb::task_group_context& context)
  171. {
  172. internal::parallel_invoke_cleaner cleaner(4, context);
  173. internal::parallel_invoke_helper& root = cleaner.root;
  174. root.add_child(f3);
  175. root.add_child(f2);
  176. root.add_child(f1);
  177. root.run_and_finish(f0);
  178. }
  179. // five arguments
  180. template<typename F0, typename F1, typename F2, typename F3, typename F4 >
  181. void parallel_invoke(const F0& f0, const F1& f1, const F2& f2, const F3& f3, const F4& f4,
  182. tbb::task_group_context& context)
  183. {
  184. internal::parallel_invoke_cleaner cleaner(3, context);
  185. internal::parallel_invoke_helper& root = cleaner.root;
  186. root.add_children(f4, f3);
  187. root.add_children(f2, f1);
  188. root.run_and_finish(f0);
  189. }
  190. // six arguments
  191. template<typename F0, typename F1, typename F2, typename F3, typename F4, typename F5>
  192. void parallel_invoke(const F0& f0, const F1& f1, const F2& f2, const F3& f3, const F4& f4, const F5& f5,
  193. tbb::task_group_context& context)
  194. {
  195. internal::parallel_invoke_cleaner cleaner(3, context);
  196. internal::parallel_invoke_helper& root = cleaner.root;
  197. root.add_children(f5, f4, f3);
  198. root.add_children(f2, f1);
  199. root.run_and_finish(f0);
  200. }
  201. // seven arguments
  202. template<typename F0, typename F1, typename F2, typename F3, typename F4, typename F5, typename F6>
  203. void parallel_invoke(const F0& f0, const F1& f1, const F2& f2, const F3& f3, const F4& f4,
  204. const F5& f5, const F6& f6,
  205. tbb::task_group_context& context)
  206. {
  207. internal::parallel_invoke_cleaner cleaner(3, context);
  208. internal::parallel_invoke_helper& root = cleaner.root;
  209. root.add_children(f6, f5, f4);
  210. root.add_children(f3, f2, f1);
  211. root.run_and_finish(f0);
  212. }
  213. // eight arguments
  214. template<typename F0, typename F1, typename F2, typename F3, typename F4,
  215. typename F5, typename F6, typename F7>
  216. void parallel_invoke(const F0& f0, const F1& f1, const F2& f2, const F3& f3, const F4& f4,
  217. const F5& f5, const F6& f6, const F7& f7,
  218. tbb::task_group_context& context)
  219. {
  220. internal::parallel_invoke_cleaner cleaner(4, context);
  221. internal::parallel_invoke_helper& root = cleaner.root;
  222. root.add_children(f7, f6, f5);
  223. root.add_children(f4, f3);
  224. root.add_children(f2, f1);
  225. root.run_and_finish(f0);
  226. }
  227. // nine arguments
  228. template<typename F0, typename F1, typename F2, typename F3, typename F4,
  229. typename F5, typename F6, typename F7, typename F8>
  230. void parallel_invoke(const F0& f0, const F1& f1, const F2& f2, const F3& f3, const F4& f4,
  231. const F5& f5, const F6& f6, const F7& f7, const F8& f8,
  232. tbb::task_group_context& context)
  233. {
  234. internal::parallel_invoke_cleaner cleaner(4, context);
  235. internal::parallel_invoke_helper& root = cleaner.root;
  236. root.add_children(f8, f7, f6);
  237. root.add_children(f5, f4, f3);
  238. root.add_children(f2, f1);
  239. root.run_and_finish(f0);
  240. }
  241. // ten arguments
  242. template<typename F0, typename F1, typename F2, typename F3, typename F4,
  243. typename F5, typename F6, typename F7, typename F8, typename F9>
  244. void parallel_invoke(const F0& f0, const F1& f1, const F2& f2, const F3& f3, const F4& f4,
  245. const F5& f5, const F6& f6, const F7& f7, const F8& f8, const F9& f9,
  246. tbb::task_group_context& context)
  247. {
  248. internal::parallel_invoke_cleaner cleaner(4, context);
  249. internal::parallel_invoke_helper& root = cleaner.root;
  250. root.add_children(f9, f8, f7);
  251. root.add_children(f6, f5, f4);
  252. root.add_children(f3, f2, f1);
  253. root.run_and_finish(f0);
  254. }
  255. // two arguments
  256. template<typename F0, typename F1>
  257. void parallel_invoke(const F0& f0, const F1& f1) {
  258. task_group_context context;
  259. parallel_invoke<F0, F1>(f0, f1, context);
  260. }
  261. // three arguments
  262. template<typename F0, typename F1, typename F2>
  263. void parallel_invoke(const F0& f0, const F1& f1, const F2& f2) {
  264. task_group_context context;
  265. parallel_invoke<F0, F1, F2>(f0, f1, f2, context);
  266. }
  267. // four arguments
  268. template<typename F0, typename F1, typename F2, typename F3 >
  269. void parallel_invoke(const F0& f0, const F1& f1, const F2& f2, const F3& f3) {
  270. task_group_context context;
  271. parallel_invoke<F0, F1, F2, F3>(f0, f1, f2, f3, context);
  272. }
  273. // five arguments
  274. template<typename F0, typename F1, typename F2, typename F3, typename F4>
  275. void parallel_invoke(const F0& f0, const F1& f1, const F2& f2, const F3& f3, const F4& f4) {
  276. task_group_context context;
  277. parallel_invoke<F0, F1, F2, F3, F4>(f0, f1, f2, f3, f4, context);
  278. }
  279. // six arguments
  280. template<typename F0, typename F1, typename F2, typename F3, typename F4, typename F5>
  281. void parallel_invoke(const F0& f0, const F1& f1, const F2& f2, const F3& f3, const F4& f4, const F5& f5) {
  282. task_group_context context;
  283. parallel_invoke<F0, F1, F2, F3, F4, F5>(f0, f1, f2, f3, f4, f5, context);
  284. }
  285. // seven arguments
  286. template<typename F0, typename F1, typename F2, typename F3, typename F4, typename F5, typename F6>
  287. void parallel_invoke(const F0& f0, const F1& f1, const F2& f2, const F3& f3, const F4& f4,
  288. const F5& f5, const F6& f6)
  289. {
  290. task_group_context context;
  291. parallel_invoke<F0, F1, F2, F3, F4, F5, F6>(f0, f1, f2, f3, f4, f5, f6, context);
  292. }
  293. // eigth arguments
  294. template<typename F0, typename F1, typename F2, typename F3, typename F4,
  295. typename F5, typename F6, typename F7>
  296. void parallel_invoke(const F0& f0, const F1& f1, const F2& f2, const F3& f3, const F4& f4,
  297. const F5& f5, const F6& f6, const F7& f7)
  298. {
  299. task_group_context context;
  300. parallel_invoke<F0, F1, F2, F3, F4, F5, F6, F7>(f0, f1, f2, f3, f4, f5, f6, f7, context);
  301. }
  302. // nine arguments
  303. template<typename F0, typename F1, typename F2, typename F3, typename F4,
  304. typename F5, typename F6, typename F7, typename F8>
  305. void parallel_invoke(const F0& f0, const F1& f1, const F2& f2, const F3& f3, const F4& f4,
  306. const F5& f5, const F6& f6, const F7& f7, const F8& f8)
  307. {
  308. task_group_context context;
  309. parallel_invoke<F0, F1, F2, F3, F4, F5, F6, F7, F8>(f0, f1, f2, f3, f4, f5, f6, f7, f8, context);
  310. }
  311. // ten arguments
  312. template<typename F0, typename F1, typename F2, typename F3, typename F4,
  313. typename F5, typename F6, typename F7, typename F8, typename F9>
  314. void parallel_invoke(const F0& f0, const F1& f1, const F2& f2, const F3& f3, const F4& f4,
  315. const F5& f5, const F6& f6, const F7& f7, const F8& f8, const F9& f9)
  316. {
  317. task_group_context context;
  318. parallel_invoke<F0, F1, F2, F3, F4, F5, F6, F7, F8, F9>(f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, context);
  319. }
  320. //@}
  321. } // namespace
  322. #endif /* __TBB_parallel_invoke_H */