[coco] Support Bag replace (#1467)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 12 Sep 2018 06:04:11 +0000 (15:04 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 12 Sep 2018 06:04:11 +0000 (15:04 +0900)
This commit introduces replaceWith method in Bag, which allows users to
replace all the occurences of a bag in IR with another bag.

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

index d9a54a8..b3e4bf8 100644 (file)
@@ -65,6 +65,12 @@ public:
   // @brief Return the set of Update links that point to this bag
   const UpdateSet *updates(void) const;
 
+public:
+  // @brief Replace all the occurence of a bag (except those in Input/Output) with another bag
+  //
+  // NOTE reaplceWith(b) works correctly only when b is neither Input nor Output
+  void replaceWith(Bag *b);
+
 private:
   std::unique_ptr<BagInfo> _info;
 };
index c368b5c..5371a42 100644 (file)
@@ -229,3 +229,33 @@ TEST(IR, caffe_conv)
     }
   }
 }
+
+//
+// This test demonstrates how to use 'replaceWith' method
+//
+TEST(IR, bag_replaceWith)
+{
+  auto m = coco::Module::create();
+
+  auto bag_1 = m->entity()->bag()->create(1);
+  auto bag_2 = m->entity()->bag()->create(1);
+
+  auto obj = m->entity()->object()->create(nncc::core::ADT::feature::Shape{1, 1, 1});
+  obj->bag(bag_1);
+
+  auto shuffle_1 = m->entity()->instr()->create<coco::Shuffle>();
+  shuffle_1->into(bag_1);
+
+  auto shuffle_2 = m->entity()->instr()->create<coco::Shuffle>();
+  shuffle_2->from(bag_1);
+
+  ASSERT_EQ(obj->bag(), bag_1);
+  ASSERT_EQ(shuffle_1->into(), bag_1);
+  ASSERT_EQ(shuffle_2->from(), bag_1);
+
+  bag_1->replaceWith(bag_2);
+
+  ASSERT_EQ(obj->bag(), bag_2);
+  ASSERT_EQ(shuffle_1->into(), bag_2);
+  ASSERT_EQ(shuffle_2->from(), bag_2);
+}
index 1e2e4e4..4038028 100644 (file)
@@ -34,6 +34,39 @@ const DepSet *Bag::deps(void) const { return _info->deps(); }
 const ReadSet *Bag::reads(void) const { return _info->reads(); }
 const UpdateSet *Bag::updates(void) const { return _info->updates(); }
 
+void Bag::replaceWith(Bag *b)
+{
+  assert(!isInput() && !isOutput());
+
+  // Replace all the occurence inside Dep
+  while (!(deps()->empty()))
+  {
+    auto dep = *(deps()->begin());
+    assert(dep->bag() == this);
+    dep->bag(b);
+  }
+
+  // Replace all the occurence inside Read
+  while (!(reads()->empty()))
+  {
+    auto read = *(reads()->begin());
+    assert(read->bag() == this);
+    read->bag(b);
+  }
+
+  // Replace all the occurence insider Update
+  while (!(updates()->empty()))
+  {
+    auto update = *(updates()->begin());
+    assert(update->bag() == this);
+    update->bag(b);
+  }
+
+  assert(deps()->empty());
+  assert(reads()->empty());
+  assert(updates()->empty());
+}
+
 ObjectSet dependent_objects(const Bag *b)
 {
   ObjectSet res;