Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
parallel_while.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2005-2020 Intel Corporation
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 #ifndef __TBB_parallel_while
18 #define __TBB_parallel_while
19 
20 #define __TBB_parallel_while_H_include_area
22 
23 #include "task.h"
24 #include <new>
25 
26 namespace tbb {
27 
28 template<typename Body>
30 
32 namespace internal {
33 
34  template<typename Stream, typename Body> class while_task;
35 
37 
39  template<typename Body>
40  class while_iteration_task: public task {
41  const Body& my_body;
42  typename Body::argument_type my_value;
45  return NULL;
46  }
47  while_iteration_task( const typename Body::argument_type& value, const Body& body ) :
48  my_body(body), my_value(value)
49  {}
50  template<typename Body_> friend class while_group_task;
51  friend class tbb::parallel_while<Body>;
52  };
53 
55 
57  template<typename Body>
58  class while_group_task: public task {
59  static const size_t max_arg_size = 4;
60  const Body& my_body;
61  size_t size;
62  typename Body::argument_type my_arg[max_arg_size];
63  while_group_task( const Body& body ) : my_body(body), size(0) {}
65  typedef while_iteration_task<Body> iteration_type;
66  __TBB_ASSERT( size>0, NULL );
67  task_list list;
68  task* t;
69  size_t k=0;
70  for(;;) {
71  t = new( allocate_child() ) iteration_type(my_arg[k],my_body);
72  if( ++k==size ) break;
73  list.push_back(*t);
74  }
75  set_ref_count(int(k+1));
76  spawn(list);
78  return NULL;
79  }
80  template<typename Stream, typename Body_> friend class while_task;
81  };
82 
84 
86  template<typename Stream, typename Body>
87  class while_task: public task {
88  Stream& my_stream;
89  const Body& my_body;
92  typedef while_group_task<Body> block_type;
93  block_type& t = *new( allocate_additional_child_of(my_barrier) ) block_type(my_body);
94  size_t k=0;
95  while( my_stream.pop_if_present(t.my_arg[k]) ) {
96  if( ++k==block_type::max_arg_size ) {
97  // There might be more iterations.
98  recycle_to_reexecute();
99  break;
100  }
101  }
102  if( k==0 ) {
103  destroy(t);
104  return NULL;
105  } else {
106  t.size = k;
107  return &t;
108  }
109  }
110  while_task( Stream& stream, const Body& body, empty_task& barrier ) :
111  my_stream(stream),
112  my_body(body),
113  my_barrier(barrier)
114  {}
115  friend class tbb::parallel_while<Body>;
116  };
117 
118 } // namespace internal
120 
122 
127 template<typename Body>
129 public:
131  parallel_while() : my_body(NULL), my_barrier(NULL) {}
132 
135  if( my_barrier ) {
136  my_barrier->destroy(*my_barrier);
137  my_barrier = NULL;
138  }
139  }
140 
142  typedef typename Body::argument_type value_type;
143 
145 
148  template<typename Stream>
149  void run( Stream& stream, const Body& body );
150 
152 
153  void add( const value_type& item );
154 
155 private:
156  const Body* my_body;
158 };
159 
160 template<typename Body>
161 template<typename Stream>
162 void parallel_while<Body>::run( Stream& stream, const Body& body ) {
163  using namespace internal;
164  empty_task& barrier = *new( task::allocate_root() ) empty_task();
165  my_body = &body;
166  my_barrier = &barrier;
167  my_barrier->set_ref_count(2);
168  while_task<Stream,Body>& w = *new( my_barrier->allocate_child() ) while_task<Stream,Body>( stream, body, barrier );
169  my_barrier->spawn_and_wait_for_all(w);
170  my_barrier->destroy(*my_barrier);
171  my_barrier = NULL;
172  my_body = NULL;
173 }
174 
175 template<typename Body>
177  __TBB_ASSERT(my_barrier,"attempt to add to parallel_while that is not running");
178  typedef internal::while_iteration_task<Body> iteration_type;
179  iteration_type& i = *new( task::allocate_additional_child_of(*my_barrier) ) iteration_type(item,*my_body);
180  task::self().spawn( i );
181 }
182 
183 } // namespace
184 
186 #undef __TBB_parallel_while_H_include_area
187 
188 #endif /* __TBB_parallel_while */
tbb::internal::while_group_task::while_group_task
while_group_task(const Body &body)
Definition: parallel_while.h:63
tbb::task_list
A list of children.
Definition: task.h:1074
tbb::internal::while_group_task::execute
task * execute() __TBB_override
Should be overridden by derived classes.
Definition: parallel_while.h:64
tbb::parallel_while::parallel_while
parallel_while()
Construct empty non-running parallel while.
Definition: parallel_while.h:131
tbb::internal::while_group_task::size
size_t size
Definition: parallel_while.h:61
internal
Definition: _flow_graph_async_msg_impl.h:24
tbb::task::spawn_and_wait_for_all
void spawn_and_wait_for_all(task &child)
Similar to spawn followed by wait_for_all, but more efficient.
Definition: task.h:800
tbb::internal::while_task::execute
task * execute() __TBB_override
Definition: parallel_while.h:91
__TBB_ASSERT
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
tbb::internal::while_task::my_body
const Body & my_body
Definition: parallel_while.h:89
tbb::internal::while_iteration_task
For internal use only.
Definition: parallel_while.h:40
tbb::internal::while_task::my_stream
Stream & my_stream
Definition: parallel_while.h:88
tbb
The graph class.
Definition: serial/tbb/parallel_for.h:46
tbb::internal::while_iteration_task::my_value
Body::argument_type my_value
Definition: parallel_while.h:42
tbb::task
Base class for user-defined tasks.
Definition: task.h:615
tbb::internal::while_iteration_task::while_iteration_task
while_iteration_task(const typename Body::argument_type &value, const Body &body)
Definition: parallel_while.h:47
tbb::internal::while_iteration_task::execute
task * execute() __TBB_override
Should be overridden by derived classes.
Definition: parallel_while.h:43
tbb::task::allocate_root
static internal::allocate_root_proxy allocate_root()
Returns proxy for overloaded new that allocates a root task.
Definition: task.h:663
tbb::parallel_while::~parallel_while
~parallel_while()
Destructor cleans up data members before returning.
Definition: parallel_while.h:134
tbb::task::self
static task &__TBB_EXPORTED_FUNC self()
The innermost task being executed or destroyed by the current thread at the moment.
Definition: task.cpp:201
tbb::task::set_ref_count
void set_ref_count(int count)
Set reference count.
Definition: task.h:761
tbb::task::allocate_child
internal::allocate_child_proxy & allocate_child()
Returns proxy for overloaded new that allocates a child task of *this.
Definition: task.h:681
tbb::internal::while_task
For internal use only.
Definition: parallel_while.h:34
tbb::internal::while_task::while_task
while_task(Stream &stream, const Body &body, empty_task &barrier)
Definition: parallel_while.h:110
task.h
tbb::internal::while_group_task::max_arg_size
static const size_t max_arg_size
Definition: parallel_while.h:59
tbb::parallel_while::add
void add(const value_type &item)
Add a work item while running.
Definition: parallel_while.h:176
tbb::internal::while_group_task
For internal use only.
Definition: parallel_while.h:58
tbb::parallel_while
Parallel iteration over a stream, with optional addition of more work.
Definition: parallel_while.h:29
tbb::internal::while_iteration_task::my_body
const Body & my_body
Definition: parallel_while.h:41
tbb::internal::no_copy
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:330
_warning_suppress_disable_notice.h
__TBB_override
#define __TBB_override
Definition: tbb_stddef.h:240
tbb::internal::while_group_task::my_body
const Body & my_body
Definition: parallel_while.h:60
tbb::internal::while_task::my_barrier
empty_task & my_barrier
Definition: parallel_while.h:90
tbb::empty_task
task that does nothing. Useful for synchronization.
Definition: task.h:1042
value
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long value
Definition: ittnotify_static.h:192
tbb::internal::while_group_task::my_arg
Body::argument_type my_arg[max_arg_size]
Definition: parallel_while.h:62
tbb::parallel_while::run
void run(Stream &stream, const Body &body)
Apply body.apply to each item in the stream.
Definition: parallel_while.h:162
tbb::parallel_while::value_type
Body::argument_type value_type
Type of items.
Definition: parallel_while.h:142
_warning_suppress_enable_notice.h
tbb::parallel_while::my_body
const Body * my_body
Definition: parallel_while.h:156
tbb::parallel_while::my_barrier
empty_task * my_barrier
Definition: parallel_while.h:157
tbb::task_list::push_back
void push_back(task &task)
Push task onto back of list.
Definition: task.h:1091

Copyright © 2005-2020 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.