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.

354 lines
14 KiB

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