[coco] Introduce OpManager::destroy_all (#2498)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 5 Dec 2018 03:41:26 +0000 (12:41 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 5 Dec 2018 03:41:26 +0000 (12:41 +0900)
With this commit, OpManager now allows users to destroy a Op tree via
destroy_all method.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/coco/core/include/coco/IR/OpManager.h
contrib/coco/core/src/IR/OpManager.cpp
contrib/coco/core/src/IR/OpManager.test.cpp

index 95ac849..95769f9 100644 (file)
@@ -44,6 +44,11 @@ public:
   //
   // NOTE destroy(op) WILL NOT update op->parent(). Client SHOULD detach op before destroy(op) call
   void destroy(Op *);
+
+  // @brief Destroy a Op tree
+  //
+  // @require op->parent() == nullptr && op->up() == nullptr
+  void destroy_all(Op *);
 };
 
 } // namespace coco
index 5f8965c..ce94b2d 100644 (file)
@@ -19,6 +19,7 @@
 #include <nncc/foundation/Memory.h>
 
 #include <cassert>
+#include <queue>
 
 using nncc::foundation::make_unique;
 
@@ -44,4 +45,29 @@ void OpManager::destroy(Op *op)
   release(op);
 }
 
+void OpManager::destroy_all(Op *op)
+{
+  assert(op->parent() == nullptr);
+  assert(op->up() == nullptr);
+
+  std::queue<coco::Op *> q;
+
+  q.emplace(op);
+
+  while (q.size() > 0)
+  {
+    auto cur = q.front();
+    q.pop();
+
+    // Insert child op nodes
+    for (uint32_t n = 0; n < cur->arity(); ++n)
+    {
+      q.emplace(cur->arg(n));
+    }
+
+    // Destroy the current op node
+    destroy(cur);
+  }
+}
+
 } // namespace coco
index daaea4c..3afc2da 100644 (file)
@@ -74,3 +74,16 @@ TEST_F(OpManagerTest, destroy)
   mgr.destroy(op);
   ASSERT_EQ(mgr.size(), 0);
 }
+
+TEST_F(OpManagerTest, destroy_all)
+{
+  // Create a Op tree
+  auto load_op = mgr.create<coco::Load>();
+  auto conv_op = mgr.create<coco::Conv2D>();
+
+  conv_op->arg(load_op);
+
+  mgr.destroy_all(conv_op);
+
+  ASSERT_EQ(mgr.size(), 0);
+}