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