Arcane  v4.1.0.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
Task.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/* Task.h (C) 2000-2025 */
9/* */
10/* Classes gérant les tâches concurrentes. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_BASE_TASK_H
13#define ARCCORE_BASE_TASK_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arccore/base/RangeFunctor.h"
18#include "arccore/base/FatalErrorException.h"
19#include "arccore/base/ForLoopTraceInfo.h"
20
21#include "arccore/concurrency/ParallelLoopOptions.h"
22#include "arccore/concurrency/ForLoopRunInfo.h"
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27namespace Arcane
28{
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32/*
33 * TODO:
34 * - Vérifier les fuites memoires
35 * - BIEN INDIQUER QU'IL NE FAUT PLUS UTILISER UNE TACHE APRES LE WAIT!!!
36 * - Regarder mecanisme pour les exceptions.
37 * - Surcharger les For et Foreach sans specifier le block_size
38 */
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42/*!
43 * \brief Contexte d'éxecution d'une tâche.
44 * \ingroup Concurrency
45 */
46class ARCCORE_CONCURRENCY_EXPORT TaskContext
47{
48 public:
49
50 explicit TaskContext(ITask* atask)
51 : m_task(atask)
52 {}
53
54 public:
55
56 //! Tâche courante.
57 ITask* task() const { return m_task; }
58
59 private:
60
61 ITask* m_task;
62};
63
64/*---------------------------------------------------------------------------*/
65/*---------------------------------------------------------------------------*/
66/*!
67 * \internal
68 * \brief Interface d'un fonctor pour une tâche.
69 * \ingroup Concurrency
70 */
71class ARCCORE_CONCURRENCY_EXPORT ITaskFunctor
72{
73 public:
74
75 virtual ~ITaskFunctor() = default;
76
77 protected:
78
79 ITaskFunctor(const ITaskFunctor&) = default;
80 ITaskFunctor() = default;
81
82 public:
83
84 //! Exécute la méthode associé
85 virtual void executeFunctor(const TaskContext& tc) = 0;
86 virtual ITaskFunctor* clone(void* buffer, Integer size) = 0;
87};
88
89/*---------------------------------------------------------------------------*/
90/*---------------------------------------------------------------------------*/
91/*!
92 * \internal
93 * \brief Fonctor sans argument pour une tâche.
94 * \ingroup Concurrency
95 */
96template <typename InstanceType>
97class TaskFunctor
98: public ITaskFunctor
99{
100 public:
101
102 typedef void (InstanceType::*FunctorType)();
103
104 public:
105
106 TaskFunctor(InstanceType* instance, FunctorType func)
107 : m_instance(instance)
108 , m_function(func)
109 {
110 }
111 TaskFunctor(const TaskFunctor& rhs) = default;
112 TaskFunctor& operator=(const TaskFunctor& rhs) = delete;
113
114 public:
115
116 //! Exécute la méthode associé
117 void executeFunctor(const TaskContext& /*tc*/) override
118 {
119 (m_instance->*m_function)();
120 }
121 ITaskFunctor* clone(void* buffer, Integer size) override
122 {
123 if (sizeof(*this) > (size_t)size)
124 ARCCORE_FATAL("INTERNAL: task functor buffer is too small");
125 return new (buffer) TaskFunctor<InstanceType>(*this);
126 }
127
128 private:
129
130 InstanceType* m_instance;
131 FunctorType m_function;
132};
133
134/*---------------------------------------------------------------------------*/
135/*---------------------------------------------------------------------------*/
136/*!
137 * \internal
138 * \brief Fonctor pour une tâche prenant un TaskContext en argument.
139 * \ingroup Concurrency
140 */
141template <typename InstanceType>
142class TaskFunctorWithContext
143: public ITaskFunctor
144{
145 public:
146
147 typedef void (InstanceType::*FunctorType)(const TaskContext& tc);
148
149 public:
150
151 TaskFunctorWithContext(InstanceType* instance, FunctorType func)
152 : ITaskFunctor()
153 , m_instance(instance)
154 , m_function(func)
155 {
156 }
157
158 public:
159
160 //! Exécute la méthode associé
161 void executeFunctor(const TaskContext& tc) override
162 {
163 (m_instance->*m_function)(tc);
164 }
165 ITaskFunctor* clone(void* buffer, Integer size) override
166 {
167 if (sizeof(*this) > (size_t)size)
168 ARCCORE_FATAL("INTERNAL: task functor buffer is too small");
169 return new (buffer) TaskFunctorWithContext<InstanceType>(*this);
170 }
171
172 private:
173
174 InstanceType* m_instance = nullptr;
175 FunctorType m_function;
176};
177
178/*---------------------------------------------------------------------------*/
179/*---------------------------------------------------------------------------*/
180/*!
181 * \ingroup Concurrency
182 * \brief Interface d'une tâche concourante.
183 *
184 * Les tâches sont créées via TaskFactory.
185 */
186class ARCCORE_CONCURRENCY_EXPORT ITask
187{
188 friend class TaskFactory;
189
190 public:
191
192 virtual ~ITask() = default;
193
194 public:
195
196 /*!
197 * \brief Lance la tâche et bloque jusqu'à ce qu'elle se termine.
198 *
199 * Après appel à cette fonction, la tâche est détruite et ne doit
200 * plus être utilisée.
201 */
202 virtual void launchAndWait() = 0;
203 /*!
204 * \brief Lance les tâches filles \a tasks et bloque
205 * jusqu'à ce qu'elles se terminent.
206 */
207 virtual void launchAndWait(ConstArrayView<ITask*> tasks) = 0;
208
209 protected:
210
211 virtual ITask* _createChildTask(ITaskFunctor* functor) = 0;
212};
213
214/*---------------------------------------------------------------------------*/
215/*---------------------------------------------------------------------------*/
216
217} // End namespace Arcane
218
219/*---------------------------------------------------------------------------*/
220/*---------------------------------------------------------------------------*/
221
222#endif
Vue constante d'un tableau de type T.
virtual void executeFunctor(const TaskContext &tc)=0
Exécute la méthode associé
Interface d'une tâche concourante.
Definition Task.h:187
virtual void launchAndWait()=0
Lance la tâche et bloque jusqu'à ce qu'elle se termine.
virtual void launchAndWait(ConstArrayView< ITask * > tasks)=0
Lance les tâches filles tasks et bloque jusqu'à ce qu'elles se terminent.
Contexte d'éxecution d'une tâche.
Definition Task.h:47
ITask * task() const
Tâche courante.
Definition Task.h:57
void executeFunctor(const TaskContext &tc) override
Exécute la méthode associé
Definition Task.h:161
void executeFunctor(const TaskContext &) override
Exécute la méthode associé
Definition Task.h:117
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
Int32 Integer
Type représentant un entier.