[enco] Decouple buffer enumerator and memory allocator (#1941)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 23 Oct 2018 02:19:23 +0000 (11:19 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 23 Oct 2018 02:19:23 +0000 (11:19 +0900)
The current implementation of intermediate buffer enumerator is a bit
complicated as it should exclude input/output bags due to the design of
host memory allocator.

With this commit, host memory allocator is responsible for input/output
bag management. This design allows us to reduce coupling between intermediate
buffer enumerator and host memory allocator.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/enco/core/src/CppCode.cpp
contrib/enco/core/src/CppGen/MemoryContext.cpp
contrib/enco/core/src/CppGen/MemoryContext.h

index 39ac7a0..7ef0cb8 100644 (file)
@@ -67,6 +67,7 @@ private:
 };
 
 /** @brief Return a set of Bag to be allocated */
+// NOTE An input/output bag may be included in the retrun set of "intermediates"
 std::set<coco::Bag *> intermediates(const enco::Code *code)
 {
   // NOTE The current implementation may allocate unnecessary buffers (such as weight)
@@ -115,7 +116,7 @@ std::set<coco::Bag *> intermediates(const enco::Code *code)
   {
     auto bag = code->module()->entity()->bag()->at(n);
 
-    if (!bag->isInput() && !bag->isOutput())
+    // TODO Remove unnecessary indentation
     {
       if (accessed_by_host(bag))
       {
@@ -245,6 +246,12 @@ void CppCode::dump(std::ostream &os) const
   // Set dedicated memory reigion for intermediate buffer(s)
   for (const auto &bag : intermediates(_code))
   {
+    // Skip if a bag is already allocated
+    if (mem.member(bag))
+    {
+      continue;
+    }
+
     auto name = invoke.local();
 
     invoke.head.append("auto ", name, " = new uint8_t[", bag->size() * sizeof(float), "];");
index 1daca15..e522968 100644 (file)
 
 #include "MemoryContext.h"
 
+#include <cassert>
+
 namespace enco
 {
 
+bool MemoryContext::member(const coco::Bag *bag) const
+{
+  // NOTE _base and _size SHOULD BE consistent
+  if (_base.find(bag) != _base.end())
+  {
+    assert(_size.find(bag) != _size.end());
+    return true;
+  }
+
+  assert(_size.find(bag) == _size.end());
+  return false;
+}
+
 void MemoryContext::base(const coco::Bag *bag, const std::string &exp) { _base[bag] = exp; }
 void MemoryContext::size(const coco::Bag *bag, const std::string &exp) { _size[bag] = exp; }
 
index c2c3bfc..99c20f3 100644 (file)
@@ -32,6 +32,12 @@ namespace enco
 class MemoryContext
 {
 public:
+  /**
+   * @brief Check whether a base/size expression for a given bag
+   */
+  bool member(const coco::Bag *bag) const;
+
+public:
   void base(const coco::Bag *bag, const std::string &exp);
   void size(const coco::Bag *bag, const std::string &exp);