Arcane  v4.1.0.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
TaskFactory.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* TaskFactory.h (C) 2000-2025 */
9/* */
10/* Fabrique pour les tâches. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_BASE_TASKFACTORY_H
13#define ARCCORE_BASE_TASKFACTORY_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arccore/concurrency/Task.h"
18#include "arccore/concurrency/ITaskImplementation.h"
19
20/*---------------------------------------------------------------------------*/
21/*---------------------------------------------------------------------------*/
22
23namespace Arcane
24{
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28/*!
29 * \ingroup Concurrency
30 * \brief Fabrique pour les tâches.
31 */
32class ARCCORE_CONCURRENCY_EXPORT TaskFactory
33{
34 friend TaskFactoryInternal;
35
36 public:
37
38 TaskFactory() = delete;
39
40 public:
41
42 /*!
43 * \brief Créé une tâche.
44 * Lors de l'exécution, la tâche appellera la méthode \a function via
45 * l'instance \a instance.
46 */
47 template <typename InstanceType> static ITask*
48 createTask(InstanceType* instance, void (InstanceType::*function)(const TaskContext& tc))
49 {
50 TaskFunctorWithContext<InstanceType> functor(instance, function);
51 return m_impl->createRootTask(&functor);
52 }
53
54 /*!
55 * \brief Créé une tâche.
56 * Lors de l'exécution, la tâche appellera la méthode \a function via
57 * l'instance \a instance.
58 */
59 template <typename InstanceType> static ITask*
60 createTask(InstanceType* instance, void (InstanceType::*function)())
61 {
62 TaskFunctor<InstanceType> functor(instance, function);
63 return m_impl->createRootTask(&functor);
64 }
65
66 /*!
67 * \brief Créé une tâche fille.
68 *
69 * Lors de l'exécution, la tâche appellera la méthode \a function via
70 * l'instance \a instance.
71 */
72 template <typename InstanceType> static ITask*
73 createChildTask(ITask* parent_task, InstanceType* instance, void (InstanceType::*function)(const TaskContext& tc))
74 {
75 ARCCORE_CHECK_POINTER(parent_task);
76 TaskFunctorWithContext<InstanceType> functor(instance, function);
77 return parent_task->_createChildTask(&functor);
78 }
79
80 /*!
81 * \brief Créé une tâche fille.
82 *
83 * Lors de l'exécution, la tâche appellera la méthode \a function via
84 * l'instance \a instance.
85 */
86 template <typename InstanceType> static ITask*
87 createChildTask(ITask* parent_task, InstanceType* instance, void (InstanceType::*function)())
88 {
89 ARCCORE_CHECK_POINTER(parent_task);
90 TaskFunctor<InstanceType> functor(instance, function);
91 return parent_task->_createChildTask(&functor);
92 }
93
94 //! Exécute le fonctor \a f en concurrence.
95 static void executeParallelFor(Integer begin, Integer size, const ParallelLoopOptions& options, IRangeFunctor* f)
96 {
97 m_impl->executeParallelFor(begin, size, options, f);
98 }
99
100 //! Exécute le fonctor \a f en concurrence.
101 static void executeParallelFor(Integer begin, Integer size, Integer block_size, IRangeFunctor* f)
102 {
103 m_impl->executeParallelFor(begin, size, block_size, f);
104 }
105
106 //! Exécute le fonctor \a f en concurrence.
107 static void executeParallelFor(Integer begin, Integer size, IRangeFunctor* f)
108 {
109 m_impl->executeParallelFor(begin, size, f);
110 }
111
112 //! Exécute la boucle \a loop_info en concurrence.
113 static void executeParallelFor(const ParallelFor1DLoopInfo& loop_info)
114 {
115 m_impl->executeParallelFor(loop_info);
116 }
117
118 //! Exécute une boucle simple
119 static void executeParallelFor(const ComplexForLoopRanges<1>& loop_ranges,
120 const ParallelLoopOptions& options,
121 IMDRangeFunctor<1>* functor)
122 {
123 m_impl->executeParallelFor(loop_ranges, ForLoopRunInfo(options), functor);
124 }
125
126 //! Exécute une boucle simple
127 static void executeParallelFor(const ComplexForLoopRanges<1>& loop_ranges,
128 const ForLoopRunInfo& run_info,
129 IMDRangeFunctor<1>* functor)
130 {
131 m_impl->executeParallelFor(loop_ranges, run_info, functor);
132 }
133
134 //! Exécute une boucle 2D
135 static void executeParallelFor(const ComplexForLoopRanges<2>& loop_ranges,
136 const ParallelLoopOptions& options,
137 IMDRangeFunctor<2>* functor)
138 {
139 m_impl->executeParallelFor(loop_ranges, ForLoopRunInfo(options), functor);
140 }
141
142 //! Exécute une boucle 2D
143 static void executeParallelFor(const ComplexForLoopRanges<2>& loop_ranges,
144 const ForLoopRunInfo& run_info,
145 IMDRangeFunctor<2>* functor)
146 {
147 m_impl->executeParallelFor(loop_ranges, run_info, functor);
148 }
149
150 //! Exécute une boucle 3D
151 static void executeParallelFor(const ComplexForLoopRanges<3>& loop_ranges,
152 const ParallelLoopOptions& options,
153 IMDRangeFunctor<3>* functor)
154 {
155 m_impl->executeParallelFor(loop_ranges, ForLoopRunInfo(options), functor);
156 }
157
158 //! Exécute une boucle 3D
159 static void executeParallelFor(const ComplexForLoopRanges<3>& loop_ranges,
160 const ForLoopRunInfo& run_info,
161 IMDRangeFunctor<3>* functor)
162 {
163 m_impl->executeParallelFor(loop_ranges, run_info, functor);
164 }
165
166 //! Exécute une boucle 4D
167 static void executeParallelFor(const ComplexForLoopRanges<4>& loop_ranges,
168 const ParallelLoopOptions& options,
169 IMDRangeFunctor<4>* functor)
170 {
171 m_impl->executeParallelFor(loop_ranges, ForLoopRunInfo(options), functor);
172 }
173
174 //! Exécute une boucle 4D
175 static void executeParallelFor(const ComplexForLoopRanges<4>& loop_ranges,
176 const ForLoopRunInfo& run_info,
177 IMDRangeFunctor<4>* functor)
178 {
179 m_impl->executeParallelFor(loop_ranges, run_info, functor);
180 }
181
182 //! Nombre de threads utilisés au maximum pour gérer les tâches.
184 {
185 return m_impl->nbAllowedThread();
186 }
187
188 /*!
189 * \brief Indice (entre 0 et nbAllowedThread()-1) du thread exécutant la tâche actuelle.
190 *
191 * Pour des raisons de performance, il est préférable d'appeler cette méthode
192 * le moins possible. L'idéal est de ne le faire qu'au début de l'exécution de la tâche
193 * et ensuite d'utiliser la valeur retournée.
194 */
196 {
197 return m_impl->currentTaskThreadIndex();
198 }
199
200 /*!
201 * \brief Indice (entre 0 et nbAllowedThread()-1) de la tâche actuelle.
202 *
203 * Cet indice est le même que currentTaskThreadIndex() sauf dans le cas
204 * où on se trouve dans un executeParallelFor() avec un partitionnement
205 * déterministe (ParallelLoopOptions::Partitioner::Deterministic).
206 * Dans ce dernier cas, le numéro de la tâche est assigné de manière
207 * déterministe qui ne dépend que du nombre de threads alloués pour la
208 * tâche et de ParallelLoopOptions::grainSize().
209 *
210 * Si le thread courant n'exécute pas une tâche associé à cette implémentation,
211 * retourne (-1).
212 */
214 {
215 return m_impl->currentTaskIndex();
216 }
217
218 public:
219
220 //! Positionne les valeurs par défaut d'exécution d'une boucle parallèle
222 {
223 m_default_loop_options = v;
224 }
225
226 //! Valeurs par défaut d'exécution d'une boucle parallèle
228 {
229 return m_default_loop_options;
230 }
231
232 public:
233
234 /*!
235 * \brief Indique si les tâches sont actives.
236 * Les tâches sont actives si une implémentation est disponible et si le nombre
237 * de threads demandé est strictement supérieur à 1.
238 */
239 static bool isActive()
240 {
241 return m_impl->isActive();
242 }
243
244 /*!
245 * \brief Affiche les informations sur l'implémentation.
246 *
247 * Les informations sont par exemple le numéro de version ou le nom
248 * de l'implémentation.
249 */
250 static void printInfos(std::ostream& o)
251 {
252 return m_impl->printInfos(o);
253 }
254
255 /*!
256 * \brief Observable appelé lors de la création d'un thread pour une tâche.
257 *
258 * \warning L'instance de l'observable est créée lors du premier appel
259 * à cette méthode. Elle n'est donc pas thread-safe. De même,
260 * la modification de l'observable (ajout/suppression d'observateur)
261 * n'est pas thread-safe.
262 */
263 ARCCORE_DEPRECATED_REASON("Y2024: This method is internal to Arcane. Do not use it")
264 static IObservable* createThreadObservable();
265
266 /*!
267 * \brief Observable appelé lors de la destruction d'un thread pour une tâche.
268 *
269 * \warning L'instance de l'observable est créée lors du premier appel
270 * à cette méthode. Elle n'est donc pas thread-safe. De même,
271 * la modification de l'observable (ajout/suppression d'observateur)
272 * n'est pas thread-safe.
273 */
274 ARCCORE_DEPRECATED_REASON("Y2024: This method is internal to Arcane. Do not use it")
275 static IObservable* destroyThreadObservable();
276
277 /*!
278 * \internal
279 * \brief Indique qu'on n'utilisera plus les threads.
280 * Cette méthode ne doit pas être appelée lorsque des tâches sont actives.
281 */
282 static void terminate();
283
284 public:
285
286 //! Positionne le niveau de verbosité (0 pour pas d'affichage qui est le défaut)
287 static void setVerboseLevel(Integer v) { m_verbose_level = v; }
288
289 //! Niveau de verbosité
290 static Integer verboseLevel() { return m_verbose_level; }
291
292 public:
293
294 //! \internal
295 ARCCORE_DEPRECATED_REASON("Y2024: This method is internal to Arcane. "
296 "Use TaskFactoryInternal::setImplementation() instead")
297 static void _internalSetImplementation(ITaskImplementation* task_impl);
298
299 private:
300
301 static ITaskImplementation* m_impl;
302 static Int32 m_verbose_level;
303 static ParallelLoopOptions m_default_loop_options;
304};
305
306/*---------------------------------------------------------------------------*/
307/*---------------------------------------------------------------------------*/
308
309} // End namespace Arcane
310
311/*---------------------------------------------------------------------------*/
312/*---------------------------------------------------------------------------*/
313
314#endif
Informations d'exécution d'une boucle.
Interface d'un fonctor sur un interval d'itération multi-dimensionnel de dimension RankValue.
Interface d'un fonctor sur un interval d'itération.
Interface d'une tâche concourante.
Definition Task.h:187
Caractéristiques d'un boucle 1D multi-thread.
Definition ParallelFor.h:34
Options d'exécution d'une boucle parallèle en multi-thread.
Contexte d'éxecution d'une tâche.
Definition Task.h:47
static Int32 currentTaskIndex()
Indice (entre 0 et nbAllowedThread()-1) de la tâche actuelle.
static void executeParallelFor(const ParallelFor1DLoopInfo &loop_info)
Exécute la boucle loop_info en concurrence.
static void setVerboseLevel(Integer v)
Positionne le niveau de verbosité (0 pour pas d'affichage qui est le défaut)
static Int32 nbAllowedThread()
Nombre de threads utilisés au maximum pour gérer les tâches.
static void executeParallelFor(Integer begin, Integer size, IRangeFunctor *f)
Exécute le fonctor f en concurrence.
static ITask * createTask(InstanceType *instance, void(InstanceType::*function)())
Créé une tâche. Lors de l'exécution, la tâche appellera la méthode function via l'instance instance.
Definition TaskFactory.h:60
static const ParallelLoopOptions & defaultParallelLoopOptions()
Valeurs par défaut d'exécution d'une boucle parallèle.
static void executeParallelFor(const ComplexForLoopRanges< 1 > &loop_ranges, const ParallelLoopOptions &options, IMDRangeFunctor< 1 > *functor)
Exécute une boucle simple.
static void executeParallelFor(const ComplexForLoopRanges< 1 > &loop_ranges, const ForLoopRunInfo &run_info, IMDRangeFunctor< 1 > *functor)
Exécute une boucle simple.
static void executeParallelFor(const ComplexForLoopRanges< 2 > &loop_ranges, const ForLoopRunInfo &run_info, IMDRangeFunctor< 2 > *functor)
Exécute une boucle 2D.
static Integer verboseLevel()
Niveau de verbosité
static void executeParallelFor(const ComplexForLoopRanges< 4 > &loop_ranges, const ParallelLoopOptions &options, IMDRangeFunctor< 4 > *functor)
Exécute une boucle 4D.
static void executeParallelFor(const ComplexForLoopRanges< 2 > &loop_ranges, const ParallelLoopOptions &options, IMDRangeFunctor< 2 > *functor)
Exécute une boucle 2D.
static void setDefaultParallelLoopOptions(const ParallelLoopOptions &v)
Positionne les valeurs par défaut d'exécution d'une boucle parallèle.
static void executeParallelFor(Integer begin, Integer size, const ParallelLoopOptions &options, IRangeFunctor *f)
Exécute le fonctor f en concurrence.
Definition TaskFactory.h:95
static void executeParallelFor(Integer begin, Integer size, Integer block_size, IRangeFunctor *f)
Exécute le fonctor f en concurrence.
static void executeParallelFor(const ComplexForLoopRanges< 3 > &loop_ranges, const ParallelLoopOptions &options, IMDRangeFunctor< 3 > *functor)
Exécute une boucle 3D.
static ITask * createChildTask(ITask *parent_task, InstanceType *instance, void(InstanceType::*function)(const TaskContext &tc))
Créé une tâche fille.
Definition TaskFactory.h:73
static Int32 currentTaskThreadIndex()
Indice (entre 0 et nbAllowedThread()-1) du thread exécutant la tâche actuelle.
static ITask * createChildTask(ITask *parent_task, InstanceType *instance, void(InstanceType::*function)())
Créé une tâche fille.
Definition TaskFactory.h:87
static bool isActive()
Indique si les tâches sont actives. Les tâches sont actives si une implémentation est disponible et s...
static ITask * createTask(InstanceType *instance, void(InstanceType::*function)(const TaskContext &tc))
Créé une tâche. Lors de l'exécution, la tâche appellera la méthode function via l'instance instance.
Definition TaskFactory.h:48
static void executeParallelFor(const ComplexForLoopRanges< 4 > &loop_ranges, const ForLoopRunInfo &run_info, IMDRangeFunctor< 4 > *functor)
Exécute une boucle 4D.
static void printInfos(std::ostream &o)
Affiche les informations sur l'implémentation.
static void executeParallelFor(const ComplexForLoopRanges< 3 > &loop_ranges, const ForLoopRunInfo &run_info, IMDRangeFunctor< 3 > *functor)
Exécute une boucle 3D.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
Int32 Integer
Type représentant un entier.
std::int32_t Int32
Type entier signé sur 32 bits.