[locomotiv] Introduce NodeExecution helper class (#3402)
author박천교/On-Device Lab(SR)/Engineer/삼성전자 <ch.bahk@samsung.com>
Wed, 8 May 2019 01:29:58 +0000 (10:29 +0900)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Wed, 8 May 2019 01:29:58 +0000 (10:29 +0900)
This commit introduces NodeExecution class. This class serves as helper
class for Session::infer(). This class is responsible to calculate one
specific node.

Signed-off-by: Cheongyo Bahk <ch.bahk@samsung.com>
contrib/locomotiv/CMakeLists.txt
contrib/locomotiv/src/Node/Push.cpp [new file with mode: 0644]
contrib/locomotiv/src/NodeExecution.cpp [new file with mode: 0644]
contrib/locomotiv/src/NodeExecution.h [new file with mode: 0644]

index 27afa0d..d3a64a4 100644 (file)
@@ -4,6 +4,7 @@ list(REMOVE_ITEM SOURCES ${TESTS})
 
 add_library(locomotiv SHARED ${SOURCES})
 target_include_directories(locomotiv PUBLIC include)
+target_include_directories(locomotiv PRIVATE src)
 target_link_libraries(locomotiv PUBLIC loco)
 target_link_libraries(locomotiv PUBLIC angkor)
 # Let's apply nncc common compile options
diff --git a/contrib/locomotiv/src/Node/Push.cpp b/contrib/locomotiv/src/Node/Push.cpp
new file mode 100644 (file)
index 0000000..c4c5fcd
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * 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.
+ */
+
+#include "NodeExecution.h"
+
+#include <stdexcept>
+
+namespace locomotiv
+{
+
+void NodeExecution::execute(loco::Push *)
+{
+  // to be filled
+  throw std::runtime_error("NYI");
+}
+
+} // namespace locomotiv
diff --git a/contrib/locomotiv/src/NodeExecution.cpp b/contrib/locomotiv/src/NodeExecution.cpp
new file mode 100644 (file)
index 0000000..5330810
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * 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.
+ */
+
+#include "NodeExecution.h"
+
+#include <stdexcept>
+
+namespace locomotiv
+{
+
+// TODO Use visitor pattern of loco when available
+void NodeExecution::run(loco::Node *node)
+{
+  if (as<loco::Push>(node))
+  {
+    execute(as<loco::Push>(node));
+  }
+  else
+  {
+    throw std::runtime_error("Not supported loco::Node type");
+  }
+}
+
+} // namespace locomotiv
diff --git a/contrib/locomotiv/src/NodeExecution.h b/contrib/locomotiv/src/NodeExecution.h
new file mode 100644 (file)
index 0000000..8065bc9
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * 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 _LOCOMOTIV_NODEEXECUTION_H_
+#define _LOCOMOTIV_NODEEXECUTION_H_
+
+#include <loco.h>
+
+namespace locomotiv
+{
+
+/**
+ * @brief Helper class for Session, responsible to process one node calculation.
+ */
+class NodeExecution
+{
+public:
+  /// @brief Run calculation for one unspecified Node
+  void run(loco::Node *node);
+
+  static NodeExecution &get()
+  {
+    static NodeExecution me;
+    return me;
+  }
+
+private:
+  NodeExecution() {}
+
+  template <typename Derived> Derived *as(loco::Node *node)
+  {
+    return dynamic_cast<Derived *>(node);
+  }
+
+  /**
+   * @brief Calculate for one specified node and update its result as NodeData.
+   *        Abort program when its ingredients are not ready or not supported.
+   *
+   * @note Definitions of overloaded execute() are in 'Node/' directory
+   */
+  void execute(loco::Push *);
+};
+
+} // namespace locomotiv
+
+#endif // _LOCOMOTIV_NODEEXECUTION_H_