Introduce IStage, AtomicStage and CompositStage (#4374)
author김용섭/On-Device Lab(SR)/Engineer/삼성전자 <yons.kim@samsung.com>
Sun, 10 Feb 2019 23:54:24 +0000 (08:54 +0900)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Sun, 10 Feb 2019 23:54:24 +0000 (08:54 +0900)
* Introduce IStage, AtomicStage and CompositStage

Introduce IStage, AtomicStage and CompositStage
- IStage: pure interface for replacing current Stage
- AtomicStage: Stage which can be called as atomic op
- CompositStage: A Stage or Stages which can be grouped

Signed-off-by: Yongseop Kim <yons.kim@samsung.com>
* Move {Atomic,Composit}Stage to each own file

* Change struct to final class

* Change CompositStage to StageSequence

* final class to class final

runtimes/neurun/src/backend/AtomicStage.h [new file with mode: 0644]
runtimes/neurun/src/backend/StageSequence.h [new file with mode: 0644]
runtimes/neurun/src/backend/interface/IStage.h [new file with mode: 0644]
runtimes/neurun/src/backend/interface/IStageGenerator.h

diff --git a/runtimes/neurun/src/backend/AtomicStage.h b/runtimes/neurun/src/backend/AtomicStage.h
new file mode 100644 (file)
index 0000000..f46e3d8
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __NEURUN_BACKEND_ATOMIC_STAGE_H__
+#define __NEURUN_BACKEND_ATOMIC_STAGE_H__
+
+#include <vector>
+#include <functional>
+
+#include "backend/interface/IStage.h"
+
+namespace neurun
+{
+namespace backend
+{
+
+class AtomicStage final : public IStage
+{
+public:
+  AtomicStage() = default;
+  template <typename F> AtomicStage(F f) : fn(f) {}
+
+  virtual void operator()(IExecutionBuilder &execution_builder) const override
+  {
+    fn(execution_builder);
+  }
+
+  virtual void operator<<(StageFn f) override { fn = f; }
+
+private:
+  StageFn fn = nullptr;
+};
+
+} // namespace backend
+} // namespace neurun
+
+#endif // __NEURUN_BACKEND_ATOMIC_STAGE_H__
diff --git a/runtimes/neurun/src/backend/StageSequence.h b/runtimes/neurun/src/backend/StageSequence.h
new file mode 100644 (file)
index 0000000..bfada22
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __NEURUN_BACKEND_STAGE_SEQUENCE_H__
+#define __NEURUN_BACKEND_STAGE_SEQUENCE_H__
+
+#include <vector>
+#include <functional>
+
+#include "backend/interface/IStage.h"
+#include "cpp14/memory.h"
+
+namespace neurun
+{
+namespace backend
+{
+
+class StageSequence final : public IStage
+{
+public:
+  StageSequence() = default;
+  template <typename F> StageSequence(F f)
+  {
+    stages.emplace_back(nnfw::cpp14::make_unique<AtomicStage>(f));
+  }
+
+  virtual void operator()(IExecutionBuilder &execution_builder) const override
+  {
+    for (const auto &stage : stages)
+      (*stage)(execution_builder);
+  }
+
+  virtual void operator<<(StageFn f) override
+  {
+    stages.emplace_back(nnfw::cpp14::make_unique<AtomicStage>(f));
+  }
+
+private:
+  std::vector<std::unique_ptr<IStage>> stages;
+};
+
+} // namespace backend
+} // namespace neurun
+
+#endif // __NEURUN_BACKEND_STAGE_SEQUENCE_H__
diff --git a/runtimes/neurun/src/backend/interface/IStage.h b/runtimes/neurun/src/backend/interface/IStage.h
new file mode 100644 (file)
index 0000000..49612cf
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __NEURUN_BACKEND_ISTAGE_H__
+#define __NEURUN_BACKEND_ISTAGE_H__
+
+#include <functional>
+
+struct IExecutionBuilder;
+
+namespace neurun
+{
+namespace backend
+{
+
+using StageFn = std::function<void(IExecutionBuilder &)>;
+
+struct IStage
+{
+  IStage() = default;
+  virtual ~IStage() = default;
+
+  virtual void operator()(IExecutionBuilder &execution_builder) const = 0;
+  virtual void operator<<(StageFn f) = 0;
+};
+
+} // namespace backend
+} // namespace neurun
+
+#endif // __NEURUN_BACKEND_ISTAGE_H__
index 20fe3c2..5966923 100644 (file)
@@ -32,6 +32,7 @@ struct IExecutionBuilder
   virtual void append(std::unique_ptr<::neurun::exec::IFunction> &&f) = 0;
 };
 
+// TODO: Replace as IStage
 using Stage = std::function<void(IExecutionBuilder &)>;
 
 namespace neurun