Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Public Member Functions | List of all members
Nektar::Thread::ThreadManager Class Referenceabstract

The interface class for the controller for worker threads and jobs. More...

#include <Thread.h>

Inheritance diagram for Nektar::Thread::ThreadManager:
Inheritance graph
[legend]
Collaboration diagram for Nektar::Thread::ThreadManager:
Collaboration graph
[legend]

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)
 

Detailed Description

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.

Definition at line 167 of file Thread.h.

Constructor & Destructor Documentation

Nektar::Thread::ThreadManager::~ThreadManager ( )
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.

115 {
116  // empty
117 }

Member Function Documentation

virtual unsigned int Nektar::Thread::ThreadManager::GetMaxNumWorkers ( )
pure virtual

Gets the maximum available number of threads.

Returns
The maximum number of workers.

Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.

Referenced by GetPartitionFromRankThr(), GetRankFromPartition(), and GetThrFromPartition().

virtual unsigned int Nektar::Thread::ThreadManager::GetNumWorkers ( )
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.

int Nektar::Thread::ThreadManager::GetPartitionFromRankThr ( int  pRank,
unsigned int  pThr 
)
inline

Definition at line 318 of file Thread.h.

References GetMaxNumWorkers().

319  {
320  return pRank * GetMaxNumWorkers() + pThr;
321  }
virtual unsigned int GetMaxNumWorkers()=0
Gets the maximum available number of threads.
int Nektar::Thread::ThreadManager::GetRankFromPartition ( int  pPartition)
inline

Definition at line 313 of file Thread.h.

References GetMaxNumWorkers().

314  {
315  return pPartition / GetMaxNumWorkers();
316  }
virtual unsigned int GetMaxNumWorkers()=0
Gets the maximum available number of threads.
int Nektar::Thread::ThreadManager::GetThrFromPartition ( int  pPartition)
inline

Definition at line 308 of file Thread.h.

References GetMaxNumWorkers().

309  {
310  return pPartition % GetMaxNumWorkers();
311  }
virtual unsigned int GetMaxNumWorkers()=0
Gets the maximum available number of threads.
virtual const std::string& Nektar::Thread::ThreadManager::GetType ( ) const
pure virtual

Returns a description of the type of threading.

E.g. "Threading with Boost"

Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.

virtual unsigned int Nektar::Thread::ThreadManager::GetWorkerNum ( )
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.

virtual void Nektar::Thread::ThreadManager::Hold ( )
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.

Note
Behaviour is likely undefined if the number of active workers is altered after a thread has called this method. It is only safe to call SetNumWorkers() when no threads are holding.

Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.

virtual bool Nektar::Thread::ThreadManager::InThread ( )
pure virtual

Indicates whether the code is in a worker thread or not.

Returns
True if the caller is in a worker thread.

Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.

bool Nektar::Thread::ThreadManager::IsInitialised ( )
virtual

ThreadManager implementation.

Implementations should not override this function, since they are initialised.

Returns
True if this ThreadManager has been initialised.

Reimplemented in Nektar::Thread::ThreadStartupManager.

Definition at line 102 of file Thread.cpp.

103 {
104  return true;
105 }
virtual void Nektar::Thread::ThreadManager::QueueJob ( ThreadJob job)
pure virtual

Pass a single job to the master queue.

Parameters
jobA 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.

virtual void Nektar::Thread::ThreadManager::QueueJobs ( std::vector< ThreadJob * > &  joblist)
pure virtual

Pass a list of tasklets to the master queue.

Parameters
joblistVector 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.

See also
SchedType

Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.

virtual void Nektar::Thread::ThreadManager::SetChunkSize ( unsigned int  chnk)
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.

See also
SchedType
SetSchedType()

Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.

virtual void Nektar::Thread::ThreadManager::SetNumWorkers ( const unsigned int  num)
pure virtual

Sets the number of active workers.

Parameters
numThe 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.

virtual void Nektar::Thread::ThreadManager::SetNumWorkers ( )
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.

virtual void Nektar::Thread::ThreadManager::SetSchedType ( SchedType  s)
pure virtual

Sets the current scheduling algorithm.

See also
SetChunkSize()

Implemented in Nektar::Thread::ThreadStartupManager, and Nektar::Thread::ThreadManagerBoost.

virtual void Nektar::Thread::ThreadManager::Wait ( )
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:

// assume ThreadManager* tm
// assume SomeJob is subclass of ThreadJob
// assume SomeJob job
tm->SetNumWorkers(0);
tm->QueueJob(job);
tm->Wait();

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.