24#ifndef _GEMA_THREAD_MANAGER_H_
25#define _GEMA_THREAD_MANAGER_H_
32#include <QWaitCondition>
44template <
class T,
bool Align>
class GmTLS;
53#define GmThreadLocal __declspec(thread)
55#define GmThreadLocal __thread
107 LuaEnv* threadLuaEnv(
int tid)
const;
111 void execTasks(
int nthreads);
126 int numTasks()
const;
161 static int maxProcThreads() { assert(_maxProcThreads > 0);
return _maxProcThreads; }
164 static int numProcCores() { assert(_numProcCores > 0);
return _numProcCores; }
215 void initProcAffinity();
218 void winInitProcOrThreadSoftAffinity(
bool thread,
const QVector<quint64>& maskVector);
221 int clearTaskQueues();
222 int numTasksInQueues()
const;
276 int id()
const {
return _tid; }
295 bool wait(
unsigned long time = ULONG_MAX);
298 void initThreadAffinity();
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