Skip to content

Commit 7b5640e

Browse files
Tsung-Wei HuangTsung-Wei Huang
authored andcommitted
added debug example and fix dump error
1 parent 6af4df6 commit 7b5640e

4 files changed

Lines changed: 54 additions & 7 deletions

File tree

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ message(STATUS "EXAMPLE_EXE_LINKER_FLAGS: " ${EXAMPLE_EXE_LINKER_FLAGS})
5757
add_executable(simple example/simple.cpp)
5858
target_link_libraries(simple ${EXAMPLE_EXE_LINKER_FLAGS})
5959

60+
add_executable(debug example/debug.cpp)
61+
target_link_libraries(debug ${EXAMPLE_EXE_LINKER_FLAGS})
62+
6063
add_executable(matrix example/matrix.cpp)
6164
target_compile_options(matrix PRIVATE ${EXAMPLE_CXX_FLAGS})
6265
target_link_libraries(matrix ${EXAMPLE_EXE_LINKER_FLAGS})

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ The folder `example/` contains several examples and is a great place to learn to
471471
| [simple.cpp](./example/simple.cpp) | use basic task building blocks to create a trivial taskflow graph |
472472
| [matrix.cpp](./example/matrix.cpp) | create two set of matrices and multiply each individually in parallel |
473473
| [parallel_for.cpp](./example/parallel_for.cpp)| parallelize a for loop with unbalanced workload |
474+
| [debug.cpp](./example/debug.cpp)| inspect a taskflow through the dump method |
474475

475476
# Get Involved
476477
+ Report bugs/issues by submitting a [Github issue][Github issues].

example/debug.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// This example demonstrates how to use 'dump' method to inspect
2+
// a taskflow graph.
3+
4+
#include "taskflow.hpp"
5+
6+
int main(){
7+
8+
tf::Taskflow tf;
9+
10+
auto [A, B, C, D, E] = tf.silent_emplace(
11+
[] () { std::cout << "Task A" << std::endl; },
12+
[] () { std::cout << "Task B" << std::endl; },
13+
[] () { std::cout << "Task C" << std::endl; },
14+
[] () { std::cout << "Task D" << std::endl; },
15+
[] () { std::cout << "Task E" << std::endl; }
16+
);
17+
18+
A.broadcast(B, C, E);
19+
C.precede(D);
20+
B.broadcast(D, E);
21+
22+
std::cout << "dumpe without name assignment\n";
23+
std::cout << tf.dump() << std::endl;
24+
25+
std::cout << "dumpe with name assignment\n";
26+
A.name("A");
27+
B.name("B");
28+
C.name("C");
29+
D.name("D");
30+
E.name("E");
31+
std::cout << tf.dump() << std::endl;
32+
33+
return 0;
34+
}

taskflow.hpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <list>
4040
#include <forward_list>
4141
#include <numeric>
42+
#include <iomanip>
4243

4344
namespace tf {
4445

@@ -1032,18 +1033,26 @@ std::string BasicTaskflow<F>::dump() const {
10321033
os << "digraph Taskflow {\n";
10331034

10341035
for(const auto& node : _nodes) {
1035-
1036-
os << " \"" << (node.name().empty() ? &node : node.name()) << "\";\n";
1036+
1037+
if(node.name().empty()) os << '\"' << &node << '\"';
1038+
else os << std::quoted(node.name());
1039+
os << ";\n";
10371040

10381041
for(const auto s : node._successors) {
1039-
os << " \"" << (node.name().empty() ? &node : node.name())
1040-
<< "\" -> \""
1041-
<< (s->name().empty() ? s : s->name())
1042-
<< "\";\n";
1042+
1043+
if(node.name().empty()) os << '\"' << &node << '\"';
1044+
else os << std::quoted(node.name());
1045+
1046+
os << " -> ";
1047+
1048+
if(s->name().empty()) os << '\"' << &node << '\"';
1049+
else os << std::quoted(s->name());
1050+
1051+
os << ";\n";
10431052
}
10441053
}
10451054

1046-
os << "}\n";
1055+
os << "}";
10471056

10481057
return os.str();
10491058
}

0 commit comments

Comments
 (0)