[coco] Update dependent objects list of each bag (#844)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Fri, 27 Jul 2018 02:12:00 +0000 (11:12 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Fri, 27 Jul 2018 02:12:00 +0000 (11:12 +0900)
This commit revises 'Object' class to update dependent object lists of
backing bag whenever it updates its backing bag.

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

index e9b8c46..c4c86d1 100644 (file)
@@ -17,7 +17,7 @@ namespace coco
 class FeatureObject final : public Object
 {
 public:
-  explicit FeatureObject(const nncc::core::ADT::feature::Shape &shape);
+  explicit FeatureObject(const PtrLink<Bag, BagInfo> *link, const nncc::core::ADT::feature::Shape &shape);
 
 public:
   virtual ~FeatureObject() = default;
index e72b867..aa83c53 100644 (file)
@@ -17,7 +17,7 @@ namespace coco
 class KernelObject final : public Object
 {
 public:
-  explicit KernelObject(const nncc::core::ADT::kernel::Shape &shape);
+  explicit KernelObject(const PtrLink<Bag, BagInfo> *link, const nncc::core::ADT::kernel::Shape &shape);
 
 public:
   virtual ~KernelObject() = default;
index bb148a9..88ec48b 100644 (file)
@@ -6,6 +6,8 @@
 #include "coco/IR/FeatureObject.forward.h"
 #include "coco/IR/KernelObject.forward.h"
 
+#include "coco/ADT/PtrLink.h"
+
 namespace coco
 {
 
@@ -15,11 +17,14 @@ namespace coco
 class Object
 {
 public:
-  Object();
+  Object(const PtrLink<Bag, BagInfo> *link);
 
 public:
   virtual ~Object() = default;
 
+private:
+  const PtrLink<Bag, BagInfo> * const _link;
+
 protected:
   virtual void onUpdate(Bag *) { return; }
 
index 0572a5f..1a48fb4 100644 (file)
@@ -12,7 +12,8 @@ static nncc::core::ADT::feature::CHWLayout l{};
 namespace coco
 {
 
-FeatureObject::FeatureObject(const nncc::core::ADT::feature::Shape &shape) : _shape{shape}
+FeatureObject::FeatureObject(const PtrLink<Bag, BagInfo> *link, const nncc::core::ADT::feature::Shape &shape)
+    : Object{link}, _shape{shape}
 {
   _map.resize(nncc::core::ADT::feature::num_elements(shape));
 }
index 8cf7983..36f1c3d 100644 (file)
@@ -4,20 +4,24 @@
 
 TEST(IR_FEATURE_OBJECT, ctor_should_set_size)
 {
+  coco::PtrLink<coco::Bag, coco::BagInfo> link;
+
   const nncc::core::ADT::feature::Shape shape{1, 3, 3};
-  const coco::FeatureObject o{shape};
+  const coco::FeatureObject o{&link, shape};
 
   ASSERT_EQ(o.shape(), shape);
 }
 
 TEST(IR_FEATURE_OBJECT, at)
 {
+  coco::PtrLink<coco::Bag, coco::BagInfo> link;
+
   const uint32_t C = 1;
   const uint32_t H = 3;
   const uint32_t W = 3;
 
   const nncc::core::ADT::feature::Shape shape{C, H, W};
-  coco::FeatureObject o{shape};
+  coco::FeatureObject o{&link, shape};
 
   coco::FeatureObject *mutable_ptr = &o;
   const coco::FeatureObject *immutable_ptr = &o;
@@ -47,8 +51,10 @@ TEST(IR_FEATURE_OBJECT, at)
 
 TEST(IR_FEATURE_OBJECT, asFeature)
 {
+  coco::PtrLink<coco::Bag, coco::BagInfo> link;
+
   const nncc::core::ADT::feature::Shape shape{1, 3, 3};
-  coco::FeatureObject o{shape};
+  coco::FeatureObject o{&link, shape};
 
   coco::Object *mutable_object = &o;
   const coco::Object *immutable_object = &o;
index 4217371..354792f 100644 (file)
@@ -12,7 +12,8 @@ static nncc::core::ADT::kernel::NCHWLayout l{};
 namespace coco
 {
 
-KernelObject::KernelObject(const nncc::core::ADT::kernel::Shape &shape) : _shape{shape}
+KernelObject::KernelObject(const PtrLink<Bag, BagInfo> *link, const nncc::core::ADT::kernel::Shape &shape)
+    : Object{link}, _shape{shape}
 {
   _map.resize(nncc::core::ADT::kernel::num_elements(shape));
 }
index a543b9f..67cd86b 100644 (file)
@@ -4,8 +4,10 @@
 
 TEST(IR_KERNEL_OBJECT, ctor_should_set_size)
 {
+  coco::PtrLink<coco::Bag, coco::BagInfo> link;
+
   const nncc::core::ADT::kernel::Shape shape{1, 1, 3, 3};
-  const coco::KernelObject o{shape};
+  const coco::KernelObject o{&link, shape};
 
   // TODO Use ASSERT_EQ(o.shape(), shape) (operator== overload is not yet available)
   ASSERT_EQ(o.shape().depth(), shape.depth());
@@ -16,8 +18,10 @@ TEST(IR_KERNEL_OBJECT, ctor_should_set_size)
 
 TEST(IR_KERNEL_OBJECT, asKernel)
 {
+  coco::PtrLink<coco::Bag, coco::BagInfo> link;
+
   const nncc::core::ADT::kernel::Shape shape{1, 1, 3, 3};
-  coco::KernelObject o{shape};
+  coco::KernelObject o{&link, shape};
 
   coco::Object *mutable_object = &o;
   const coco::Object *immutable_object = &o;
@@ -28,13 +32,15 @@ TEST(IR_KERNEL_OBJECT, asKernel)
 
 TEST(IR_KERNEL_OBJECT, at)
 {
+  coco::PtrLink<coco::Bag, coco::BagInfo> link;
+
   const uint32_t N = 1;
   const uint32_t C = 1;
   const uint32_t H = 3;
   const uint32_t W = 3;
 
   const nncc::core::ADT::kernel::Shape shape{N, C, H, W};
-  coco::KernelObject o{shape};
+  coco::KernelObject o{&link, shape};
 
   coco::KernelObject *mutable_ptr = &o;
   const coco::KernelObject *immutable_ptr = &o;
index 92700ad..95c5c81 100644 (file)
@@ -1,19 +1,36 @@
 #include "coco/IR/Object.h"
 
+#include <cassert>
+#include <stdexcept>
+
 namespace coco
 {
 
-Object::Object() : _bag{nullptr}
+Object::Object(const PtrLink<Bag, BagInfo> *link) : _link{link}, _bag{nullptr}
 {
   // DO NOTHING
 }
 
 void Object::bag(coco::Bag *bag)
 {
-  // TODO Update bag-object relation
-  _bag = bag;
-  // Notify descendant
-  onUpdate(bag);
+  if (_bag != nullptr)
+  {
+    throw std::runtime_error{"Bag unlink is not supported, yet"};
+  }
+
+  assert(_bag == nullptr);
+
+  if (bag != nullptr)
+  {
+    // TODO Update bag-object relation
+    _bag = bag;
+    // Notify descendant
+    onUpdate(bag);
+
+    auto bag_info = _link->find(_bag);
+    assert(bag_info != nullptr);
+    bag_info->object()->insert(this);
+  }
 }
 
 } // namespace coco
index 0bff886..da39d8f 100644 (file)
@@ -1,4 +1,5 @@
 #include "coco/IR/Object.h"
+#include "coco/IR/BagManager.h"
 
 #include <vector>
 
@@ -9,7 +10,8 @@ namespace
 struct DummyObject : public coco::Object
 {
 public:
-  DummyObject(std::vector<coco::Bag *> &updates) : _updates(updates)
+  DummyObject(const coco::PtrLink<coco::Bag, coco::BagInfo> *link, std::vector<coco::Bag *> &updates)
+      : coco::Object{link}, _updates(updates)
   {
     // DO NOTHING
   }
@@ -27,7 +29,8 @@ private:
 
 TEST(IR_OBJECT, ctor)
 {
-  coco::Object obj{};
+  coco::PtrLink<coco::Bag, coco::BagInfo> link;
+  coco::Object obj{&link};
 
   // Newly created object should not have a backing bag
   ASSERT_EQ(obj.bag(), nullptr);
@@ -35,15 +38,27 @@ TEST(IR_OBJECT, ctor)
 
 TEST(IR_OBJECT, bag_update)
 {
-  std::vector<coco::Bag *> updates;
+  coco::PtrLink<coco::Bag, coco::BagInfo> link;
+
+  // Prepare bag
+  coco::BagManager bag_mgr{&link};
+
+  auto bag = bag_mgr.create(1);
 
-  coco::Bag bag{1};
-  DummyObject obj{updates};
+  // Test 'Object' class through 'DummyObject'
+  std::vector<coco::Bag *> updates;
+  DummyObject obj{&link, updates};
 
-  obj.bag(&bag);
+  obj.bag(bag);
 
   // 'bag(Bag *)' should affect the return of 'bag(void)'
-  ASSERT_EQ(obj.bag(), &bag);
+  ASSERT_EQ(obj.bag(), bag);
 
   ASSERT_EQ(updates.size(), 1);
+
+  // The dependent object list of each bag should be updated on 'bag(...)' call.
+  auto bag_info = link.find(bag);
+
+  ASSERT_EQ(bag_info->object()->size(), 1);
+  ASSERT_EQ(bag_info->object()->at(0), &obj);
 }
index 099928b..8692749 100644 (file)
@@ -12,12 +12,12 @@ namespace coco
 
 FeatureObject *ObjectManager::create(const nncc::core::ADT::feature::Shape &shape)
 {
-  return take(make_unique<FeatureObject>(shape));
+  return take(make_unique<FeatureObject>(_link, shape));
 }
 
 KernelObject *ObjectManager::create(const nncc::core::ADT::kernel::Shape &shape)
 {
-  return take(make_unique<KernelObject>(shape));
+  return take(make_unique<KernelObject>(_link, shape));
 }
 
 } // namespace coco