From 89c54237152b4df24fbcfa3b57165495e2736e08 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: Fri, 2 Aug 2019 14:54:34 +0900 Subject: [PATCH] [neuurn] Use mutex on ExecutorBase (#6131) * Use mutex on ExecutorBase Use mutex on ExecutorBase for thread-safe executor Enable multi-thread execution test Signed-off-by: Hyeongseok Oh * Use std::lock_guard --- runtimes/neurun/core/src/exec/ExecutorBase.cc | 7 ++++++- runtimes/neurun/core/src/exec/ExecutorBase.h | 3 +++ runtimes/neurun/test/core/exec/ExecInstance.cc | 27 ++++++-------------------- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/runtimes/neurun/core/src/exec/ExecutorBase.cc b/runtimes/neurun/core/src/exec/ExecutorBase.cc index 3ed3abf..8590387 100644 --- a/runtimes/neurun/core/src/exec/ExecutorBase.cc +++ b/runtimes/neurun/core/src/exec/ExecutorBase.cc @@ -28,7 +28,7 @@ ExecutorBase::ExecutorBase(const std::shared_ptr &model, std::unique_ptr mem_mgrs) : _observers(), _model{model}, _subgraphs{std::move(subgraphs)}, _operand_context{operand_context}, _lower_info{std::move(lower_info)}, - _mem_mgrs{std::move(mem_mgrs)} + _mem_mgrs{std::move(mem_mgrs)}, _mutex() { // DO NOTHING } @@ -76,6 +76,11 @@ std::unique_ptr ExecutorBase::sink(const model::IOIndex &index, const mod void ExecutorBase::execute(const IODescription &desc) { + // For thread-safe, use mutex + // TODO: if all used backends on this executor are thread-safe, + // do not need to use mutex (otherwise, use mutex) + std::lock_guard lock(_mutex); + std::vector> sources{_model->inputs.size()}; std::vector> sinks{_model->outputs.size()}; diff --git a/runtimes/neurun/core/src/exec/ExecutorBase.h b/runtimes/neurun/core/src/exec/ExecutorBase.h index 7b8fefd..ef66111 100644 --- a/runtimes/neurun/core/src/exec/ExecutorBase.h +++ b/runtimes/neurun/core/src/exec/ExecutorBase.h @@ -17,6 +17,8 @@ #ifndef __NEURUN_EXEC_EXECUTOR_BASE_H__ #define __NEURUN_EXEC_EXECUTOR_BASE_H__ +#include + #include "Source.h" #include "exec/ExecutionObservers.h" #include "Sink.h" @@ -113,6 +115,7 @@ protected: std::shared_ptr _operand_context; std::unique_ptr _lower_info; std::unique_ptr _mem_mgrs; + std::mutex _mutex; }; } // namespace exec diff --git a/runtimes/neurun/test/core/exec/ExecInstance.cc b/runtimes/neurun/test/core/exec/ExecInstance.cc index 07725ef..35dd527 100644 --- a/runtimes/neurun/test/core/exec/ExecInstance.cc +++ b/runtimes/neurun/test/core/exec/ExecInstance.cc @@ -211,22 +211,6 @@ TEST(ExecInstance, twoExecution) delete execution2; } -// TODO 1: Support multi-thread execution -// If executor(backend) can support, these instances run independently -// Otherwise, execute() shuold wait other execution end (need lock/unlock mutex) -// -// ... in thread1 -// auto exec_instance = new neurun::exec::ExecInstance{executor}; -// exec_instance->setInput(...); -// exec_instance->setOutput(...); -// exec_instance->execute(); -// -// .. in thread2 -// auto exec_instance = new neurun::exec::ExecInstance{executor}; -// exec_instance->setInput(...); -// exec_instance->setOutput(...); -// exec_instance->execute(); - class Inference { public: @@ -260,7 +244,8 @@ private: std::shared_ptr &_executor; }; -TEST(ExecInstance, DISABLED_twoThreads) +// Support multi-thread execution +TEST(ExecInstance, twoThreads) { auto mockup = CompiledMockUpModel(); auto executor = mockup.executor; @@ -292,10 +277,10 @@ TEST(ExecInstance, DISABLED_twoThreads) } } -// TODO 2: Support asynchronous execution -// If executor(backend) can support, execute() makes new thread and return -// and support wait function -// Otherwise, instance run until execution end and return (wait function do nothing) +// TODO: Support asynchronous execution +// If executor(backend) can support, execute() makes new thread and return +// and support wait function +// Otherwise, instance run until execution end and return (wait function do nothing) // // auto exec_instance1 = new neurun::exec::ExecInstance{executor}; // exec_instance1->setInput(...); -- 2.7.4