ArmNN
 22.05
AsyncExecutionCallback.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2021 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 InferenceId AsyncExecutionCallback::nextID = 0u;
72 
73 // Manager to create and monitor AsyncExecutionCallbacks
74 // GetNewCallback will create a callback for use in Threadpool::Schedule
75 // GetNotifiedCallback will return the first callback to be notified (finished execution)
77 {
78 public:
79  std::shared_ptr<AsyncExecutionCallback> GetNewCallback();
80  std::shared_ptr<AsyncExecutionCallback> GetNotifiedCallback();
81 
82 private:
83 #if !defined(ARMNN_DISABLE_THREADS)
84  std::mutex m_Mutex;
85  std::condition_variable m_Condition;
86 #endif
87  std::unordered_map<InferenceId, std::shared_ptr<AsyncExecutionCallback>> m_Callbacks;
88  std::queue<InferenceId> m_NotificationQueue;
89 };
90 
91 } // namespace experimental
92 
93 } // namespace armnn
std::chrono::high_resolution_clock::time_point HighResolutionClock
Define a timer and associated inference ID for recording execution times.
Definition: Types.hpp:382
void Notify(armnn::Status status, InferenceTimingPair timeTaken)
Copyright (c) 2021 ARM Limited and Contributors.
AsyncExecutionCallback(std::queue< InferenceId > &notificationQueue, std::mutex &mutex, std::condition_variable &condition)
Status
enumeration
Definition: Types.hpp:42
std::pair< HighResolutionClock, HighResolutionClock > InferenceTimingPair
Definition: Types.hpp:383