Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2021 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include <AsyncExecutionCallback.hpp> |
| 7 | |
| 8 | namespace armnn |
| 9 | { |
| 10 | |
| 11 | namespace experimental |
| 12 | { |
| 13 | |
| 14 | void AsyncExecutionCallback::Notify(armnn::Status status, InferenceTimingPair timeTaken) |
| 15 | { |
| 16 | { |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame] | 17 | #if !defined(ARMNN_DISABLE_THREADS) |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 18 | std::lock_guard<std::mutex> hold(m_Mutex); |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame] | 19 | #endif |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 20 | // store results and mark as notified |
| 21 | m_Status = status; |
| 22 | m_StartTime = timeTaken.first; |
| 23 | m_EndTime = timeTaken.second; |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 24 | m_NotificationQueue.push(m_InferenceId); |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 25 | } |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame] | 26 | #if !defined(ARMNN_DISABLE_THREADS) |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 27 | m_Condition.notify_all(); |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame] | 28 | #endif |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 29 | } |
| 30 | |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 31 | armnn::Status AsyncExecutionCallback::GetStatus() const |
| 32 | { |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 33 | return m_Status; |
| 34 | } |
| 35 | |
| 36 | HighResolutionClock AsyncExecutionCallback::GetStartTime() const |
| 37 | { |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 38 | return m_StartTime; |
| 39 | } |
| 40 | |
| 41 | HighResolutionClock AsyncExecutionCallback::GetEndTime() const |
| 42 | { |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 43 | return m_EndTime; |
| 44 | } |
| 45 | |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 46 | std::shared_ptr<AsyncExecutionCallback> AsyncCallbackManager::GetNewCallback() |
| 47 | { |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame] | 48 | auto cb = std::make_unique<AsyncExecutionCallback>(m_NotificationQueue |
| 49 | #if !defined(ARMNN_DISABLE_THREADS) |
| 50 | , m_Mutex |
| 51 | , m_Condition |
| 52 | #endif |
| 53 | ); |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 54 | InferenceId id = cb->GetInferenceId(); |
| 55 | m_Callbacks.insert({id, std::move(cb)}); |
| 56 | |
| 57 | return m_Callbacks.at(id); |
| 58 | } |
| 59 | |
| 60 | std::shared_ptr<AsyncExecutionCallback> AsyncCallbackManager::GetNotifiedCallback() |
| 61 | { |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame] | 62 | #if !defined(ARMNN_DISABLE_THREADS) |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 63 | std::unique_lock<std::mutex> lock(m_Mutex); |
| 64 | |
| 65 | m_Condition.wait(lock, [this] { return !m_NotificationQueue.empty(); }); |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame] | 66 | #endif |
Finn Williams | f364d53 | 2021-06-09 17:07:33 +0100 | [diff] [blame] | 67 | InferenceId id = m_NotificationQueue.front(); |
| 68 | m_NotificationQueue.pop(); |
| 69 | |
| 70 | std::shared_ptr<AsyncExecutionCallback> callback = m_Callbacks.at(id); |
| 71 | m_Callbacks.erase(id); |
| 72 | return callback; |
| 73 | } |
| 74 | |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame] | 75 | } // namespace experimental |
| 76 | |
Jim Flynn | 870b96c | 2022-03-25 21:24:56 +0000 | [diff] [blame] | 77 | } // namespace armnn |