3#include "../utility/macros.hpp"
4#include "../utility/traits.hpp"
5#include "../utility/iterator.hpp"
7#ifdef TF_ENABLE_TASK_POOL
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 using Placeholder = std::monostate;
337 template <
typename C>
340 std::function<void()> work;
346 template <
typename C>
349 std::function<void(tf::Runtime&)> work;
352 struct NonpreemptiveRuntime {
354 template <
typename C>
355 NonpreemptiveRuntime(C&&);
357 std::function<void(tf::NonpreemptiveRuntime&)> work;
363 template <
typename C>
366 std::function<void(tf::Subflow&)> work;
373 template <
typename C>
376 std::function<int()> work;
380 struct MultiCondition {
382 template <
typename C>
385 std::function<SmallVector<int>()> work;
397 struct AdoptedModule {
399 AdoptedModule(Graph&&);
407 template <
typename T>
411 std::function<void()>,
412 std::function<void(tf::Runtime&)>,
413 std::function<void(tf::Runtime&,
bool)>
418 struct DependentAsync {
420 template <
typename C>
424 std::function<void()>,
425 std::function<void(tf::Runtime&)>,
426 std::function<void(tf::Runtime&,
bool)>
434 using handle_t = std::variant<
438 NonpreemptiveRuntime,
449 SmallVector<Semaphore*> to_acquire;
450 SmallVector<Semaphore*> to_release;
456 constexpr static auto PLACEHOLDER = get_index_v<Placeholder, handle_t>;
457 constexpr static auto STATIC = get_index_v<Static, handle_t>;
458 constexpr static auto RUNTIME = get_index_v<Runtime, handle_t>;
459 constexpr static auto NONPREEMPTIVE_RUNTIME = get_index_v<NonpreemptiveRuntime, handle_t>;
460 constexpr static auto SUBFLOW = get_index_v<Subflow, handle_t>;
461 constexpr static auto CONDITION = get_index_v<Condition, handle_t>;
462 constexpr static auto MULTI_CONDITION = get_index_v<MultiCondition, handle_t>;
463 constexpr static auto MODULE = get_index_v<Module, handle_t>;
464 constexpr static auto ADOPTED_MODULE = get_index_v<AdoptedModule, handle_t>;
465 constexpr static auto ASYNC = get_index_v<Async, handle_t>;
466 constexpr static auto DEPENDENT_ASYNC = get_index_v<DependentAsync, handle_t>;
470 template <
typename... Args>
471 Node(nstate_t, estate_t,
const TaskParams&, Topology*, NodeBase*,
size_t, Args&&...);
473 template <
typename... Args>
474 Node(nstate_t, estate_t,
const DefaultTaskParams&, Topology*, NodeBase*,
size_t, Args&&...);
476 size_t num_successors()
const;
477 size_t num_predecessors()
const;
478 size_t num_strong_dependencies()
const;
479 size_t num_weak_dependencies()
const;
481 const std::string& name()
const;
487 void* _data {
nullptr};
489 Topology* _topology {
nullptr};
491 size_t _num_successors {0};
492 SmallVector<Node*, 4> _edges;
496 std::unique_ptr<Semaphores> _semaphores;
498 bool _is_parent_cancelled()
const;
499 bool _is_conditioner()
const;
500 bool _acquire_all(SmallVector<Node*>&);
501 void _release_all(SmallVector<Node*>&);
502 void _precede(Node*);
503 void _set_up_join_counter();
505 void _remove_successors(Node*);
506 void _remove_predecessors(Node*);
516Node::Static::Static(C&& c) : work {std::forward<C>(c)} {
525Node::Runtime::Runtime(C&& c) : work {std::forward<C>(c)} {
530Node::NonpreemptiveRuntime::NonpreemptiveRuntime(C&& c) : work {std::forward<C>(c)} {
539Node::Subflow::Subflow(C&& c) : work {std::forward<C>(c)} {
548Node::Condition::Condition(C&& c) : work {std::forward<C>(c)} {
557Node::MultiCondition::MultiCondition(C&& c) : work {std::forward<C>(c)} {
565inline Node::Module::Module(Graph& g) : graph(g){
569inline Node::AdoptedModule::AdoptedModule(Graph&& g) : graph(std::move(g)){
578Node::Async::Async(C&& c) : work {std::forward<C>(c)} {
587Node::DependentAsync::DependentAsync(C&& c) : work {std::forward<C>(c)} {
595template <
typename... Args>
599 const TaskParams& params,
605 NodeBase(nstate, estate, parent, join_counter),
608 _topology {topology},
609 _handle {std::forward<Args>(args)...} {
613template <
typename... Args>
617 const DefaultTaskParams&,
623 NodeBase(nstate, estate, parent, join_counter),
624 _topology {topology},
625 _handle {std::forward<Args>(args)...} {
692inline void Node::_precede(Node* v) {
694 std::swap(_edges[_num_successors++], _edges[_edges.size() - 1]);
695 v->_edges.push_back(
this);
699inline void Node::_remove_successors(Node* node) {
700 auto sit = std::remove(_edges.begin(), _edges.begin() + _num_successors, node);
701 size_t new_num_successors = std::distance(_edges.begin(), sit);
702 std::move(_edges.begin() + _num_successors, _edges.end(), sit);
703 _edges.resize(_edges.size() - (_num_successors - new_num_successors));
704 _num_successors = new_num_successors;
708inline void Node::_remove_predecessors(Node* node) {
710 std::remove(_edges.begin() + _num_successors, _edges.end(), node), _edges.end()
715inline size_t Node::num_successors()
const {
716 return _num_successors;
720inline size_t Node::num_predecessors()
const {
721 return _edges.size() - _num_successors;
725inline size_t Node::num_weak_dependencies()
const {
727 for(
size_t i=_num_successors; i<_edges.size(); i++) {
728 n += _edges[i]->_is_conditioner();
734inline size_t Node::num_strong_dependencies()
const {
736 for(
size_t i=_num_successors; i<_edges.size(); i++) {
737 n += !_edges[i]->_is_conditioner();
743inline const std::string& Node::name()
const {
748inline bool Node::_is_conditioner()
const {
749 return _handle.index() == Node::CONDITION ||
750 _handle.index() == Node::MULTI_CONDITION;
754inline bool Node::_is_parent_cancelled()
const {
755 return (_topology && (_topology->_estate.load(std::memory_order_relaxed) & (ESTATE::CANCELLED | ESTATE::EXCEPTION)))
757 (_parent && (_parent->_estate.load(std::memory_order_relaxed) & (ESTATE::CANCELLED | ESTATE::EXCEPTION)));
761inline void Node::_set_up_join_counter() {
763 for(
size_t i=_num_successors; i<_edges.size(); i++) {
764 _nstate += !_edges[i]->_is_conditioner();
766 _join_counter.store(_nstate & NSTATE::STRONG_DEPENDENCIES_MASK, std::memory_order_relaxed);
771inline bool Node::_acquire_all(SmallVector<Node*>& nodes) {
773 auto& to_acquire = _semaphores->to_acquire;
774 for(
size_t i = 0; i < to_acquire.size(); ++i) {
775 if(!to_acquire[i]->_try_acquire_or_wait(
this)) {
776 for(
size_t j = 1; j <= i; ++j) {
777 to_acquire[i-j]->_release(nodes);
786inline void Node::_release_all(SmallVector<Node*>& nodes) {
788 auto& to_release = _semaphores->to_release;
789 for(
const auto& sem : to_release) {
790 sem->_release(nodes);
803class ExplicitAnchorGuard {
809 ExplicitAnchorGuard(NodeBase* node_base) : _node_base{node_base} {
810 _node_base->_estate.fetch_or(ESTATE::EXPLICITLY_ANCHORED, std::memory_order_relaxed);
813 ~ExplicitAnchorGuard() {
814 _node_base->_estate.fetch_and(~ESTATE::EXPLICITLY_ANCHORED, std::memory_order_relaxed);
819 NodeBase* _node_base;
829#ifdef TF_ENABLE_TASK_POOL
834 AtomicIntrusiveStack<NodeBase*, &NodeBase::_parent> _stack;
838 template <
typename... ArgsT>
839 Node* animate(ArgsT&&... args) {
840 if(
auto n = _stack.pop(); n) {
841 return new(n) Node(std::forward<ArgsT>(args)...);
843 return new Node(std::forward<ArgsT>(args)...);
846 void recycle(Node* ptr) {
848 _stack.push(
static_cast<NodeBase*
>(ptr));
866inline NodePool _node_pool;
872template <
typename... ArgsT>
873TF_FORCE_INLINE Node* animate(ArgsT&&... args) {
874#ifdef TF_ENABLE_TASK_POOL
875 return _node_pool.animate(std::forward<ArgsT>(args)...);
877 return new Node(std::forward<ArgsT>(args)...);
884TF_FORCE_INLINE
void recycle(Node* ptr) {
885#ifdef TF_ENABLE_TASK_POOL
886 _node_pool.recycle(ptr);
904 _nodes {std::move(other._nodes)} {
910 _nodes = std::move(other._nodes);
916 for(
auto node : _nodes) {
924 return _nodes.size();
929 return _nodes.empty();
934 return _nodes.begin();
944 return _nodes.begin();
953inline void Graph::_erase(Node* node) {
959 std::remove_if(_nodes.begin(), _nodes.end(), [&](
auto& p){
973template <
typename ...ArgsT>
974Node* Graph::_emplace_back(ArgsT&&... args) {
975 _nodes.push_back(animate(std::forward<ArgsT>(args)...));
976 return _nodes.back();
1013template <
typename T>
1016 { t.graph() } -> std::convertible_to<Graph&>;
1052template <HasGraph T>
1054 if constexpr (
requires { target.graph(); }) {
1055 return target.graph();
1057 return static_cast<Graph&
>(target);
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:928
~Graph()
destroys the graph object
Definition graph.hpp:898
auto end()
returns an iterator past the last element of this graph
Definition graph.hpp:938
size_t size() const
returns the number of nodes in the graph
Definition graph.hpp:923
void clear()
clears the graph
Definition graph.hpp:915
auto begin()
returns an iterator to the first node of this graph
Definition graph.hpp:933
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:1722
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:1240
class to create a task handle over a taskflow node
Definition task.hpp:263
class to create a taskflow object
Definition taskflow.hpp:64
concept that determines if a type owns or provides access to a tf::Graph
Definition graph.hpp:1014
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 &target)
retrieves a reference to the underlying tf::Graph from an object
Definition graph.hpp:1053
constexpr bool is_task_params_v
determines if a type is a task parameter type (variable template)
Definition graph.hpp:190