parallel_while.h
Go to the documentation of this file.
/*
Copyright (c) 2005-2020 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef __TBB_parallel_while
#define __TBB_parallel_while
#define __TBB_parallel_while_H_include_area
#include "internal/_warning_suppress_enable_notice.h"
#include "task.h"
#include <new>
namespace tbb {
template<typename Body>
class parallel_while;
namespace internal {
template<typename Stream, typename Body> class while_task;
template<typename Body>
class while_iteration_task: public task {
const Body& my_body;
typename Body::argument_type my_value;
task* execute() __TBB_override {
my_body(my_value);
return NULL;
}
while_iteration_task( const typename Body::argument_type& value, const Body& body ) :
my_body(body), my_value(value)
{}
template<typename Body_> friend class while_group_task;
friend class tbb::parallel_while<Body>;
};
template<typename Body>
class while_group_task: public task {
static const size_t max_arg_size = 4;
const Body& my_body;
size_t size;
typename Body::argument_type my_arg[max_arg_size];
while_group_task( const Body& body ) : my_body(body), size(0) {}
task* execute() __TBB_override {
typedef while_iteration_task<Body> iteration_type;
__TBB_ASSERT( size>0, NULL );
task_list list;
task* t;
size_t k=0;
for(;;) {
t = new( allocate_child() ) iteration_type(my_arg[k],my_body);
if( ++k==size ) break;
list.push_back(*t);
}
set_ref_count(int(k+1));
spawn(list);
spawn_and_wait_for_all(*t);
return NULL;
}
template<typename Stream, typename Body_> friend class while_task;
};
template<typename Stream, typename Body>
class while_task: public task {
Stream& my_stream;
const Body& my_body;
empty_task& my_barrier;
task* execute() __TBB_override {
typedef while_group_task<Body> block_type;
block_type& t = *new( allocate_additional_child_of(my_barrier) ) block_type(my_body);
size_t k=0;
while( my_stream.pop_if_present(t.my_arg[k]) ) {
if( ++k==block_type::max_arg_size ) {
// There might be more iterations.
recycle_to_reexecute();
break;
}
}
if( k==0 ) {
destroy(t);
return NULL;
} else {
t.size = k;
return &t;
}
}
while_task( Stream& stream, const Body& body, empty_task& barrier ) :
my_stream(stream),
my_body(body),
my_barrier(barrier)
{}
friend class tbb::parallel_while<Body>;
};
} // namespace internal
template<typename Body>
class parallel_while: internal::no_copy {
public:
parallel_while() : my_body(NULL), my_barrier(NULL) {}
~parallel_while() {
if( my_barrier ) {
my_barrier->destroy(*my_barrier);
my_barrier = NULL;
}
}
typedef typename Body::argument_type value_type;
template<typename Stream>
void run( Stream& stream, const Body& body );
void add( const value_type& item );
private:
const Body* my_body;
empty_task* my_barrier;
};
template<typename Body>
template<typename Stream>
void parallel_while<Body>::run( Stream& stream, const Body& body ) {
using namespace internal;
empty_task& barrier = *new( task::allocate_root() ) empty_task();
my_body = &body;
my_barrier = &barrier;
my_barrier->set_ref_count(2);
while_task<Stream,Body>& w = *new( my_barrier->allocate_child() ) while_task<Stream,Body>( stream, body, barrier );
my_barrier->spawn_and_wait_for_all(w);
my_barrier->destroy(*my_barrier);
my_barrier = NULL;
my_body = NULL;
}
template<typename Body>
void parallel_while<Body>::add( const value_type& item ) {
__TBB_ASSERT(my_barrier,"attempt to add to parallel_while that is not running");
typedef internal::while_iteration_task<Body> iteration_type;
iteration_type& i = *new( task::allocate_additional_child_of(*my_barrier) ) iteration_type(item,*my_body);
task::self().spawn( i );
}
} // namespace
#include "internal/_warning_suppress_disable_notice.h"
#undef __TBB_parallel_while_H_include_area
#endif /* __TBB_parallel_while */
_warning_suppress_disable_notice.h
_warning_suppress_enable_notice.h
task.h
__TBB_ASSERT
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
__TBB_override
#define __TBB_override
Definition: tbb_stddef.h:240
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
The graph class.
Definition: serial/tbb/parallel_for.h:46
internal
Definition: _flow_graph_async_msg_impl.h:24
tbb::parallel_while
Parallel iteration over a stream, with optional addition of more work.
Definition: parallel_while.h:128
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::my_body
const Body * my_body
Definition: parallel_while.h:156
tbb::parallel_while::parallel_while
parallel_while()
Construct empty non-running parallel while.
Definition: parallel_while.h:131
tbb::parallel_while::value_type
Body::argument_type value_type
Type of items.
Definition: parallel_while.h:142
tbb::parallel_while::add
void add(const value_type &item)
Add a work item while running.
Definition: parallel_while.h:176
tbb::parallel_while::my_barrier
empty_task * my_barrier
Definition: parallel_while.h:157
tbb::parallel_while::~parallel_while
~parallel_while()
Destructor cleans up data members before returning.
Definition: parallel_while.h:134
tbb::internal::while_task
For internal use only.
Definition: parallel_while.h:87
tbb::internal::while_task::execute
task * execute() __TBB_override
Should be overridden by derived classes.
Definition: parallel_while.h:91
tbb::internal::while_task::my_stream
Stream & my_stream
Definition: parallel_while.h:88
tbb::internal::while_task::my_body
const Body & my_body
Definition: parallel_while.h:89
tbb::internal::while_task::my_barrier
empty_task & my_barrier
Definition: parallel_while.h:90
tbb::internal::while_task::while_task
while_task(Stream &stream, const Body &body, empty_task &barrier)
Definition: parallel_while.h:110
tbb::internal::while_iteration_task
For internal use only.
Definition: parallel_while.h:40
tbb::internal::while_iteration_task::my_value
Body::argument_type my_value
Definition: parallel_while.h:42
tbb::internal::while_iteration_task::execute
task * execute() __TBB_override
Should be overridden by derived classes.
Definition: parallel_while.h:43
tbb::internal::while_iteration_task::my_body
const Body & my_body
Definition: parallel_while.h:41
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_group_task
For internal use only.
Definition: parallel_while.h:58
tbb::internal::while_group_task::execute
task * execute() __TBB_override
Should be overridden by derived classes.
Definition: parallel_while.h:64
tbb::internal::while_group_task::max_arg_size
static const size_t max_arg_size
Definition: parallel_while.h:59
tbb::internal::while_group_task::size
size_t size
Definition: parallel_while.h:61
tbb::internal::while_group_task::while_group_task
while_group_task(const Body &body)
Definition: parallel_while.h:63
tbb::internal::while_group_task::my_arg
Body::argument_type my_arg[max_arg_size]
Definition: parallel_while.h:62
tbb::internal::while_group_task::my_body
const Body & my_body
Definition: parallel_while.h:60
tbb::task
Base class for user-defined tasks.
Definition: task.h:615
tbb::task::recycle_to_reexecute
void recycle_to_reexecute()
Schedule this for reexecution after current execute() returns.
Definition: task.h:741
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::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::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::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::task::set_ref_count
void set_ref_count(int count)
Set reference count.
Definition: task.h:761
tbb::empty_task
task that does nothing. Useful for synchronization.
Definition: task.h:1042
tbb::task_list
A list of children.
Definition: task.h:1074
tbb::task_list::push_back
void push_back(task &task)
Push task onto back of list.
Definition: task.h:1091
tbb::internal::no_copy
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:330

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.