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