ArmNN
 24.08
AsyncExecutionCallback.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2021-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
10 #include <armnn/Types.hpp>
11 
12 #include <condition_variable>
13 #include <mutex>
14 #include <thread>
15 #include <queue>
16 #include <unordered_map>
17 
18 namespace armnn
19 {
20 
21 namespace experimental
22 {
23 
24 using InferenceId = uint64_t;
26 {
27 private:
28  static InferenceId nextID;
29 
30 public:
31  AsyncExecutionCallback(std::queue<InferenceId>& notificationQueue
32 #if !defined(ARMNN_DISABLE_THREADS)
33  , std::mutex& mutex
34  , std::condition_variable& condition
35 #endif
36  )
37  : m_NotificationQueue(notificationQueue)
38 #if !defined(ARMNN_DISABLE_THREADS)
39  , m_Mutex(mutex)
40  , m_Condition(condition)
41 #endif
42  , m_InferenceId(++nextID)
43  {}
44 
46  {}
47 
48  void Notify(armnn::Status status, InferenceTimingPair timeTaken);
49 
51  {
52  return m_InferenceId;
53  }
54 
55  armnn::Status GetStatus() const;
58 
59 private:
60  std::queue<InferenceId>& m_NotificationQueue;
61 #if !defined(ARMNN_DISABLE_THREADS)
62  std::mutex& m_Mutex;
63  std::condition_variable& m_Condition;
64 #endif
65 
66  HighResolutionClock m_StartTime;
67  HighResolutionClock m_EndTime;
68  armnn::Status m_Status = Status::Failure;
69  InferenceId m_InferenceId;
70 };
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)
76 {
77 public:
78  std::shared_ptr<AsyncExecutionCallback> GetNewCallback();
79  std::shared_ptr<AsyncExecutionCallback> GetNotifiedCallback();
80 
81 private:
82 #if !defined(ARMNN_DISABLE_THREADS)
83  std::mutex m_Mutex;
84  std::condition_variable m_Condition;
85 #endif
86  std::unordered_map<InferenceId, std::shared_ptr<AsyncExecutionCallback>> m_Callbacks;
87  std::queue<InferenceId> m_NotificationQueue;
88 };
89 
90 } // namespace experimental
91 
92 } // namespace armnn
armnn::InferenceTimingPair
std::pair< HighResolutionClock, HighResolutionClock > InferenceTimingPair
Definition: Types.hpp:402
armnn::experimental::AsyncExecutionCallback
Definition: AsyncExecutionCallback.hpp:25
IAsyncExecutionCallback.hpp
armnn::experimental::InferenceId
uint64_t InferenceId
Definition: AsyncExecutionCallback.hpp:24
armnn::experimental::AsyncExecutionCallback::Notify
void Notify(armnn::Status status, InferenceTimingPair timeTaken)
Definition: AsyncExecutionCallback.cpp:16
armnn::experimental::AsyncCallbackManager
Definition: AsyncExecutionCallback.hpp:75
armnn::experimental::AsyncExecutionCallback::GetStatus
armnn::Status GetStatus() const
Definition: AsyncExecutionCallback.cpp:33
armnn::experimental::AsyncExecutionCallback::GetStartTime
HighResolutionClock GetStartTime() const
Definition: AsyncExecutionCallback.cpp:38
armnn::experimental::AsyncCallbackManager::GetNewCallback
std::shared_ptr< AsyncExecutionCallback > GetNewCallback()
Definition: AsyncExecutionCallback.cpp:48
armnn::experimental::AsyncExecutionCallback::AsyncExecutionCallback
AsyncExecutionCallback(std::queue< InferenceId > &notificationQueue, std::mutex &mutex, std::condition_variable &condition)
Definition: AsyncExecutionCallback.hpp:31
armnn::experimental::AsyncExecutionCallback::~AsyncExecutionCallback
~AsyncExecutionCallback()
Definition: AsyncExecutionCallback.hpp:45
armnn::Status
Status
Definition: Types.hpp:42
armnn::experimental::IAsyncExecutionCallback
Definition: IAsyncExecutionCallback.hpp:19
armnn::experimental::AsyncCallbackManager::GetNotifiedCallback
std::shared_ptr< AsyncExecutionCallback > GetNotifiedCallback()
Definition: AsyncExecutionCallback.cpp:62
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
Types.hpp
armnn::experimental::AsyncExecutionCallback::GetInferenceId
InferenceId GetInferenceId()
Definition: AsyncExecutionCallback.hpp:50
armnn::HighResolutionClock
std::chrono::high_resolution_clock::time_point HighResolutionClock
Define a timer and associated inference ID for recording execution times.
Definition: Types.hpp:401
armnn::Status::Failure
@ Failure
armnn::experimental::AsyncExecutionCallback::GetEndTime
HighResolutionClock GetEndTime() const
Definition: AsyncExecutionCallback.cpp:43
IWorkingMemHandle.hpp