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
41namespace Nektar
42{
43namespace Thread
44{
45
46/**
47 *
48 */
50{
51 static ThreadManagerFactory instance;
52 return instance;
53}
54
55/**
56 * @brief ThreadJob implementation
57 */
59{
60 // empty
61}
62
63/**
64 *
65 */
67{
68 // empty
69}
70
71/**
72 * Part of the thread interface. Do not use unless you're
73 * an implementation of ThreadManager.
74 * @warning Do not use: needs to be public so thread implementation can call it.
75 */
76void ThreadJob::SetWorkerNum(unsigned int num)
77{
78 m_workerNum = num;
79}
80
81/**
82 *
83 */
85{
86 return m_workerNum;
87}
88
89/**
90 * Implementations should not override this function, since they
91 * are initialised.
92 *
93 * @returns True if this ThreadManager has been initialised.
94 */
96{
97 return true;
98}
99
100/**
101 * Should stop worker threads and clean up.
102 * This shouldn't be called until the program is exiting
103 * anyway, some implementations may need to unlock threads
104 * to allow clean exit.
105 */
107{
108 // empty
109}
110
111/**
112 * ThreadMaster implementation
113 */
115 : m_threadManagers(THREADMANAGER_MAX), m_mutex(), m_threadingType()
116{
117 // empty
118}
119
120/**
121 * Will clear the list of ThreadManagers, destructing them.
122 */
124{
125 // Locking is a bit pointless, since the map is empty after this call.
126 m_threadManagers.clear();
127}
128
129/**
130 *
131 */
133{
134 static ThreadMaster instance;
135 return instance;
136}
137
138/**
139 * @param p_type String to be passed to the ThreadManagerFactory
140 *
141 * Subsequent CreateInstance calls will pass this string to the
142 * ThreadManagerFactory to create a ThreadManager.
143 *
144 * It is an error to call this more than once (since having different kinds of
145 * ThreadManager active is probably a stupendously bad idea).
146 */
147void ThreadMaster::SetThreadingType(const std::string &p_type)
148{
150 "Tried to SetThreadingType when it was already set");
151 m_threadingType = p_type;
152}
153
154/**
155 * @returns a shared pointer to /em either a ThreadStartupManager
156 * or whatever ThreadManager has been created for the string s
157 * with CreateInstance.
158 *
159 * Calling code may store the result if it is sure the call to
160 * GetInstance(s) has occurred after the call to CreateInstance(s).
161 * This cannot be before threadedcommReader::StartThreads(), as that's
162 * where SetThreadingType is called.
163 *
164 * @warning Static initialisation may want to access a ThreadManager.
165 * Such code must be able to cope with the temporary ThreadStartupManager.
166 */
168{
169 if (!m_threadManagers[t])
170 {
173 return m_threadManagers[t];
174 }
175 return m_threadManagers[t];
176}
177
178/**
179 * @return Shared pointer to the created ThreadManager.
180 *
181 * An error occurs if this is called before SetThreadingType.
182 */
184 unsigned int nThr)
185{
186 ASSERTL0(!m_threadingType.empty(),
187 "Trying to create a ThreadManager before SetThreadingType called");
188 return m_threadManagers[t] =
190 nThr);
191}
192
193/**
194 * @brief ThreadDefaultManager
195 */
196ThreadStartupManager::ThreadStartupManager() : m_type("Threading starting up")
197{
198 // empty
199}
200
201/**
202 *
203 */
205{
206 // empty
207}
208
209/**
210 *
211 */
212void ThreadStartupManager::v_QueueJobs(std::vector<ThreadJob *> &joblist)
213{
214 boost::ignore_unused(joblist);
216 "Attempted to QueueJobs in ThreadDefaultManager");
217}
218
219/**
220 *
221 */
223{
224 boost::ignore_unused(job);
226 "Attempted to QueueJob in ThreadDefaultManager");
227}
228
229/**
230 *
231 */
233{
234 return 1;
235}
236
237/**
238 *
239 */
241{
242 return 0;
243}
244
245/**
246 *
247 */
248void ThreadStartupManager::v_SetNumWorkers(const unsigned int num)
249{
250 ASSERTL0(num == 1,
251 "Attempted to SetNumWorkers to != 1 in ThreadDefaultManager");
252}
253
254/**
255 *
256 */
258{
259 return;
260}
261
262/**
263 *
264 */
266{
267 return 1;
268}
269
270/**
271 *
272 */
274{
275 return;
276}
277
278/**
279 *
280 */
282{
283 boost::ignore_unused(chnk);
285 "Attempted to SetChunkSize in ThreadDefaultManager");
286}
287
288/**
289 *
290 */
292{
293 boost::ignore_unused(s);
295 "Attempted to SetSchedType in ThreadDefaultManager");
296}
297
298/**
299 *
300 */
302{
303 return false;
304}
305
306/**
307 *
308 */
310{
311 return;
312}
313
314/**
315 *
316 */
318{
319 return false;
320}
321
322/**
323 *
324 */
325const std::string &ThreadStartupManager::v_GetType() const
326{
327 return m_type;
328}
329
330/**
331 * @brief ThreadDefaultManager copy constructor
332 */
334 const ThreadStartupManager &src)
335{
336 boost::ignore_unused(src);
337 return *this;
338}
339
340} // namespace Thread
341} /* namespace Nektar */
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
#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:144
Base class for tasks to be sent to the ThreadManager to run.
Definition: Thread.h:97
virtual ~ThreadJob()
Base destructor.
Definition: Thread.cpp:66
void SetWorkerNum(unsigned int num)
Set number of worker threads.
Definition: Thread.cpp:76
ThreadJob()
Base constructor.
Definition: Thread.cpp:58
unsigned int m_workerNum
Definition: Thread.h:123
unsigned int GetWorkerNum()
Definition: Thread.cpp:84
virtual ~ThreadManager()
Destructor.
Definition: Thread.cpp:106
virtual bool v_IsInitialised()
Definition: Thread.cpp:95
~ThreadMaster()
Destructor.
Definition: Thread.cpp:123
void SetThreadingType(const std::string &p_type)
Sets what ThreadManagers will be created in CreateInstance.
Definition: Thread.cpp:147
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:183
ThreadManagerSharedPtr & GetInstance(const ThreadManagerName t)
Gets the ThreadManager associated with string s.
Definition: Thread.cpp:167
ThreadMaster()
Constructor.
Definition: Thread.cpp:114
std::vector< ThreadManagerSharedPtr > m_threadManagers
Definition: Thread.h:402
std::string m_threadingType
Definition: Thread.h:404
A default ThreadManager.
Definition: Thread.h:439
virtual bool v_IsInitialised() override
Definition: Thread.cpp:317
virtual unsigned int v_GetWorkerNum() override
Definition: Thread.cpp:240
ThreadStartupManager & operator=(const ThreadStartupManager &src)
ThreadDefaultManager copy constructor.
Definition: Thread.cpp:333
virtual unsigned int v_GetMaxNumWorkers() override
Definition: Thread.cpp:265
ThreadStartupManager()
ThreadDefaultManager.
Definition: Thread.cpp:196
virtual void v_SetSchedType(SchedType s) override
Definition: Thread.cpp:291
virtual void v_SetNumWorkers() override
Definition: Thread.cpp:257
virtual void v_QueueJob(ThreadJob *job) override
Definition: Thread.cpp:222
virtual bool v_InThread() override
Definition: Thread.cpp:301
virtual void v_QueueJobs(std::vector< ThreadJob * > &joblist) override
Definition: Thread.cpp:212
virtual unsigned int v_GetNumWorkers() override
Definition: Thread.cpp:232
virtual void v_Hold() override
Definition: Thread.cpp:309
virtual void v_SetChunkSize(unsigned int chnk) override
Definition: Thread.cpp:281
virtual void v_Wait() override
Definition: Thread.cpp:273
virtual const std::string & v_GetType() const override
Definition: Thread.cpp:325
SchedType
Identifies the algorithm for scheduling.
Definition: Thread.h:66
ThreadManagerFactory & GetThreadManagerFactory()
Definition: Thread.cpp:49
std::shared_ptr< ThreadManager > ThreadManagerSharedPtr
Definition: Thread.h:72
ThreadMaster & GetThreadMaster()
Definition: Thread.cpp:132
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2