[nncc.core] Copyable/movable tensor layout (#369)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 27 Jun 2018 02:00:22 +0000 (11:00 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 27 Jun 2018 02:00:22 +0000 (11:00 +0900)
This commit revises 'ADT::tensor::Layout' to be copyable and movable.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
libs/core/include/nncc/core/ADT/tensor/Layout.h
libs/core/include/nncc/core/ADT/tensor/LexicalLayout.h
libs/core/src/ADT/tensor/Layout.cpp [new file with mode: 0644]
libs/core/src/ADT/tensor/Layout.test.cpp [new file with mode: 0644]
libs/core/src/ADT/tensor/LexicalLayout.cpp

index 68fc067..3f7b52a 100644 (file)
@@ -13,11 +13,19 @@ namespace ADT
 namespace tensor
 {
 
-struct Layout
+class Layout
 {
-  virtual ~Layout() = default;
+public:
+  using Func = uint32_t (*)(const Shape &, const Index &);
 
-  virtual uint32_t offset(const Shape &, const Index &) const = 0;
+public:
+  explicit Layout(const Func &func);
+
+public:
+  uint32_t offset(const Shape &shape, const Index &index) const { return _func(shape, index); }
+
+private:
+  Func _func;
 };
 
 } // namespace tensor
index 8012b68..25ee762 100644 (file)
@@ -14,9 +14,7 @@ namespace tensor
 
 struct LexicalLayout final : public Layout
 {
-  ~LexicalLayout() = default;
-
-  uint32_t offset(const Shape &, const Index &) const override;
+  LexicalLayout();
 };
 
 } // namespace tensor
diff --git a/libs/core/src/ADT/tensor/Layout.cpp b/libs/core/src/ADT/tensor/Layout.cpp
new file mode 100644 (file)
index 0000000..b45b1b5
--- /dev/null
@@ -0,0 +1,19 @@
+#include "nncc/core/ADT/tensor/Layout.h"
+
+#include <cassert>
+
+namespace nncc
+{
+namespace core
+{
+namespace ADT
+{
+namespace tensor
+{
+
+Layout::Layout(const Func &func) : _func{func} { assert(_func != nullptr); }
+
+} // namespace tensor
+} // namespace ADT
+} // namespace core
+} // namespace nncc
diff --git a/libs/core/src/ADT/tensor/Layout.test.cpp b/libs/core/src/ADT/tensor/Layout.test.cpp
new file mode 100644 (file)
index 0000000..4173135
--- /dev/null
@@ -0,0 +1,40 @@
+#include "nncc/core/ADT/tensor/Layout.h"
+
+#include <gtest/gtest.h>
+
+using nncc::core::ADT::tensor::Shape;
+using nncc::core::ADT::tensor::Index;
+
+static uint32_t offset_0(const Shape &, const Index &) { return 0; }
+static uint32_t offset_1(const Shape &, const Index &) { return 1; }
+
+TEST(ADT_TENSOR_LAYOUT, ctor)
+{
+  nncc::core::ADT::tensor::Layout l{offset_0};
+
+  ASSERT_EQ(l.offset(Shape{4, 3, 6}, Index{1, 1, 1}), 0);
+}
+
+TEST(ADT_TENSOR_LAYOUT, copy)
+{
+  nncc::core::ADT::tensor::Layout orig{offset_0};
+  nncc::core::ADT::tensor::Layout copy{offset_1};
+
+  ASSERT_EQ(copy.offset(Shape{4, 3, 6}, Index{1, 1, 1}), 1);
+
+  copy = orig;
+
+  ASSERT_EQ(copy.offset(Shape{4, 3, 6}, Index{1, 1, 1}), 0);
+}
+
+TEST(ADT_TENSOR_LAYOUT, move)
+{
+  nncc::core::ADT::tensor::Layout orig{offset_0};
+  nncc::core::ADT::tensor::Layout move{offset_1};
+
+  ASSERT_EQ(move.offset(Shape{4, 3, 6}, Index{1, 1, 1}), 1);
+
+  move = std::move(orig);
+
+  ASSERT_EQ(move.offset(Shape{4, 3, 6}, Index{1, 1, 1}), 0);
+}
index 5db8661..44bc55f 100644 (file)
@@ -2,16 +2,11 @@
 
 #include <cassert>
 
-namespace nncc
-{
-namespace core
-{
-namespace ADT
-{
-namespace tensor
-{
+using nncc::core::ADT::tensor::Shape;
+using nncc::core::ADT::tensor::Index;
 
-uint32_t LexicalLayout::offset(const Shape &shape, const Index &index) const
+// NOTE This forward declaration is introduced to minimize code diff
+static uint32_t lexical_offset(const Shape &shape, const Index &index)
 {
   assert(shape.rank() > 0);
   assert(shape.rank() == index.rank());
@@ -29,6 +24,20 @@ uint32_t LexicalLayout::offset(const Shape &shape, const Index &index) const
   return res;
 }
 
+namespace nncc
+{
+namespace core
+{
+namespace ADT
+{
+namespace tensor
+{
+
+LexicalLayout::LexicalLayout() : Layout(lexical_offset)
+{
+  // DO NOTHING
+}
+
 } // namespace tensor
 } // namespace ADT
 } // namespace core