From a51ebfaad6d18d55089a5d8102be0518df1a8db5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=98=A4=ED=98=95=EC=84=9D/On-Device=20Lab=28SR=29/Staff?= =?utf8?q?=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Thu, 8 Aug 2019 18:52:39 +0900 Subject: [PATCH] Support async execution in NNAPI (#6395) Implement Event class to support async execution Use internal async execution function for startCompute and wait Signed-off-by: Hyeongseok Oh --- runtimes/neurun/frontend/nnapi/event.cc | 5 +++ runtimes/neurun/frontend/nnapi/execution.cc | 5 ++- .../frontend/nnapi/wrapper/ANeuralNetworksEvent.cc | 43 ++++++++++++++++++++++ .../frontend/nnapi/wrapper/ANeuralNetworksEvent.h | 20 ++++++++++ .../nnapi/wrapper/ANeuralNetworksExecution.cc | 9 ++++- .../nnapi/wrapper/ANeuralNetworksExecution.h | 10 +++-- 6 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksEvent.cc diff --git a/runtimes/neurun/frontend/nnapi/event.cc b/runtimes/neurun/frontend/nnapi/event.cc index 05e4b9e..593b74e 100644 --- a/runtimes/neurun/frontend/nnapi/event.cc +++ b/runtimes/neurun/frontend/nnapi/event.cc @@ -25,6 +25,11 @@ int ANeuralNetworksEvent_wait(ANeuralNetworksEvent *event) return ANEURALNETWORKS_UNEXPECTED_NULL; } + if (!event->waitFinish()) + { + return ANEURALNETWORKS_BAD_STATE; + } + return ANEURALNETWORKS_NO_ERROR; } diff --git a/runtimes/neurun/frontend/nnapi/execution.cc b/runtimes/neurun/frontend/nnapi/execution.cc index 3773c4d..837cab0 100644 --- a/runtimes/neurun/frontend/nnapi/execution.cc +++ b/runtimes/neurun/frontend/nnapi/execution.cc @@ -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 index 0000000..b09f9ab --- /dev/null +++ b/runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksEvent.cc @@ -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 &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; +} diff --git a/runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksEvent.h b/runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksEvent.h index d144b7c..e499bab 100644 --- a/runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksEvent.h +++ b/runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksEvent.h @@ -17,8 +17,28 @@ #ifndef __EVENT_H__ #define __EVENT_H__ +#include + +#include + +namespace neurun +{ +namespace exec +{ +class Execution; +} // namespace exec +} // namespace neurun + struct ANeuralNetworksEvent { +public: + ANeuralNetworksEvent(const std::shared_ptr &execution); + +public: + bool waitFinish(void) noexcept; + +private: + const std::shared_ptr _execution; }; #endif diff --git a/runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksExecution.cc b/runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksExecution.cc index 0a953b6..6598c61 100644 --- a/runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksExecution.cc +++ b/runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksExecution.cc @@ -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 ANeuralNetworksExecution::instance(void) noexcept +{ + return _execution; +} diff --git a/runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksExecution.h b/runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksExecution.h index 6fd2832..946a12d 100644 --- a/runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksExecution.h +++ b/runtimes/neurun/frontend/nnapi/wrapper/ANeuralNetworksExecution.h @@ -19,14 +19,15 @@ #include +#include + #include "exec/Execution.h" -#include struct ANeuralNetworksExecution { public: ANeuralNetworksExecution(const std::shared_ptr &executor) - : _execution{nnfw::cpp14::make_unique(executor)} + : _execution{std::make_shared(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 instance(void) noexcept; private: - std::unique_ptr _execution; + std::shared_ptr _execution; }; #endif -- 2.7.4