Nektar++
|
The interface class for the controller for worker threads and jobs. More...
#include <Thread.h>
Public Member Functions | |
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) |
Protected Member Functions | |
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 () |
The interface class for the controller for worker threads and jobs.
There may be multiple instances of implementations of this class. They are instantiated by ThreadMaster::CreateInstance(string, int). Use ThreadMaster::GetInstance(string) to get a pointer to the instance. Each ThreadManager is associated in ThreadMaster with a string.
Jobs are sent to the worker threads by placing them in the queue with QueueJob() and QueueJobs(). Jobs may be run in indeterminate order. Jobs are permitted to run immediately they are queued. If this is not desired set the number of active workers to 0 until all jobs are queued.
Jobs are taken from the master queue by active worker threads.
We have the concept of the master thread. This is the thread that instantiates the ThreadManager, calls QueueJob(), Wait() and so on. This thread must be included in the number of threads started when the ThreadManager is created, so that no more than this number of threads are ever executing work at any time. The master thread is assumed to be working unless it is in Wait().
Thus if the ThreadManager is called with CreateInstance(string s, int N) there should be N threads /em including the master thread. If the master thread calls Wait() (which causes the master thread to sleep until the job queue is empty) then the master thread should become available to run jobs from the queue.
Only the master thread should call ThreadManager methods. Although it should always be possible for code to identify whether it's the master thread (through design) there is a InThread() method that returns true if the thread is a worker.
|
virtual |
Destructor.
Should stop worker threads and clean up. This shouldn't be called until the program is exiting anyway, some implementations may need to unlock threads to allow clean exit.
Definition at line 102 of file Thread.cpp.
|
inline |
Gets the maximum available number of threads.
Definition at line 251 of file Thread.h.
References v_GetMaxNumWorkers().
Referenced by GetPartitionFromRankThr(), GetRankFromPartition(), and GetThrFromPartition().
|
inline |
Return the number of active workers.
Active workers are threads that are either running jobs or are waiting for jobs to be queued.
Definition at line 198 of file Thread.h.
References v_GetNumWorkers().
|
inline |
Definition at line 352 of file Thread.h.
References GetMaxNumWorkers().
|
inline |
Definition at line 347 of file Thread.h.
References GetMaxNumWorkers().
|
inline |
Definition at line 342 of file Thread.h.
References GetMaxNumWorkers().
|
inline |
Returns a description of the type of threading.
E.g. "Threading with Boost"
Definition at line 331 of file Thread.h.
References v_GetType().
|
inline |
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.
Definition at line 220 of file Thread.h.
References v_GetWorkerNum().
|
inline |
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.
Definition at line 322 of file Thread.h.
References v_Hold().
|
inline |
Indicates whether the code is in a worker thread or not.
Definition at line 306 of file Thread.h.
References v_InThread().
|
inline |
ThreadManager implementation.
Definition at line 337 of file Thread.h.
References v_IsInitialised().
Pass a single job to the master queue.
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.
Definition at line 188 of file Thread.h.
References v_QueueJob().
Referenced by Nektar::Thread::ThreadManagerStd::v_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.
Definition at line 176 of file Thread.h.
References v_QueueJobs().
|
inline |
Controls how many jobs are sent to each worker at a time.
The exact meaning of this parameter depends on the current scheduling algorithm.
Definition at line 290 of file Thread.h.
References v_SetChunkSize().
|
inline |
Sets the number of active workers to the maximum.
Sets the number of active workers to the maximum available.
Definition at line 243 of file Thread.h.
References v_SetNumWorkers().
|
inline |
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.
Definition at line 234 of file Thread.h.
References v_SetNumWorkers().
Sets the current scheduling algorithm.
Definition at line 298 of file Thread.h.
References v_SetSchedType().
|
protectedpure virtual |
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerStd.
Referenced by GetMaxNumWorkers().
|
protectedpure virtual |
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerStd.
Referenced by GetNumWorkers().
|
protectedpure virtual |
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerStd.
Referenced by GetType().
|
protectedpure virtual |
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerStd.
Referenced by GetWorkerNum().
|
protectedpure virtual |
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerStd.
Referenced by Hold().
|
protectedpure virtual |
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerStd.
Referenced by InThread().
|
protectedvirtual |
Implementations should not override this function, since they are initialised.
Reimplemented in Nektar::Thread::ThreadStartupManager.
Definition at line 91 of file Thread.cpp.
Referenced by IsInitialised().
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerStd.
Referenced by QueueJob().
|
protectedpure virtual |
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerStd.
Referenced by QueueJobs().
|
protectedpure virtual |
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerStd.
Referenced by SetChunkSize().
|
protectedpure virtual |
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerStd.
Referenced by SetNumWorkers().
|
protectedpure virtual |
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerStd.
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerStd.
Referenced by SetSchedType().
|
protectedpure virtual |
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerStd.
Referenced by Wait().
|
inline |
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().
Definition at line 277 of file Thread.h.
References v_Wait().