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