[coco] Introduce FeatureObjectInfo (#964)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Fri, 10 Aug 2018 08:57:23 +0000 (17:57 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Fri, 10 Aug 2018 08:57:23 +0000 (17:57 +0900)
* [coco] Introduce FeatureObjectInfo

This commit introduce FeatureObjectInfo class which internally manages
mutable information related with each FeatureObject.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
* Use correct incorrect ifdef guard

contrib/coco/core/include/coco/IR/FeatureObject.h
contrib/coco/core/include/coco/IR/FeatureObjectInfo.forward.h [new file with mode: 0644]
contrib/coco/core/include/coco/IR/FeatureObjectInfo.h [new file with mode: 0644]
contrib/coco/core/src/IR/FeatureObject.cpp
contrib/coco/core/src/IR/FeatureObject.test.cpp
contrib/coco/core/src/IR/FeatureObjectInfo.test.cpp [new file with mode: 0644]
contrib/coco/core/src/IR/ObjectManager.cpp

index 7b3bfbd..0c36145 100644 (file)
@@ -2,6 +2,7 @@
 #define __COCO_IR_FEATURE_OBJECT_H__
 
 #include "coco/IR/Object.h"
+#include "coco/IR/FeatureObjectInfo.h"
 #include "coco/IR/ElemID.h"
 
 #include <nncc/core/ADT/feature/Shape.h>
@@ -18,21 +19,18 @@ namespace coco
 class FeatureObject final : public Object
 {
 public:
-  explicit FeatureObject(const PtrLink<Bag, BagInfo> *link,
-                         const nncc::core::ADT::feature::Shape &shape);
+  explicit FeatureObject(std::unique_ptr<FeatureObjectInfo> &&info,
+                         const PtrLink<Bag, BagInfo> *link);
 
 public:
-  virtual ~FeatureObject() = default;
+  ~FeatureObject();
 
 public:
   FeatureObject *asFeature(void) override { return this; }
   const FeatureObject *asFeature(void) const override { return this; }
 
 public:
-  const nncc::core::ADT::feature::Shape &shape(void) const { return _shape; }
-
-private:
-  nncc::core::ADT::feature::Shape const _shape;
+  const nncc::core::ADT::feature::Shape &shape(void) const;
 
 public:
   ElemID &at(uint32_t ch, uint32_t row, uint32_t col);
@@ -43,6 +41,8 @@ public:
   template <typename LayoutImpl> void reorder(void) { reorder(LayoutImpl{}); }
 
 private:
+  std::unique_ptr<FeatureObjectInfo> _info;
+
   std::vector<ElemID> _map;
 };
 
diff --git a/contrib/coco/core/include/coco/IR/FeatureObjectInfo.forward.h b/contrib/coco/core/include/coco/IR/FeatureObjectInfo.forward.h
new file mode 100644 (file)
index 0000000..84456d2
--- /dev/null
@@ -0,0 +1,11 @@
+#ifndef __COCO_IR_FEATURE_OBJECT_INFO_FORWARD_INFO_H__
+#define __COCO_IR_FEATURE_OBJECT_INFO_FORWARD_INFO_H__
+
+namespace coco
+{
+
+class FeatureObjectInfo;
+
+} // namespace coco
+
+#endif // __COCO_IR_FEATURE_OBJECT_INFO_FORWARD_INFO_H__
diff --git a/contrib/coco/core/include/coco/IR/FeatureObjectInfo.h b/contrib/coco/core/include/coco/IR/FeatureObjectInfo.h
new file mode 100644 (file)
index 0000000..86f659d
--- /dev/null
@@ -0,0 +1,26 @@
+#ifndef __COCO_IR_FEATURE_OBJECT_INFO_H__
+#define __COCO_IR_FEATURE_OBJECT_INFO_H__
+
+#include <nncc/core/ADT/feature/Shape.h>
+
+namespace coco
+{
+
+class FeatureObjectInfo final
+{
+public:
+  FeatureObjectInfo(const nncc::core::ADT::feature::Shape &shape) : _shape{shape}
+  {
+    // DO NOTHING
+  }
+
+public:
+  const nncc::core::ADT::feature::Shape &shape(void) const { return _shape; }
+
+private:
+  nncc::core::ADT::feature::Shape const _shape;
+};
+
+} // namespace coco
+
+#endif // __COCO_IR_FEATURE_OBJECT_INFO_H__
index 36aa772..8c63c41 100644 (file)
@@ -1,4 +1,5 @@
 #include "coco/IR/FeatureObject.h"
+#include "coco/IR/FeatureObjectInfo.h"
 
 #include <nncc/core/ADT/feature/CHWLayout.h>
 
@@ -12,32 +13,39 @@ static nncc::core::ADT::feature::CHWLayout l{};
 namespace coco
 {
 
-FeatureObject::FeatureObject(const PtrLink<Bag, BagInfo> *link,
-                             const nncc::core::ADT::feature::Shape &shape)
-    : Object{link}, _shape{shape}
+FeatureObject::FeatureObject(std::unique_ptr<FeatureObjectInfo> &&info,
+                             const PtrLink<Bag, BagInfo> *link)
+    : Object{link}, _info(std::move(info))
 {
-  _map.resize(nncc::core::ADT::feature::num_elements(shape));
+  _map.resize(nncc::core::ADT::feature::num_elements(shape()));
 }
 
+FeatureObject::~FeatureObject()
+{
+  // DO NOTHING
+}
+
+const nncc::core::ADT::feature::Shape &FeatureObject::shape(void) const { return _info->shape(); }
+
 ElemID &FeatureObject::at(uint32_t ch, uint32_t row, uint32_t col)
 {
-  return _map.at(l.offset(_shape, ch, row, col));
+  return _map.at(l.offset(shape(), ch, row, col));
 }
 
 const ElemID &FeatureObject::at(uint32_t ch, uint32_t row, uint32_t col) const
 {
-  return _map.at(l.offset(_shape, ch, row, col));
+  return _map.at(l.offset(shape(), ch, row, col));
 }
 
 void FeatureObject::reorder(const nncc::core::ADT::feature::Layout &l)
 {
-  for (uint32_t ch = 0; ch < _shape.depth(); ++ch)
+  for (uint32_t ch = 0; ch < shape().depth(); ++ch)
   {
-    for (uint32_t row = 0; row < _shape.height(); ++row)
+    for (uint32_t row = 0; row < shape().height(); ++row)
     {
-      for (uint32_t col = 0; col < _shape.width(); ++col)
+      for (uint32_t col = 0; col < shape().width(); ++col)
       {
-        at(ch, row, col) = ElemID{l.offset(_shape, ch, row, col)};
+        at(ch, row, col) = ElemID{l.offset(shape(), ch, row, col)};
       }
     }
   }
index f3726f0..2139f19 100644 (file)
@@ -1,4 +1,7 @@
 #include "coco/IR/FeatureObject.h"
+#include "coco/IR/FeatureObjectInfo.h"
+
+#include <nncc/foundation/Memory.h>
 
 #include <vector>
 #include <memory>
@@ -6,6 +9,7 @@
 #include <gtest/gtest.h>
 
 using namespace nncc::core::ADT;
+using nncc::foundation::make_unique;
 
 namespace
 {
@@ -14,7 +18,8 @@ class FeatureObjectTest : public ::testing::Test
 protected:
   coco::FeatureObject *allocate(const feature::Shape &shape)
   {
-    auto o = new coco::FeatureObject{&_bag_link, shape};
+    auto info = make_unique<coco::FeatureObjectInfo>(shape);
+    auto o = new coco::FeatureObject{std::move(info), &_bag_link};
     _allocated.emplace_back(o);
     return o;
   }
diff --git a/contrib/coco/core/src/IR/FeatureObjectInfo.test.cpp b/contrib/coco/core/src/IR/FeatureObjectInfo.test.cpp
new file mode 100644 (file)
index 0000000..54d9e1b
--- /dev/null
@@ -0,0 +1,11 @@
+#include "coco/IR/FeatureObjectInfo.h"
+
+#include <gtest/gtest.h>
+
+TEST(FeatureObjectInfoTest, constructor)
+{
+  const nncc::core::ADT::feature::Shape shape{1, 3, 3};
+  const coco::FeatureObjectInfo info{shape};
+
+  ASSERT_EQ(info.shape(), shape);
+}
index 8692749..d7d1e3d 100644 (file)
@@ -1,6 +1,7 @@
 #include "coco/IR/ObjectManager.h"
 
 #include "coco/IR/FeatureObject.h"
+#include "coco/IR/FeatureObjectInfo.h"
 #include "coco/IR/KernelObject.h"
 
 #include <nncc/foundation/Memory.h>
@@ -12,7 +13,8 @@ namespace coco
 
 FeatureObject *ObjectManager::create(const nncc::core::ADT::feature::Shape &shape)
 {
-  return take(make_unique<FeatureObject>(_link, shape));
+  auto info = make_unique<FeatureObjectInfo>(shape);
+  return take(make_unique<FeatureObject>(std::move(info), _link));
 }
 
 KernelObject *ObjectManager::create(const nncc::core::ADT::kernel::Shape &shape)