GemaCoreLib
The GeMA Core library
Loading...
Searching...
No Matches
gmThreadManager.h
Go to the documentation of this file.
1/************************************************************************
2**
3** Copyright (C) 2014 by Carlos Augusto Teixera Mendes
4** All rights reserved.
5**
6** This file is part of the "GeMA" software. It's use should respect
7** the terms in the license agreement that can be found together
8** with this source code.
9** It is provided AS IS, with NO WARRANTY OF ANY KIND,
10** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
11** A PARTICULAR PURPOSE.
12**
13************************************************************************/
14
24#ifndef _GEMA_THREAD_MANAGER_H_
25#define _GEMA_THREAD_MANAGER_H_
26
27#include "gmCoreConfig.h"
28#include "gmLog.h"
29
30#include <assert.h>
31
32#include <QWaitCondition>
33#include <QMutex>
34#include <QQueue>
35
36class QThread;
38class GmThread;
40class LuaEnv;
41
42struct QtHardwareInfo;
43
44template <class T, bool Align> class GmTLS; // We can not include gmThreadLocalStorage.h without creating a circular reference
45
52#ifdef Q_OS_WIN
53#define GmThreadLocal __declspec(thread)
54#else
55#define GmThreadLocal __thread
56#endif
57
58// Global variable for thread id optimized management. Defined in the .cpp file.
60
68
71{
72public:
74 GmThreadTask() : _taskIndex(-1) {}
75
77 virtual ~GmThreadTask() {}
78
82 virtual GmThreadTaskResult run(const GmThread* thread) = 0;
83
85 virtual QString taskDescription() { return ""; }
86
88 int taskIndex() const { return _taskIndex; }
89
91 void setTaskIndex(int index) { _taskIndex = index; }
92
93private:
94 int _taskIndex;
95};
96
97
100{
101public:
104
105 void setWorkerThreadLuaEnv(LuaEnv* mainEnv, const QList<LuaEnv*>& envList);
106
107 LuaEnv* threadLuaEnv(int tid) const;
108
109 void addTask(GmThreadTask* task, int tid = 0);
110 GmThreadTaskResult runTasks (int nthreads);
111 void execTasks(int nthreads);
112
124 const QVector<int>& taskAffinity() const { return _taskAffinity; }
125
126 int numTasks() const;
127
129 bool cancelRequested() const { return _cancelRequested; }
130
132 bool cancelPending() const { return _cancelRequested || _abortRequested; }
133
134
149 void setCancelFlag(bool mode = true) { _cancelRequested = mode; }
150
152 const GmLogCategory& logger() { return _logger; }
153
158 static int maxWorkerThreads() { assert(_maxNumThreads >= 0); return _maxNumThreads; }
159
161 static int maxProcThreads() { assert(_maxProcThreads > 0); return _maxProcThreads; }
162
164 static int numProcCores() { assert(_numProcCores > 0); return _numProcCores; }
165
171 static int currentId() { return GmThreadManagerThreadId; }
172
174 static bool inMainThread() { return currentId() == 0; }
175
176private:
177 friend class GmThread;
178
181 {
182 BelowNormal,
183 Normal,
184 AboveNormal,
185 High,
186 };
187
190 {
191 NoAffinity,
192 QoSAffinity,
193 SoftAffinity,
194 HardAffinity,
195 };
196
213
214 void parseConfigOptions(GmSimulationData* simData);
215 void initProcAffinity();
216
217#ifdef Q_OS_WIN
218 void winInitProcOrThreadSoftAffinity(bool thread, const QVector<quint64>& maskVector);
219#endif
220
221 int clearTaskQueues();
222 int numTasksInQueues() const;
223
224#ifdef ENABLE_TESTS
225 // Allows test function to have access to the ThreadManager logger
226 friend GMC_API_EXPORT void TEST_GmThreadManager(const GmLogCategory& logger, GmLogLevel level);
227#endif
228
230
231 static int _maxNumThreads;
232 static int _maxProcThreads;
233 static int _numProcCores;
234
235 QtHardwareInfo* _hwInfo;
237
245
249
252
254
256};
257
258
268{
269public:
270 GmThread(GmThreadManager* manager, int tid);
271 GmThread(GmThreadManager* manager);
272
273 ~GmThread();
274
276 int id() const { return _tid; }
277
279 GmThreadManager* threadManager() const { return _tm; }
280
282 QThread* thread() const { return _thread; }
283
285 bool cancelPending() const { return _tm->cancelPending(); }
286
288 inline LuaEnv* luaEnv() const { return _tm->threadLuaEnv(_tid); }
289
290private:
291 friend class GmThreadManager;
292 friend class GmInternalThread;
293
294 void start();
295 bool wait(unsigned long time = ULONG_MAX);
296 void run();
297
298 void initThreadAffinity();
299
300 int _tid;
304};
305
306
307#endif
The internal thread class, inheriting from QThread, used for calling the GmThread::run() method.
Definition gmThreadManager.cpp:59
Class representing a category with multiple logging levels.
Definition gmLog.h:58
Auxiliar class used to store the complete set of simulation data.
Definition gmSimulationData.h:55
A class that works together with GmThreadManager to provide thread local storage.
Definition gmThreadLocalStorage.h:132
Our thread wrapper, specialized for running tasks from the thread manager queue.
Definition gmThreadManager.h:268
QThread * _thread
The thread.
Definition gmThreadManager.h:301
GmThreadManager * threadManager() const
Returns a pointer to the thread manager.
Definition gmThreadManager.h:279
int id() const
Returns the thread id (a value between 1 and the maximum number of threads)
Definition gmThreadManager.h:276
QThread * thread() const
Returns the pointer to the associated Qt thread.
Definition gmThreadManager.h:282
GmThreadManager * _tm
The thread manager.
Definition gmThreadManager.h:302
LuaEnv * luaEnv() const
Returns the Lua environment associated with this thread.
Definition gmThreadManager.h:288
bool cancelPending() const
Do we have a pending request to cancel this thread work or any other thread has aborted?
Definition gmThreadManager.h:285
int _tid
Our thread id.
Definition gmThreadManager.h:300
QWaitCondition _idleCond
The condition variable used to wake up a thread waiting for jobs.
Definition gmThreadManager.h:303
Thread manager used for handling parallel executions.
Definition gmThreadManager.h:100
GmThread * _mainThread
A pointer to a GmThread wrapper for the main thread.
Definition gmThreadManager.h:238
bool cancelPending() const
Do we have a pending request to cancel the simulation or any other thread has aborted?
Definition gmThreadManager.h:132
QMutex _taskMutex
The mutex controlling access to thread tasks.
Definition gmThreadManager.h:250
QQueue< GmThreadTask * > GmTaskQueue
Type for a task queue.
Definition gmThreadManager.h:229
static int currentId()
Returns the id of the current thread, which MUST be either the main thread or a thread created by the...
Definition gmThreadManager.h:171
void setCancelFlag(bool mode=true)
Informs the thread manager that the user wants to cancel the simulation. Can be called from the serve...
Definition gmThreadManager.h:149
static int _maxNumThreads
The maximum number of threads that can be used.
Definition gmThreadManager.h:231
static int numProcCores()
Returns the number of processor cores (physical processors - no hyper threading)
Definition gmThreadManager.h:164
int _active
Flag for controlling if runTasks is active, and how many threads should be used (prevents a thread to...
Definition gmThreadManager.h:246
static int maxProcThreads()
Returns the maximum number of concurrent threads supported by the processor.
Definition gmThreadManager.h:161
bool cancelRequested() const
Do we have a pending request to cancel the simulation?
Definition gmThreadManager.h:129
const GmLogCategory & logger()
Returns the thread manager logger.
Definition gmThreadManager.h:152
bool _abortRequested
Flag for aborting tasks due to another task failure.
Definition gmThreadManager.h:248
QWaitCondition _managerCond
The condition variable used to block the manager until tasks are finished.
Definition gmThreadManager.h:251
int _npendingTasks
The number of still pending tasks.
Definition gmThreadManager.h:242
int _numTasks
The number of task added to the queues before runTasks()
Definition gmThreadManager.h:241
ProcAffinityMode
Process affinity mode.
Definition gmThreadManager.h:190
bool _cancelRequested
Flag for thread cancelation due to an external request / thread manager deletion.
Definition gmThreadManager.h:247
QVector< int > _taskAffinity
The vector storing the id of the thread used to execute each task.
Definition gmThreadManager.h:243
GmLogCategory _logger
Basic logger object for thread manager messages.
Definition gmThreadManager.h:253
const QVector< int > & taskAffinity() const
Returns a vector with size equal to the number of tasks executed in the last call to runTasks() stori...
Definition gmThreadManager.h:124
static int _numProcCores
The number of processor cores (physical processors - no hyper threading)
Definition gmThreadManager.h:233
QtHardwareInfo * _hwInfo
A pointer to the hardware info structure provided by the SimulationData object.
Definition gmThreadManager.h:235
int _maxAffinityId
The biggest tid requested in a call to addTask()
Definition gmThreadManager.h:244
static bool inMainThread()
Is the current thread the main thread? Equivalent to comparing the currentId() with 0.
Definition gmThreadManager.h:174
GmTLS< GmTaskQueue, false > * _taskQueues
The task queues. Queue 0 is a global queue shared by all threads, while other queues can be used to m...
Definition gmThreadManager.h:240
static int maxWorkerThreads()
Returns the maximum number of allowed working threads.
Definition gmThreadManager.h:158
QList< GmThread * > _threadList
A list with all the allocated threads.
Definition gmThreadManager.h:239
static int _maxProcThreads
The maximum number of supported concurrent threads (logical processors)
Definition gmThreadManager.h:232
ThreadPriority
Base process / thread priority levels.
Definition gmThreadManager.h:181
GmTLS< LuaEnv *, false > * _luaEnv
The per thread Lua environment. Must be a pointer to avoid include order problems with GmTLS.
Definition gmThreadManager.h:255
ThreadConfig _config
The set of user given configuration options.
Definition gmThreadManager.h:236
Interface for a task executed by a thread manager thread.
Definition gmThreadManager.h:71
virtual ~GmThreadTask()
Virtual destructor.
Definition gmThreadManager.h:77
void setTaskIndex(int index)
Updates the task index inside the task manager for this task.
Definition gmThreadManager.h:91
virtual GmThreadTaskResult run(const GmThread *thread)=0
Virtual function used to execute the task. Gets as parameter the current thread in which the task is ...
GmThreadTask()
Default constructor. Task index MUST be set by a call to setTaskIndex()
Definition gmThreadManager.h:74
virtual QString taskDescription()
A task description that can be used for debug purposes.
Definition gmThreadManager.h:85
int taskIndex() const
Returns the task index (the order in which the task was added to the thread manager)
Definition gmThreadManager.h:88
Declaration of useful configuration definitions for the Core library.
#define GMC_API_EXPORT
Macro for controlling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_L...
Definition gmCoreConfig.h:35
Declaration of support functions and macros for information logging.
GmLogLevel
Available log levels list.
Definition gmLog.h:36
GmThreadLocal int GmThreadManagerThreadId
Local storage for thread ids. More efficient than using QThread::currentThread() for then accessing t...
Definition gmThreadManager.cpp:55
#define GmThreadLocal
Macro used to declare that a global / static variable has a per thread value (Thread Local Storage)
Definition gmThreadManager.h:55
GmThreadTaskResult
Possible results for running a task.
Definition gmThreadManager.h:63
@ GM_THREAD_CANCELED
The thread was externally canceled.
Definition gmThreadManager.h:65
@ GM_THREAD_OK
The thread finished its execution without errors.
Definition gmThreadManager.h:64
@ GM_THREAD_ABORTED
The thread aborted.
Definition gmThreadManager.h:66
GmThreadLocal int GmThreadManagerThreadId
Local storage for thread ids. More efficient than using QThread::currentThread() for then accessing t...
Definition gmThreadManager.cpp:55
The set of configured thread management options.
Definition gmThreadManager.h:199
int _maxOpenMPThreads
The maxOpenMPThreads configuration option with negative user values converted. Stores -1 if not given...
Definition gmThreadManager.h:201
QVector< quint64 > _procMask
The procAffinityMask configuration option as a vector of masks (one for each process group)
Definition gmThreadManager.h:206
ThreadPriority _procPriority
The procPriorityClass configuration option.
Definition gmThreadManager.h:203
ProcAffinityMode _affMode
The procAffinityMode configuration option.
Definition gmThreadManager.h:205
QVector< quint64 > _threadMask
The threadAffinityMask configuration option as a vector of masks (one for each process group)
Definition gmThreadManager.h:207
int _maxThreads
The maxThreads configuration option. Negative values are already converted to the correct multiple of...
Definition gmThreadManager.h:200
ThreadPriority _threadPriority
The threadPriority configuration option.
Definition gmThreadManager.h:204
bool _L3ThreadScheduling
A flags set to true if threadAffinityMask was equal to "L3Cores". _threadMask is empty in this case.
Definition gmThreadManager.h:208
int _maxBlasThreads
The maxBlasThreads configuration option with negative user values converted. Stores -1 if not given b...
Definition gmThreadManager.h:202