Skip to content

Commit e3f2021

Browse files
Tsung-Wei HuangTsung-Wei Huang
authored andcommitted
add dump_graphviz
1 parent 443e87f commit e3f2021

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

example/simple.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ int main(){
1616
[] () { std::cout << "TaskD\n"; }
1717
);
1818

19-
A.precede(B); // B runs after A
19+
A.name("A").precede(B); // B runs after A
2020
A.precede(C); // C runs after A
2121
B.precede(D); // D runs after B
2222
C.precede(D); // C runs after D
2323

24+
std::cout << tf.dump_graphviz();
25+
2426
tf.wait_for_all(); // block until all task finish
2527

2628
return 0;

taskflow.hpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ class BasicTaskflow {
402402
size_t num_workers() const;
403403
size_t num_topologies() const;
404404

405-
std::string dump() const;
405+
std::string dump_graphviz() const;
406406

407407
private:
408408

@@ -830,6 +830,32 @@ void BasicTaskflow<F>::_schedule(Node& task) {
830830
});
831831
}
832832

833+
// Function: dump
834+
// Dumps the taskflow in graphviz. The result can be viewed at http://www.webgraphviz.com/.
835+
template <typename F>
836+
std::string BasicTaskflow<F>::dump_graphviz() const {
837+
838+
std::ostringstream os;
839+
840+
os << "digraph Taskflow {\n";
841+
842+
for(const auto& node : _nodes) {
843+
for(const auto s : node._successors) {
844+
os << " \"";
845+
if(node.name() != "") os << node.name();
846+
else os << &node;
847+
os << "\" -> \"";
848+
if(s->name() != "") os << s->name();
849+
else os << s;
850+
os << "\"\n";
851+
}
852+
}
853+
854+
os << "}\n";
855+
856+
return os.str();
857+
}
858+
833859
// Operator <<
834860
template <typename F>
835861
std::ostream& operator << (std::ostream& os, const BasicTaskflow<F>& tf) {
@@ -850,14 +876,6 @@ std::ostream& operator << (std::ostream& os, const BasicTaskflow<F>& tf) {
850876
return os;
851877
}
852878

853-
//// Function: dump
854-
template <typename F>
855-
std::string BasicTaskflow<F>::dump() const {
856-
std::ostringstream oss;
857-
oss << *this;
858-
return oss.str();
859-
}
860-
861879
//-------------------------------------------------------------------------------------------------
862880

863881
using Taskflow = BasicTaskflow<std::function<void()>>;

0 commit comments

Comments
 (0)