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.

194 lines
6.6 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_while
  24. #define __TBB_parallel_while
  25. #include "task.h"
  26. #include <new>
  27. namespace tbb {
  28. template<typename Body>
  29. class parallel_while;
  30. //! @cond INTERNAL
  31. namespace internal {
  32. template<typename Stream, typename Body> class while_task;
  33. //! For internal use only.
  34. /** Executes one iteration of a while.
  35. @ingroup algorithms */
  36. template<typename Body>
  37. class while_iteration_task: public task {
  38. const Body& my_body;
  39. typename Body::argument_type my_value;
  40. /*override*/ task* execute() {
  41. my_body(my_value);
  42. return NULL;
  43. }
  44. while_iteration_task( const typename Body::argument_type& value, const Body& body ) :
  45. my_body(body), my_value(value)
  46. {}
  47. template<typename Body_> friend class while_group_task;
  48. friend class tbb::parallel_while<Body>;
  49. };
  50. //! For internal use only
  51. /** Unpacks a block of iterations.
  52. @ingroup algorithms */
  53. template<typename Body>
  54. class while_group_task: public task {
  55. static const size_t max_arg_size = 4;
  56. const Body& my_body;
  57. size_t size;
  58. typename Body::argument_type my_arg[max_arg_size];
  59. while_group_task( const Body& body ) : my_body(body), size(0) {}
  60. /*override*/ task* execute() {
  61. typedef while_iteration_task<Body> iteration_type;
  62. __TBB_ASSERT( size>0, NULL );
  63. task_list list;
  64. task* t;
  65. size_t k=0;
  66. for(;;) {
  67. t = new( allocate_child() ) iteration_type(my_arg[k],my_body);
  68. if( ++k==size ) break;
  69. list.push_back(*t);
  70. }
  71. set_ref_count(int(k+1));
  72. spawn(list);
  73. spawn_and_wait_for_all(*t);
  74. return NULL;
  75. }
  76. template<typename Stream, typename Body_> friend class while_task;
  77. };
  78. //! For internal use only.
  79. /** Gets block of iterations from a stream and packages them into a while_group_task.
  80. @ingroup algorithms */
  81. template<typename Stream, typename Body>
  82. class while_task: public task {
  83. Stream& my_stream;
  84. const Body& my_body;
  85. empty_task& my_barrier;
  86. /*override*/ task* execute() {
  87. typedef while_group_task<Body> block_type;
  88. block_type& t = *new( allocate_additional_child_of(my_barrier) ) block_type(my_body);
  89. size_t k=0;
  90. while( my_stream.pop_if_present(t.my_arg[k]) ) {
  91. if( ++k==block_type::max_arg_size ) {
  92. // There might be more iterations.
  93. recycle_to_reexecute();
  94. break;
  95. }
  96. }
  97. if( k==0 ) {
  98. destroy(t);
  99. return NULL;
  100. } else {
  101. t.size = k;
  102. return &t;
  103. }
  104. }
  105. while_task( Stream& stream, const Body& body, empty_task& barrier ) :
  106. my_stream(stream),
  107. my_body(body),
  108. my_barrier(barrier)
  109. {}
  110. friend class tbb::parallel_while<Body>;
  111. };
  112. } // namespace internal
  113. //! @endcond
  114. //! Parallel iteration over a stream, with optional addition of more work.
  115. /** The Body b has the requirement: \n
  116. "b(v)" \n
  117. "b.argument_type" \n
  118. where v is an argument_type
  119. @ingroup algorithms */
  120. template<typename Body>
  121. class parallel_while: internal::no_copy {
  122. public:
  123. //! Construct empty non-running parallel while.
  124. parallel_while() : my_body(NULL), my_barrier(NULL) {}
  125. //! Destructor cleans up data members before returning.
  126. ~parallel_while() {
  127. if( my_barrier ) {
  128. my_barrier->destroy(*my_barrier);
  129. my_barrier = NULL;
  130. }
  131. }
  132. //! Type of items
  133. typedef typename Body::argument_type value_type;
  134. //! Apply body.apply to each item in the stream.
  135. /** A Stream s has the requirements \n
  136. "S::value_type" \n
  137. "s.pop_if_present(value) is convertible to bool */
  138. template<typename Stream>
  139. void run( Stream& stream, const Body& body );
  140. //! Add a work item while running.
  141. /** Should be executed only by body.apply or a thread spawned therefrom. */
  142. void add( const value_type& item );
  143. private:
  144. const Body* my_body;
  145. empty_task* my_barrier;
  146. };
  147. template<typename Body>
  148. template<typename Stream>
  149. void parallel_while<Body>::run( Stream& stream, const Body& body ) {
  150. using namespace internal;
  151. empty_task& barrier = *new( task::allocate_root() ) empty_task();
  152. my_body = &body;
  153. my_barrier = &barrier;
  154. my_barrier->set_ref_count(2);
  155. while_task<Stream,Body>& w = *new( my_barrier->allocate_child() ) while_task<Stream,Body>( stream, body, barrier );
  156. my_barrier->spawn_and_wait_for_all(w);
  157. my_barrier->destroy(*my_barrier);
  158. my_barrier = NULL;
  159. my_body = NULL;
  160. }
  161. template<typename Body>
  162. void parallel_while<Body>::add( const value_type& item ) {
  163. __TBB_ASSERT(my_barrier,"attempt to add to parallel_while that is not running");
  164. typedef internal::while_iteration_task<Body> iteration_type;
  165. iteration_type& i = *new( task::allocate_additional_child_of(*my_barrier) ) iteration_type(item,*my_body);
  166. task::self().spawn( i );
  167. }
  168. } // namespace
  169. #endif /* __TBB_parallel_while */