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