Nektar++
|
The interface class for the controller for worker threads and jobs. More...
#include <Thread.h>
Public Member Functions | |
virtual | ~ThreadManager () |
Destructor. More... | |
virtual void | QueueJobs (std::vector< ThreadJob * > &joblist)=0 |
Pass a list of tasklets to the master queue. More... | |
virtual void | QueueJob (ThreadJob *job)=0 |
Pass a single job to the master queue. More... | |
virtual unsigned int | GetNumWorkers ()=0 |
Return the number of active workers. More... | |
virtual unsigned int | GetWorkerNum ()=0 |
Returns the worker number of the executing thread. More... | |
virtual void | SetNumWorkers (const unsigned int num)=0 |
Sets the number of active workers. More... | |
virtual void | SetNumWorkers ()=0 |
Sets the number of active workers to the maximum. More... | |
virtual unsigned int | GetMaxNumWorkers ()=0 |
Gets the maximum available number of threads. More... | |
virtual void | Wait ()=0 |
Waits until all queued jobs are finished. More... | |
virtual void | SetChunkSize (unsigned int chnk)=0 |
Controls how many jobs are sent to each worker at a time. More... | |
virtual void | SetSchedType (SchedType s)=0 |
Sets the current scheduling algorithm. More... | |
virtual bool | InThread ()=0 |
Indicates whether the code is in a worker thread or not. More... | |
virtual void | Hold ()=0 |
A calling threads holds until all active threads call this method. More... | |
virtual const std::string & | GetType () const =0 |
Returns a description of the type of threading. More... | |
virtual bool | IsInitialised () |
ThreadManager implementation. More... | |
int | GetThrFromPartition (int pPartition) |
int | GetRankFromPartition (int pPartition) |
int | GetPartitionFromRankThr (int pRank, unsigned int pThr) |
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 114 of file Thread.cpp.
|
pure virtual |
Gets the maximum available number of threads.
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.
Referenced by GetPartitionFromRankThr(), GetRankFromPartition(), and GetThrFromPartition().
|
pure virtual |
Return the number of active workers.
Active workers are threads that are either running jobs or are waiting for jobs to be queued.
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.
|
inline |
Definition at line 318 of file Thread.h.
References GetMaxNumWorkers().
|
inline |
Definition at line 313 of file Thread.h.
References GetMaxNumWorkers().
|
inline |
Definition at line 308 of file Thread.h.
References GetMaxNumWorkers().
|
pure virtual |
Returns a description of the type of threading.
E.g. "Threading with Boost"
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.
|
pure 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.
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.
|
pure 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.
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.
|
pure virtual |
Indicates whether the code is in a worker thread or not.
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.
|
virtual |
ThreadManager implementation.
Implementations should not override this function, since they are initialised.
Reimplemented in Nektar::Thread::ThreadStartupManager.
Definition at line 102 of file Thread.cpp.
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.
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.
|
pure virtual |
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.
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.
|
pure 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.
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.
|
pure 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.
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.
|
pure virtual |
Sets the number of active workers to the maximum.
Sets the number of active workers to the maximum available.
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.
Sets the current scheduling algorithm.
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.
|
pure 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().
Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.