Nektar++
|
Implementation of ThreadManager using Boost threads. More...
#include <ThreadBoost.h>
Public Member Functions | |
ThreadManagerBoost (unsigned int numWorkers) | |
Constructs a ThreadManagerBoost. More... | |
virtual | ~ThreadManagerBoost () |
Shuts down threading. More... | |
virtual void | QueueJobs (std::vector< ThreadJob * > &joblist) |
Pass a list of tasklets to the master queue. More... | |
virtual void | QueueJob (ThreadJob *job) |
Pass a single job to the master queue. More... | |
virtual unsigned int | GetNumWorkers () |
Return the number of active workers. More... | |
virtual unsigned int | GetWorkerNum () |
Returns the worker number of the executing thread. More... | |
virtual void | SetNumWorkers (unsigned int num) |
Sets the number of active workers. More... | |
virtual void | SetNumWorkers () |
Sets the number of active workers to the maximum. More... | |
virtual unsigned int | GetMaxNumWorkers () |
Gets the maximum available number of threads. More... | |
virtual void | Wait () |
Waits until all queued jobs are finished. More... | |
virtual void | SetChunkSize (unsigned int chnk) |
Controls how many jobs are sent to each worker at a time. More... | |
virtual void | SetSchedType (SchedType s) |
Sets the current scheduling algorithm. More... | |
virtual bool | InThread () |
Indicates whether the code is in a worker thread or not. More... | |
virtual void | Hold () |
A calling threads holds until all active threads call this method. More... | |
virtual const std::string & | GetType () const |
Returns a description of the type of threading. More... | |
Public Member Functions inherited from Nektar::Thread::ThreadManager | |
virtual | ~ThreadManager () |
Destructor. More... | |
virtual 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... | |
Private Member Functions | |
ThreadManagerBoost () | |
ThreadManagerBoost (const ThreadManagerBoost &) | |
bool | IsWorking () |
void | SetNumWorkersImpl (const unsigned int num) |
Private Attributes | |
const unsigned int | m_numThreads |
unsigned int | m_numWorkers |
std::queue< ThreadJob * > | m_masterQueue |
boost::mutex | m_masterQueueMutex |
boost::mutex | m_masterActiveMutex |
boost::condition_variable | m_masterQueueCondVar |
boost::condition_variable | m_masterActiveCondVar |
ThreadWorkerBoost ** | m_threadList |
boost::thread ** | m_threadThreadList |
boost::thread::id | m_masterThreadId |
bool * | m_threadBusyList |
bool * | m_threadActiveList |
unsigned int | m_chunkSize |
SchedType | m_schedType |
boost::barrier * | m_barrier |
std::map< boost::thread::id, unsigned int > | m_threadMap |
std::string | m_type |
Static Private Attributes | |
static std::string | className |
Friends | |
class | ThreadWorkerBoost |
Implementation of ThreadManager using Boost threads.
Definition at line 59 of file ThreadBoost.h.
Nektar::Thread::ThreadManagerBoost::ThreadManagerBoost | ( | unsigned int | numT | ) |
Constructs a ThreadManagerBoost.
numWorkers | The number of threads to start (including master thread). |
Definition at line 56 of file ThreadBoost.cpp.
References m_barrier, m_masterThreadId, m_numThreads, m_numWorkers, m_threadActiveList, m_threadBusyList, m_threadList, m_threadMap, m_threadThreadList, m_type, and ThreadWorkerBoost.
|
virtual |
Shuts down threading.
Terminates all running threads (they will finish their current job), releases resources and destructs.
Definition at line 118 of file ThreadBoost.cpp.
References m_barrier, m_masterActiveCondVar, m_masterQueueCondVar, m_numThreads, m_threadActiveList, m_threadBusyList, m_threadList, m_threadThreadList, and Nektar::Thread::ThreadWorkerBoost::Stop().
|
private |
Referenced by Create().
|
private |
|
inlinestatic |
Called by the factory method.
Definition at line 87 of file ThreadBoost.h.
References ThreadManagerBoost().
|
virtual |
Gets the maximum available number of threads.
Implements Nektar::Thread::ThreadManager.
Definition at line 299 of file ThreadBoost.cpp.
References m_numThreads.
|
virtual |
Return the number of active workers.
Active workers are threads that are either running jobs or are waiting for jobs to be queued.
Implements Nektar::Thread::ThreadManager.
Definition at line 236 of file ThreadBoost.cpp.
References m_numWorkers.
|
virtual |
Returns a description of the type of threading.
E.g. "Threading with Boost"
Implements Nektar::Thread::ThreadManager.
Definition at line 317 of file ThreadBoost.cpp.
References m_type.
|
virtual |
Returns the worker number of the executing thread.
Returns an unsigned int between 0 and N-1 where N is the number of active worker threads. Repeated calls from within this thread will always return the same value and the value will be the same as returned from ThreadJob.GetWorkerNum(). The same thread will run a job until it finishes.
Although if there are active threads then thread 0 is always one of them, it is possible that thread 0 does not run for a given set of jobs. For example, if there are 4 active threads and 3 jobs are submitted with a e_static scheduling strategy and a chunksize of 1, then it is possible that threads 1,2, and 3 pick up the jobs and thread 0 remains idle.
Returns 0 if called by non-thread.
Implements Nektar::Thread::ThreadManager.
Definition at line 245 of file ThreadBoost.cpp.
References m_threadMap.
|
virtual |
A calling threads holds until all active threads call this method.
When called, the calling thread will sleep until all active workers have called this method. Once all have done so all threads awake and continue execution.
Implements Nektar::Thread::ThreadManager.
Definition at line 308 of file ThreadBoost.cpp.
References m_barrier.
|
virtual |
Indicates whether the code is in a worker thread or not.
Implements Nektar::Thread::ThreadManager.
Definition at line 206 of file ThreadBoost.cpp.
References m_masterThreadId.
|
private |
Definition at line 171 of file ThreadBoost.cpp.
References m_masterActiveMutex, m_numWorkers, and m_threadBusyList.
Referenced by Wait().
Pass a single job to the master queue.
job | A pointer to a ThreadJob subclass. |
The job may become available for running immediately. If this is an issue then suspend the workers with SetNumWorkers(0) until the jobs are queued.
Implements Nektar::Thread::ThreadManager.
Definition at line 160 of file ThreadBoost.cpp.
References m_masterQueue, m_masterQueueCondVar, and m_masterQueueMutex.
Referenced by QueueJobs().
Pass a list of tasklets to the master queue.
joblist | Vector of ThreadJob pointers. |
The list of jobs is copied into the master queue. Jobs may be available for running immediately, even before the list has been fully copied. This can have consequences for the scheduling. If this is an issue then suspend the workers with SetNumWorkers(0) until the jobs are queued.
Implements Nektar::Thread::ThreadManager.
Definition at line 147 of file ThreadBoost.cpp.
References Nektar::iterator, and QueueJob().
|
virtual |
Controls how many jobs are sent to each worker at a time.
The exact meaning of this parameter depends on the current scheduling algorithm.
Implements Nektar::Thread::ThreadManager.
Definition at line 186 of file ThreadBoost.cpp.
References m_chunkSize, and m_masterQueueMutex.
|
virtual |
Sets the number of active workers.
num | The number of active workers. |
Active workers are threads that are either running jobs or are waiting for jobs to be queued.
If num is greater than the maximum allowed number of active workers, then the maximum value will be used instead.
Implements Nektar::Thread::ThreadManager.
Definition at line 279 of file ThreadBoost.cpp.
References m_numThreads, and SetNumWorkersImpl().
|
virtual |
Sets the number of active workers to the maximum.
Sets the number of active workers to the maximum available.
Implements Nektar::Thread::ThreadManager.
Definition at line 290 of file ThreadBoost.cpp.
References m_numThreads, and SetNumWorkersImpl().
|
private |
Definition at line 255 of file ThreadBoost.cpp.
References m_barrier, m_masterActiveCondVar, m_masterActiveMutex, m_numThreads, m_numWorkers, and m_threadActiveList.
Referenced by SetNumWorkers().
Sets the current scheduling algorithm.
Implements Nektar::Thread::ThreadManager.
Definition at line 196 of file ThreadBoost.cpp.
References m_masterQueueMutex, and m_schedType.
|
virtual |
Waits until all queued jobs are finished.
If there are no jobs running or queued this method returns immediately. Otherwise it blocks until the queue is empty and the worker threads are idle.
Implementations must ensure that trivial deadlocks are not possible from this method, that is, that this code:
does not wait forever. Since the master thread is counted in the number of worker threads, implementations should increase the number of active workers by 1 on entering Wait().
Implements Nektar::Thread::ThreadManager.
Definition at line 216 of file ThreadBoost.cpp.
References IsWorking(), m_masterActiveCondVar, m_masterQueue, m_masterQueueCondVar, and m_masterQueueMutex.
|
friend |
So the workers can access the master queue and locks.
Definition at line 64 of file ThreadBoost.h.
Referenced by ThreadManagerBoost().
|
staticprivate |
Definition at line 116 of file ThreadBoost.h.
|
private |
Definition at line 114 of file ThreadBoost.h.
Referenced by Hold(), SetNumWorkersImpl(), ThreadManagerBoost(), and ~ThreadManagerBoost().
|
private |
Definition at line 112 of file ThreadBoost.h.
Referenced by Nektar::Thread::ThreadWorkerBoost::GetNumToLoad(), and SetChunkSize().
|
private |
Definition at line 106 of file ThreadBoost.h.
Referenced by SetNumWorkersImpl(), Wait(), Nektar::Thread::ThreadWorkerBoost::WaitForActive(), and ~ThreadManagerBoost().
|
private |
Definition at line 104 of file ThreadBoost.h.
Referenced by IsWorking(), Nektar::Thread::ThreadWorkerBoost::LoadJobs(), SetNumWorkersImpl(), and Nektar::Thread::ThreadWorkerBoost::WaitForActive().
|
private |
Definition at line 102 of file ThreadBoost.h.
Referenced by Nektar::Thread::ThreadWorkerBoost::GetNumToLoad(), Nektar::Thread::ThreadWorkerBoost::LoadJobs(), QueueJob(), and Wait().
|
private |
Definition at line 105 of file ThreadBoost.h.
Referenced by Nektar::Thread::ThreadWorkerBoost::LoadJobs(), QueueJob(), Wait(), and ~ThreadManagerBoost().
|
private |
Definition at line 103 of file ThreadBoost.h.
Referenced by Nektar::Thread::ThreadWorkerBoost::LoadJobs(), QueueJob(), SetChunkSize(), SetSchedType(), and Wait().
|
private |
Definition at line 109 of file ThreadBoost.h.
Referenced by InThread(), and ThreadManagerBoost().
|
private |
Definition at line 100 of file ThreadBoost.h.
Referenced by GetMaxNumWorkers(), SetNumWorkers(), SetNumWorkersImpl(), ThreadManagerBoost(), and ~ThreadManagerBoost().
|
private |
Definition at line 101 of file ThreadBoost.h.
Referenced by Nektar::Thread::ThreadWorkerBoost::GetNumToLoad(), GetNumWorkers(), IsWorking(), SetNumWorkersImpl(), and ThreadManagerBoost().
|
private |
Definition at line 113 of file ThreadBoost.h.
Referenced by Nektar::Thread::ThreadWorkerBoost::GetNumToLoad(), and SetSchedType().
|
private |
Definition at line 111 of file ThreadBoost.h.
Referenced by Nektar::Thread::ThreadWorkerBoost::LoadJobs(), SetNumWorkersImpl(), ThreadManagerBoost(), Nektar::Thread::ThreadWorkerBoost::WaitForActive(), and ~ThreadManagerBoost().
|
private |
Definition at line 110 of file ThreadBoost.h.
Referenced by IsWorking(), Nektar::Thread::ThreadWorkerBoost::LoadJobs(), ThreadManagerBoost(), and ~ThreadManagerBoost().
|
private |
Definition at line 107 of file ThreadBoost.h.
Referenced by ThreadManagerBoost(), and ~ThreadManagerBoost().
|
private |
Definition at line 115 of file ThreadBoost.h.
Referenced by GetWorkerNum(), and ThreadManagerBoost().
|
private |
Definition at line 108 of file ThreadBoost.h.
Referenced by ThreadManagerBoost(), and ~ThreadManagerBoost().
|
private |
Definition at line 117 of file ThreadBoost.h.
Referenced by GetType(), and ThreadManagerBoost().