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