From a8ede5b7f0f88ce99ca62912ba8d0c0a12c5b8d4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Fri, 27 Jul 2018 11:12:00 +0900 Subject: [PATCH] [coco] Update dependent objects list of each bag (#844) This commit revises 'Object' class to update dependent object lists of backing bag whenever it updates its backing bag. Signed-off-by: Jonghyun Park --- contrib/coco/core/include/coco/IR/FeatureObject.h | 2 +- contrib/coco/core/include/coco/IR/KernelObject.h | 2 +- contrib/coco/core/include/coco/IR/Object.h | 7 +++++- contrib/coco/core/src/IR/FeatureObject.cpp | 3 ++- contrib/coco/core/src/IR/FeatureObject.test.cpp | 12 +++++++--- contrib/coco/core/src/IR/KernelObject.cpp | 3 ++- contrib/coco/core/src/IR/KernelObject.test.cpp | 12 +++++++--- contrib/coco/core/src/IR/Object.cpp | 27 +++++++++++++++++---- contrib/coco/core/src/IR/Object.test.cpp | 29 +++++++++++++++++------ contrib/coco/core/src/IR/ObjectManager.cpp | 4 ++-- 10 files changed, 76 insertions(+), 25 deletions(-) diff --git a/contrib/coco/core/include/coco/IR/FeatureObject.h b/contrib/coco/core/include/coco/IR/FeatureObject.h index e9b8c46..c4c86d1 100644 --- a/contrib/coco/core/include/coco/IR/FeatureObject.h +++ b/contrib/coco/core/include/coco/IR/FeatureObject.h @@ -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 *link, const nncc::core::ADT::feature::Shape &shape); public: virtual ~FeatureObject() = default; diff --git a/contrib/coco/core/include/coco/IR/KernelObject.h b/contrib/coco/core/include/coco/IR/KernelObject.h index e72b867..aa83c53 100644 --- a/contrib/coco/core/include/coco/IR/KernelObject.h +++ b/contrib/coco/core/include/coco/IR/KernelObject.h @@ -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 *link, const nncc::core::ADT::kernel::Shape &shape); public: virtual ~KernelObject() = default; diff --git a/contrib/coco/core/include/coco/IR/Object.h b/contrib/coco/core/include/coco/IR/Object.h index bb148a9..88ec48b 100644 --- a/contrib/coco/core/include/coco/IR/Object.h +++ b/contrib/coco/core/include/coco/IR/Object.h @@ -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 *link); public: virtual ~Object() = default; +private: + const PtrLink * const _link; + protected: virtual void onUpdate(Bag *) { return; } diff --git a/contrib/coco/core/src/IR/FeatureObject.cpp b/contrib/coco/core/src/IR/FeatureObject.cpp index 0572a5f..1a48fb4 100644 --- a/contrib/coco/core/src/IR/FeatureObject.cpp +++ b/contrib/coco/core/src/IR/FeatureObject.cpp @@ -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 *link, const nncc::core::ADT::feature::Shape &shape) + : Object{link}, _shape{shape} { _map.resize(nncc::core::ADT::feature::num_elements(shape)); } diff --git a/contrib/coco/core/src/IR/FeatureObject.test.cpp b/contrib/coco/core/src/IR/FeatureObject.test.cpp index 8cf7983..36f1c3d 100644 --- a/contrib/coco/core/src/IR/FeatureObject.test.cpp +++ b/contrib/coco/core/src/IR/FeatureObject.test.cpp @@ -4,20 +4,24 @@ TEST(IR_FEATURE_OBJECT, ctor_should_set_size) { + coco::PtrLink 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 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 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; diff --git a/contrib/coco/core/src/IR/KernelObject.cpp b/contrib/coco/core/src/IR/KernelObject.cpp index 4217371..354792f 100644 --- a/contrib/coco/core/src/IR/KernelObject.cpp +++ b/contrib/coco/core/src/IR/KernelObject.cpp @@ -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 *link, const nncc::core::ADT::kernel::Shape &shape) + : Object{link}, _shape{shape} { _map.resize(nncc::core::ADT::kernel::num_elements(shape)); } diff --git a/contrib/coco/core/src/IR/KernelObject.test.cpp b/contrib/coco/core/src/IR/KernelObject.test.cpp index a543b9f..67cd86b 100644 --- a/contrib/coco/core/src/IR/KernelObject.test.cpp +++ b/contrib/coco/core/src/IR/KernelObject.test.cpp @@ -4,8 +4,10 @@ TEST(IR_KERNEL_OBJECT, ctor_should_set_size) { + coco::PtrLink 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 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 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; diff --git a/contrib/coco/core/src/IR/Object.cpp b/contrib/coco/core/src/IR/Object.cpp index 92700ad..95c5c81 100644 --- a/contrib/coco/core/src/IR/Object.cpp +++ b/contrib/coco/core/src/IR/Object.cpp @@ -1,19 +1,36 @@ #include "coco/IR/Object.h" +#include +#include + namespace coco { -Object::Object() : _bag{nullptr} +Object::Object(const PtrLink *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 diff --git a/contrib/coco/core/src/IR/Object.test.cpp b/contrib/coco/core/src/IR/Object.test.cpp index 0bff886..da39d8f 100644 --- a/contrib/coco/core/src/IR/Object.test.cpp +++ b/contrib/coco/core/src/IR/Object.test.cpp @@ -1,4 +1,5 @@ #include "coco/IR/Object.h" +#include "coco/IR/BagManager.h" #include @@ -9,7 +10,8 @@ namespace struct DummyObject : public coco::Object { public: - DummyObject(std::vector &updates) : _updates(updates) + DummyObject(const coco::PtrLink *link, std::vector &updates) + : coco::Object{link}, _updates(updates) { // DO NOTHING } @@ -27,7 +29,8 @@ private: TEST(IR_OBJECT, ctor) { - coco::Object obj{}; + coco::PtrLink 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 updates; + coco::PtrLink 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 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); } diff --git a/contrib/coco/core/src/IR/ObjectManager.cpp b/contrib/coco/core/src/IR/ObjectManager.cpp index 099928b..8692749 100644 --- a/contrib/coco/core/src/IR/ObjectManager.cpp +++ b/contrib/coco/core/src/IR/ObjectManager.cpp @@ -12,12 +12,12 @@ namespace coco FeatureObject *ObjectManager::create(const nncc::core::ADT::feature::Shape &shape) { - return take(make_unique(shape)); + return take(make_unique(_link, shape)); } KernelObject *ObjectManager::create(const nncc::core::ADT::kernel::Shape &shape) { - return take(make_unique(shape)); + return take(make_unique(_link, shape)); } } // namespace coco -- 2.7.4