Skip to content

Commit 199324f

Browse files
updated taskflow.hpp
1 parent 172e06d commit 199324f

1 file changed

Lines changed: 59 additions & 49 deletions

File tree

taskflow/taskflow.hpp

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class Topology;
139139
class Task;
140140
class FlowBuilder;
141141
class SubflowBuilder;
142-
class BasicTaskflow;
142+
class Taskflow;
143143

144144
using Graph = std::forward_list<Node>;
145145

@@ -150,7 +150,7 @@ class Node {
150150

151151
friend class Task;
152152
friend class Topology;
153-
friend class BasicTaskflow;
153+
friend class Taskflow;
154154

155155
using StaticWork = std::function<void()>;
156156
using DynamicWork = std::function<void(SubflowBuilder&)>;
@@ -271,7 +271,7 @@ inline void Node::_dump(std::ostream& os) const {
271271
// class: Topology
272272
class Topology {
273273

274-
friend class BasicTaskflow;
274+
friend class Taskflow;
275275

276276
public:
277277

@@ -354,7 +354,7 @@ inline std::string Topology::dump() const {
354354
class Task {
355355

356356
friend class FlowBuilder;
357-
friend class BasicTaskflow;
357+
friend class Taskflow;
358358

359359
public:
360360

@@ -1047,44 +1047,35 @@ inline auto FlowBuilder::silent_emplace(C&& c) {
10471047
// Taskflow Definition
10481048
// ============================================================================
10491049

1050-
// Class: BasicTaskflow
1051-
class BasicTaskflow : public FlowBuilder {
1052-
1053-
struct Work {
1050+
// Class: Taskflow
1051+
class Taskflow : public FlowBuilder {
10541052

1055-
Work() = default;
1056-
Work(const Work&) = delete;
1057-
1058-
Work(Work&& rhs) : taskflow {rhs.taskflow}, node {rhs.node} {
1059-
rhs.taskflow = nullptr;
1060-
rhs.node = nullptr;
1061-
}
1053+
// Closure
1054+
struct Closure {
1055+
1056+
Closure() = default;
1057+
Closure(const Closure&) = delete;
1058+
Closure(Closure&&);
1059+
Closure(Taskflow&, Node&);
10621060

1063-
Work(BasicTaskflow& t, Node& n) : taskflow{&t}, node {&n} {}
1061+
Closure& operator = (Closure&&);
1062+
Closure& operator = (const Closure&) = delete;
10641063

1065-
BasicTaskflow* taskflow {nullptr};
1066-
Node* node {nullptr};
1067-
1068-
void operator () ();
1069-
1070-
Work& operator = (Work&& rhs) {
1071-
taskflow = rhs.taskflow;
1072-
node = rhs.node;
1073-
rhs.taskflow = nullptr;
1074-
rhs.node = nullptr;
1075-
return *this;
1076-
}
1064+
void operator ()() const;
1065+
1066+
Taskflow* taskflow {nullptr};
1067+
Node* node {nullptr};
10771068
};
10781069

10791070
public:
10801071

10811072
using StaticWork = typename Node::StaticWork;
10821073
using DynamicWork = typename Node::DynamicWork;
10831074

1084-
explicit BasicTaskflow();
1085-
explicit BasicTaskflow(unsigned);
1075+
explicit Taskflow();
1076+
explicit Taskflow(unsigned);
10861077

1087-
~BasicTaskflow();
1078+
~Taskflow();
10881079

10891080
std::shared_future<void> dispatch();
10901081

@@ -1101,7 +1092,7 @@ class BasicTaskflow : public FlowBuilder {
11011092

11021093
private:
11031094

1104-
SimpleThreadpool2<Work> _executor;
1095+
SimpleThreadpool2<Closure> _executor;
11051096

11061097
Graph _graph;
11071098

@@ -1110,8 +1101,29 @@ class BasicTaskflow : public FlowBuilder {
11101101
void _schedule(Node&);
11111102
};
11121103

1113-
// Operator
1114-
void BasicTaskflow::Work::operator () () {
1104+
// Constructor
1105+
inline Taskflow::Closure::Closure(Closure&& rhs) :
1106+
taskflow {rhs.taskflow}, node {rhs.node} {
1107+
rhs.taskflow = nullptr;
1108+
rhs.node = nullptr;
1109+
}
1110+
1111+
// Constructor
1112+
inline Taskflow::Closure::Closure(Taskflow& t, Node& n) :
1113+
taskflow{&t}, node {&n} {
1114+
}
1115+
1116+
// Move assignment
1117+
inline Taskflow::Closure& Taskflow::Closure::operator = (Closure&& rhs) {
1118+
taskflow = rhs.taskflow;
1119+
node = rhs.node;
1120+
rhs.taskflow = nullptr;
1121+
rhs.node = nullptr;
1122+
return *this;
1123+
}
1124+
1125+
// Operator ()
1126+
inline void Taskflow::Closure::operator () () const {
11151127

11161128
assert(taskflow && node);
11171129

@@ -1182,40 +1194,40 @@ void BasicTaskflow::Work::operator () () {
11821194
}
11831195

11841196
// Constructor
1185-
inline BasicTaskflow::BasicTaskflow() :
1197+
inline Taskflow::Taskflow() :
11861198
FlowBuilder {_graph, std::thread::hardware_concurrency()},
11871199
_executor {std::thread::hardware_concurrency()} {
11881200
}
11891201

11901202
// Constructor
1191-
inline BasicTaskflow::BasicTaskflow(unsigned N) :
1203+
inline Taskflow::Taskflow(unsigned N) :
11921204
FlowBuilder {_graph, std::thread::hardware_concurrency()},
11931205
_executor {N} {
11941206
}
11951207

11961208
// Destructor
1197-
inline BasicTaskflow::~BasicTaskflow() {
1209+
inline Taskflow::~Taskflow() {
11981210
wait_for_topologies();
11991211
}
12001212

12011213
// Function: num_nodes
1202-
inline size_t BasicTaskflow::num_nodes() const {
1214+
inline size_t Taskflow::num_nodes() const {
12031215
//return _nodes.size();
12041216
return std::distance(_graph.begin(), _graph.end());
12051217
}
12061218

12071219
// Function: num_workers
1208-
inline size_t BasicTaskflow::num_workers() const {
1220+
inline size_t Taskflow::num_workers() const {
12091221
return _executor.num_workers();
12101222
}
12111223

12121224
// Function: num_topologies
1213-
inline size_t BasicTaskflow::num_topologies() const {
1225+
inline size_t Taskflow::num_topologies() const {
12141226
return std::distance(_topologies.begin(), _topologies.end());
12151227
}
12161228

12171229
// Procedure: silent_dispatch
1218-
inline void BasicTaskflow::silent_dispatch() {
1230+
inline void Taskflow::silent_dispatch() {
12191231

12201232
if(_graph.empty()) return;
12211233

@@ -1226,7 +1238,7 @@ inline void BasicTaskflow::silent_dispatch() {
12261238
}
12271239

12281240
// Procedure: dispatch
1229-
inline std::shared_future<void> BasicTaskflow::dispatch() {
1241+
inline std::shared_future<void> Taskflow::dispatch() {
12301242

12311243
if(_graph.empty()) {
12321244
return std::async(std::launch::deferred, [](){}).share();
@@ -1241,15 +1253,15 @@ inline std::shared_future<void> BasicTaskflow::dispatch() {
12411253
}
12421254

12431255
// Procedure: wait_for_all
1244-
inline void BasicTaskflow::wait_for_all() {
1256+
inline void Taskflow::wait_for_all() {
12451257
if(!_graph.empty()) {
12461258
silent_dispatch();
12471259
}
12481260
wait_for_topologies();
12491261
}
12501262

12511263
// Procedure: wait_for_topologies
1252-
inline void BasicTaskflow::wait_for_topologies() {
1264+
inline void Taskflow::wait_for_topologies() {
12531265
for(auto& t: _topologies){
12541266
t._future.get();
12551267
}
@@ -1259,12 +1271,12 @@ inline void BasicTaskflow::wait_for_topologies() {
12591271
// Procedure: _schedule
12601272
// The main procedure to schedule a give task node.
12611273
// Each task node has two types of tasks - regular and subflow.
1262-
inline void BasicTaskflow::_schedule(Node& node) {
1274+
inline void Taskflow::_schedule(Node& node) {
12631275
_executor.emplace(*this, node);
12641276
}
12651277

12661278
// Function: dump_topology
1267-
inline std::string BasicTaskflow::dump_topologies() const {
1279+
inline std::string Taskflow::dump_topologies() const {
12681280

12691281
std::ostringstream os;
12701282

@@ -1277,7 +1289,7 @@ inline std::string BasicTaskflow::dump_topologies() const {
12771289

12781290
// Function: dump
12791291
// Dumps the taskflow in graphviz. The result can be viewed at http://www.webgraphviz.com/.
1280-
inline std::string BasicTaskflow::dump() const {
1292+
inline std::string Taskflow::dump() const {
12811293

12821294
std::ostringstream os;
12831295

@@ -1294,8 +1306,6 @@ inline std::string BasicTaskflow::dump() const {
12941306

12951307
//-----------------------------------------------------------------------------
12961308

1297-
using Taskflow = BasicTaskflow;
1298-
12991309
}; // end of namespace tf. ---------------------------------------------------
13001310

13011311

0 commit comments

Comments
 (0)