[neurun] Introduce `operand::BackendSet` (#2750)
author이한종/동작제어Lab(SR)/Engineer/삼성전자 <hanjoung.lee@samsung.com>
Tue, 18 Sep 2018 10:57:14 +0000 (19:57 +0900)
committer이춘석/동작제어Lab(SR)/Staff Engineer/삼성전자 <chunseok.lee@samsung.com>
Tue, 18 Sep 2018 10:57:14 +0000 (19:57 +0900)
BackendSet is a class that is a set of Backends. It will be used from
`operand::LowerInfo` to express which backends need to allocate for a
operand.

Signed-off-by: Hanjoung Lee <hanjoung.lee@samsung.com>
runtimes/neurun/src/graph/operand/BackendSet.cc [new file with mode: 0644]
runtimes/neurun/src/graph/operand/BackendSet.h [new file with mode: 0644]

diff --git a/runtimes/neurun/src/graph/operand/BackendSet.cc b/runtimes/neurun/src/graph/operand/BackendSet.cc
new file mode 100644 (file)
index 0000000..cd99e00
--- /dev/null
@@ -0,0 +1,61 @@
+#include "BackendSet.h"
+
+#include <cassert>
+
+namespace neurun
+{
+namespace graph
+{
+namespace operand
+{
+
+BackendSet::BackendSet(std::initializer_list<const backend::Backend *> backends)
+{
+  for (auto backend : backends)
+  {
+    _set.insert(backend);
+  }
+}
+
+const backend::Backend *BackendSet::getOnlyElement() const
+{
+  assert(_set.size() == 1u);
+  return *_set.begin();
+}
+
+BackendSet BackendSet::operator|(const BackendSet &other) const
+{
+  auto ret = *this;
+  for (auto backend : other)
+  {
+    ret.add(backend);
+  }
+  return ret;
+}
+
+BackendSet BackendSet::operator&(const BackendSet &other) const
+{
+  BackendSet ret;
+  for (auto backend : other)
+  {
+    if (contains(backend))
+    {
+      ret.add(backend);
+    }
+  }
+  return ret;
+}
+
+BackendSet BackendSet::operator-(const BackendSet &other) const
+{
+  auto ret = *this;
+  for (auto backend : other)
+  {
+    ret.remove(backend);
+  }
+  return ret;
+}
+
+} // namespace operand
+} // namespace graph
+} // namespace neurun
diff --git a/runtimes/neurun/src/graph/operand/BackendSet.h b/runtimes/neurun/src/graph/operand/BackendSet.h
new file mode 100644 (file)
index 0000000..b81e76e
--- /dev/null
@@ -0,0 +1,56 @@
+#ifndef __NEURUN_GRAPH_OPERAND_BACKEND_SET_H__
+#define __NEURUN_GRAPH_OPERAND_BACKEND_SET_H__
+
+#include <initializer_list>
+#include <unordered_set>
+
+namespace neurun
+{
+namespace backend
+{
+class Backend;
+} // namespace backend
+} // namespace neurun
+
+namespace neurun
+{
+namespace graph
+{
+namespace operand
+{
+
+class BackendSet
+{
+public:
+  BackendSet() = default;
+  BackendSet(std::initializer_list<const backend::Backend *> backends);
+
+public:
+  void add(const backend::Backend *backend) { _set.insert(backend); }
+  void remove(const backend::Backend *backend) { _set.erase(backend); }
+  uint32_t size() const { return static_cast<uint32_t>(_set.size()); }
+  bool empty() const { return _set.empty(); }
+  bool contains(const backend::Backend *backend) const { return _set.find(backend) != _set.end(); }
+  const backend::Backend *getOnlyElement() const;
+
+public:
+  BackendSet operator|(const BackendSet &other) const; // Union
+  BackendSet operator&(const BackendSet &other) const; // Intersect
+  BackendSet operator-(const BackendSet &other) const; // Minus
+
+public:
+  std::unordered_set<const backend::Backend *>::const_iterator begin() const
+  {
+    return _set.begin();
+  }
+  std::unordered_set<const backend::Backend *>::const_iterator end() const { return _set.end(); }
+
+private:
+  std::unordered_set<const backend::Backend *> _set;
+};
+
+} // namespace operand
+} // namespace graph
+} // namespace neurun
+
+#endif // __NEURUN_GRAPH_OPERAND_BACKEND_SET_H__