ArmNN
 24.02
Threadpool Class Reference

#include <Threadpool.hpp>

Public Member Functions

 Threadpool (std::size_t numThreads, IRuntime *runtimePtr, std::vector< std::shared_ptr< IWorkingMemHandle >> memHandles)
 
 ~Threadpool ()
 
void LoadMemHandles (std::vector< std::shared_ptr< IWorkingMemHandle >> memHandles)
 
void UnloadMemHandles (NetworkId networkId)
 
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. More...
 
void TerminateThreadPool () noexcept
 

Detailed Description

Definition at line 30 of file Threadpool.hpp.

Constructor & Destructor Documentation

◆ Threadpool()

Threadpool ( std::size_t  numThreads,
IRuntime runtimePtr,
std::vector< std::shared_ptr< IWorkingMemHandle >>  memHandles 
)

Definition at line 16 of file Threadpool.cpp.

19  : m_RuntimePtr(runtimePtr)
20 {
21  for (auto i = 0u; i < numThreads; ++i)
22  {
23  m_Threads.emplace_back(std::make_unique<std::thread>(&Threadpool::ProcessExecPriorities, this, i));
24  }
25 
26  LoadMemHandles(memHandles);
27 }

References Threadpool::LoadMemHandles().

◆ ~Threadpool()

~Threadpool ( )
inline

Definition at line 37 of file Threadpool.hpp.

38  {
40  }

References Threadpool::TerminateThreadPool().

Member Function Documentation

◆ LoadMemHandles()

void LoadMemHandles ( std::vector< std::shared_ptr< IWorkingMemHandle >>  memHandles)

Definition at line 29 of file Threadpool.cpp.

30 {
31  if (memHandles.size() == 0)
32  {
33  throw armnn::RuntimeException("Threadpool::UnloadMemHandles: Size of memHandles vector must be greater than 0");
34  }
35 
36  if (memHandles.size() != m_Threads.size())
37  {
39  "Threadpool::UnloadMemHandles: Size of memHandles vector must match the number of threads");
40  }
41 
42  NetworkId networkId = memHandles[0]->GetNetworkId();
43  for (uint32_t i = 1; i < memHandles.size(); ++i)
44  {
45  if (networkId != memHandles[i]->GetNetworkId())
46  {
48  "Threadpool::UnloadMemHandles: All network ids must be identical in memHandles");
49  }
50  }
51 
52  std::pair<NetworkId, std::vector<std::shared_ptr<IWorkingMemHandle>>> pair {networkId, memHandles};
53 
54  m_WorkingMemHandleMap.insert(pair);
55 }

Referenced by Threadpool::Threadpool().

◆ Schedule()

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 at line 69 of file Threadpool.cpp.

74 {
75  if (m_WorkingMemHandleMap.find(networkId) == m_WorkingMemHandleMap.end())
76  {
77  throw armnn::RuntimeException("Threadpool::UnloadMemHandles: Unknown NetworkId");
78  }
79 
80  // Group execution parameters so that they can be easily added to the queue
81  ExecutionTuple groupExecParams = std::make_tuple(networkId, inputTensors, outputTensors, cb);
82 
83  std::shared_ptr<ExecutionTuple> operation = std::make_shared<ExecutionTuple>(groupExecParams);
84 
85  // Add a message to the queue and notify the request thread
86  std::unique_lock<std::mutex> lock(m_ThreadPoolMutex);
87  switch (priority)
88  {
90  m_HighPriorityQueue.push(operation);
91  break;
93  m_LowPriorityQueue.push(operation);
94  break;
96  default:
97  m_MediumPriorityQueue.push(operation);
98  }
99  m_ThreadPoolEvent.notify_one();
100 }

References armnn::High, armnn::Low, and armnn::Medium.

◆ TerminateThreadPool()

void TerminateThreadPool ( )
noexcept

Definition at line 102 of file Threadpool.cpp.

103 {
104  {
105  std::unique_lock<std::mutex> threadPoolLock(m_ThreadPoolMutex);
106  m_TerminatePool = true;
107  }
108 
109  m_ThreadPoolEvent.notify_all();
110 
111  for (auto &thread : m_Threads)
112  {
113  thread->join();
114  }
115 }

Referenced by Threadpool::~Threadpool().

◆ UnloadMemHandles()

void UnloadMemHandles ( NetworkId  networkId)

Definition at line 57 of file Threadpool.cpp.

58 {
59  if (m_WorkingMemHandleMap.find(networkId) != m_WorkingMemHandleMap.end())
60  {
61  m_WorkingMemHandleMap.erase(networkId);
62  }
63  else
64  {
65  throw armnn::RuntimeException("Threadpool::UnloadMemHandles: Unknown NetworkId");
66  }
67 }

The documentation for this class was generated from the following files:
armnn::QosExecPriority::Medium
@ Medium
armnn::QosExecPriority::High
@ High
armnn::QosExecPriority::Low
@ Low
armnn::experimental::Threadpool::LoadMemHandles
void LoadMemHandles(std::vector< std::shared_ptr< IWorkingMemHandle >> memHandles)
Definition: Threadpool.cpp:29
armnn::NetworkId
int NetworkId
Definition: IRuntime.hpp:35
armnn::RuntimeException
Definition: Exceptions.hpp:120
armnn::experimental::Threadpool::TerminateThreadPool
void TerminateThreadPool() noexcept
Definition: Threadpool.cpp:102