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  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  /// Called by the factory method.
85  static ThreadManagerSharedPtr Create(unsigned int numT)
86  {
87  return std::shared_ptr<ThreadManager>(new ThreadManagerBoost(numT));
88  }
89 
90 private:
93  bool IsWorking();
94  void SetNumWorkersImpl(const unsigned int num);
95 
96  // Member variables
97  const unsigned int m_numThreads;
98  unsigned int m_numWorkers;
99  std::queue<ThreadJob *> m_masterQueue;
100  boost::mutex m_masterQueueMutex;
101  boost::mutex m_masterActiveMutex;
102  boost::condition_variable m_masterQueueCondVar;
103  boost::condition_variable m_masterActiveCondVar;
105  boost::thread **m_threadThreadList;
106  boost::thread::id m_masterThreadId;
109  unsigned int m_chunkSize;
111  boost::barrier *m_barrier;
112  std::map<boost::thread::id, unsigned int> m_threadMap;
113  static std::string className;
114  std::string m_type;
115 };
116 
117 /**
118  * @brief Implementation class for ThreadManagerBoost.
119  *
120  * Each instance of this class corresponds to a worker thread.
121  * Instances manage their own queue of jobs to run, grabbing new
122  * jobs from the master queue when it is exhausted.
123  */
125 {
126 public:
127  /// Constructor
128  ThreadWorkerBoost(ThreadManagerBoost *threadManager,
129  unsigned int workerNum);
130  /// Destructor.
132  /// This provides the interface that boost::thread uses to start the
133  /// worker.
134  void operator()()
135  {
136  MainLoop();
137  };
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()
144  {
145  return m_threadNum;
146  };
147  /**
148  * @brief A signal to shut down.
149  *
150  * If this method is called the worker will shut down. Used by the
151  * ThreadManagerBoost to stop threading.
152  */
153  void Stop()
154  {
155  m_keepgoing = false;
156  };
157 
158 private:
161  void MainLoop();
162  void LoadJobs();
163  unsigned int GetNumToLoad();
164  void WaitForActive();
165  void RunJobs();
166 
167  // Member variables
169  std::queue<ThreadJob *> m_workerQueue;
171  unsigned int m_threadNum;
172 };
173 
174 } // namespace Thread
175 } // namespace Nektar
176 #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
ThreadWorkerBoost ** m_threadList
Definition: ThreadBoost.h:104
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.
boost::condition_variable m_masterQueueCondVar
Definition: ThreadBoost.h:102
virtual bool InThread()
Indicates whether the code is in a worker thread or not.
boost::thread::id m_masterThreadId
Definition: ThreadBoost.h:106
const unsigned int m_numThreads
Definition: ThreadBoost.h:97
virtual void SetNumWorkers()
Sets the number of active workers to the maximum.
std::queue< ThreadJob * > m_masterQueue
Definition: ThreadBoost.h:99
static ThreadManagerSharedPtr Create(unsigned int numT)
Called by the factory method.
Definition: ThreadBoost.h:85
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:112
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:103
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:163
Implementation class for ThreadManagerBoost.
Definition: ThreadBoost.h:125
ThreadWorkerBoost(const ThreadWorkerBoost &)
unsigned int GetWorkerNum()
Return the index of the worker thread.
Definition: ThreadBoost.h:143
void Stop()
A signal to shut down.
Definition: ThreadBoost.h:153
std::queue< ThreadJob * > m_workerQueue
Definition: ThreadBoost.h:169
void operator()()
This provides the interface that boost::thread uses to start the worker.
Definition: ThreadBoost.h:134
ThreadManagerBoost * m_threadManager
Definition: ThreadBoost.h:168
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:1