Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 1 | // |
Ryan OShea | b554054 | 2022-07-06 09:52:52 +0100 | [diff] [blame] | 2 | // Copyright © 2021-2023 Arm Ltd and Contributors. All rights reserved. |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <armnn/IAsyncExecutionCallback.hpp> |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 9 | #include <armnn/IWorkingMemHandle.hpp> |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 10 | #include <armnn/Types.hpp> |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 11 | |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 12 | #include <condition_variable> |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 13 | #include <mutex> |
| 14 | #include <thread> |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 15 | #include <queue> |
| 16 | #include <unordered_map> |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 17 | |
| 18 | namespace armnn |
| 19 | { |
| 20 | |
| 21 | namespace experimental |
| 22 | { |
| 23 | |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 24 | using InferenceId = uint64_t; |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 25 | class AsyncExecutionCallback final : public IAsyncExecutionCallback |
| 26 | { |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 27 | private: |
| 28 | static InferenceId nextID; |
| 29 | |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 30 | public: |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame] | 31 | AsyncExecutionCallback(std::queue<InferenceId>& notificationQueue |
| 32 | #if !defined(ARMNN_DISABLE_THREADS) |
| 33 | , std::mutex& mutex |
| 34 | , std::condition_variable& condition |
| 35 | #endif |
| 36 | ) |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 37 | : m_NotificationQueue(notificationQueue) |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame] | 38 | #if !defined(ARMNN_DISABLE_THREADS) |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 39 | , m_Mutex(mutex) |
| 40 | , m_Condition(condition) |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame] | 41 | #endif |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 42 | , m_InferenceId(++nextID) |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 43 | {} |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 44 | |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 45 | ~AsyncExecutionCallback() |
| 46 | {} |
| 47 | |
| 48 | void Notify(armnn::Status status, InferenceTimingPair timeTaken); |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 49 | |
| 50 | InferenceId GetInferenceId() |
| 51 | { |
| 52 | return m_InferenceId; |
| 53 | } |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 54 | |
| 55 | armnn::Status GetStatus() const; |
| 56 | HighResolutionClock GetStartTime() const; |
| 57 | HighResolutionClock GetEndTime() const; |
| 58 | |
| 59 | private: |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 60 | std::queue<InferenceId>& m_NotificationQueue; |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame] | 61 | #if !defined(ARMNN_DISABLE_THREADS) |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 62 | std::mutex& m_Mutex; |
| 63 | std::condition_variable& m_Condition; |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame] | 64 | #endif |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 65 | |
| 66 | HighResolutionClock m_StartTime; |
| 67 | HighResolutionClock m_EndTime; |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 68 | armnn::Status m_Status = Status::Failure; |
| 69 | InferenceId m_InferenceId; |
| 70 | }; |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 71 | |
| 72 | // Manager to create and monitor AsyncExecutionCallbacks |
| 73 | // GetNewCallback will create a callback for use in Threadpool::Schedule |
| 74 | // GetNotifiedCallback will return the first callback to be notified (finished execution) |
| 75 | class AsyncCallbackManager |
| 76 | { |
| 77 | public: |
| 78 | std::shared_ptr<AsyncExecutionCallback> GetNewCallback(); |
| 79 | std::shared_ptr<AsyncExecutionCallback> GetNotifiedCallback(); |
| 80 | |
| 81 | private: |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame] | 82 | #if !defined(ARMNN_DISABLE_THREADS) |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 83 | std::mutex m_Mutex; |
| 84 | std::condition_variable m_Condition; |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame] | 85 | #endif |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 86 | std::unordered_map<InferenceId, std::shared_ptr<AsyncExecutionCallback>> m_Callbacks; |
| 87 | std::queue<InferenceId> m_NotificationQueue; |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | } // namespace experimental |
| 91 | |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame] | 92 | } // namespace armnn |