parallel_scan.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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_scan_H
  14. #define __TBB_parallel_scan_H
  15. #define __TBB_parallel_scan_H_include_area
  16. #include "internal/_warning_suppress_enable_notice.h"
  17. #include "task.h"
  18. #include "aligned_space.h"
  19. #include <new>
  20. #include "partitioner.h"
  21. namespace tbb {
  22. //! Used to indicate that the initial scan is being performed.
  23. /** @ingroup algorithms */
  24. struct pre_scan_tag {
  25. static bool is_final_scan() {return false;}
  26. operator bool() {return is_final_scan();}
  27. };
  28. //! Used to indicate that the final scan is being performed.
  29. /** @ingroup algorithms */
  30. struct final_scan_tag {
  31. static bool is_final_scan() {return true;}
  32. operator bool() {return is_final_scan();}
  33. };
  34. //! @cond INTERNAL
  35. namespace internal {
  36. //! Performs final scan for a leaf
  37. /** @ingroup algorithms */
  38. template<typename Range, typename Body>
  39. class final_sum: public task {
  40. public:
  41. Body my_body;
  42. private:
  43. aligned_space<Range> my_range;
  44. //! Where to put result of last subrange, or NULL if not last subrange.
  45. Body* my_stuff_last;
  46. public:
  47. final_sum( Body& body_ ) :
  48. my_body(body_,split())
  49. {
  50. poison_pointer(my_stuff_last);
  51. }
  52. ~final_sum() {
  53. my_range.begin()->~Range();
  54. }
  55. void finish_construction( const Range& range_, Body* stuff_last_ ) {
  56. new( my_range.begin() ) Range(range_);
  57. my_stuff_last = stuff_last_;
  58. }
  59. private:
  60. task* execute() __TBB_override {
  61. my_body( *my_range.begin(), final_scan_tag() );
  62. if( my_stuff_last )
  63. my_stuff_last->assign(my_body);
  64. return NULL;
  65. }
  66. };
  67. //! Split work to be done in the scan.
  68. /** @ingroup algorithms */
  69. template<typename Range, typename Body>
  70. class sum_node: public task {
  71. typedef final_sum<Range,Body> final_sum_type;
  72. public:
  73. final_sum_type *my_incoming;
  74. final_sum_type *my_body;
  75. Body *my_stuff_last;
  76. private:
  77. final_sum_type *my_left_sum;
  78. sum_node *my_left;
  79. sum_node *my_right;
  80. bool my_left_is_final;
  81. Range my_range;
  82. sum_node( const Range range_, bool left_is_final_ ) :
  83. my_stuff_last(NULL),
  84. my_left_sum(NULL),
  85. my_left(NULL),
  86. my_right(NULL),
  87. my_left_is_final(left_is_final_),
  88. my_range(range_)
  89. {
  90. // Poison fields that will be set by second pass.
  91. poison_pointer(my_body);
  92. poison_pointer(my_incoming);
  93. }
  94. task* create_child( const Range& range_, final_sum_type& f, sum_node* n, final_sum_type* incoming_, Body* stuff_last_ ) {
  95. if( !n ) {
  96. f.recycle_as_child_of( *this );
  97. f.finish_construction( range_, stuff_last_ );
  98. return &f;
  99. } else {
  100. n->my_body = &f;
  101. n->my_incoming = incoming_;
  102. n->my_stuff_last = stuff_last_;
  103. return n;
  104. }
  105. }
  106. task* execute() __TBB_override {
  107. if( my_body ) {
  108. if( my_incoming )
  109. my_left_sum->my_body.reverse_join( my_incoming->my_body );
  110. recycle_as_continuation();
  111. sum_node& c = *this;
  112. task* b = c.create_child(Range(my_range,split()),*my_left_sum,my_right,my_left_sum,my_stuff_last);
  113. task* a = my_left_is_final ? NULL : c.create_child(my_range,*my_body,my_left,my_incoming,NULL);
  114. set_ref_count( (a!=NULL)+(b!=NULL) );
  115. my_body = NULL;
  116. if( a ) spawn(*b);
  117. else a = b;
  118. return a;
  119. } else {
  120. return NULL;
  121. }
  122. }
  123. template<typename Range_,typename Body_,typename Partitioner_>
  124. friend class start_scan;
  125. template<typename Range_,typename Body_>
  126. friend class finish_scan;
  127. };
  128. //! Combine partial results
  129. /** @ingroup algorithms */
  130. template<typename Range, typename Body>
  131. class finish_scan: public task {
  132. typedef sum_node<Range,Body> sum_node_type;
  133. typedef final_sum<Range,Body> final_sum_type;
  134. final_sum_type** const my_sum;
  135. sum_node_type*& my_return_slot;
  136. public:
  137. final_sum_type* my_right_zombie;
  138. sum_node_type& my_result;
  139. task* execute() __TBB_override {
  140. __TBB_ASSERT( my_result.ref_count()==(my_result.my_left!=NULL)+(my_result.my_right!=NULL), NULL );
  141. if( my_result.my_left )
  142. my_result.my_left_is_final = false;
  143. if( my_right_zombie && my_sum )
  144. ((*my_sum)->my_body).reverse_join(my_result.my_left_sum->my_body);
  145. __TBB_ASSERT( !my_return_slot, NULL );
  146. if( my_right_zombie || my_result.my_right ) {
  147. my_return_slot = &my_result;
  148. } else {
  149. destroy( my_result );
  150. }
  151. if( my_right_zombie && !my_sum && !my_result.my_right ) {
  152. destroy(*my_right_zombie);
  153. my_right_zombie = NULL;
  154. }
  155. return NULL;
  156. }
  157. finish_scan( sum_node_type*& return_slot_, final_sum_type** sum_, sum_node_type& result_ ) :
  158. my_sum(sum_),
  159. my_return_slot(return_slot_),
  160. my_right_zombie(NULL),
  161. my_result(result_)
  162. {
  163. __TBB_ASSERT( !my_return_slot, NULL );
  164. }
  165. };
  166. //! Initial task to split the work
  167. /** @ingroup algorithms */
  168. template<typename Range, typename Body, typename Partitioner=simple_partitioner>
  169. class start_scan: public task {
  170. typedef sum_node<Range,Body> sum_node_type;
  171. typedef final_sum<Range,Body> final_sum_type;
  172. final_sum_type* my_body;
  173. /** Non-null if caller is requesting total. */
  174. final_sum_type** my_sum;
  175. sum_node_type** my_return_slot;
  176. /** Null if computing root. */
  177. sum_node_type* my_parent_sum;
  178. bool my_is_final;
  179. bool my_is_right_child;
  180. Range my_range;
  181. typename Partitioner::partition_type my_partition;
  182. task* execute() __TBB_override ;
  183. public:
  184. start_scan( sum_node_type*& return_slot_, start_scan& parent_, sum_node_type* parent_sum_ ) :
  185. my_body(parent_.my_body),
  186. my_sum(parent_.my_sum),
  187. my_return_slot(&return_slot_),
  188. my_parent_sum(parent_sum_),
  189. my_is_final(parent_.my_is_final),
  190. my_is_right_child(false),
  191. my_range(parent_.my_range,split()),
  192. my_partition(parent_.my_partition,split())
  193. {
  194. __TBB_ASSERT( !*my_return_slot, NULL );
  195. }
  196. start_scan( sum_node_type*& return_slot_, const Range& range_, final_sum_type& body_, const Partitioner& partitioner_) :
  197. my_body(&body_),
  198. my_sum(NULL),
  199. my_return_slot(&return_slot_),
  200. my_parent_sum(NULL),
  201. my_is_final(true),
  202. my_is_right_child(false),
  203. my_range(range_),
  204. my_partition(partitioner_)
  205. {
  206. __TBB_ASSERT( !*my_return_slot, NULL );
  207. }
  208. static void run( const Range& range_, Body& body_, const Partitioner& partitioner_ ) {
  209. if( !range_.empty() ) {
  210. typedef internal::start_scan<Range,Body,Partitioner> start_pass1_type;
  211. internal::sum_node<Range,Body>* root = NULL;
  212. final_sum_type* temp_body = new(task::allocate_root()) final_sum_type( body_ );
  213. start_pass1_type& pass1 = *new(task::allocate_root()) start_pass1_type(
  214. /*my_return_slot=*/root,
  215. range_,
  216. *temp_body,
  217. partitioner_ );
  218. temp_body->my_body.reverse_join(body_);
  219. task::spawn_root_and_wait( pass1 );
  220. if( root ) {
  221. root->my_body = temp_body;
  222. root->my_incoming = NULL;
  223. root->my_stuff_last = &body_;
  224. task::spawn_root_and_wait( *root );
  225. } else {
  226. body_.assign(temp_body->my_body);
  227. temp_body->finish_construction( range_, NULL );
  228. temp_body->destroy(*temp_body);
  229. }
  230. }
  231. }
  232. };
  233. template<typename Range, typename Body, typename Partitioner>
  234. task* start_scan<Range,Body,Partitioner>::execute() {
  235. typedef internal::finish_scan<Range,Body> finish_pass1_type;
  236. finish_pass1_type* p = my_parent_sum ? static_cast<finish_pass1_type*>( parent() ) : NULL;
  237. // Inspecting p->result.left_sum would ordinarily be a race condition.
  238. // But we inspect it only if we are not a stolen task, in which case we
  239. // know that task assigning to p->result.left_sum has completed.
  240. bool treat_as_stolen = my_is_right_child && (is_stolen_task() || my_body!=p->my_result.my_left_sum);
  241. if( treat_as_stolen ) {
  242. // Invocation is for right child that has been really stolen or needs to be virtually stolen
  243. p->my_right_zombie = my_body = new( allocate_root() ) final_sum_type(my_body->my_body);
  244. my_is_final = false;
  245. }
  246. task* next_task = NULL;
  247. if( (my_is_right_child && !treat_as_stolen) || !my_range.is_divisible() || my_partition.should_execute_range(*this) ) {
  248. if( my_is_final )
  249. (my_body->my_body)( my_range, final_scan_tag() );
  250. else if( my_sum )
  251. (my_body->my_body)( my_range, pre_scan_tag() );
  252. if( my_sum )
  253. *my_sum = my_body;
  254. __TBB_ASSERT( !*my_return_slot, NULL );
  255. } else {
  256. sum_node_type* result;
  257. if( my_parent_sum )
  258. result = new(allocate_additional_child_of(*my_parent_sum)) sum_node_type(my_range,/*my_left_is_final=*/my_is_final);
  259. else
  260. result = new(task::allocate_root()) sum_node_type(my_range,/*my_left_is_final=*/my_is_final);
  261. finish_pass1_type& c = *new( allocate_continuation()) finish_pass1_type(*my_return_slot,my_sum,*result);
  262. // Split off right child
  263. start_scan& b = *new( c.allocate_child() ) start_scan( /*my_return_slot=*/result->my_right, *this, result );
  264. b.my_is_right_child = true;
  265. // Left child is recycling of *this. Must recycle this before spawning b,
  266. // otherwise b might complete and decrement c.ref_count() to zero, which
  267. // would cause c.execute() to run prematurely.
  268. recycle_as_child_of(c);
  269. c.set_ref_count(2);
  270. c.spawn(b);
  271. my_sum = &result->my_left_sum;
  272. my_return_slot = &result->my_left;
  273. my_is_right_child = false;
  274. next_task = this;
  275. my_parent_sum = result;
  276. __TBB_ASSERT( !*my_return_slot, NULL );
  277. }
  278. return next_task;
  279. }
  280. template<typename Range, typename Value, typename Scan, typename ReverseJoin>
  281. class lambda_scan_body : no_assign {
  282. Value my_sum;
  283. const Value& identity_element;
  284. const Scan& my_scan;
  285. const ReverseJoin& my_reverse_join;
  286. public:
  287. lambda_scan_body( const Value& identity, const Scan& scan, const ReverseJoin& rev_join)
  288. : my_sum(identity)
  289. , identity_element(identity)
  290. , my_scan(scan)
  291. , my_reverse_join(rev_join) {}
  292. lambda_scan_body( lambda_scan_body& b, split )
  293. : my_sum(b.identity_element)
  294. , identity_element(b.identity_element)
  295. , my_scan(b.my_scan)
  296. , my_reverse_join(b.my_reverse_join) {}
  297. template<typename Tag>
  298. void operator()( const Range& r, Tag tag ) {
  299. my_sum = my_scan(r, my_sum, tag);
  300. }
  301. void reverse_join( lambda_scan_body& a ) {
  302. my_sum = my_reverse_join(a.my_sum, my_sum);
  303. }
  304. void assign( lambda_scan_body& b ) {
  305. my_sum = b.my_sum;
  306. }
  307. Value result() const {
  308. return my_sum;
  309. }
  310. };
  311. } // namespace internal
  312. //! @endcond
  313. // Requirements on Range concept are documented in blocked_range.h
  314. /** \page parallel_scan_body_req Requirements on parallel_scan body
  315. Class \c Body implementing the concept of parallel_scan body must define:
  316. - \code Body::Body( Body&, split ); \endcode Splitting constructor.
  317. Split \c b so that \c this and \c b can accumulate separately
  318. - \code Body::~Body(); \endcode Destructor
  319. - \code void Body::operator()( const Range& r, pre_scan_tag ); \endcode
  320. Preprocess iterations for range \c r
  321. - \code void Body::operator()( const Range& r, final_scan_tag ); \endcode
  322. Do final processing for iterations of range \c r
  323. - \code void Body::reverse_join( Body& a ); \endcode
  324. Merge preprocessing state of \c a into \c this, where \c a was
  325. created earlier from \c b by b's splitting constructor
  326. **/
  327. /** \name parallel_scan
  328. See also requirements on \ref range_req "Range" and \ref parallel_scan_body_req "parallel_scan Body". **/
  329. //@{
  330. //! Parallel prefix with default partitioner
  331. /** @ingroup algorithms **/
  332. template<typename Range, typename Body>
  333. void parallel_scan( const Range& range, Body& body ) {
  334. internal::start_scan<Range,Body,__TBB_DEFAULT_PARTITIONER>::run(range,body,__TBB_DEFAULT_PARTITIONER());
  335. }
  336. //! Parallel prefix with simple_partitioner
  337. /** @ingroup algorithms **/
  338. template<typename Range, typename Body>
  339. void parallel_scan( const Range& range, Body& body, const simple_partitioner& partitioner ) {
  340. internal::start_scan<Range,Body,simple_partitioner>::run(range,body,partitioner);
  341. }
  342. //! Parallel prefix with auto_partitioner
  343. /** @ingroup algorithms **/
  344. template<typename Range, typename Body>
  345. void parallel_scan( const Range& range, Body& body, const auto_partitioner& partitioner ) {
  346. internal::start_scan<Range,Body,auto_partitioner>::run(range,body,partitioner);
  347. }
  348. //! Parallel prefix with default partitioner
  349. /** @ingroup algorithms **/
  350. template<typename Range, typename Value, typename Scan, typename ReverseJoin>
  351. Value parallel_scan( const Range& range, const Value& identity, const Scan& scan, const ReverseJoin& reverse_join ) {
  352. internal::lambda_scan_body<Range, Value, Scan, ReverseJoin> body(identity, scan, reverse_join);
  353. tbb::parallel_scan(range,body,__TBB_DEFAULT_PARTITIONER());
  354. return body.result();
  355. }
  356. //! Parallel prefix with simple_partitioner
  357. /** @ingroup algorithms **/
  358. template<typename Range, typename Value, typename Scan, typename ReverseJoin>
  359. Value parallel_scan( const Range& range, const Value& identity, const Scan& scan, const ReverseJoin& reverse_join, const simple_partitioner& partitioner ) {
  360. internal::lambda_scan_body<Range, Value, Scan, ReverseJoin> body(identity, scan, reverse_join);
  361. tbb::parallel_scan(range,body,partitioner);
  362. return body.result();
  363. }
  364. //! Parallel prefix with auto_partitioner
  365. /** @ingroup algorithms **/
  366. template<typename Range, typename Value, typename Scan, typename ReverseJoin>
  367. Value parallel_scan( const Range& range, const Value& identity, const Scan& scan, const ReverseJoin& reverse_join, const auto_partitioner& partitioner ) {
  368. internal::lambda_scan_body<Range, Value, Scan, ReverseJoin> body(identity, scan, reverse_join);
  369. tbb::parallel_scan(range,body,partitioner);
  370. return body.result();
  371. }
  372. //@}
  373. } // namespace tbb
  374. #include "internal/_warning_suppress_disable_notice.h"
  375. #undef __TBB_parallel_scan_H_include_area
  376. #endif /* __TBB_parallel_scan_H */