Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] Remove DFS in DOT generation #1896

Merged
merged 1 commit into from
Sep 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 32 additions & 48 deletions taichi/program/state_flow_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,60 +613,44 @@ std::string StateFlowGraph::dump_dot(const std::optional<std::string> &rankdir,
nodes_with_no_inputs.push_back(n);
}
ss << "\n";
{
// DFS to draw edges
std::unordered_set<const SFGNode *> visited;
std::vector<const SFGNode *> stack(nodes_with_no_inputs);
while (!stack.empty()) {
auto *from = stack.back();
stack.pop_back();
if (visited.find(from) == visited.end()) {
visited.insert(from);
for (const auto &p : from->output_edges) {
for (const auto *to : p.second) {
stack.push_back(to);

const bool states_embedded =
(nodes_with_embedded_states.find(from) !=
nodes_with_embedded_states.end());
std::string from_node_port = node_id(from);
std::stringstream attribs;
if (states_embedded) {
// The state is embedded inside the node. We draw the edge from
// the port corresponding to this state.
// Format is "{node}:{port}"
from_node_port += fmt::format(":{}", p.first.name());
} else {
// Show the state on the edge label
attribs << fmt::format("label=\"{}\"", p.first.name());
}

if (!from->has_state_flow(p.first, to)) {
attribs << " style=dotted";
}
for (const auto &n : nodes_) {
auto *from = n.get();
for (const auto &p : from->output_edges) {
for (const auto *to : p.second) {
const bool states_embedded = (nodes_with_embedded_states.find(from) !=
nodes_with_embedded_states.end());
std::string from_node_port = node_id(from);
std::stringstream attribs;
if (states_embedded) {
// The state is embedded inside the node. We draw the edge from
// the port corresponding to this state.
// Format is "{node}:{port}"
from_node_port += fmt::format(":{}", p.first.name());
} else {
// Show the state on the edge label
attribs << fmt::format("label=\"{}\"", p.first.name());
}

if (highlight_single_state) {
if (state_selected(p.first)) {
attribs << " penwidth=5 color=red";
} else {
attribs << " color=lightgrey";
}
}
if (!from->has_state_flow(p.first, to)) {
attribs << " style=dotted";
}

if (node_selected(from) && node_selected(to)) {
ss << " "
<< fmt::format("{} -> {} [{}]", from_node_port, node_id(to),
attribs.str())
<< '\n';
}
if (highlight_single_state) {
if (state_selected(p.first)) {
attribs << " penwidth=5 color=red";
} else {
attribs << " color=lightgrey";
}
}

if (node_selected(from) && node_selected(to)) {
ss << " "
<< fmt::format("{} -> {} [{}]", from_node_port, node_id(to),
attribs.str())
<< '\n';
}
}
}
TI_WARN_IF(
visited.size() > nodes_.size(),
"Visited more nodes than what we actually have. The graph may be "
"malformed.");
}
ss << "}\n"; // closes "dirgraph {"
return ss.str();
Expand Down