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