Nektar++
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Nektar::Thread::ThreadWorkerBoost Class Reference

Implementation class for ThreadManagerBoost. More...

#include <ThreadBoost.h>

Public Member Functions

 ThreadWorkerBoost (ThreadManagerBoost *threadManager, unsigned int workerNum)
 Constructor. More...
 
 ~ThreadWorkerBoost ()
 Destructor. More...
 
void operator() ()
 This provides the interface that boost::thread uses to start the worker. More...
 
unsigned int GetWorkerNum ()
 Return the index of the worker thread. More...
 
void Stop ()
 A signal to shut down. More...
 

Private Member Functions

 ThreadWorkerBoost ()
 
 ThreadWorkerBoost (const ThreadWorkerBoost &)
 
void MainLoop ()
 
void LoadJobs ()
 
unsigned int GetNumToLoad ()
 
void WaitForActive ()
 
void RunJobs ()
 

Private Attributes

ThreadManagerBoostm_threadManager
 
std::queue< ThreadJob * > m_workerQueue
 
bool m_keepgoing
 
unsigned int m_threadNum
 

Detailed Description

Implementation class for ThreadManagerBoost.

Each instance of this class corresponds to a worker thread. Instances manage their own queue of jobs to run, grabbing new jobs from the master queue when it is exhausted.

Definition at line 126 of file ThreadBoost.h.

Constructor & Destructor Documentation

◆ ThreadWorkerBoost() [1/3]

Nektar::Thread::ThreadWorkerBoost::ThreadWorkerBoost ( ThreadManagerBoost tm,
unsigned int  workerNum 
)

Constructor.

Parameters
threadManagerPointer to the ThreadManagerBoost that is controlling this worker.
workerNumUnique number from 0..(number_of_threads - 1)

Called by the ThreadManagerBoost instance.

Definition at line 330 of file ThreadBoost.cpp.

331  :
333  m_keepgoing(true), m_threadNum(workerNum)
334 {
335  // Nothing to see here
336 
337 }
ThreadManagerBoost * m_threadManager
Definition: ThreadBoost.h:161
std::queue< ThreadJob * > m_workerQueue
Definition: ThreadBoost.h:162

◆ ~ThreadWorkerBoost()

Nektar::Thread::ThreadWorkerBoost::~ThreadWorkerBoost ( )

Destructor.

Winds up this thread's execution. Jobs in its queue are lost.

Definition at line 343 of file ThreadBoost.cpp.

References m_keepgoing, and m_threadNum.

344 {
345  if (m_keepgoing)
346  {
347  std::cerr << "Warning: ThreadWorker: " << m_threadNum
348  << "destroyed while running!" << std::endl;
349  }
350  // on destuction the m_workerQueue will be destructed and that
351  // will destruct any ThreadJobs still in there.
352 }

◆ ThreadWorkerBoost() [2/3]

Nektar::Thread::ThreadWorkerBoost::ThreadWorkerBoost ( )
private

◆ ThreadWorkerBoost() [3/3]

Nektar::Thread::ThreadWorkerBoost::ThreadWorkerBoost ( const ThreadWorkerBoost )
private

Member Function Documentation

◆ GetNumToLoad()

unsigned int Nektar::Thread::ThreadWorkerBoost::GetNumToLoad ( )
private

Definition at line 394 of file ThreadBoost.cpp.

References Nektar::Thread::e_dynamic, Nektar::Thread::e_guided, Nektar::ErrorUtil::efatal, Nektar::Thread::ThreadManagerBoost::m_chunkSize, Nektar::Thread::ThreadManagerBoost::m_masterQueue, Nektar::Thread::ThreadManagerBoost::m_numWorkers, Nektar::Thread::ThreadManagerBoost::m_schedType, m_threadManager, and NEKERROR.

Referenced by LoadJobs().

395 {
396  unsigned int numToLoad = 0;
397  switch (m_threadManager->m_schedType)
398  {
399  case e_guided:
400  numToLoad = std::max(
401  static_cast<unsigned long>(m_threadManager->m_chunkSize),
402  static_cast<unsigned long>(
404  / (2*m_threadManager->m_numWorkers +1)));
405  break;
406 
407  case e_dynamic:
408  numToLoad = m_threadManager->m_chunkSize;
409  break;
410 
411  default:
412  NEKERROR(ErrorUtil::efatal, "Invalid value for SchedType.");
413  break;
414  }
415  return numToLoad;
416 }
ThreadManagerBoost * m_threadManager
Definition: ThreadBoost.h:161
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:209
std::queue< ThreadJob * > m_masterQueue
Definition: ThreadBoost.h:101

◆ GetWorkerNum()

unsigned int Nektar::Thread::ThreadWorkerBoost::GetWorkerNum ( )
inline

Return the index of the worker thread.

Returns
Index of worker thread, an integer between 0 and (number_of_threads - 1)

Definition at line 142 of file ThreadBoost.h.

142 { return m_threadNum; };

◆ LoadJobs()

void Nektar::Thread::ThreadWorkerBoost::LoadJobs ( )
private

Definition at line 358 of file ThreadBoost.cpp.

References GetNumToLoad(), m_keepgoing, Nektar::Thread::ThreadManagerBoost::m_masterActiveMutex, Nektar::Thread::ThreadManagerBoost::m_masterQueue, Nektar::Thread::ThreadManagerBoost::m_masterQueueCondVar, Nektar::Thread::ThreadManagerBoost::m_masterQueueMutex, Nektar::Thread::ThreadManagerBoost::m_threadActiveList, Nektar::Thread::ThreadManagerBoost::m_threadBusyList, m_threadManager, m_threadNum, and m_workerQueue.

Referenced by MainLoop().

359 {
360  // Lock the master queue
361  Lock masterQueueLock(m_threadManager->m_masterQueueMutex);
364  while (m_threadManager->m_masterQueue.empty()
365  && m_keepgoing)
366  {
367  // while waiting, master queue is unlocked
368  m_threadManager->m_masterQueueCondVar.wait(masterQueueLock);
369  // on exiting wait master queue is locked again
370  }
371  bool active;
372  {
373  Lock masterActiveLock(m_threadManager->m_masterActiveMutex);
375  }
376  if (active && m_keepgoing)
377  {
378  unsigned int numToLoad = GetNumToLoad();
379  while (m_workerQueue.size() < numToLoad
380  && !m_threadManager->m_masterQueue.empty())
381  {
382  ThreadJob *tj = m_threadManager->m_masterQueue.front();
383  m_workerQueue.push(tj);
385  }
386  }
388 } // lock on master queue released here
ThreadManagerBoost * m_threadManager
Definition: ThreadBoost.h:161
std::queue< ThreadJob * > m_workerQueue
Definition: ThreadBoost.h:162
boost::condition_variable m_masterQueueCondVar
Definition: ThreadBoost.h:104
std::queue< ThreadJob * > m_masterQueue
Definition: ThreadBoost.h:101
boost::unique_lock< boost::mutex > Lock
Definition: ThreadBoost.h:51

◆ MainLoop()

void Nektar::Thread::ThreadWorkerBoost::MainLoop ( )
private

Definition at line 439 of file ThreadBoost.cpp.

References LoadJobs(), m_keepgoing, RunJobs(), and WaitForActive().

440 {
441  while (m_keepgoing)
442  {
443  WaitForActive();
444  LoadJobs();
445  RunJobs();
446  }
447 } // exiting here should terminate the thread

◆ operator()()

void Nektar::Thread::ThreadWorkerBoost::operator() ( )
inline

This provides the interface that boost::thread uses to start the worker.

Definition at line 136 of file ThreadBoost.h.

◆ RunJobs()

void Nektar::Thread::ThreadWorkerBoost::RunJobs ( )
private

Definition at line 453 of file ThreadBoost.cpp.

References m_keepgoing, m_threadNum, m_workerQueue, Nektar::Thread::ThreadJob::Run(), and Nektar::Thread::ThreadJob::SetWorkerNum().

Referenced by MainLoop().

454 {
455  while (!m_workerQueue.empty() && m_keepgoing)
456  {
457  ThreadJob * tj;
458  try
459  {
460  tj = m_workerQueue.front();
461  tj->SetWorkerNum(m_threadNum);
462  tj->Run();
463  m_workerQueue.pop();
464  delete tj;
465  } catch(...)
466  {
467  // something bad happened, probably time to die
468  // maybe signal ThreadManager
469  throw;
470  }
471  }
472 }
std::queue< ThreadJob * > m_workerQueue
Definition: ThreadBoost.h:162

◆ Stop()

void Nektar::Thread::ThreadWorkerBoost::Stop ( )
inline

A signal to shut down.

If this method is called the worker will shut down. Used by the ThreadManagerBoost to stop threading.

Definition at line 149 of file ThreadBoost.h.

References Nektar::Thread::ThreadManagerBoost::ThreadWorkerBoost.

Referenced by Nektar::Thread::ThreadManagerBoost::~ThreadManagerBoost().

◆ WaitForActive()

void Nektar::Thread::ThreadWorkerBoost::WaitForActive ( )
private

Definition at line 422 of file ThreadBoost.cpp.

References m_keepgoing, Nektar::Thread::ThreadManagerBoost::m_masterActiveCondVar, Nektar::Thread::ThreadManagerBoost::m_masterActiveMutex, Nektar::Thread::ThreadManagerBoost::m_threadActiveList, m_threadManager, and m_threadNum.

Referenced by MainLoop().

423 {
424  Lock masterActiveLock(m_threadManager->m_masterActiveMutex);
425 
427  && m_keepgoing)
428  {
429  // while waiting, master active is unlocked
430  m_threadManager->m_masterActiveCondVar.wait(masterActiveLock);
431  // on exiting wait master active is locked again
432  }
433 }
ThreadManagerBoost * m_threadManager
Definition: ThreadBoost.h:161
boost::condition_variable m_masterActiveCondVar
Definition: ThreadBoost.h:105
boost::unique_lock< boost::mutex > Lock
Definition: ThreadBoost.h:51

Member Data Documentation

◆ m_keepgoing

bool Nektar::Thread::ThreadWorkerBoost::m_keepgoing
private

Definition at line 163 of file ThreadBoost.h.

Referenced by LoadJobs(), MainLoop(), RunJobs(), WaitForActive(), and ~ThreadWorkerBoost().

◆ m_threadManager

ThreadManagerBoost* Nektar::Thread::ThreadWorkerBoost::m_threadManager
private

Definition at line 161 of file ThreadBoost.h.

Referenced by GetNumToLoad(), LoadJobs(), and WaitForActive().

◆ m_threadNum

unsigned int Nektar::Thread::ThreadWorkerBoost::m_threadNum
private

Definition at line 164 of file ThreadBoost.h.

Referenced by LoadJobs(), RunJobs(), WaitForActive(), and ~ThreadWorkerBoost().

◆ m_workerQueue

std::queue<ThreadJob *> Nektar::Thread::ThreadWorkerBoost::m_workerQueue
private

Definition at line 162 of file ThreadBoost.h.

Referenced by LoadJobs(), and RunJobs().