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 <boost/thread/barrier.hpp>
40 #include <map>
41 #include <queue>
42 #include <vector>
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 
71  /// Called by the factory method.
72  static ThreadManagerSharedPtr Create(unsigned int numT)
73  {
74  return std::shared_ptr<ThreadManager>(new ThreadManagerBoost(numT));
75  }
76 
77 protected:
78  virtual void v_QueueJobs(std::vector<ThreadJob *> &joblist) override;
79  virtual void v_QueueJob(ThreadJob *job) override;
80  virtual unsigned int v_GetNumWorkers() override;
81  virtual unsigned int v_GetWorkerNum() override;
82  virtual void v_SetNumWorkers(const unsigned int num) override;
83  virtual void v_SetNumWorkers() override;
84  virtual unsigned int v_GetMaxNumWorkers() override;
85  virtual void v_Wait() override;
86  virtual void v_SetChunkSize(unsigned int chnk) override;
87  virtual void v_SetSchedType(SchedType s) override;
88  virtual bool v_InThread() override;
89  virtual void v_Hold() override;
90  virtual const std::string &v_GetType() const override;
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()()
137  {
138  MainLoop();
139  };
140  /**
141  * @brief Return the index of the worker thread.
142  * @return Index of worker thread, an integer between 0 and
143  * (number_of_threads - 1)
144  */
145  unsigned int GetWorkerNum()
146  {
147  return m_threadNum;
148  };
149  /**
150  * @brief A signal to shut down.
151  *
152  * If this method is called the worker will shut down. Used by the
153  * ThreadManagerBoost to stop threading.
154  */
155  void Stop()
156  {
157  m_keepgoing = false;
158  };
159 
160 private:
163  void MainLoop();
164  void LoadJobs();
165  unsigned int GetNumToLoad();
166  void WaitForActive();
167  void RunJobs();
168 
169  // Member variables
171  std::queue<ThreadJob *> m_workerQueue;
173  unsigned int m_threadNum;
174 };
175 
176 } // namespace Thread
177 } // namespace Nektar
178 #endif /* THREADBOOST_H_ */
Base class for tasks to be sent to the ThreadManager to run.
Definition: Thread.h:97
Implementation of ThreadManager using Boost threads.
Definition: ThreadBoost.h:59
virtual void v_QueueJob(ThreadJob *job) override
ThreadWorkerBoost ** m_threadList
Definition: ThreadBoost.h:106
virtual const std::string & v_GetType() const override
virtual ~ThreadManagerBoost()
Shuts down threading.
virtual unsigned int v_GetMaxNumWorkers() override
virtual void v_SetChunkSize(unsigned int chnk) override
virtual void v_SetSchedType(SchedType s) override
boost::condition_variable m_masterQueueCondVar
Definition: ThreadBoost.h:104
virtual bool v_InThread() override
virtual unsigned int v_GetNumWorkers() override
boost::thread::id m_masterThreadId
Definition: ThreadBoost.h:108
const unsigned int m_numThreads
Definition: ThreadBoost.h:99
std::queue< ThreadJob * > m_masterQueue
Definition: ThreadBoost.h:101
static ThreadManagerSharedPtr Create(unsigned int numT)
Called by the factory method.
Definition: ThreadBoost.h:72
ThreadManagerBoost(const ThreadManagerBoost &)
virtual void v_Wait() override
std::map< boost::thread::id, unsigned int > m_threadMap
Definition: ThreadBoost.h:114
virtual unsigned int v_GetWorkerNum() override
virtual void v_Hold() override
void SetNumWorkersImpl(const unsigned int num)
virtual void v_SetNumWorkers() override
virtual void v_QueueJobs(std::vector< ThreadJob * > &joblist) override
boost::condition_variable m_masterActiveCondVar
Definition: ThreadBoost.h:105
The interface class for the controller for worker threads and jobs.
Definition: Thread.h:163
Implementation class for ThreadManagerBoost.
Definition: ThreadBoost.h:127
ThreadWorkerBoost(const ThreadWorkerBoost &)
unsigned int GetWorkerNum()
Return the index of the worker thread.
Definition: ThreadBoost.h:145
void Stop()
A signal to shut down.
Definition: ThreadBoost.h:155
std::queue< ThreadJob * > m_workerQueue
Definition: ThreadBoost.h:171
void operator()()
This provides the interface that boost::thread uses to start the worker.
Definition: ThreadBoost.h:136
ThreadManagerBoost * m_threadManager
Definition: ThreadBoost.h:170
SchedType
Identifies the algorithm for scheduling.
Definition: Thread.h:66
std::shared_ptr< ThreadManager > ThreadManagerSharedPtr
Definition: Thread.h:71
boost::unique_lock< boost::mutex > Lock
Definition: ThreadBoost.h:51
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2