Nektar++
Public Member Functions | Private 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:
[legend]

Public Member Functions

 ThreadStartupManager ()
 ThreadDefaultManager. More...
 
 ThreadStartupManager (const ThreadStartupManager &src)=default
 
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 Member Functions

ThreadStartupManageroperator= (const ThreadStartupManager &src)
 ThreadDefaultManager copy constructor. More...
 

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 381 of file Thread.h.

Constructor & Destructor Documentation

◆ ThreadStartupManager() [1/2]

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

ThreadDefaultManager.

Definition at line 208 of file Thread.cpp.

208  : m_type("Threading starting up")
209 {
210  // empty
211 }

◆ ThreadStartupManager() [2/2]

Nektar::Thread::ThreadStartupManager::ThreadStartupManager ( const ThreadStartupManager src)
default

◆ ~ThreadStartupManager()

Nektar::Thread::ThreadStartupManager::~ThreadStartupManager ( )
virtual

Definition at line 217 of file Thread.cpp.

218 {
219  // empty
220 }

Member Function Documentation

◆ GetMaxNumWorkers()

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 285 of file Thread.cpp.

286 {
287  return 1;
288 }

◆ GetNumWorkers()

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 248 of file Thread.cpp.

249 {
250  return 1;
251 }

◆ GetType()

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 352 of file Thread.cpp.

References m_type.

353 {
354  return m_type;
355 }

◆ GetWorkerNum()

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 257 of file Thread.cpp.

258 {
259  return 0;
260 }

◆ Hold()

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 334 of file Thread.cpp.

335 {
336  return;
337 }

◆ InThread()

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 325 of file Thread.cpp.

326 {
327  return false;
328 }

◆ IsInitialised()

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 343 of file Thread.cpp.

344 {
345  return false;
346 }

◆ operator=()

ThreadStartupManager & Nektar::Thread::ThreadStartupManager::operator= ( const ThreadStartupManager src)
private

ThreadDefaultManager copy constructor.

Definition at line 361 of file Thread.cpp.

363 {
364  boost::ignore_unused(src);
365  return *this;
366 }

◆ QueueJob()

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 237 of file Thread.cpp.

References Nektar::ErrorUtil::efatal, and NEKERROR.

238 {
239  boost::ignore_unused(job);
241  "Attempted to QueueJob in ThreadDefaultManager");
242 }
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:209

◆ QueueJobs()

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 226 of file Thread.cpp.

References Nektar::ErrorUtil::efatal, and NEKERROR.

227 {
228  boost::ignore_unused(joblist);
230  "Attempted to QueueJobs in ThreadDefaultManager");
231 }
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:209

◆ SetChunkSize()

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 303 of file Thread.cpp.

References Nektar::ErrorUtil::efatal, and NEKERROR.

304 {
305  boost::ignore_unused(chnk);
307  "Attempted to SetChunkSize in ThreadDefaultManager");
308 }
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:209

◆ SetNumWorkers() [1/2]

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 266 of file Thread.cpp.

References ASSERTL0.

267 {
268  ASSERTL0(num==1,
269  "Attempted to SetNumWorkers to != 1 in ThreadDefaultManager");
270 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216

◆ SetNumWorkers() [2/2]

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 276 of file Thread.cpp.

277 {
278  return;
279 }

◆ SetSchedType()

void Nektar::Thread::ThreadStartupManager::SetSchedType ( SchedType  s)
virtual

Sets the current scheduling algorithm.

See also
SetChunkSize()

Implements Nektar::Thread::ThreadManager.

Definition at line 314 of file Thread.cpp.

References Nektar::ErrorUtil::efatal, and NEKERROR.

315 {
316  boost::ignore_unused(s);
318  "Attempted to SetSchedType in ThreadDefaultManager");
319 }
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:209

◆ Wait()

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 294 of file Thread.cpp.

295 {
296  return;
297 }

Member Data Documentation

◆ m_type

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

Definition at line 405 of file Thread.h.

Referenced by GetType().