telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | 93e4898 | 2018-09-05 13:05:09 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <queue> |
| 9 | #include <thread> |
| 10 | #include <mutex> |
| 11 | #include <condition_variable> |
| 12 | |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 13 | #include "ArmnnDriver.hpp" |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 14 | #include "ArmnnDriverImpl.hpp" |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 15 | |
| 16 | #include <CpuExecutor.h> |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 17 | #include <armnn/ArmNN.hpp> |
| 18 | |
| 19 | namespace armnn_driver |
| 20 | { |
| 21 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 22 | template<typename HalVersion> |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 23 | class ArmnnPreparedModel; |
| 24 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 25 | template<typename HalVersion> |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 26 | class RequestThread |
| 27 | { |
| 28 | public: |
| 29 | /// Constructor creates the thread |
| 30 | RequestThread(); |
| 31 | |
| 32 | /// Destructor terminates the thread |
| 33 | ~RequestThread(); |
| 34 | |
| 35 | /// Add a message to the thread queue. |
| 36 | /// @param[in] model pointer to the prepared model handling the request |
| 37 | /// @param[in] memPools pointer to the memory pools vector for the tensors |
| 38 | /// @param[in] inputTensors pointer to the input tensors for the request |
| 39 | /// @param[in] outputTensors pointer to the output tensors for the request |
| 40 | /// @param[in] callback the android notification callback |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 41 | void PostMsg(armnn_driver::ArmnnPreparedModel<HalVersion>* model, |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 42 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& memPools, |
| 43 | std::shared_ptr<armnn::InputTensors>& inputTensors, |
| 44 | std::shared_ptr<armnn::OutputTensors>& outputTensors, |
| 45 | const ::android::sp<IExecutionCallback>& callback); |
| 46 | |
| 47 | private: |
| 48 | RequestThread(const RequestThread&) = delete; |
| 49 | RequestThread& operator=(const RequestThread&) = delete; |
| 50 | |
| 51 | /// storage for a prepared model and args for the asyncExecute call |
| 52 | struct AsyncExecuteData |
| 53 | { |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 54 | AsyncExecuteData(ArmnnPreparedModel<HalVersion>* model, |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 55 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>>& memPools, |
| 56 | std::shared_ptr<armnn::InputTensors>& inputTensors, |
| 57 | std::shared_ptr<armnn::OutputTensors>& outputTensors, |
| 58 | const ::android::sp<IExecutionCallback>& cb) |
| 59 | : m_Model(model) |
| 60 | , m_MemPools(memPools) |
| 61 | , m_InputTensors(inputTensors) |
| 62 | , m_OutputTensors(outputTensors) |
| 63 | , m_callback(cb) |
| 64 | { |
| 65 | } |
| 66 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 67 | armnn_driver::ArmnnPreparedModel<HalVersion>* m_Model; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 68 | std::shared_ptr<std::vector<::android::nn::RunTimePoolInfo>> m_MemPools; |
| 69 | std::shared_ptr<armnn::InputTensors> m_InputTensors; |
| 70 | std::shared_ptr<armnn::OutputTensors> m_OutputTensors; |
| 71 | const ::android::sp<IExecutionCallback> m_callback; |
| 72 | }; |
| 73 | |
| 74 | enum class ThreadMsgType |
| 75 | { |
| 76 | EXIT, // exit the thread |
| 77 | REQUEST // user request to process |
| 78 | }; |
| 79 | |
| 80 | /// storage for the thread message type and data |
| 81 | struct ThreadMsg |
| 82 | { |
| 83 | ThreadMsg(ThreadMsgType msgType, |
| 84 | std::shared_ptr<AsyncExecuteData>& msgData) |
| 85 | : type(msgType) |
| 86 | , data(msgData) |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | ThreadMsgType type; |
| 91 | std::shared_ptr<AsyncExecuteData> data; |
| 92 | }; |
| 93 | |
| 94 | /// Add a prepared thread message to the thread queue. |
| 95 | /// @param[in] threadMsg the message to add to the queue |
| 96 | void PostMsg(std::shared_ptr<ThreadMsg>& pThreadMsg); |
| 97 | |
| 98 | /// Entry point for the request thread |
| 99 | void Process(); |
| 100 | |
| 101 | std::unique_ptr<std::thread> m_Thread; |
| 102 | std::queue<std::shared_ptr<ThreadMsg>> m_Queue; |
| 103 | std::mutex m_Mutex; |
| 104 | std::condition_variable m_Cv; |
| 105 | }; |
| 106 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 107 | } // namespace armnn_driver |