Nektar++
Thread.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File Thread.cpp
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: Thread manager
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
35 #include <iostream>
36 
37 #include <boost/core/ignore_unused.hpp>
38 
40 
41 namespace Nektar
42 {
43 namespace Thread
44 {
45 
46 
47 /**
48  *
49  */
51 {
52  static ThreadManagerFactory instance;
53  return instance;
54 }
55 
56 /**
57  * @brief ThreadJob implementation
58  */
60 {
61  // empty
62 }
63 
64 
65 /**
66  *
67  */
69 {
70  // empty
71 }
72 
73 
74 /**
75  * Part of the thread interface. Do not use unless you're
76  * an implementation of ThreadManager.
77  * @warning Do not use: needs to be public so thread implementation can call it.
78  */
79 void ThreadJob::SetWorkerNum(unsigned int num)
80 {
81  m_workerNum = num;
82 }
83 
84 
85 /**
86  *
87  */
89 {
90  return m_workerNum;
91 }
92 
93 
94 /**
95  * Implementations should not override this function, since they
96  * are initialised.
97  *
98  * @returns True if this ThreadManager has been initialised.
99  */
101 {
102  return true;
103 }
104 
105 
106 /**
107  * Should stop worker threads and clean up.
108  * This shouldn't be called until the program is exiting
109  * anyway, some implementations may need to unlock threads
110  * to allow clean exit.
111  */
113 {
114  // empty
115 }
116 
117 
118 /**
119  * ThreadMaster implementation
120  */
121 ThreadMaster::ThreadMaster() : m_threadManagers(THREADMANAGER_MAX),
122  m_mutex(), m_threadingType()
123 {
124  // empty
125 }
126 
127 
128 /**
129  * Will clear the list of ThreadManagers, destructing them.
130  */
132 {
133  // Locking is a bit pointless, since the map is empty after this call.
134  m_threadManagers.clear();
135 }
136 
137 
138 /**
139  *
140  */
142 {
143  static ThreadMaster instance;
144  return instance;
145 }
146 
147 
148 /**
149  * @param p_type String to be passed to the ThreadManagerFactory
150  *
151  * Subsequent CreateInstance calls will pass this string to the
152  * ThreadManagerFactory to create a ThreadManager.
153  *
154  * It is an error to call this more than once (since having different kinds of
155  * ThreadManager active is probably a stupendously bad idea).
156  */
157 void ThreadMaster::SetThreadingType(const std::string &p_type)
158 {
159  ASSERTL0(m_threadingType.empty(),
160  "Tried to SetThreadingType when it was already set");
161  m_threadingType = p_type;
162 }
163 
164 
165 /**
166  * @returns a shared pointer to /em either a ThreadStartupManager
167  * or whatever ThreadManager has been created for the string s
168  * with CreateInstance.
169  *
170  * Calling code may store the result if it is sure the call to
171  * GetInstance(s) has occurred after the call to CreateInstance(s).
172  * This cannot be before threadedcommReader::StartThreads(), as that's
173  * where SetThreadingType is called.
174  *
175  * @warning Static initialisation may want to access a ThreadManager.
176  * Such code must be able to cope with the temporary ThreadStartupManager.
177  */
179 {
180  if ( !m_threadManagers[t] )
181  {
183  new ThreadStartupManager());
184  return m_threadManagers[t];
185  }
186  return m_threadManagers[t];
187 }
188 
189 
190 /**
191  * @return Shared pointer to the created ThreadManager.
192  *
193  * An error occurs if this is called before SetThreadingType.
194  */
196  unsigned int nThr)
197 {
198  ASSERTL0(!m_threadingType.empty(),
199  "Trying to create a ThreadManager before SetThreadingType called");
200  return m_threadManagers[t] =
202 }
203 
204 
205 /**
206  * @brief ThreadDefaultManager
207  */
208 ThreadStartupManager::ThreadStartupManager() : m_type("Threading starting up")
209 {
210  // empty
211 }
212 
213 
214 /**
215  *
216  */
218 {
219  // empty
220 }
221 
222 
223 /**
224  *
225  */
226 void ThreadStartupManager::QueueJobs(std::vector<ThreadJob*>& joblist)
227 {
228  boost::ignore_unused(joblist);
230  "Attempted to QueueJobs in ThreadDefaultManager");
231 }
232 
233 
234 /**
235  *
236  */
238 {
239  boost::ignore_unused(job);
241  "Attempted to QueueJob in ThreadDefaultManager");
242 }
243 
244 
245 /**
246  *
247  */
249 {
250  return 1;
251 }
252 
253 
254 /**
255  *
256  */
258 {
259  return 0;
260 }
261 
262 
263 /**
264  *
265  */
266 void ThreadStartupManager::SetNumWorkers(const unsigned int num)
267 {
268  ASSERTL0(num==1,
269  "Attempted to SetNumWorkers to != 1 in ThreadDefaultManager");
270 }
271 
272 
273 /**
274  *
275  */
277 {
278  return;
279 }
280 
281 
282 /**
283  *
284  */
286 {
287  return 1;
288 }
289 
290 
291 /**
292  *
293  */
295 {
296  return;
297 }
298 
299 
300 /**
301  *
302  */
303 void ThreadStartupManager::SetChunkSize(unsigned int chnk)
304 {
305  boost::ignore_unused(chnk);
307  "Attempted to SetChunkSize in ThreadDefaultManager");
308 }
309 
310 
311 /**
312  *
313  */
315 {
316  boost::ignore_unused(s);
318  "Attempted to SetSchedType in ThreadDefaultManager");
319 }
320 
321 
322 /**
323  *
324  */
326 {
327  return false;
328 }
329 
330 
331 /**
332  *
333  */
335 {
336  return;
337 }
338 
339 
340 /**
341  *
342  */
344 {
345  return false;
346 }
347 
348 
349 /**
350  *
351  */
352 const std::string& ThreadStartupManager::GetType() const
353 {
354  return m_type;
355 }
356 
357 
358 /**
359  * @brief ThreadDefaultManager copy constructor
360  */
362  const ThreadStartupManager& src)
363 {
364  boost::ignore_unused(src);
365  return *this;
366 }
367 
368 } // Thread
369 } /* namespace Nektar */
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:209
Provides a generic Factory class.
Definition: NekFactory.hpp:105
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
Definition: NekFactory.hpp:145
Base class for tasks to be sent to the ThreadManager to run.
Definition: Thread.h:98
virtual ~ThreadJob()
Base destructor.
Definition: Thread.cpp:68
void SetWorkerNum(unsigned int num)
Set number of worker threads.
Definition: Thread.cpp:79
ThreadJob()
Base constructor.
Definition: Thread.cpp:59
unsigned int m_workerNum
Definition: Thread.h:124
unsigned int GetWorkerNum()
Definition: Thread.cpp:88
virtual ~ThreadManager()
Destructor.
Definition: Thread.cpp:112
virtual bool IsInitialised()
ThreadManager implementation.
Definition: Thread.cpp:100
~ThreadMaster()
Destructor.
Definition: Thread.cpp:131
void SetThreadingType(const std::string &p_type)
Sets what ThreadManagers will be created in CreateInstance.
Definition: Thread.cpp:157
ThreadManagerSharedPtr CreateInstance(const ThreadManagerName t, unsigned int nThr)
Creates an instance of a ThreadManager (which one is determined by a previous call to SetThreadingTyp...
Definition: Thread.cpp:195
ThreadManagerSharedPtr & GetInstance(const ThreadManagerName t)
Gets the ThreadManager associated with string s.
Definition: Thread.cpp:178
ThreadMaster()
Constructor.
Definition: Thread.cpp:121
std::vector< ThreadManagerSharedPtr > m_threadManagers
Definition: Thread.h:345
std::string m_threadingType
Definition: Thread.h:347
A default ThreadManager.
Definition: Thread.h:382
virtual bool IsInitialised()
ThreadManager implementation.
Definition: Thread.cpp:343
virtual void SetChunkSize(unsigned int chnk)
Controls how many jobs are sent to each worker at a time.
Definition: Thread.cpp:303
virtual void Wait()
Waits until all queued jobs are finished.
Definition: Thread.cpp:294
ThreadStartupManager & operator=(const ThreadStartupManager &src)
ThreadDefaultManager copy constructor.
Definition: Thread.cpp:361
virtual unsigned int GetNumWorkers()
Return the number of active workers.
Definition: Thread.cpp:248
virtual unsigned int GetMaxNumWorkers()
Gets the maximum available number of threads.
Definition: Thread.cpp:285
ThreadStartupManager()
ThreadDefaultManager.
Definition: Thread.cpp:208
virtual void SetNumWorkers()
Sets the number of active workers to the maximum.
Definition: Thread.cpp:276
virtual bool InThread()
Indicates whether the code is in a worker thread or not.
Definition: Thread.cpp:325
virtual const std::string & GetType() const
Returns a description of the type of threading.
Definition: Thread.cpp:352
virtual void QueueJobs(std::vector< ThreadJob * > &joblist)
Pass a list of tasklets to the master queue.
Definition: Thread.cpp:226
virtual void SetSchedType(SchedType s)
Sets the current scheduling algorithm.
Definition: Thread.cpp:314
virtual void QueueJob(ThreadJob *job)
Pass a single job to the master queue.
Definition: Thread.cpp:237
virtual unsigned int GetWorkerNum()
Returns the worker number of the executing thread.
Definition: Thread.cpp:257
virtual void Hold()
A calling threads holds until all active threads call this method.
Definition: Thread.cpp:334
SchedType
Identifies the algorithm for scheduling.
Definition: Thread.h:66
ThreadManagerFactory & GetThreadManagerFactory()
Definition: Thread.cpp:50
std::shared_ptr< ThreadManager > ThreadManagerSharedPtr
Definition: Thread.h:72
ThreadMaster & GetThreadMaster()
Definition: Thread.cpp:141
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1