From: 박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 Date: Wed, 5 Dec 2018 03:41:26 +0000 (+0900) Subject: [coco] Introduce OpManager::destroy_all (#2498) X-Git-Tag: nncc_backup~1200 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3f3b3f3bc3dc2a9a106c52df3a709a2d90513abe;p=platform%2Fcore%2Fml%2Fnnfw.git [coco] Introduce OpManager::destroy_all (#2498) With this commit, OpManager now allows users to destroy a Op tree via destroy_all method. Signed-off-by: Jonghyun Park --- diff --git a/contrib/coco/core/include/coco/IR/OpManager.h b/contrib/coco/core/include/coco/IR/OpManager.h index 95ac849..95769f9 100644 --- a/contrib/coco/core/include/coco/IR/OpManager.h +++ b/contrib/coco/core/include/coco/IR/OpManager.h @@ -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 diff --git a/contrib/coco/core/src/IR/OpManager.cpp b/contrib/coco/core/src/IR/OpManager.cpp index 5f8965c..ce94b2d 100644 --- a/contrib/coco/core/src/IR/OpManager.cpp +++ b/contrib/coco/core/src/IR/OpManager.cpp @@ -19,6 +19,7 @@ #include #include +#include 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 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 diff --git a/contrib/coco/core/src/IR/OpManager.test.cpp b/contrib/coco/core/src/IR/OpManager.test.cpp index daaea4c..3afc2da 100644 --- a/contrib/coco/core/src/IR/OpManager.test.cpp +++ b/contrib/coco/core/src/IR/OpManager.test.cpp @@ -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(); + auto conv_op = mgr.create(); + + conv_op->arg(load_op); + + mgr.destroy_all(conv_op); + + ASSERT_EQ(mgr.size(), 0); +}