3#include "../utility/macros.hpp"
4#include "../utility/traits.hpp"
5#include "../utility/iterator.hpp"
7#ifdef TF_ENABLE_TASK_POOL
8#include "../utility/object_pool.hpp"
11#include "../utility/os.hpp"
12#include "../utility/math.hpp"
13#include "../utility/small_vector.hpp"
14#include "../utility/serializer.hpp"
15#include "../utility/lazy_string.hpp"
17#include "declarations.hpp"
18#include "semaphore.hpp"
19#include "environment.hpp"
20#include "topology.hpp"
50 friend class FlowBuilder;
52 friend class Taskflow;
53 friend class Executor;
124 std::vector<Node*> _nodes;
131 template <
typename ...ArgsT>
132 Node* _emplace_back(ArgsT&&...);
180 std::constructible_from<std::string, P>;
211 friend class NonpreemptiveRuntime;
212 friend class ExplicitAnchorGuard;
214 friend class Algorithm;
218 nstate_t _nstate {NSTATE::NONE};
219 std::atomic<estate_t> _estate {ESTATE::NONE};
221 NodeBase* _parent {
nullptr};
222 std::atomic<size_t> _join_counter {0};
224 std::exception_ptr _exception_ptr {
nullptr};
226 NodeBase() =
default;
228 NodeBase(nstate_t nstate, estate_t estate, NodeBase* parent,
size_t join_counter) :
232 _join_counter {join_counter} {
235 void _rethrow_exception() {
237 auto e = _exception_ptr;
238 _exception_ptr =
nullptr;
239 _estate.fetch_and(~(ESTATE::EXCEPTION | ESTATE::CAUGHT), std::memory_order_relaxed);
240 std::rethrow_exception(e);
252class Topology :
public NodeBase {
254 friend class Executor;
255 friend class Subflow;
256 friend class Runtime;
257 friend class NonpreemptiveRuntime;
260 template <
typename T>
265 template <
typename Predicate,
typename OnFinish>
266 Topology(Taskflow&, Predicate&&, OnFinish&&);
268 bool cancelled()
const;
274 std::promise<void> _promise;
276 std::function<bool()> _predicate;
277 std::function<void()> _on_finish;
279 void _carry_out_promise();
283template <
typename Predicate,
typename OnFinish>
284Topology::Topology(
Taskflow& tf, Predicate&& predicate, OnFinish&& on_finish):
285 NodeBase(NSTATE::NONE, ESTATE::EXPLICITLY_ANCHORED, nullptr, 0),
287 _predicate(std::forward<Predicate>(predicate)),
288 _on_finish(std::forward<OnFinish> (on_finish)) {
292inline void Topology::_carry_out_promise() {
294 auto e = _exception_ptr;
295 _exception_ptr =
nullptr;
296 _promise.set_exception(e);
299 _promise.set_value();
304inline bool Topology::cancelled()
const {
305 return _estate.load(std::memory_order_relaxed) & (ESTATE::CANCELLED | ESTATE::EXCEPTION);
316class Node :
public NodeBase {
320 friend class AsyncTask;
321 friend class TaskView;
322 friend class Taskflow;
323 friend class Executor;
324 friend class FlowBuilder;
325 friend class Subflow;
326 friend class Runtime;
327 friend class NonpreemptiveRuntime;
328 friend class ExplicitAnchorGuard;
329 friend class TaskGroup;
330 friend class Algorithm;
332#ifdef TF_ENABLE_TASK_POOL
333 TF_ENABLE_POOLABLE_ON_THIS;
336 using Placeholder = std::monostate;
341 template <
typename C>
344 std::function<void()> work;
350 template <
typename C>
353 std::function<void(tf::Runtime&)> work;
356 struct NonpreemptiveRuntime {
358 template <
typename C>
359 NonpreemptiveRuntime(C&&);
361 std::function<void(tf::NonpreemptiveRuntime&)> work;
367 template <
typename C>
370 std::function<void(tf::Subflow&)> work;
377 template <
typename C>
380 std::function<int()> work;
384 struct MultiCondition {
386 template <
typename C>
389 std::function<SmallVector<int>()> work;
401 struct AdoptedModule {
403 AdoptedModule(Graph&&);
411 template <
typename T>
415 std::function<void()>,
416 std::function<void(tf::Runtime&)>,
417 std::function<void(tf::Runtime&,
bool)>
422 struct DependentAsync {
424 template <
typename C>
428 std::function<void()>,
429 std::function<void(tf::Runtime&)>,
430 std::function<void(tf::Runtime&,
bool)>
433 std::atomic<size_t> use_count {1};
436 using handle_t = std::variant<
440 NonpreemptiveRuntime,
451 SmallVector<Semaphore*> to_acquire;
452 SmallVector<Semaphore*> to_release;
458 constexpr static auto PLACEHOLDER = get_index_v<Placeholder, handle_t>;
459 constexpr static auto STATIC = get_index_v<Static, handle_t>;
460 constexpr static auto RUNTIME = get_index_v<Runtime, handle_t>;
461 constexpr static auto NONPREEMPTIVE_RUNTIME = get_index_v<NonpreemptiveRuntime, handle_t>;
462 constexpr static auto SUBFLOW = get_index_v<Subflow, handle_t>;
463 constexpr static auto CONDITION = get_index_v<Condition, handle_t>;
464 constexpr static auto MULTI_CONDITION = get_index_v<MultiCondition, handle_t>;
465 constexpr static auto MODULE = get_index_v<Module, handle_t>;
466 constexpr static auto ADOPTED_MODULE = get_index_v<AdoptedModule, handle_t>;
467 constexpr static auto ASYNC = get_index_v<Async, handle_t>;
468 constexpr static auto DEPENDENT_ASYNC = get_index_v<DependentAsync, handle_t>;
472 template <
typename... Args>
473 Node(nstate_t, estate_t,
const TaskParams&, Topology*, NodeBase*,
size_t, Args&&...);
475 template <
typename... Args>
476 Node(nstate_t, estate_t,
const DefaultTaskParams&, Topology*, NodeBase*,
size_t, Args&&...);
478 size_t num_successors()
const;
479 size_t num_predecessors()
const;
480 size_t num_strong_dependencies()
const;
481 size_t num_weak_dependencies()
const;
483 const std::string& name()
const;
489 void* _data {
nullptr};
491 Topology* _topology {
nullptr};
493 size_t _num_successors {0};
494 SmallVector<Node*, 4> _edges;
498 std::unique_ptr<Semaphores> _semaphores;
500 bool _is_parent_cancelled()
const;
501 bool _is_conditioner()
const;
502 bool _acquire_all(SmallVector<Node*>&);
503 void _release_all(SmallVector<Node*>&);
504 void _precede(Node*);
505 void _set_up_join_counter();
507 void _remove_successors(Node*);
508 void _remove_predecessors(Node*);
518Node::Static::Static(C&& c) : work {std::forward<C>(c)} {
527Node::Runtime::Runtime(C&& c) : work {std::forward<C>(c)} {
532Node::NonpreemptiveRuntime::NonpreemptiveRuntime(C&& c) : work {std::forward<C>(c)} {
541Node::Subflow::Subflow(C&& c) : work {std::forward<C>(c)} {
550Node::Condition::Condition(C&& c) : work {std::forward<C>(c)} {
559Node::MultiCondition::MultiCondition(C&& c) : work {std::forward<C>(c)} {
567inline Node::Module::Module(Graph& g) : graph(g){
571inline Node::AdoptedModule::AdoptedModule(Graph&& g) : graph(std::move(g)){
580Node::Async::Async(C&& c) : work {std::forward<C>(c)} {
589Node::DependentAsync::DependentAsync(C&& c) : work {std::forward<C>(c)} {
597template <
typename... Args>
601 const TaskParams& params,
607 NodeBase(nstate, estate, parent, join_counter),
610 _topology {topology},
611 _handle {std::forward<Args>(args)...} {
615template <
typename... Args>
619 const DefaultTaskParams&,
625 NodeBase(nstate, estate, parent, join_counter),
626 _topology {topology},
627 _handle {std::forward<Args>(args)...} {
694inline void Node::_precede(Node* v) {
696 std::swap(_edges[_num_successors++], _edges[_edges.size() - 1]);
697 v->_edges.push_back(
this);
701inline void Node::_remove_successors(Node* node) {
702 auto sit = std::remove(_edges.begin(), _edges.begin() + _num_successors, node);
703 size_t new_num_successors = std::distance(_edges.begin(), sit);
704 std::move(_edges.begin() + _num_successors, _edges.end(), sit);
705 _edges.resize(_edges.size() - (_num_successors - new_num_successors));
706 _num_successors = new_num_successors;
710inline void Node::_remove_predecessors(Node* node) {
712 std::remove(_edges.begin() + _num_successors, _edges.end(), node), _edges.end()
717inline size_t Node::num_successors()
const {
718 return _num_successors;
722inline size_t Node::num_predecessors()
const {
723 return _edges.size() - _num_successors;
727inline size_t Node::num_weak_dependencies()
const {
729 for(
size_t i=_num_successors; i<_edges.size(); i++) {
730 n += _edges[i]->_is_conditioner();
736inline size_t Node::num_strong_dependencies()
const {
738 for(
size_t i=_num_successors; i<_edges.size(); i++) {
739 n += !_edges[i]->_is_conditioner();
745inline const std::string& Node::name()
const {
750inline bool Node::_is_conditioner()
const {
751 return _handle.index() == Node::CONDITION ||
752 _handle.index() == Node::MULTI_CONDITION;
756inline bool Node::_is_parent_cancelled()
const {
757 return (_topology && (_topology->_estate.load(std::memory_order_relaxed) & (ESTATE::CANCELLED | ESTATE::EXCEPTION)))
759 (_parent && (_parent->_estate.load(std::memory_order_relaxed) & (ESTATE::CANCELLED | ESTATE::EXCEPTION)));
763inline void Node::_set_up_join_counter() {
765 for(
size_t i=_num_successors; i<_edges.size(); i++) {
766 _nstate += !_edges[i]->_is_conditioner();
768 _join_counter.store(_nstate & NSTATE::STRONG_DEPENDENCIES_MASK, std::memory_order_relaxed);
773inline bool Node::_acquire_all(SmallVector<Node*>& nodes) {
775 auto& to_acquire = _semaphores->to_acquire;
776 for(
size_t i = 0; i < to_acquire.size(); ++i) {
777 if(!to_acquire[i]->_try_acquire_or_wait(
this)) {
778 for(
size_t j = 1; j <= i; ++j) {
779 to_acquire[i-j]->_release(nodes);
788inline void Node::_release_all(SmallVector<Node*>& nodes) {
790 auto& to_release = _semaphores->to_release;
791 for(
const auto& sem : to_release) {
792 sem->_release(nodes);
805class ExplicitAnchorGuard {
811 ExplicitAnchorGuard(NodeBase* node_base) : _node_base{node_base} {
812 _node_base->_estate.fetch_or(ESTATE::EXPLICITLY_ANCHORED, std::memory_order_relaxed);
815 ~ExplicitAnchorGuard() {
816 _node_base->_estate.fetch_and(~ESTATE::EXPLICITLY_ANCHORED, std::memory_order_relaxed);
821 NodeBase* _node_base;
831#ifdef TF_ENABLE_TASK_POOL
832inline ObjectPool<Node> _node_pool;
838template <
typename... ArgsT>
839TF_FORCE_INLINE Node* animate(ArgsT&&... args) {
840#ifdef TF_ENABLE_TASK_POOL
841 return _node_pool.animate(std::forward<ArgsT>(args)...);
843 return new Node(std::forward<ArgsT>(args)...);
850TF_FORCE_INLINE
void recycle(Node* ptr) {
851#ifdef TF_ENABLE_TASK_POOL
852 _node_pool.recycle(ptr);
870 _nodes {std::move(other._nodes)} {
876 _nodes = std::move(other._nodes);
882 for(
auto node : _nodes) {
890 return _nodes.size();
895 return _nodes.empty();
900 return _nodes.begin();
910 return _nodes.begin();
919inline void Graph::_erase(Node* node) {
925 std::remove_if(_nodes.begin(), _nodes.end(), [&](
auto& p){
939template <
typename ...ArgsT>
940Node* Graph::_emplace_back(ArgsT&&... args) {
941 _nodes.push_back(animate(std::forward<ArgsT>(args)...));
942 return _nodes.back();
959 { t.graph() } -> std::convertible_to<Graph&>;
976 if constexpr (
requires { t.graph(); }) {
979 return static_cast<Graph&
>(t);
class to hold a dependent asynchronous task with shared ownership
Definition async_task.hpp:45
class to create an empty task parameter for compile-time optimization
Definition graph.hpp:166
class to create an executor
Definition executor.hpp:62
class to build a task dependency graph
Definition flow_builder.hpp:22
class to create a graph object
Definition graph.hpp:47
Graph & operator=(const Graph &)=delete
disabled copy assignment operator
Graph()=default
constructs the graph object
bool empty() const
queries the emptiness of the graph
Definition graph.hpp:894
~Graph()
destroys the graph object
Definition graph.hpp:864
auto end()
returns an iterator past the last element of this graph
Definition graph.hpp:904
size_t size() const
returns the number of nodes in the graph
Definition graph.hpp:889
void clear()
clears the graph
Definition graph.hpp:881
auto begin()
returns an iterator to the first node of this graph
Definition graph.hpp:899
Graph(const Graph &)=delete
disabled copy constructor
class to create a runtime task
Definition runtime.hpp:47
class to construct a subflow graph from the execution of a dynamic task
Definition flow_builder.hpp:1516
class to create a task group from a task
Definition task_group.hpp:61
class to create a task parameter object
Definition graph.hpp:146
std::string name
name of the task
Definition graph.hpp:153
void * data
C-styled pointer to user data.
Definition graph.hpp:158
class to access task information from the observer interface
Definition task.hpp:1235
class to create a task handle over a taskflow node
Definition task.hpp:263
class to create a taskflow object
Definition taskflow.hpp:64
determines if a type owns or provides a graph
Definition graph.hpp:957
determines if a type is a task parameter type
Definition graph.hpp:177
taskflow namespace
Definition small_vector.hpp:20
@ MODULE
module task type
Definition task.hpp:33
@ SUBFLOW
dynamic (subflow) task type
Definition task.hpp:29
@ CONDITION
condition task type
Definition task.hpp:31
@ ASYNC
asynchronous task type
Definition task.hpp:35
@ PLACEHOLDER
placeholder task type
Definition task.hpp:23
@ RUNTIME
runtime task type
Definition task.hpp:27
@ STATIC
static task type
Definition task.hpp:25
Graph & retrieve_graph(T &t)
retrieves a reference to the underlying tf::Graph from an object
Definition graph.hpp:975
constexpr bool is_task_params_v
determines if a type is a task parameter type (variable template)
Definition graph.hpp:190