Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Private Attributes | List of all members
Nektar::Thread::ThreadStartupManager Class Reference

A default ThreadManager. More...

#include <Thread.h>

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

Public Member Functions

 ThreadStartupManager ()
 ThreadDefaultManager. More...
 
virtual ~ThreadStartupManager ()
 
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 (const 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 bool IsInitialised ()
 ThreadManager implementation. 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...
 
int GetThrFromPartition (int pPartition)
 
int GetRankFromPartition (int pPartition)
 
int GetPartitionFromRankThr (int pRank, unsigned int pThr)
 

Private Attributes

const std::string m_type
 

Detailed Description

A default ThreadManager.

This will be returned by ThreadMaster if a ThreadManager has not been initialised, such as if the code is still in static initialisation.

This manager pretends to be a ThreadManager with 1 thread. It will cause an error if anything more than trivial functions are called.

Definition at line 383 of file Thread.h.

Constructor & Destructor Documentation

Nektar::Thread::ThreadStartupManager::ThreadStartupManager ( )

ThreadDefaultManager.

Definition at line 213 of file Thread.cpp.

213  : m_type("Threading starting up")
214 {
215  // empty
216 }
Nektar::Thread::ThreadStartupManager::~ThreadStartupManager ( )
virtual

Definition at line 222 of file Thread.cpp.

223 {
224  // empty
225 }

Member Function Documentation

unsigned int Nektar::Thread::ThreadStartupManager::GetMaxNumWorkers ( )
virtual

Gets the maximum available number of threads.

Returns
The maximum number of workers.

Implements Nektar::Thread::ThreadManager.

Definition at line 288 of file Thread.cpp.

289 {
290  return 1;
291 }
unsigned int Nektar::Thread::ThreadStartupManager::GetNumWorkers ( )
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 251 of file Thread.cpp.

252 {
253  return 1;
254 }
const std::string & Nektar::Thread::ThreadStartupManager::GetType ( ) const
virtual

Returns a description of the type of threading.

E.g. "Threading with Boost"

Implements Nektar::Thread::ThreadManager.

Definition at line 353 of file Thread.cpp.

References m_type.

354 {
355  return m_type;
356 }
unsigned int Nektar::Thread::ThreadStartupManager::GetWorkerNum ( )
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 260 of file Thread.cpp.

261 {
262  return 0;
263 }
void Nektar::Thread::ThreadStartupManager::Hold ( )
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.

Implements Nektar::Thread::ThreadManager.

Definition at line 335 of file Thread.cpp.

336 {
337  return;
338 }
bool Nektar::Thread::ThreadStartupManager::InThread ( )
virtual

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

Returns
True if the caller is in a worker thread.

Implements Nektar::Thread::ThreadManager.

Definition at line 326 of file Thread.cpp.

327 {
328  return false;
329 }
bool Nektar::Thread::ThreadStartupManager::IsInitialised ( )
virtual

ThreadManager implementation.

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

Returns
True if this ThreadManager has been initialised.

Reimplemented from Nektar::Thread::ThreadManager.

Definition at line 344 of file Thread.cpp.

345 {
346  return false;
347 }
void Nektar::Thread::ThreadStartupManager::QueueJob ( ThreadJob job)
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.

Implements Nektar::Thread::ThreadManager.

Definition at line 241 of file Thread.cpp.

References ErrorUtil::efatal, and NEKERROR.

242 {
244  "Attempted to QueueJob in ThreadDefaultManager");
245 }
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:185
void Nektar::Thread::ThreadStartupManager::QueueJobs ( std::vector< ThreadJob * > &  joblist)
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

Implements Nektar::Thread::ThreadManager.

Definition at line 231 of file Thread.cpp.

References ErrorUtil::efatal, and NEKERROR.

232 {
234  "Attempted to QueueJobs in ThreadDefaultManager");
235 }
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:185
void Nektar::Thread::ThreadStartupManager::SetChunkSize ( unsigned int  chnk)
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()

Implements Nektar::Thread::ThreadManager.

Definition at line 306 of file Thread.cpp.

References ErrorUtil::efatal, and NEKERROR.

307 {
309  "Attempted to SetChunkSize in ThreadDefaultManager");
310 }
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:185
void Nektar::Thread::ThreadStartupManager::SetNumWorkers ( const unsigned int  num)
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.

Implements Nektar::Thread::ThreadManager.

Definition at line 269 of file Thread.cpp.

References ASSERTL0.

270 {
271  ASSERTL0(num==1,
272  "Attempted to SetNumWorkers to != 1 in ThreadDefaultManager");
273 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:188
void Nektar::Thread::ThreadStartupManager::SetNumWorkers ( )
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 279 of file Thread.cpp.

280 {
281  return;
282 }
void Nektar::Thread::ThreadStartupManager::SetSchedType ( SchedType  s)
virtual

Sets the current scheduling algorithm.

See also
SetChunkSize()

Implements Nektar::Thread::ThreadManager.

Definition at line 316 of file Thread.cpp.

References ErrorUtil::efatal, and NEKERROR.

317 {
319  "Attempted to SetSchedType in ThreadDefaultManager");
320 }
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:185
void Nektar::Thread::ThreadStartupManager::Wait ( )
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().

Implements Nektar::Thread::ThreadManager.

Definition at line 297 of file Thread.cpp.

298 {
299  return;
300 }

Member Data Documentation

const std::string Nektar::Thread::ThreadStartupManager::m_type
private

Definition at line 403 of file Thread.h.

Referenced by GetType().