Nektar++
Classes | Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
Nektar::Thread::ThreadManagerStd Class Reference

Implementation of ThreadManager using std::thread. More...

#include <ThreadStd.h>

Inheritance diagram for Nektar::Thread::ThreadManagerStd:
[legend]

Classes

class  Barrier
 Lightweight barrier class. More...
 

Public Member Functions

 ThreadManagerStd (unsigned int numWorkers)
 Constructs a ThreadManagerStd. More...
 
 ~ThreadManagerStd () override
 Shuts down threading. More...
 
- Public Member Functions inherited from Nektar::Thread::ThreadManager
virtual ~ThreadManager ()
 Destructor. More...
 
void QueueJobs (std::vector< ThreadJob * > &joblist)
 Pass a list of tasklets to the master queue. More...
 
void QueueJob (ThreadJob *job)
 Pass a single job to the master queue. More...
 
unsigned int GetNumWorkers ()
 Return the number of active workers. More...
 
unsigned int GetWorkerNum ()
 Returns the worker number of the executing thread. More...
 
void SetNumWorkers (const unsigned int num)
 Sets the number of active workers. More...
 
void SetNumWorkers ()
 Sets the number of active workers to the maximum. More...
 
unsigned int GetMaxNumWorkers ()
 Gets the maximum available number of threads. More...
 
void Wait ()
 Waits until all queued jobs are finished. More...
 
void SetChunkSize (unsigned int chnk)
 Controls how many jobs are sent to each worker at a time. More...
 
void SetSchedType (SchedType s)
 Sets the current scheduling algorithm. More...
 
bool InThread ()
 Indicates whether the code is in a worker thread or not. More...
 
void Hold ()
 A calling threads holds until all active threads call this method. More...
 
const std::string & GetType ()
 Returns a description of the type of threading. More...
 
bool IsInitialised ()
 ThreadManager implementation. More...
 
int GetThrFromPartition (int pPartition)
 
int GetRankFromPartition (int pPartition)
 
int GetPartitionFromRankThr (int pRank, unsigned int pThr)
 

Static Public Member Functions

static ThreadManagerSharedPtr Create (unsigned int numT)
 Called by the factory method. More...
 

Protected Member Functions

void v_QueueJobs (std::vector< ThreadJob * > &joblist) override
 
void v_QueueJob (ThreadJob *job) override
 
unsigned int v_GetNumWorkers () override
 
unsigned int v_GetWorkerNum () override
 
void v_SetNumWorkers (const unsigned int num) override
 
void v_SetNumWorkers () override
 
unsigned int v_GetMaxNumWorkers () override
 
void v_Wait () override
 
void v_SetChunkSize (unsigned int chnk) override
 
void v_SetSchedType (SchedType s) override
 
bool v_InThread () override
 
void v_Hold () override
 
const std::string & v_GetType () const override
 
- Protected Member Functions inherited from Nektar::Thread::ThreadManager
virtual void v_QueueJobs (std::vector< ThreadJob * > &joblist)=0
 
virtual void v_QueueJob (ThreadJob *job)=0
 
virtual unsigned int v_GetNumWorkers ()=0
 
virtual unsigned int v_GetWorkerNum ()=0
 
virtual void v_SetNumWorkers (const unsigned int num)=0
 
virtual void v_SetNumWorkers ()=0
 
virtual unsigned int v_GetMaxNumWorkers ()=0
 
virtual void v_Wait ()=0
 
virtual void v_SetChunkSize (unsigned int chnk)=0
 
virtual void v_SetSchedType (SchedType s)=0
 
virtual bool v_InThread ()=0
 
virtual void v_Hold ()=0
 
virtual const std::string & v_GetType () const =0
 
virtual bool v_IsInitialised ()
 

Private Member Functions

 ThreadManagerStd ()
 
 ThreadManagerStd (const ThreadManagerStd &)
 
bool IsWorking ()
 
void SetNumWorkersImpl (const unsigned int num)
 

Private Attributes

const unsigned int m_numThreads
 
unsigned int m_numWorkers
 
std::queue< ThreadJob * > m_masterQueue
 
std::mutex m_masterQueueMutex
 
std::mutex m_masterActiveMutex
 
std::condition_variable m_masterQueueCondVar
 
std::condition_variable m_masterActiveCondVar
 
ThreadWorkerStd ** m_threadList
 
std::thread ** m_threadThreadList
 
std::thread::id m_masterThreadId
 
bool * m_threadBusyList
 
bool * m_threadActiveList
 
unsigned int m_chunkSize
 
SchedType m_schedType
 
Barrierm_barrier
 
std::map< std::thread::id, unsigned int > m_threadMap
 
std::string m_type
 

Static Private Attributes

static std::string className
 

Friends

class ThreadWorkerStd
 

Detailed Description

Implementation of ThreadManager using std::thread.

Definition at line 57 of file ThreadStd.h.

Constructor & Destructor Documentation

◆ ThreadManagerStd() [1/3]

Nektar::Thread::ThreadManagerStd::ThreadManagerStd ( unsigned int  numT)

Constructs a ThreadManagerStd.

Parameters
numWorkersThe number of threads to start (including master thread).
Note
Do not use, use factory instead.

Definition at line 54 of file ThreadStd.cpp.

55 : m_numThreads(numT), m_numWorkers(numT), m_masterQueue(),
59{
60 using namespace std;
61 try
62 {
64 m_threadThreadList = new std::thread *[m_numThreads];
67 }
68 catch (exception &e)
69 {
70 cerr << "Exception while allocating thread storage: " << e.what()
71 << endl;
72 abort();
73 }
74 unsigned int i = 0;
75 while (i < m_numThreads)
76 {
78 try
79 {
80 tw = new ThreadWorkerStd(this, i);
81 }
82 catch (exception &e)
83 {
84 cerr << "Exception while allocating worker threads: " << e.what()
85 << endl;
86 abort();
87 }
88
89 m_threadList[i] = tw;
90 m_threadBusyList[i] = false;
91 m_threadActiveList[i] = true;
92
93 try
94 {
95 m_threadThreadList[i] = new std::thread(std::ref(*tw));
96 std::thread::id id = m_threadThreadList[i]->get_id();
97 m_threadMap[id] = i;
98 }
99 catch (...)
100 {
101 std::cerr << "Exception while creating worker threads" << std::endl;
102 abort();
103 }
104 i++;
105 }
106 m_masterThreadId = std::this_thread::get_id();
107 m_barrier = new Barrier(m_numWorkers > 0 ? m_numWorkers : 1);
108 m_type = "Threading with Std";
109}
std::map< std::thread::id, unsigned int > m_threadMap
Definition: ThreadStd.h:146
ThreadWorkerStd ** m_threadList
Definition: ThreadStd.h:138
std::thread ** m_threadThreadList
Definition: ThreadStd.h:139
std::thread::id m_masterThreadId
Definition: ThreadStd.h:140
const unsigned int m_numThreads
Definition: ThreadStd.h:131
std::queue< ThreadJob * > m_masterQueue
Definition: ThreadStd.h:133
std::condition_variable m_masterQueueCondVar
Definition: ThreadStd.h:136
std::condition_variable m_masterActiveCondVar
Definition: ThreadStd.h:137
STL namespace.

References m_barrier, m_masterThreadId, m_numThreads, m_numWorkers, m_threadActiveList, m_threadBusyList, m_threadList, m_threadMap, m_threadThreadList, m_type, and ThreadWorkerStd.

◆ ~ThreadManagerStd()

Nektar::Thread::ThreadManagerStd::~ThreadManagerStd ( )
override

Shuts down threading.

Terminates all running threads (they will finish their current job), releases resources and destructs.

Definition at line 115 of file ThreadStd.cpp.

116{
117 // This is an immediate teardown. We attempt to kill everything.
118 // we daren't lock anything as we may cause a deadlock
119 for (unsigned int i = 0; i < m_numThreads; i++)
120 {
121 m_threadList[i]->Stop();
122 }
123
124 m_masterQueueCondVar.notify_all();
125 m_masterActiveCondVar.notify_all();
126 for (unsigned int i = 0; i < m_numThreads; i++)
127 {
128 m_threadThreadList[i]->join();
129 delete m_threadThreadList[i];
130 delete m_threadList[i];
131 }
132
133 delete[] m_threadList;
134 delete[] m_threadThreadList;
135 delete[] m_threadActiveList;
136 delete[] m_threadBusyList;
137 delete m_barrier;
138}
void Stop()
A signal to shut down.
Definition: ThreadStd.h:185

References m_barrier, m_masterActiveCondVar, m_masterQueueCondVar, m_numThreads, m_threadActiveList, m_threadBusyList, m_threadList, m_threadThreadList, and Nektar::Thread::ThreadWorkerStd::Stop().

◆ ThreadManagerStd() [2/3]

Nektar::Thread::ThreadManagerStd::ThreadManagerStd ( )
private

Referenced by Create().

◆ ThreadManagerStd() [3/3]

Nektar::Thread::ThreadManagerStd::ThreadManagerStd ( const ThreadManagerStd )
private

Member Function Documentation

◆ Create()

static ThreadManagerSharedPtr Nektar::Thread::ThreadManagerStd::Create ( unsigned int  numT)
inlinestatic

Called by the factory method.

Definition at line 104 of file ThreadStd.h.

105 {
106 return std::shared_ptr<ThreadManager>(new ThreadManagerStd(numT));
107 }

References ThreadManagerStd().

◆ IsWorking()

bool Nektar::Thread::ThreadManagerStd::IsWorking ( )
private

Definition at line 165 of file ThreadStd.cpp.

166{
167 bool working = false;
168 Lock masterActiveLock(m_masterActiveMutex);
169 for (unsigned int i = 0; i < m_numWorkers; i++)
170 {
171 working = working || m_threadBusyList[i];
172 }
173 return working;
174}
std::unique_lock< std::mutex > Lock
Definition: ThreadStd.h:50

References m_masterActiveMutex, m_numWorkers, and m_threadBusyList.

Referenced by v_Wait().

◆ SetNumWorkersImpl()

void Nektar::Thread::ThreadManagerStd::SetNumWorkersImpl ( const unsigned int  num)
private

Definition at line 242 of file ThreadStd.cpp.

243{
244 Lock masterActiveLock(m_masterActiveMutex); // locks the active
245
246 if (m_numWorkers == num)
247 {
248 return;
249 }
250
251 delete m_barrier;
252 m_barrier = new Barrier(num > 0 ? num : 1);
253
254 m_numWorkers = num;
255 for (unsigned int i = 0; i < m_numThreads; i++)
256 {
257 m_threadActiveList[i] = i < m_numWorkers ? true : false;
258 }
259 m_masterActiveCondVar.notify_all();
260} // Lock on active released here

References m_barrier, m_masterActiveCondVar, m_masterActiveMutex, m_numThreads, m_numWorkers, and m_threadActiveList.

Referenced by v_SetNumWorkers().

◆ v_GetMaxNumWorkers()

unsigned int Nektar::Thread::ThreadManagerStd::v_GetMaxNumWorkers ( )
overrideprotectedvirtual

Implements Nektar::Thread::ThreadManager.

Definition at line 284 of file ThreadStd.cpp.

285{
286 return m_numThreads;
287}

References m_numThreads.

◆ v_GetNumWorkers()

unsigned int Nektar::Thread::ThreadManagerStd::v_GetNumWorkers ( )
overrideprotectedvirtual

Implements Nektar::Thread::ThreadManager.

Definition at line 225 of file ThreadStd.cpp.

226{
227 return m_numWorkers;
228}

References m_numWorkers.

◆ v_GetType()

const std::string & Nektar::Thread::ThreadManagerStd::v_GetType ( ) const
overrideprotectedvirtual

Implements Nektar::Thread::ThreadManager.

Definition at line 300 of file ThreadStd.cpp.

301{
302 return m_type;
303}

References m_type.

◆ v_GetWorkerNum()

unsigned int Nektar::Thread::ThreadManagerStd::v_GetWorkerNum ( )
overrideprotectedvirtual

Implements Nektar::Thread::ThreadManager.

Definition at line 233 of file ThreadStd.cpp.

234{
235 std::thread::id id = std::this_thread::get_id();
236 return m_threadMap[id];
237}

References m_threadMap.

◆ v_Hold()

void Nektar::Thread::ThreadManagerStd::v_Hold ( )
overrideprotectedvirtual

◆ v_InThread()

bool Nektar::Thread::ThreadManagerStd::v_InThread ( )
overrideprotectedvirtual

Implements Nektar::Thread::ThreadManager.

Definition at line 197 of file ThreadStd.cpp.

198{
199 std::thread::id id = std::this_thread::get_id();
200 return (id != m_masterThreadId);
201}

References m_masterThreadId.

◆ v_QueueJob()

void Nektar::Thread::ThreadManagerStd::v_QueueJob ( ThreadJob job)
overrideprotectedvirtual

Implements Nektar::Thread::ThreadManager.

Definition at line 155 of file ThreadStd.cpp.

156{
157 Lock masterQueueLock(m_masterQueueMutex); // locks the queue
158 m_masterQueue.push(job);
159 m_masterQueueCondVar.notify_all(); // alert a waiting thread.
160} // queue unlocked

References m_masterQueue, m_masterQueueCondVar, and m_masterQueueMutex.

◆ v_QueueJobs()

void Nektar::Thread::ThreadManagerStd::v_QueueJobs ( std::vector< ThreadJob * > &  joblist)
overrideprotectedvirtual

Implements Nektar::Thread::ThreadManager.

Definition at line 143 of file ThreadStd.cpp.

144{
145 std::vector<ThreadJob *>::iterator it;
146 for (it = joblist.begin(); it < joblist.end(); ++it)
147 {
148 QueueJob(*it);
149 }
150}
void QueueJob(ThreadJob *job)
Pass a single job to the master queue.
Definition: Thread.h:188

References Nektar::Thread::ThreadManager::QueueJob().

◆ v_SetChunkSize()

void Nektar::Thread::ThreadManagerStd::v_SetChunkSize ( unsigned int  chnk)
overrideprotectedvirtual

Implements Nektar::Thread::ThreadManager.

Definition at line 179 of file ThreadStd.cpp.

180{
181 Lock masterQueueLock(m_masterQueueMutex); // locks the queue
182 m_chunkSize = std::max(chnk, 1U);
183}

References m_chunkSize, and m_masterQueueMutex.

◆ v_SetNumWorkers() [1/2]

void Nektar::Thread::ThreadManagerStd::v_SetNumWorkers ( )
overrideprotectedvirtual

Implements Nektar::Thread::ThreadManager.

Definition at line 276 of file ThreadStd.cpp.

277{
279}
void SetNumWorkersImpl(const unsigned int num)
Definition: ThreadStd.cpp:242

References m_numThreads, and SetNumWorkersImpl().

◆ v_SetNumWorkers() [2/2]

void Nektar::Thread::ThreadManagerStd::v_SetNumWorkers ( const unsigned int  num)
overrideprotectedvirtual

Implements Nektar::Thread::ThreadManager.

Definition at line 265 of file ThreadStd.cpp.

266{
267 unsigned int n;
268 n = std::min(num, m_numThreads);
269 n = std::max(n, static_cast<unsigned int>(0));
271}

References m_numThreads, and SetNumWorkersImpl().

◆ v_SetSchedType()

void Nektar::Thread::ThreadManagerStd::v_SetSchedType ( SchedType  s)
overrideprotectedvirtual

Implements Nektar::Thread::ThreadManager.

Definition at line 188 of file ThreadStd.cpp.

189{
190 Lock masterQueueLock(m_masterQueueMutex); // locks the queue
191 m_schedType = s;
192}

References m_masterQueueMutex, and m_schedType.

◆ v_Wait()

void Nektar::Thread::ThreadManagerStd::v_Wait ( )
overrideprotectedvirtual

Implements Nektar::Thread::ThreadManager.

Definition at line 206 of file ThreadStd.cpp.

207{
208 bool working;
209 Lock masterQueueLock(m_masterQueueMutex); // locks the queue
210 working = IsWorking();
211 m_masterActiveCondVar.notify_all();
212 m_masterQueueCondVar.notify_all();
213 while (!m_masterQueue.empty() || working)
214 {
215 // while waiting, master queue is unlocked
216 m_masterQueueCondVar.wait(masterQueueLock);
217 // on exiting wait master queue is locked again
218 working = IsWorking();
219 }
220}

References IsWorking(), m_masterActiveCondVar, m_masterQueue, m_masterQueueCondVar, and m_masterQueueMutex.

Friends And Related Function Documentation

◆ ThreadWorkerStd

friend class ThreadWorkerStd
friend

So the workers can access the master queue and locks.

Definition at line 95 of file ThreadStd.h.

Referenced by ThreadManagerStd().

Member Data Documentation

◆ className

std::string Nektar::Thread::ThreadManagerStd::className
staticprivate
Initial value:
=
"ThreadManagerStd", ThreadManagerStd::Create,
"Threading using std::thread.")
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
static ThreadManagerSharedPtr Create(unsigned int numT)
Called by the factory method.
Definition: ThreadStd.h:104
ThreadManagerFactory & GetThreadManagerFactory()
Definition: Thread.cpp:45

Definition at line 147 of file ThreadStd.h.

◆ m_barrier

Barrier* Nektar::Thread::ThreadManagerStd::m_barrier
private

Definition at line 145 of file ThreadStd.h.

Referenced by SetNumWorkersImpl(), ThreadManagerStd(), v_Hold(), and ~ThreadManagerStd().

◆ m_chunkSize

unsigned int Nektar::Thread::ThreadManagerStd::m_chunkSize
private

Definition at line 143 of file ThreadStd.h.

Referenced by Nektar::Thread::ThreadWorkerStd::GetNumToLoad(), and v_SetChunkSize().

◆ m_masterActiveCondVar

std::condition_variable Nektar::Thread::ThreadManagerStd::m_masterActiveCondVar
private

◆ m_masterActiveMutex

std::mutex Nektar::Thread::ThreadManagerStd::m_masterActiveMutex
private

◆ m_masterQueue

std::queue<ThreadJob *> Nektar::Thread::ThreadManagerStd::m_masterQueue
private

◆ m_masterQueueCondVar

std::condition_variable Nektar::Thread::ThreadManagerStd::m_masterQueueCondVar
private

◆ m_masterQueueMutex

std::mutex Nektar::Thread::ThreadManagerStd::m_masterQueueMutex
private

◆ m_masterThreadId

std::thread::id Nektar::Thread::ThreadManagerStd::m_masterThreadId
private

Definition at line 140 of file ThreadStd.h.

Referenced by ThreadManagerStd(), and v_InThread().

◆ m_numThreads

const unsigned int Nektar::Thread::ThreadManagerStd::m_numThreads
private

◆ m_numWorkers

unsigned int Nektar::Thread::ThreadManagerStd::m_numWorkers
private

◆ m_schedType

SchedType Nektar::Thread::ThreadManagerStd::m_schedType
private

Definition at line 144 of file ThreadStd.h.

Referenced by Nektar::Thread::ThreadWorkerStd::GetNumToLoad(), and v_SetSchedType().

◆ m_threadActiveList

bool* Nektar::Thread::ThreadManagerStd::m_threadActiveList
private

◆ m_threadBusyList

bool* Nektar::Thread::ThreadManagerStd::m_threadBusyList
private

◆ m_threadList

ThreadWorkerStd** Nektar::Thread::ThreadManagerStd::m_threadList
private

Definition at line 138 of file ThreadStd.h.

Referenced by ThreadManagerStd(), and ~ThreadManagerStd().

◆ m_threadMap

std::map<std::thread::id, unsigned int> Nektar::Thread::ThreadManagerStd::m_threadMap
private

Definition at line 146 of file ThreadStd.h.

Referenced by ThreadManagerStd(), and v_GetWorkerNum().

◆ m_threadThreadList

std::thread** Nektar::Thread::ThreadManagerStd::m_threadThreadList
private

Definition at line 139 of file ThreadStd.h.

Referenced by ThreadManagerStd(), and ~ThreadManagerStd().

◆ m_type

std::string Nektar::Thread::ThreadManagerStd::m_type
private

Definition at line 148 of file ThreadStd.h.

Referenced by ThreadManagerStd(), and v_GetType().