Nektar++
ThreadBoost.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File ThreadBoost.h
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description:
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
35 #ifndef NEKTAR_LIBUTILITIES_THREADBOOST_H_
36 #define NEKTAR_LIBUTILITIES_THREADBOOST_H_
37 
39 #include <queue>
40 #include <vector>
41 #include <map>
42 #include <boost/thread/barrier.hpp>
43 
45 
46 namespace Nektar
47 {
48 namespace Thread
49 {
50 
51 typedef boost::unique_lock<boost::mutex> Lock;
52 
53 class ThreadWorkerBoost;
54 
55 /**
56  * @brief Implementation of ThreadManager using Boost threads.
57  */
59 {
60  /**
61  * So the workers can access the master queue and locks.
62  */
63  friend class ThreadWorkerBoost;
64 
65  public:
66  /// Constructs a ThreadManagerBoost.
67  ThreadManagerBoost(unsigned int numWorkers);
68  /// Shuts down threading.
69  virtual ~ThreadManagerBoost();
70  virtual void QueueJobs(std::vector<ThreadJob*>& joblist);
71  virtual void QueueJob(ThreadJob* job);
72  virtual unsigned int GetNumWorkers();
73  virtual unsigned int GetWorkerNum();
74  virtual void SetNumWorkers(const unsigned int num);
75  virtual void SetNumWorkers();
76  virtual unsigned int GetMaxNumWorkers();
77  virtual void Wait();
78  virtual void SetChunkSize(unsigned int chnk);
79  virtual void SetSchedType(SchedType s);
80  virtual bool InThread();
81  virtual void Hold();
82  virtual const std::string& GetType() const;
83 
84 
85  /// Called by the factory method.
86  static ThreadManagerSharedPtr Create(unsigned int numT)
87  {
88  return std::shared_ptr<ThreadManager>(
89  new ThreadManagerBoost(numT));
90  }
91 
92  private:
95  bool IsWorking();
96  void SetNumWorkersImpl(const unsigned int num);
97 
98  // Member variables
99  const unsigned int m_numThreads;
100  unsigned int m_numWorkers;
101  std::queue<ThreadJob*> m_masterQueue;
102  boost::mutex m_masterQueueMutex;
103  boost::mutex m_masterActiveMutex;
104  boost::condition_variable m_masterQueueCondVar;
105  boost::condition_variable m_masterActiveCondVar;
107  boost::thread** m_threadThreadList;
108  boost::thread::id m_masterThreadId;
111  unsigned int m_chunkSize;
113  boost::barrier* m_barrier;
114  std::map<boost::thread::id, unsigned int> m_threadMap;
115  static std::string className;
116  std::string m_type;
117 };
118 
119 /**
120  * @brief Implementation class for ThreadManagerBoost.
121  *
122  * Each instance of this class corresponds to a worker thread.
123  * Instances manage their own queue of jobs to run, grabbing new
124  * jobs from the master queue when it is exhausted.
125  */
127 {
128  public:
129  /// Constructor
130  ThreadWorkerBoost(ThreadManagerBoost *threadManager,
131  unsigned int workerNum);
132  /// Destructor.
134  /// This provides the interface that boost::thread uses to start the
135  /// worker.
136  void operator()() { MainLoop(); };
137  /**
138  * @brief Return the index of the worker thread.
139  * @return Index of worker thread, an integer between 0 and
140  * (number_of_threads - 1)
141  */
142  unsigned int GetWorkerNum() { return m_threadNum; };
143  /**
144  * @brief A signal to shut down.
145  *
146  * If this method is called the worker will shut down. Used by the
147  * ThreadManagerBoost to stop threading.
148  */
149  void Stop() { m_keepgoing = false;} ;
150 
151  private:
154  void MainLoop();
155  void LoadJobs();
156  unsigned int GetNumToLoad();
157  void WaitForActive();
158  void RunJobs();
159 
160  // Member variables
162  std::queue<ThreadJob *> m_workerQueue;
164  unsigned int m_threadNum;
165 };
166 
167 
168 }
169 }
170 #endif /* THREADBOOST_H_ */
Base class for tasks to be sent to the ThreadManager to run.
Definition: Thread.h:98
Implementation of ThreadManager using Boost threads.
Definition: ThreadBoost.h:59
ThreadWorkerBoost ** m_threadList
Definition: ThreadBoost.h:106
virtual const std::string & GetType() const
Returns a description of the type of threading.
virtual unsigned int GetWorkerNum()
Returns the worker number of the executing thread.
virtual ~ThreadManagerBoost()
Shuts down threading.
virtual void QueueJob(ThreadJob *job)
Pass a single job to the master queue.
std::queue< ThreadJob * > m_masterQueue
Definition: ThreadBoost.h:101
boost::condition_variable m_masterQueueCondVar
Definition: ThreadBoost.h:104
virtual bool InThread()
Indicates whether the code is in a worker thread or not.
boost::thread::id m_masterThreadId
Definition: ThreadBoost.h:108
const unsigned int m_numThreads
Definition: ThreadBoost.h:99
virtual void SetNumWorkers()
Sets the number of active workers to the maximum.
static ThreadManagerSharedPtr Create(unsigned int numT)
Called by the factory method.
Definition: ThreadBoost.h:86
ThreadManagerBoost(const ThreadManagerBoost &)
virtual void Hold()
A calling threads holds until all active threads call this method.
std::map< boost::thread::id, unsigned int > m_threadMap
Definition: ThreadBoost.h:114
virtual unsigned int GetNumWorkers()
Return the number of active workers.
void SetNumWorkersImpl(const unsigned int num)
virtual unsigned int GetMaxNumWorkers()
Gets the maximum available number of threads.
boost::condition_variable m_masterActiveCondVar
Definition: ThreadBoost.h:105
virtual void SetChunkSize(unsigned int chnk)
Controls how many jobs are sent to each worker at a time.
virtual void QueueJobs(std::vector< ThreadJob * > &joblist)
Pass a list of tasklets to the master queue.
virtual void SetSchedType(SchedType s)
Sets the current scheduling algorithm.
virtual void Wait()
Waits until all queued jobs are finished.
The interface class for the controller for worker threads and jobs.
Definition: Thread.h:166
Implementation class for ThreadManagerBoost.
Definition: ThreadBoost.h:127
ThreadWorkerBoost(const ThreadWorkerBoost &)
unsigned int GetWorkerNum()
Return the index of the worker thread.
Definition: ThreadBoost.h:142
void Stop()
A signal to shut down.
Definition: ThreadBoost.h:149
std::queue< ThreadJob * > m_workerQueue
Definition: ThreadBoost.h:162
void operator()()
This provides the interface that boost::thread uses to start the worker.
Definition: ThreadBoost.h:136
ThreadManagerBoost * m_threadManager
Definition: ThreadBoost.h:161
SchedType
Identifies the algorithm for scheduling.
Definition: Thread.h:66
std::shared_ptr< ThreadManager > ThreadManagerSharedPtr
Definition: Thread.h:72
boost::unique_lock< boost::mutex > Lock
Definition: ThreadBoost.h:51
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1