ArmNN
 21.08
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  std::mutex& mutex,
33  std::condition_variable& condition)
34  : m_NotificationQueue(notificationQueue)
35  , m_Mutex(mutex)
36  , m_Condition(condition)
37  , m_InferenceId(++nextID)
38  {}
39 
41  {}
42 
43  void Notify(armnn::Status status, InferenceTimingPair timeTaken);
44 
46  {
47  return m_InferenceId;
48  }
49 
50  armnn::Status GetStatus() const;
53 
54 private:
55  std::queue<InferenceId>& m_NotificationQueue;
56  std::mutex& m_Mutex;
57  std::condition_variable& m_Condition;
58 
59  HighResolutionClock m_StartTime;
60  HighResolutionClock m_EndTime;
61  armnn::Status m_Status = Status::Failure;
62  InferenceId m_InferenceId;
63 };
64 InferenceId AsyncExecutionCallback::nextID = 0u;
65 
66 // Manager to create and monitor AsyncExecutionCallbacks
67 // GetNewCallback will create a callback for use in Threadpool::Schedule
68 // GetNotifiedCallback will return the first callback to be notified (finished execution)
70 {
71 public:
72  std::shared_ptr<AsyncExecutionCallback> GetNewCallback();
73  std::shared_ptr<AsyncExecutionCallback> GetNotifiedCallback();
74 
75 private:
76  std::mutex m_Mutex;
77  std::condition_variable m_Condition;
78  std::unordered_map<InferenceId, std::shared_ptr<AsyncExecutionCallback>> m_Callbacks;
79  std::queue<InferenceId> m_NotificationQueue;
80 };
81 
82 } // namespace experimental
83 
84 } // namespace armnn
std::chrono::high_resolution_clock::time_point HighResolutionClock
Define a timer and associated inference ID for recording execution times.
Definition: Types.hpp:324
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:29
std::pair< HighResolutionClock, HighResolutionClock > InferenceTimingPair
Definition: Types.hpp:325