[neurun] Fix Dfs Iterator (#3721)
author이한종/동작제어Lab(SR)/Engineer/삼성전자 <hanjoung.lee@samsung.com>
Tue, 27 Nov 2018 06:28:46 +0000 (15:28 +0900)
committer이춘석/동작제어Lab(SR)/Staff Engineer/삼성전자 <chunseok.lee@samsung.com>
Tue, 27 Nov 2018 06:28:46 +0000 (15:28 +0900)
As mentioned from #3617 by @d-poshshoev, Visit checking assumed ID range
as vector which is wrong. This commit changes variable `visit` to be a
map so it can correctly check nodes for sure.

Signed-off-by: Hanjoung Lee <hanjoung.lee@samsung.com>
runtimes/neurun/src/graph/Graph.cc

index aa26e92..33aa25c 100644 (file)
@@ -281,13 +281,15 @@ void Graph::PostDfsIterator<is_const>::iterate(GraphRef graph, const IterFn &fn)
 {
   assert(!graph.isBuildingPhase()); // Restrict iteration condition
 
-  std::vector<bool> visited(graph.operations().size(), false);
+  std::unordered_map<operation::Index, bool> visited;
+  graph.operations().iterate(
+      [&](const operation::Index &index, NodeRef) { visited[index] = false; });
 
   std::function<void(const operation::Index &, NodeRef)> dfs_recursive =
       [&](const operation::Index &index, NodeRef node) -> void {
-    if (visited[index.asInt()])
+    if (visited[index])
       return;
-    visited[index.asInt()] = true;
+    visited[index] = true;
 
     for (auto output : node.getOutputs())
     {
@@ -304,7 +306,8 @@ void Graph::PostDfsIterator<is_const>::iterate(GraphRef graph, const IterFn &fn)
   graph.operations().iterate(dfs_recursive);
 
   // All of the operations(nodes) must have been visited.
-  assert(std::all_of(visited.begin(), visited.end(), [](bool v) { return v; }));
+  assert(std::all_of(visited.begin(), visited.end(),
+                     [](const std::pair<const operation::Index, bool> &v) { return v.second; }));
 }
 
 } // namespace graph