Support async execution in NNAPI (#6395)
author오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Thu, 8 Aug 2019 09:52:39 +0000 (18:52 +0900)
committer이한종/On-Device Lab(SR)/Engineer/삼성전자 <hanjoung.lee@samsung.com>
Thu, 8 Aug 2019 09:52:39 +0000 (18:52 +0900)
Implement Event class to support async execution
Use internal async execution function for startCompute and wait

Signed-off-by: Hyeongseok Oh <hseok82.oh@samsung.com>
runtimes/neurun/frontend/nnapi/event.cc
runtimes/neurun/frontend/nnapi/execution.cc
runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksEvent.cc [new file with mode: 0644]
runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksEvent.h
runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksExecution.cc
runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksExecution.h

index 05e4b9e..593b74e 100644 (file)
@@ -25,6 +25,11 @@ int ANeuralNetworksEvent_wait(ANeuralNetworksEvent *event)
     return ANEURALNETWORKS_UNEXPECTED_NULL;
   }
 
+  if (!event->waitFinish())
+  {
+    return ANEURALNETWORKS_BAD_STATE;
+  }
+
   return ANEURALNETWORKS_NO_ERROR;
 }
 
index 3773c4d..837cab0 100644 (file)
@@ -240,14 +240,15 @@ int ANeuralNetworksExecution_startCompute(ANeuralNetworksExecution *execution,
   }
 
   // TODO: Handle event
-  *event = new (std::nothrow) ANeuralNetworksEvent{};
+  auto instance = execution->instance();
+  *event = new (std::nothrow) ANeuralNetworksEvent{instance};
   if (*event == nullptr)
   {
     VERBOSE(NNAPI::Execution) << "startCompute: Fail to create event" << std::endl;
     return ANEURALNETWORKS_OUT_OF_MEMORY;
   }
 
-  if (!execution->execute())
+  if (!execution->startExecute())
   {
     VERBOSE(NNAPI::Execution) << "startCompute: Fail to start execution" << std::endl;
     return ANEURALNETWORKS_BAD_STATE;
diff --git a/runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksEvent.cc b/runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksEvent.cc
new file mode 100644 (file)
index 0000000..b09f9ab
--- /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.
+ */
+
+#include "ANeuralNetworksEvent.h"
+
+#include "exec/Execution.h"
+#include "util/logging.h"
+
+ANeuralNetworksEvent::ANeuralNetworksEvent(
+    const std::shared_ptr<neurun::exec::Execution> &execution)
+    : _execution{execution}
+{
+  // DO NOTHING
+}
+
+bool ANeuralNetworksEvent::waitFinish(void) noexcept
+{
+  try
+  {
+    _execution->waitFinish();
+  }
+  catch (const std::exception &e)
+  {
+    VERBOSE(EXCEPTION) << e.what() << std::endl;
+
+    return false;
+  }
+
+  return true;
+}
index d144b7c..e499bab 100644 (file)
 #ifndef __EVENT_H__
 #define __EVENT_H__
 
+#include <NeuralNetworks.h>
+
+#include <memory>
+
+namespace neurun
+{
+namespace exec
+{
+class Execution;
+} // namespace exec
+} // namespace neurun
+
 struct ANeuralNetworksEvent
 {
+public:
+  ANeuralNetworksEvent(const std::shared_ptr<neurun::exec::Execution> &execution);
+
+public:
+  bool waitFinish(void) noexcept;
+
+private:
+  const std::shared_ptr<neurun::exec::Execution> _execution;
 };
 
 #endif
index 0a953b6..6598c61 100644 (file)
@@ -152,11 +152,11 @@ bool ANeuralNetworksExecution::setOutput(uint32_t index, const ANeuralNetworksOp
   return true;
 }
 
-bool ANeuralNetworksExecution::execute(void) noexcept
+bool ANeuralNetworksExecution::startExecute(void) noexcept
 {
   try
   {
-    _execution->execute();
+    _execution->startExecute();
   }
   catch (const std::exception &e)
   {
@@ -167,3 +167,8 @@ bool ANeuralNetworksExecution::execute(void) noexcept
 
   return true;
 }
+
+const std::shared_ptr<neurun::exec::Execution> ANeuralNetworksExecution::instance(void) noexcept
+{
+  return _execution;
+}
index 6fd2832..946a12d 100644 (file)
 
 #include <NeuralNetworks.h>
 
+#include <memory>
+
 #include "exec/Execution.h"
-#include <cpp14/memory.h>
 
 struct ANeuralNetworksExecution
 {
 public:
   ANeuralNetworksExecution(const std::shared_ptr<neurun::exec::IExecutor> &executor)
-      : _execution{nnfw::cpp14::make_unique<neurun::exec::Execution>(executor)}
+      : _execution{std::make_shared<neurun::exec::Execution>(executor)}
   {
     // DO NOTHING
   }
@@ -36,7 +37,7 @@ public:
                 size_t length) noexcept;
   bool setOutput(uint32_t index, const ANeuralNetworksOperandType *type, void *buffer,
                  size_t length) noexcept;
-  bool execute(void) noexcept;
+  bool startExecute(void) noexcept;
 
   const neurun::model::OperandIndex getInputOperandIndex(int32_t index) noexcept;
   const neurun::model::OperandIndex getOutputOperandIndex(int32_t index) noexcept;
@@ -46,9 +47,10 @@ public:
                     const neurun::model::OperandIndex index) noexcept;
   bool haveUnspecifiedDims(const neurun::model::OperandIndex index) noexcept;
   size_t getOperandSize(const neurun::model::OperandIndex index) noexcept;
+  const std::shared_ptr<neurun::exec::Execution> instance(void) noexcept;
 
 private:
-  std::unique_ptr<neurun::exec::Execution> _execution;
+  std::shared_ptr<neurun::exec::Execution> _execution;
 };
 
 #endif