ArmNN
 22.05
Threadpool.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 #if !defined(ARMNN_DISABLE_THREADS)
6 
7 #pragma once
8 
9 #include <armnn/Tensor.hpp>
10 #include <armnn/Types.hpp>
11 
12 #include "INetwork.hpp"
13 #include "IRuntime.hpp"
14 
15 #include <thread>
16 #include <mutex>
17 #include <condition_variable>
18 #include <unordered_map>
19 #include <queue>
20 
21 namespace armnn
22 {
23 namespace experimental
24 {
26 {
27 public:
28  Threadpool(std::size_t numThreads,
29  IRuntime* runtimePtr,
30  std::vector<std::shared_ptr<IWorkingMemHandle>> memHandles);
31 
33  {
35  }
36 
37  void LoadMemHandles(std::vector<std::shared_ptr<IWorkingMemHandle>> memHandles);
38  void UnloadMemHandles(NetworkId networkId);
39 
40  /// Schedule an asynchronous execution on the loaded network
41  void Schedule(NetworkId networkId,
42  const InputTensors &inputTensors,
43  const OutputTensors &outputTensors,
44  const QosExecPriority priority,
45  std::shared_ptr<IAsyncExecutionCallback> cb);
46 
47  void TerminateThreadPool() noexcept;
48 
49 private:
50  using ExecutionTuple = std::tuple<NetworkId,
53  std::shared_ptr<IAsyncExecutionCallback>>;
54 
55  using ExecutionQueue = std::queue<std::shared_ptr<ExecutionTuple>>;
56 
57  void ProcessExecPriorities(uint32_t index);
58 
59  IRuntime* m_RuntimePtr;
60 
61  ExecutionQueue m_HighPriorityQueue;
62  ExecutionQueue m_MediumPriorityQueue;
63  ExecutionQueue m_LowPriorityQueue;
64 
65  // Condition Variables require mutex which will guard the shared state.
66  // Has an event happened? Stop signal for example
67  std::condition_variable m_ThreadPoolEvent;
68  std::mutex m_ThreadPoolMutex;
69 
70  // The shared state for conditional variable
71  bool m_TerminatePool = false;
72 
73  std::unordered_map<NetworkId, std::vector<std::shared_ptr<IWorkingMemHandle>>> m_WorkingMemHandleMap;
74  std::vector<std::unique_ptr<std::thread>> m_Threads;
75 };
76 
77 } // namespace experimental
78 
79 } // namespace armnn
80 
81 #endif
void LoadMemHandles(std::vector< std::shared_ptr< IWorkingMemHandle >> memHandles)
Definition: Threadpool.cpp:29
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
Definition: Tensor.hpp:392
Copyright (c) 2021 ARM Limited and Contributors.
void TerminateThreadPool() noexcept
Definition: Threadpool.cpp:102
void Schedule(NetworkId networkId, const InputTensors &inputTensors, const OutputTensors &outputTensors, const QosExecPriority priority, std::shared_ptr< IAsyncExecutionCallback > cb)
Schedule an asynchronous execution on the loaded network.
Definition: Threadpool.cpp:69
int NetworkId
Definition: IRuntime.hpp:27
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
Definition: Tensor.hpp:393
Threadpool(std::size_t numThreads, IRuntime *runtimePtr, std::vector< std::shared_ptr< IWorkingMemHandle >> memHandles)
Definition: Threadpool.cpp:16
void UnloadMemHandles(NetworkId networkId)
Definition: Threadpool.cpp:57
QosExecPriority
Definition: Types.hpp:79