parallel_do.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __TBB_parallel_do_H
  14. #define __TBB_parallel_do_H
  15. #define __TBB_parallel_do_H_include_area
  16. #include "internal/_warning_suppress_enable_notice.h"
  17. #include "internal/_range_iterator.h"
  18. #include "internal/_template_helpers.h"
  19. #include "task.h"
  20. #include "aligned_space.h"
  21. #include <iterator>
  22. namespace tbb {
  23. namespace interface9 {
  24. //! @cond INTERNAL
  25. namespace internal {
  26. template<typename Body, typename Item> class parallel_do_feeder_impl;
  27. } // namespace internal
  28. //! @endcond
  29. //! Class the user supplied algorithm body uses to add new tasks
  30. /** \param Item Work item type **/
  31. template<typename Item>
  32. class parallel_do_feeder: ::tbb::internal::no_copy
  33. {
  34. parallel_do_feeder() {}
  35. virtual ~parallel_do_feeder () {}
  36. virtual void internal_add_copy( const Item& item ) = 0;
  37. #if __TBB_CPP11_RVALUE_REF_PRESENT
  38. virtual void internal_add_move( Item&& item ) = 0;
  39. #endif
  40. template<typename Body_, typename Item_> friend class internal::parallel_do_feeder_impl;
  41. public:
  42. //! Add a work item to a running parallel_do.
  43. void add( const Item& item ) {internal_add_copy(item);}
  44. #if __TBB_CPP11_RVALUE_REF_PRESENT
  45. void add( Item&& item ) {internal_add_move(std::move(item));}
  46. #endif
  47. };
  48. //! @cond INTERNAL
  49. namespace internal {
  50. template<typename Body> class do_group_task;
  51. //! For internal use only.
  52. /** Selects one of the two possible forms of function call member operator.
  53. @ingroup algorithms **/
  54. template<class Body, typename Item>
  55. class parallel_do_operator_selector
  56. {
  57. typedef parallel_do_feeder<Item> Feeder;
  58. template<typename A1, typename A2, typename CvItem >
  59. static void internal_call( const Body& obj, __TBB_FORWARDING_REF(A1) arg1, A2&, void (Body::*)(CvItem) const ) {
  60. obj(tbb::internal::forward<A1>(arg1));
  61. }
  62. template<typename A1, typename A2, typename CvItem >
  63. static void internal_call( const Body& obj, __TBB_FORWARDING_REF(A1) arg1, A2& arg2, void (Body::*)(CvItem, parallel_do_feeder<Item>&) const ) {
  64. obj(tbb::internal::forward<A1>(arg1), arg2);
  65. }
  66. template<typename A1, typename A2, typename CvItem >
  67. static void internal_call( const Body& obj, __TBB_FORWARDING_REF(A1) arg1, A2&, void (Body::*)(CvItem&) const ) {
  68. obj(arg1);
  69. }
  70. template<typename A1, typename A2, typename CvItem >
  71. static void internal_call( const Body& obj, __TBB_FORWARDING_REF(A1) arg1, A2& arg2, void (Body::*)(CvItem&, parallel_do_feeder<Item>&) const ) {
  72. obj(arg1, arg2);
  73. }
  74. public:
  75. template<typename A1, typename A2>
  76. static void call( const Body& obj, __TBB_FORWARDING_REF(A1) arg1, A2& arg2 )
  77. {
  78. internal_call( obj, tbb::internal::forward<A1>(arg1), arg2, &Body::operator() );
  79. }
  80. };
  81. //! For internal use only.
  82. /** Executes one iteration of a do.
  83. @ingroup algorithms */
  84. template<typename Body, typename Item>
  85. class do_iteration_task: public task
  86. {
  87. typedef parallel_do_feeder_impl<Body, Item> feeder_type;
  88. Item my_value;
  89. feeder_type& my_feeder;
  90. do_iteration_task( const Item& value, feeder_type& feeder ) :
  91. my_value(value), my_feeder(feeder)
  92. {}
  93. #if __TBB_CPP11_RVALUE_REF_PRESENT
  94. do_iteration_task( Item&& value, feeder_type& feeder ) :
  95. my_value(std::move(value)), my_feeder(feeder)
  96. {}
  97. #endif
  98. task* execute() __TBB_override
  99. {
  100. parallel_do_operator_selector<Body, Item>::call(*my_feeder.my_body, tbb::internal::move(my_value), my_feeder);
  101. return NULL;
  102. }
  103. template<typename Body_, typename Item_> friend class parallel_do_feeder_impl;
  104. }; // class do_iteration_task
  105. template<typename Iterator, typename Body, typename Item>
  106. class do_iteration_task_iter: public task
  107. {
  108. typedef parallel_do_feeder_impl<Body, Item> feeder_type;
  109. Iterator my_iter;
  110. feeder_type& my_feeder;
  111. do_iteration_task_iter( const Iterator& iter, feeder_type& feeder ) :
  112. my_iter(iter), my_feeder(feeder)
  113. {}
  114. task* execute() __TBB_override
  115. {
  116. parallel_do_operator_selector<Body, Item>::call(*my_feeder.my_body, *my_iter, my_feeder);
  117. return NULL;
  118. }
  119. template<typename Iterator_, typename Body_, typename Item_> friend class do_group_task_forward;
  120. template<typename Body_, typename Item_> friend class do_group_task_input;
  121. template<typename Iterator_, typename Body_, typename Item_> friend class do_task_iter;
  122. }; // class do_iteration_task_iter
  123. //! For internal use only.
  124. /** Implements new task adding procedure.
  125. @ingroup algorithms **/
  126. template<class Body, typename Item>
  127. class parallel_do_feeder_impl : public parallel_do_feeder<Item>
  128. {
  129. #if __TBB_CPP11_RVALUE_REF_PRESENT
  130. //Avoiding use of copy constructor in a virtual method if the type does not support it
  131. void internal_add_copy_impl(std::true_type, const Item& item) {
  132. typedef do_iteration_task<Body, Item> iteration_type;
  133. iteration_type& t = *new (task::allocate_additional_child_of(*my_barrier)) iteration_type(item, *this);
  134. task::spawn(t);
  135. }
  136. void internal_add_copy_impl(std::false_type, const Item&) {
  137. __TBB_ASSERT(false, "Overloading for r-value reference doesn't work or it's not movable and not copyable object");
  138. }
  139. void internal_add_copy( const Item& item ) __TBB_override
  140. {
  141. #if __TBB_CPP11_IS_COPY_CONSTRUCTIBLE_PRESENT
  142. internal_add_copy_impl(typename std::is_copy_constructible<Item>::type(), item);
  143. #else
  144. internal_add_copy_impl(std::true_type(), item);
  145. #endif
  146. }
  147. void internal_add_move( Item&& item ) __TBB_override
  148. {
  149. typedef do_iteration_task<Body, Item> iteration_type;
  150. iteration_type& t = *new (task::allocate_additional_child_of(*my_barrier)) iteration_type(std::move(item), *this);
  151. task::spawn(t);
  152. }
  153. #else /* ! __TBB_CPP11_RVALUE_REF_PRESENT */
  154. void internal_add_copy(const Item& item) __TBB_override {
  155. typedef do_iteration_task<Body, Item> iteration_type;
  156. iteration_type& t = *new (task::allocate_additional_child_of(*my_barrier)) iteration_type(item, *this);
  157. task::spawn(t);
  158. }
  159. #endif /* __TBB_CPP11_RVALUE_REF_PRESENT */
  160. public:
  161. const Body* my_body;
  162. empty_task* my_barrier;
  163. parallel_do_feeder_impl()
  164. {
  165. my_barrier = new( task::allocate_root() ) empty_task();
  166. __TBB_ASSERT(my_barrier, "root task allocation failed");
  167. }
  168. #if __TBB_TASK_GROUP_CONTEXT
  169. parallel_do_feeder_impl(tbb::task_group_context &context)
  170. {
  171. my_barrier = new( task::allocate_root(context) ) empty_task();
  172. __TBB_ASSERT(my_barrier, "root task allocation failed");
  173. }
  174. #endif
  175. ~parallel_do_feeder_impl()
  176. {
  177. my_barrier->destroy(*my_barrier);
  178. }
  179. }; // class parallel_do_feeder_impl
  180. //! For internal use only
  181. /** Unpacks a block of iterations.
  182. @ingroup algorithms */
  183. template<typename Iterator, typename Body, typename Item>
  184. class do_group_task_forward: public task
  185. {
  186. static const size_t max_arg_size = 4;
  187. typedef parallel_do_feeder_impl<Body, Item> feeder_type;
  188. feeder_type& my_feeder;
  189. Iterator my_first;
  190. size_t my_size;
  191. do_group_task_forward( Iterator first, size_t size, feeder_type& feeder )
  192. : my_feeder(feeder), my_first(first), my_size(size)
  193. {}
  194. task* execute() __TBB_override
  195. {
  196. typedef do_iteration_task_iter<Iterator, Body, Item> iteration_type;
  197. __TBB_ASSERT( my_size>0, NULL );
  198. task_list list;
  199. task* t;
  200. size_t k=0;
  201. for(;;) {
  202. t = new( allocate_child() ) iteration_type( my_first, my_feeder );
  203. ++my_first;
  204. if( ++k==my_size ) break;
  205. list.push_back(*t);
  206. }
  207. set_ref_count(int(k+1));
  208. spawn(list);
  209. spawn_and_wait_for_all(*t);
  210. return NULL;
  211. }
  212. template<typename Iterator_, typename Body_, typename _Item> friend class do_task_iter;
  213. }; // class do_group_task_forward
  214. template<typename Body, typename Item>
  215. class do_group_task_input: public task
  216. {
  217. static const size_t max_arg_size = 4;
  218. typedef parallel_do_feeder_impl<Body, Item> feeder_type;
  219. feeder_type& my_feeder;
  220. size_t my_size;
  221. aligned_space<Item, max_arg_size> my_arg;
  222. do_group_task_input( feeder_type& feeder )
  223. : my_feeder(feeder), my_size(0)
  224. {}
  225. task* execute() __TBB_override
  226. {
  227. #if __TBB_CPP11_RVALUE_REF_PRESENT
  228. typedef std::move_iterator<Item*> Item_iterator;
  229. #else
  230. typedef Item* Item_iterator;
  231. #endif
  232. typedef do_iteration_task_iter<Item_iterator, Body, Item> iteration_type;
  233. __TBB_ASSERT( my_size>0, NULL );
  234. task_list list;
  235. task* t;
  236. size_t k=0;
  237. for(;;) {
  238. t = new( allocate_child() ) iteration_type( Item_iterator(my_arg.begin() + k), my_feeder );
  239. if( ++k==my_size ) break;
  240. list.push_back(*t);
  241. }
  242. set_ref_count(int(k+1));
  243. spawn(list);
  244. spawn_and_wait_for_all(*t);
  245. return NULL;
  246. }
  247. ~do_group_task_input(){
  248. for( size_t k=0; k<my_size; ++k)
  249. (my_arg.begin() + k)->~Item();
  250. }
  251. template<typename Iterator_, typename Body_, typename Item_> friend class do_task_iter;
  252. }; // class do_group_task_input
  253. //! For internal use only.
  254. /** Gets block of iterations and packages them into a do_group_task.
  255. @ingroup algorithms */
  256. template<typename Iterator, typename Body, typename Item>
  257. class do_task_iter: public task
  258. {
  259. typedef parallel_do_feeder_impl<Body, Item> feeder_type;
  260. public:
  261. do_task_iter( Iterator first, Iterator last , feeder_type& feeder ) :
  262. my_first(first), my_last(last), my_feeder(feeder)
  263. {}
  264. private:
  265. Iterator my_first;
  266. Iterator my_last;
  267. feeder_type& my_feeder;
  268. /* Do not merge run(xxx) and run_xxx() methods. They are separated in order
  269. to make sure that compilers will eliminate unused argument of type xxx
  270. (that is will not put it on stack). The sole purpose of this argument
  271. is overload resolution.
  272. An alternative could be using template functions, but explicit specialization
  273. of member function templates is not supported for non specialized class
  274. templates. Besides template functions would always fall back to the least
  275. efficient variant (the one for input iterators) in case of iterators having
  276. custom tags derived from basic ones. */
  277. task* execute() __TBB_override
  278. {
  279. typedef typename std::iterator_traits<Iterator>::iterator_category iterator_tag;
  280. return run( (iterator_tag*)NULL );
  281. }
  282. /** This is the most restricted variant that operates on input iterators or
  283. iterators with unknown tags (tags not derived from the standard ones). **/
  284. inline task* run( void* ) { return run_for_input_iterator(); }
  285. task* run_for_input_iterator() {
  286. typedef do_group_task_input<Body, Item> block_type;
  287. block_type& t = *new( allocate_additional_child_of(*my_feeder.my_barrier) ) block_type(my_feeder);
  288. size_t k=0;
  289. while( !(my_first == my_last) ) {
  290. // Move semantics are automatically used when supported by the iterator
  291. new (t.my_arg.begin() + k) Item(*my_first);
  292. ++my_first;
  293. if( ++k==block_type::max_arg_size ) {
  294. if ( !(my_first == my_last) )
  295. recycle_to_reexecute();
  296. break;
  297. }
  298. }
  299. if( k==0 ) {
  300. destroy(t);
  301. return NULL;
  302. } else {
  303. t.my_size = k;
  304. return &t;
  305. }
  306. }
  307. inline task* run( std::forward_iterator_tag* ) { return run_for_forward_iterator(); }
  308. task* run_for_forward_iterator() {
  309. typedef do_group_task_forward<Iterator, Body, Item> block_type;
  310. Iterator first = my_first;
  311. size_t k=0;
  312. while( !(my_first==my_last) ) {
  313. ++my_first;
  314. if( ++k==block_type::max_arg_size ) {
  315. if ( !(my_first==my_last) )
  316. recycle_to_reexecute();
  317. break;
  318. }
  319. }
  320. return k==0 ? NULL : new( allocate_additional_child_of(*my_feeder.my_barrier) ) block_type(first, k, my_feeder);
  321. }
  322. inline task* run( std::random_access_iterator_tag* ) { return run_for_random_access_iterator(); }
  323. task* run_for_random_access_iterator() {
  324. typedef do_group_task_forward<Iterator, Body, Item> block_type;
  325. typedef do_iteration_task_iter<Iterator, Body, Item> iteration_type;
  326. size_t k = static_cast<size_t>(my_last-my_first);
  327. if( k > block_type::max_arg_size ) {
  328. Iterator middle = my_first + k/2;
  329. empty_task& c = *new( allocate_continuation() ) empty_task;
  330. do_task_iter& b = *new( c.allocate_child() ) do_task_iter(middle, my_last, my_feeder);
  331. recycle_as_child_of(c);
  332. my_last = middle;
  333. c.set_ref_count(2);
  334. c.spawn(b);
  335. return this;
  336. }else if( k != 0 ) {
  337. task_list list;
  338. task* t;
  339. size_t k1=0;
  340. for(;;) {
  341. t = new( allocate_child() ) iteration_type(my_first, my_feeder);
  342. ++my_first;
  343. if( ++k1==k ) break;
  344. list.push_back(*t);
  345. }
  346. set_ref_count(int(k+1));
  347. spawn(list);
  348. spawn_and_wait_for_all(*t);
  349. }
  350. return NULL;
  351. }
  352. }; // class do_task_iter
  353. //! For internal use only.
  354. /** Implements parallel iteration over a range.
  355. @ingroup algorithms */
  356. template<typename Iterator, typename Body, typename Item>
  357. void run_parallel_do( Iterator first, Iterator last, const Body& body
  358. #if __TBB_TASK_GROUP_CONTEXT
  359. , task_group_context& context
  360. #endif
  361. )
  362. {
  363. typedef do_task_iter<Iterator, Body, Item> root_iteration_task;
  364. #if __TBB_TASK_GROUP_CONTEXT
  365. parallel_do_feeder_impl<Body, Item> feeder(context);
  366. #else
  367. parallel_do_feeder_impl<Body, Item> feeder;
  368. #endif
  369. feeder.my_body = &body;
  370. root_iteration_task &t = *new( feeder.my_barrier->allocate_child() ) root_iteration_task(first, last, feeder);
  371. feeder.my_barrier->set_ref_count(2);
  372. feeder.my_barrier->spawn_and_wait_for_all(t);
  373. }
  374. //! For internal use only.
  375. /** Detects types of Body's operator function arguments.
  376. @ingroup algorithms **/
  377. template<typename Iterator, typename Body, typename Item>
  378. void select_parallel_do( Iterator first, Iterator last, const Body& body, void (Body::*)(Item) const
  379. #if __TBB_TASK_GROUP_CONTEXT
  380. , task_group_context& context
  381. #endif
  382. )
  383. {
  384. run_parallel_do<Iterator, Body, typename ::tbb::internal::strip<Item>::type>( first, last, body
  385. #if __TBB_TASK_GROUP_CONTEXT
  386. , context
  387. #endif
  388. );
  389. }
  390. //! For internal use only.
  391. /** Detects types of Body's operator function arguments.
  392. @ingroup algorithms **/
  393. template<typename Iterator, typename Body, typename Item, typename _Item>
  394. void select_parallel_do( Iterator first, Iterator last, const Body& body, void (Body::*)(Item, parallel_do_feeder<_Item>&) const
  395. #if __TBB_TASK_GROUP_CONTEXT
  396. , task_group_context& context
  397. #endif
  398. )
  399. {
  400. run_parallel_do<Iterator, Body, typename ::tbb::internal::strip<Item>::type>( first, last, body
  401. #if __TBB_TASK_GROUP_CONTEXT
  402. , context
  403. #endif
  404. );
  405. }
  406. } // namespace internal
  407. } // namespace interface9
  408. //! @endcond
  409. /** \page parallel_do_body_req Requirements on parallel_do body
  410. Class \c Body implementing the concept of parallel_do body must define:
  411. - \code
  412. B::operator()(
  413. cv_item_type item,
  414. parallel_do_feeder<item_type>& feeder
  415. ) const
  416. OR
  417. B::operator()( cv_item_type& item ) const
  418. \endcode Process item.
  419. May be invoked concurrently for the same \c this but different \c item.
  420. - \code item_type( const item_type& ) \endcode
  421. Copy a work item.
  422. - \code ~item_type() \endcode Destroy a work item
  423. **/
  424. /** \name parallel_do
  425. See also requirements on \ref parallel_do_body_req "parallel_do Body". **/
  426. //@{
  427. //! Parallel iteration over a range, with optional addition of more work.
  428. /** @ingroup algorithms */
  429. template<typename Iterator, typename Body>
  430. void parallel_do( Iterator first, Iterator last, const Body& body )
  431. {
  432. if ( first == last )
  433. return;
  434. #if __TBB_TASK_GROUP_CONTEXT
  435. task_group_context context(internal::PARALLEL_DO);
  436. #endif
  437. interface9::internal::select_parallel_do( first, last, body, &Body::operator()
  438. #if __TBB_TASK_GROUP_CONTEXT
  439. , context
  440. #endif
  441. );
  442. }
  443. template<typename Range, typename Body>
  444. void parallel_do(Range& rng, const Body& body) {
  445. parallel_do(tbb::internal::first(rng), tbb::internal::last(rng), body);
  446. }
  447. template<typename Range, typename Body>
  448. void parallel_do(const Range& rng, const Body& body) {
  449. parallel_do(tbb::internal::first(rng), tbb::internal::last(rng), body);
  450. }
  451. #if __TBB_TASK_GROUP_CONTEXT
  452. //! Parallel iteration over a range, with optional addition of more work and user-supplied context
  453. /** @ingroup algorithms */
  454. template<typename Iterator, typename Body>
  455. void parallel_do( Iterator first, Iterator last, const Body& body, task_group_context& context )
  456. {
  457. if ( first == last )
  458. return;
  459. interface9::internal::select_parallel_do( first, last, body, &Body::operator(), context );
  460. }
  461. template<typename Range, typename Body>
  462. void parallel_do(Range& rng, const Body& body, task_group_context& context) {
  463. parallel_do(tbb::internal::first(rng), tbb::internal::last(rng), body, context);
  464. }
  465. template<typename Range, typename Body>
  466. void parallel_do(const Range& rng, const Body& body, task_group_context& context) {
  467. parallel_do(tbb::internal::first(rng), tbb::internal::last(rng), body, context);
  468. }
  469. #endif // __TBB_TASK_GROUP_CONTEXT
  470. //@}
  471. using interface9::parallel_do_feeder;
  472. } // namespace
  473. #include "internal/_warning_suppress_disable_notice.h"
  474. #undef __TBB_parallel_do_H_include_area
  475. #endif /* __TBB_parallel_do_H */