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