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 
57  bool empty() const;
58 
64  tf::Task composed_of(Taskflow& taskflow);
65 
71  tf::Taskflow& name(const std::string&);
72 
76  const std::string& name() const ;
77 
81  void clear();
82 
83  private:
84 
85  std::string _name;
86 
87  Graph _graph;
88 
89  std::mutex _mtx;
90 
91  std::list<Topology> _topologies;
92 
93  //std::deque<Topology*> _topologies;
94 };
95 
96 // Constructor
98  FlowBuilder {_graph},
99  _name {name} {
100 }
101 
102 // Constructor
103 inline Taskflow::Taskflow() : FlowBuilder{_graph} {
104 }
105 
106 // Destructor
108  assert(_topologies.empty());
109 }
110 
111 // Procedure:
112 inline void Taskflow::clear() {
113  _graph.clear();
114 }
115 
116 // Function: num_noces
117 inline size_t Taskflow::num_nodes() const {
118  return _graph.size();
119 }
120 
121 // Function: empty
122 inline bool Taskflow::empty() const {
123  return _graph.empty();
124 }
125 
126 // Function: name
128  _name = name;
129  return *this;
130 }
131 
132 // Function: name
133 inline const std::string& Taskflow::name() const {
134  return _name;
135 }
136 
137 // Function: composed_of
139  auto &node = _graph.emplace_back();
140  node._module = &taskflow;
141  return Task(node);
142 }
143 
144 // Procedure: dump
145 inline std::string Taskflow::dump() const {
146  std::ostringstream oss;
147  dump(oss);
148  return oss.str();
149 }
150 
151 // Function: dump
152 inline void Taskflow::dump(std::ostream& os) const {
153 
156 
157  os << "digraph Taskflow_";
158  if(_name.empty()) os << 'p' << this;
159  else os << _name;
160  os << " {\nrankdir=\"LR\";\n";
161 
162  stack.push(this);
163  visited.insert(this);
164 
165  while(!stack.empty()) {
166 
167  auto f = stack.top();
168  stack.pop();
169 
170  // create a subgraph field for this taskflow
171  os << "subgraph cluster_";
172  if(f->_name.empty()) os << 'p' << f;
173  else os << f->_name;
174  os << " {\n";
175 
176  os << "label=\"Taskflow_";
177  if(f->_name.empty()) os << 'p' << f;
178  else os << f->_name;
179  os << "\";\n";
180 
181  // dump the details of this taskflow
182  for(const auto& n : f->_graph.nodes()) {
183 
184  // regular task
185  if(auto module = n->_module; !module) {
186  n->dump(os);
187  }
188  // module task
189  else {
190  os << 'p' << n.get() << "[shape=box3d, color=blue, label=\"";
191  if(n->_name.empty()) os << n.get();
192  else os << n->_name;
193  os << " (Taskflow_";
194  if(module->_name.empty()) os << module;
195  else os << module->_name;
196  os << ")\"];\n";
197 
198  if(visited.find(module) == visited.end()) {
199  visited.insert(module);
200  stack.push(module);
201  }
202 
203  for(const auto s : n->_successors) {
204  os << 'p' << n.get() << "->" << 'p' << s << ";\n";
205  }
206  }
207  }
208  os << "}\n";
209  }
210 
211  os << "}\n";
212 }
213 
214 // Backward compatibility
215 using Framework = Taskflow;
216 
217 } // end of namespace tf. ---------------------------------------------------
218 
Taskflow()
constructs a taskflow
Definition: core/taskflow.hpp:103
Definition: taskflow.hpp:5
const std::string & name() const
queries the name of the taskflow
Definition: core/taskflow.hpp:133
void clear()
clears the associated task dependency graph
Definition: core/taskflow.hpp:112
size_t num_nodes() const
queries the number of nodes in the taskflow
Definition: core/taskflow.hpp:117
the class to create a task dependency graph
Definition: core/taskflow.hpp:15
bool empty() const
queries the emptiness of the taskflow
Definition: core/taskflow.hpp:122
std::string dump() const
dumps the taskflow in DOT format to a std::string
Definition: core/taskflow.hpp:145
tf::Task composed_of(Taskflow &taskflow)
creates a module task from a taskflow
Definition: core/taskflow.hpp:138
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:88
virtual ~Taskflow()
destroy the taskflow (virtual call)
Definition: core/taskflow.hpp:107