Cpp-Taskflow  2.2.0
core/taskflow.hpp
1 #pragma once
2 
3 #include <stack>
4 #include "flow_builder.hpp"
5 #include "topology.hpp"
6 
7 namespace tf {
8 
15 class Taskflow : public FlowBuilder {
16 
17  friend class Topology;
18  friend class Executor;
19 
20  public:
21 
25  Taskflow(const std::string& name);
26 
30  Taskflow();
31 
35  virtual ~Taskflow();
36 
42  void dump(std::ostream& ostream) const;
43 
47  std::string dump() const;
48 
52  size_t num_nodes() const;
53 
59  tf::Task composed_of(Taskflow& taskflow);
60 
66  tf::Taskflow& name(const std::string&);
67 
71  const std::string& name() const ;
72 
76  void clear();
77 
78  private:
79 
80  std::string _name;
81 
82  Graph _graph;
83 
84  std::mutex _mtx;
85 
86  std::list<Topology> _topologies;
87 
88  //std::deque<Topology*> _topologies;
89 };
90 
91 // Constructor
93  FlowBuilder {_graph},
94  _name {name} {
95 }
96 
97 // Constructor
98 inline Taskflow::Taskflow() : FlowBuilder{_graph} {
99 }
100 
101 // Destructor
103  assert(_topologies.empty());
104 }
105 
106 // Procedure:
107 inline void Taskflow::clear() {
108  _graph.clear();
109 }
110 
111 // Function: num_noces
112 inline size_t Taskflow::num_nodes() const {
113  return _graph.size();
114 }
115 
116 // Function: name
118  _name = name;
119  return *this;
120 }
121 
122 // Function: name
123 inline const std::string& Taskflow::name() const {
124  return _name;
125 }
126 
127 // Function: composed_of
129  auto &node = _graph.emplace_back();
130  node._module = &taskflow;
131  return Task(node);
132 }
133 
134 // Procedure: dump
135 inline std::string Taskflow::dump() const {
136  std::ostringstream oss;
137  dump(oss);
138  return oss.str();
139 }
140 
141 // Function: dump
142 inline void Taskflow::dump(std::ostream& os) const {
143 
146 
147  os << "digraph Taskflow_";
148  if(_name.empty()) os << 'p' << this;
149  else os << _name;
150  os << " {\nrankdir=\"LR\";\n";
151 
152  stack.push(this);
153  visited.insert(this);
154 
155  while(!stack.empty()) {
156 
157  auto f = stack.top();
158  stack.pop();
159 
160  // create a subgraph field for this taskflow
161  os << "subgraph cluster_";
162  if(f->_name.empty()) os << 'p' << f;
163  else os << f->_name;
164  os << " {\n";
165 
166  os << "label=\"Taskflow_";
167  if(f->_name.empty()) os << 'p' << f;
168  else os << f->_name;
169  os << "\";\n";
170 
171  // dump the details of this taskflow
172  for(const auto n : f->_graph.nodes()) {
173 
174  // regular task
175  if(auto module = n->_module; !module) {
176  n->dump(os);
177  }
178  // module task
179  else {
180  os << 'p' << n << "[shape=box3d, color=blue, label=\"";
181  if(n->_name.empty()) os << n;
182  else os << n->_name;
183  os << " (Taskflow_";
184  if(module->_name.empty()) os << module;
185  else os << module->_name;
186  os << ")\"];\n";
187 
188  if(visited.find(module) == visited.end()) {
189  visited.insert(module);
190  stack.push(module);
191  }
192 
193  for(const auto s : n->_successors) {
194  os << 'p' << n << "->" << 'p' << s << ";\n";
195  }
196  }
197  }
198  os << "}\n";
199  }
200 
201  os << "}\n";
202 }
203 
204 // Backward compatibility
205 using Framework = Taskflow;
206 
207 } // end of namespace tf. ---------------------------------------------------
208 
Taskflow()
constructs a taskflow
Definition: core/taskflow.hpp:98
Definition: taskflow.hpp:5
const std::string & name() const
queries the name of the taskflow
Definition: core/taskflow.hpp:123
void clear()
clears the associated task dependency graph
Definition: core/taskflow.hpp:107
size_t num_nodes() const
queries the number of nodes in the taskflow
Definition: core/taskflow.hpp:112
the class to create a task dependency graph
Definition: core/taskflow.hpp:15
std::string dump() const
dumps the taskflow in DOT format to a std::string
Definition: core/taskflow.hpp:135
tf::Task composed_of(Taskflow &taskflow)
creates a module task from a taskflow
Definition: core/taskflow.hpp:128
Building blocks of a task dependency graph.
Definition: flow_builder.hpp:13
Handle to modify and access a task.
Definition: task.hpp:18
The executor class to run a taskflow graph.
Definition: executor.hpp:73
virtual ~Taskflow()
destroy the taskflow (virtual call)
Definition: core/taskflow.hpp:102