24#ifndef _GEMA_TASK_MANAGER_H_
25#define _GEMA_TASK_MANAGER_H_
37#if (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L) || (__cplusplus >= 202002L)
91 NPS_SEQUENTIAL_BLOCK = 0,
107 CPS_SEQUENTIAL_BLOCK = 0,
120 static NodePartitionStrategy str2NodePartitionStrategy(
QString str);
121 static CellPartitionStrategy str2CellPartitionStrategy(
QString str);
127 int numTasks()
const {
return _threadManager->numTasks(); }
131 int ntasks = 0, NodePartitionStrategy strategy = NPS_DEFAULT,
136 int ntasks = 0, NodePartitionStrategy strategy = NPS_DEFAULT,
141 int ntasks = 0, NodePartitionStrategy strategy = NPS_DEFAULT,
146 int ntasks = 0, NodePartitionStrategy strategy = NPS_DEFAULT,
150 void execParallelNodeLoop(
const GmNodeSet* m, T& taskObj,
151 int ntasks = 0, NodePartitionStrategy strategy = NPS_DEFAULT,
156 int ntasks = 0, CellPartitionStrategy strategy = CPS_DEFAULT,
161 int ntasks = 0, CellPartitionStrategy strategy = CPS_DEFAULT,
165 void execParallelCellLoop(
GmCellMesh* m,
bool activeOnly, T& taskObj,
166 int ntasks = 0, CellPartitionStrategy strategy = CPS_DEFAULT,
169 template <
class M,
class T>
bool addNodeTasks(
const M* m,
GmAffectedNodes affNodes, T& taskObj,
170 int ntasks, NodePartitionStrategy strategy,
int nworkers,
173 template <
class M,
class T>
bool addCellTasks(M* m,
bool activeOnly, T& taskObj,
174 int ntasks, CellPartitionStrategy strategy,
int nworkers,
182#if (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L) || (__cplusplus >= 202002L)
189template<
typename Accessor,
typename ReduceF,
typename JoinF,
typename InitV>
190 void traverseAccessor(
int nworkers,
const Accessor* acc,
const GmCellMesh* cmesh,
const ReduceF& reduce,
const JoinF& join, InitV& val);
192template<
typename Accessor,
typename ReduceF,
typename JoinF,
typename InitV>
193 void traverseAccessor(
int nworkers,
const Accessor* acc1,
const Accessor* acc2,
const GmCellMesh* cmesh,
const ReduceF& reduce,
const JoinF& join, InitV& val);
196 void adjustExecParameters(
int* ntasks,
int* nworkers);
200 NodePartitionStrategy strategy)
const;
204 CellPartitionStrategy strategy)
const;
265 int ntasks, NodePartitionStrategy strategy,
281 int ntasks, NodePartitionStrategy strategy,
318 int ntasks, NodePartitionStrategy strategy,
343template <
class M,
class T>
356 assert(ntasks > 0 && nworkers >= 0 && strategy >= 0 && strategy <
NPS_NUM_STRATEGIES);
364 bool useAffinity = !taskAffinity.
isEmpty();
365 if(useAffinity && taskData.
size() != taskAffinity.
size())
367 qDeleteAll(taskData);
378 NodeTask(T& task,
GmNodeTaskDataSet* dataSet) : _task(task), _dataSet(dataSet) {}
379 virtual ~NodeTask() {
delete _dataSet; }
489template <
class M,
class T>
502 assert(ntasks > 0 && nworkers >= 0 && strategy >= 0 && strategy <
CPS_NUM_STRATEGIES);
510 bool useAffinity = !taskAffinity.
isEmpty();
511 if(useAffinity && taskData.
size() != taskAffinity.
size())
513 qDeleteAll(taskData);
524 CellTask(T& task,
GmCellTaskDataSet* dataSet) : _task(task), _dataSet(dataSet) {}
525 virtual ~CellTask() {
delete _dataSet; }
546template<
typename Accessor,
typename ReduceF,
typename JoinF,
typename InitV>
547#if (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L) || (__cplusplus >= 202002L)
548 requires std::invocable<ReduceF, const double*, InitV&>
549&& std::invocable<JoinF, const InitV&, InitV&>
550&& (std::same_as<Accessor, GmValueAccessor> || std::same_as<Accessor, GmCellAccessor> || std::same_as<Accessor, GmGaussAccessor>)
552void GmTaskManager::traverseAccessor(
int nworkers,
const Accessor* acc,
const GmCellMesh* cmesh,
const ReduceF& reduce,
const JoinF& join, InitV& val)
561 if constexpr (std::is_same_v<Accessor, GmGaussAccessor>)
563 gi = acc->gaussIndex();
567 else if constexpr (std::is_same_v<Accessor, GmCellAccessor>)
570 else if constexpr (std::is_same_v<Accessor, GmValueAccessor>)
582 while ((i = dataSet->
nextNode()) != -1)
584 if (thread->cancelPending())
588 if constexpr (std::is_same_v<Accessor, GmGaussAccessor>)
594 int cId = gi->
cellIp(i, &ip);
595 v = acc->valueAt(emesh->
cell(cId), ip, NULL);
597 else if constexpr (std::is_same_v<Accessor, GmCellAccessor>)
607 v = acc->valueAt(cmesh->
cell(i), NULL, ip);
609 else if constexpr (std::is_same_v<Accessor, GmValueAccessor>)
618 runParallelCountLoop(acc->size(), task, nworkers);
621 for (
int i = 0; i < tval.
size(); ++i) {
629template<
typename Accessor,
typename ReduceF,
typename JoinF,
typename InitV>
630#if (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L) || (__cplusplus >= 202002L)
631 requires std::invocable<ReduceF, const double*, const double*, InitV&>
632&& std::invocable<JoinF, const InitV&, InitV&>
633&& (std::same_as<Accessor, GmValueAccessor> || std::same_as<Accessor, GmCellAccessor> || std::same_as<Accessor, GmGaussAccessor>)
635void GmTaskManager::traverseAccessor(
int nworkers,
const Accessor* acc1,
const Accessor* acc2,
const GmCellMesh* cmesh,
const ReduceF& reduce,
const JoinF& join, InitV& val)
638 assert(acc1 && acc2);
644 if constexpr (std::is_same_v<Accessor, GmGaussAccessor>)
646 gi = acc1->gaussIndex();
648 assert(emesh == cmesh);
650 else if constexpr (std::is_same_v<Accessor, GmCellAccessor>) {}
651 else if constexpr (std::is_same_v<Accessor, GmValueAccessor>) {}
656 assert(acc1->size() == acc2->size());
662 while ((i = dataSet->
nextNode()) != -1)
664 if (thread->cancelPending())
667 const double* v1, * v2;
668 if constexpr (std::is_same_v<Accessor, GmGaussAccessor>)
674 int cId = gi->
cellIp(i, &ip);
676 v1 = acc1->valueAt(cell, ip, NULL);
677 v2 = acc2->valueAt(cell, ip, NULL);
679 else if constexpr (std::is_same_v<Accessor, GmCellAccessor>)
682 v1 = acc1->valueAt(cell, NULL);
683 v2 = acc2->valueAt(cell, NULL);
685 else if constexpr (std::is_same_v<Accessor, GmValueAccessor>)
690 reduce(v1, v2, lval);
695 runParallelCountLoop(acc1->size(), task, nworkers);
698 for (
int i = 0; i < tval.
size(); ++i) {
Interface for helping managing mesh cell groups and returning the elements in their union.
Definition gmCellGroupSet.h:35
Base interface for mesh cells.
Definition gmCell.h:88
Base interface class for CellMesh type plugins.
Definition gmCellMesh.h:40
virtual const GmCell * cell(int cellIndex) const =0
Returns a cell of the mesh.
Base class for accessing the set of cells belonging to a mesh partition.
Definition gmTaskManager.h:70
virtual ~GmCellTaskDataSet()
Virtual destructor.
Definition gmTaskManager.h:73
virtual GmCell * nextCell()=0
Returns the next cell in the set. Returns NULL when there are no more cells to treat.
virtual void reset()=0
Resets the data set iterator so that the next call to nextCell will return the first cell in the set.
Base interface for FEM (finite element) meshes.
Definition gmElementMesh.h:42
A Gauss index interface always indexing ALL the elements of a mesh, with support for mesh growing.
Definition gmGaussIndex.h:275
const GmElementMesh * mesh() const
Returns the associated mesh.
Definition gmGaussIndex.h:287
Base interface class for Mesh type plugins.
Definition gmMesh.h:48
This class stores a set of nodes belonging to a mesh. The nodes can belong to the mesh boundary but t...
Definition gmNodeSet.h:34
Base class for accessing the set of nodes belonging to a mesh partition.
Definition gmTaskManager.h:51
virtual ~GmNodeTaskDataSet()
Virtual destructor.
Definition gmTaskManager.h:54
virtual int nextNode()=0
Returns the next node in the set. Returns -1 when there are no more nodes to treat....
virtual void reset()=0
Resets the data set iterator so that the next call to nextNode will return the first node in the set.
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
T & localData(int tid)
Returns the given thread local data as a modifiable reference.
Definition gmThreadLocalStorage.h:163
int size() const
Returns the number of values stored in the TLS object. Equal to the max number of threads + 1 (for th...
Definition gmThreadLocalStorage.h:160
void init(const T &val)
Initializes the value for ALL threads with the given value.
Definition gmThreadLocalStorage.h:153
Task manager used for handling parallel executions.
Definition gmTaskManager.h:84
GmThreadTaskResult runParallelNodeLoop(const GmMesh *m, GmAffectedNodes affNodes, T &taskObj, int ntasks=0, NodePartitionStrategy strategy=NPS_DEFAULT, int nworkers=-1, const QVector< int > &taskAffinity=QVector< int >())
Parallel executes the given task over mesh nodes.
Definition gmTaskManager.h:248
void adjustExecParameters(int *ntasks, int *nworkers)
Adjusts task execution parameters, converting possible default options to real values.
Definition gmTaskManager.cpp:252
bool addCellTasks(M *m, bool activeOnly, T &taskObj, int ntasks, CellPartitionStrategy strategy, int nworkers, const QVector< int > &taskAffinity, bool *ok)
Worker function for adding cell tasks to the thread manager. See runParallelCellLoop() for parameter ...
Definition gmTaskManager.h:490
GmThreadTaskResult runParallelCellLoop(GmCellMesh *m, bool activeOnly, T &taskObj, int ntasks=0, CellPartitionStrategy strategy=CPS_DEFAULT, int nworkers=-1, const QVector< int > &taskAffinity=QVector< int >())
Parallel executes the given task over mesh cells.
Definition gmTaskManager.h:430
void execParallelNodeLoop(const GmMesh *m, GmAffectedNodes affNodes, T &taskObj, int ntasks=0, NodePartitionStrategy strategy=NPS_DEFAULT, int nworkers=-1, const QVector< int > &taskAffinity=QVector< int >())
Similar to runParallelNodeLoop() but raising a lua error if the execution was either canceled or abor...
Definition gmTaskManager.h:297
QVector< GmCellTaskDataSet * > buildCellPartition(M *m, int n, bool activeOnly, CellPartitionStrategy strategy) const
Creates a list with the 'n' parallel cell tasks to be executed, given the destination mesh,...
Definition gmTaskManager.cpp:356
GmThreadManager * threadManager() const
Returns the associated thread manager.
Definition gmTaskManager.h:124
void execParallelCellLoop(GmCellMesh *m, bool activeOnly, T &taskObj, int ntasks=0, CellPartitionStrategy strategy=CPS_DEFAULT, int nworkers=-1, const QVector< int > &taskAffinity=QVector< int >())
Similar to runParallelCellLoop() but raising a lua error if the execution was either canceled or abor...
Definition gmTaskManager.h:464
QVector< GmNodeTaskDataSet * > buildNodePartition(const M *m, int n, GmAffectedNodes affNodes, NodePartitionStrategy strategy) const
Creates a list with the 'n' parallel node tasks to be executed, given the destination mesh,...
Definition gmTaskManager.cpp:270
bool addNodeTasks(const M *m, GmAffectedNodes affNodes, T &taskObj, int ntasks, NodePartitionStrategy strategy, int nworkers, const QVector< int > &taskAffinity, bool *ok)
Worker function for adding node tasks to the thread manager. See runParallelNodeLoop() for parameter ...
Definition gmTaskManager.h:344
int _defNumTasks
The default number of tasks for a parallel loop. Negative numbers are multiples of the number of work...
Definition gmTaskManager.h:208
NodePartitionStrategy
Partition strategies for a parallel node loop.
Definition gmTaskManager.h:89
@ NPS_DEFAULT
Uses the default strategy given by simulation options or NPS_SEQUENTIAL_BLOCK if missing.
Definition gmTaskManager.h:90
@ NPS_NUM_STRATEGIES
The number of partition strategies.
Definition gmTaskManager.h:100
NodePartitionStrategy _defNodeStrategy
The default strategy for node partitioning.
Definition gmTaskManager.h:209
CellPartitionStrategy
Partition strategies for a parallel cell loop.
Definition gmTaskManager.h:105
@ CPS_NUM_STRATEGIES
The number of partition strategies.
Definition gmTaskManager.h:115
@ CPS_DEFAULT
Uses the default strategy given by simulation options or CPS_SEQUENTIAL_BLOCK if missing.
Definition gmTaskManager.h:106
GmThreadManager * _threadManager
The thread manager used to run tasks in multiple threads.
Definition gmTaskManager.h:206
int numTasks() const
Returns the number of tasks added to the queue by addNodetasks or addCellTasks.
Definition gmTaskManager.h:127
CellPartitionStrategy _defCellStrategy
The default strategy for cell partitioning.
Definition gmTaskManager.h:210
Our thread wrapper, specialized for running tasks from the thread manager queue.
Definition gmThreadManager.h:268
Thread manager used for handling parallel executions.
Definition gmThreadManager.h:100
void execTasks(int nthreads)
Similar to runTasks() but raising a lua error if the execution was either canceled or aborted.
Definition gmThreadManager.cpp:951
void addTask(GmThreadTask *task, int tid=0)
Adds a task to the manager. The manager gets the task ownership and will delete it after execution....
Definition gmThreadManager.cpp:776
LuaEnv * threadLuaEnv(int tid) const
Returns the Lua environment associated with the given thread id.
Definition gmThreadManager.cpp:726
const GmLogCategory & logger()
Returns the thread manager logger.
Definition gmThreadManager.h:152
GmThreadTaskResult runTasks(int nthreads)
Executes the set of tasks in the queue using at most 'nthreads'. Waits for the threads to finish befo...
Definition gmThreadManager.cpp:820
Interface for a task executed by a thread manager thread.
Definition gmThreadManager.h:71
int cellIp(int gpIndex, int *ip) const
Given a Gauss point "linear" index (a value from 0 to numPoints()-1), returns the mesh cell index + i...
Definition gmGaussIndex.h:135
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 the GmThreadManager class.
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
Auxiliary configuration file used to enable or disable compiling the GeMA tools with support for usin...
#define S_TRACE()
Macro for run time stack tracking at release build.
Definition gmTrace.h:44
Declaration of the GmValueInfo class.
GmAffectedNodes
Affected node types for node based values (GM_NODE_COORDINATES, GM_NODE_ATTRIBUTE or GM_NODE_STATEVAR...
Definition gmValueInfo.h:97
@ GM_BOTH
Specifies that this value exists over mesh and ghost nodes.
Definition gmValueInfo.h:100
QString tr(const char *sourceText, const char *disambiguation, int n)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool isEmpty() const const