Arcane  v4.1.0.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
Concurrency.h
Aller à la documentation de ce fichier.
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/* Concurrency.h (C) 2000-2025 */
9/* */
10/* Classes gérant la concurrence (tâches, boucles parallèles, ...) */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CORE_CONCURRENCY_H
13#define ARCANE_CORE_CONCURRENCY_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18
19#include "arcane/core/Item.h"
20#include "arcane/core/ItemFunctor.h"
21#include "arcane/core/ItemGroup.h"
22
23#include "arcane/core/materials/MatItem.h"
24
25#include <algorithm>
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30namespace Arcane::impl
31{
32inline Int32
33adaptGrainSize(const ForLoopRunInfo& run_info)
34{
35 const std::optional<ParallelLoopOptions>& options = run_info.options();
36 Int32 grain_size = AbstractItemRangeFunctor::DEFAULT_GRAIN_SIZE;
37 if (options.has_value())
38 if (options.value().hasGrainSize())
39 grain_size = options.value().grainSize();
40 return grain_size;
41}
42} // namespace Arcane::impl
43
44namespace Arcane
45{
46
47/*---------------------------------------------------------------------------*/
48/*---------------------------------------------------------------------------*/
49/*!
50 * \ingroup Concurrency
51 *
52 * \brief Applique en concurrence la méthode \a function de l'instance
53 * \a instance sur la vue \a items_view avec les options \a options.
54 */
55template <typename InstanceType, typename ItemType> inline void
56arcaneParallelForeach(const ItemVectorView& items_view, const ForLoopRunInfo& run_info,
57 InstanceType* instance, void (InstanceType::*function)(ItemVectorViewT<ItemType> items))
58{
59 Int32 grain_size = impl::adaptGrainSize(run_info);
60 ItemRangeFunctorT<InstanceType, ItemType> ipf(items_view, instance, function, grain_size);
61
62 ForLoopRunInfo adapted_run_info(run_info);
63 ParallelLoopOptions loop_opt(run_info.options().value_or(TaskFactory::defaultParallelLoopOptions()));
64 loop_opt.setGrainSize(ipf.blockGrainSize());
65 adapted_run_info.addOptions(loop_opt);
66
67 ParallelFor1DLoopInfo loop_info(0, ipf.nbBlock(), &ipf, adapted_run_info);
69}
70
71/*---------------------------------------------------------------------------*/
72/*---------------------------------------------------------------------------*/
73/*!
74 * \brief Applique en concurrence la fonction lambda \a lambda_function
75 * \a instance sur la vue \a items_view avec les options \a options
76 * \ingroup Concurrency
77 */
78template <typename LambdaType> inline void
79arcaneParallelForeach(const ItemVectorView& items_view, const ForLoopRunInfo& run_info,
80 const LambdaType& lambda_function)
81{
82 Int32 grain_size = impl::adaptGrainSize(run_info);
83 LambdaItemRangeFunctorT<LambdaType> ipf(items_view, lambda_function, grain_size);
84
85 ForLoopRunInfo adapted_run_info(run_info);
86 ParallelLoopOptions loop_opt(run_info.options().value_or(TaskFactory::defaultParallelLoopOptions()));
87 loop_opt.setGrainSize(ipf.blockGrainSize());
88 adapted_run_info.addOptions(loop_opt);
89
90 ParallelFor1DLoopInfo loop_info(0, ipf.nbBlock(), &ipf, adapted_run_info);
92}
93
94/*---------------------------------------------------------------------------*/
95/*---------------------------------------------------------------------------*/
96
97/*!
98 * \ingroup Concurrency
99 *
100 * \brief Applique en concurrence la méthode \a function de l'instance
101 * \a instance sur la vue \a items_view avec les options \a options.
102 */
103template <typename InstanceType, typename ItemType> inline void
105 InstanceType* instance, void (InstanceType::*function)(ItemVectorViewT<ItemType> items))
106{
107 arcaneParallelForeach(items_view, ForLoopRunInfo(options), instance, function);
108}
109
110/*!
111 * \ingroup Concurrency
112 *
113 * \brief Applique en concurrence la méthode \a function de l'instance
114 * \a instance sur le groupe \a items avec les options \a options.
115 */
116template <typename InstanceType, typename ItemType> inline void
117arcaneParallelForeach(const ItemGroup& items, const ForLoopRunInfo& run_info,
118 InstanceType* instance, void (InstanceType::*function)(ItemVectorViewT<ItemType> items))
119{
120 arcaneParallelForeach(items._paddedView(), run_info, instance, function);
121}
122
123/*!
124 * \ingroup Concurrency
125 * \brief Applique en concurrence la méthode \a function de l'instance
126 * \a instance sur la vue \a items_view.
127 */
128template <typename InstanceType, typename ItemType> inline void
130 InstanceType* instance, void (InstanceType::*function)(ItemVectorViewT<ItemType> items))
131{
132 arcaneParallelForeach(items_view, ForLoopRunInfo(), instance, function);
133}
134
135/*!
136 * \ingroup Concurrency
137 * \brief Applique en concurrence la méthode \a function de l'instance
138 * \a instance sur le groupe \a items.
139 */
140template <typename InstanceType, typename ItemType> inline void
142 InstanceType* instance, void (InstanceType::*function)(ItemVectorViewT<ItemType> items))
143{
144 arcaneParallelForeach(items._paddedView(), ForLoopRunInfo(), instance, function);
145}
146
147/*!
148 * \brief Applique en concurrence la fonction lambda \a lambda_function
149 * \a instance sur la vue \a items_view avec les options \a options
150 * \ingroup Concurrency
151 */
152template <typename LambdaType> inline void
154 const LambdaType& lambda_function)
155{
156 arcaneParallelForeach(items_view, ForLoopRunInfo(options), lambda_function);
157}
158
159/*!
160 * \brief Applique en concurrence la fonction lambda \a lambda_function
161 * sur le groupe \a items avec les options \a options
162 * \ingroup Concurrency
163 */
164template <typename LambdaType> inline void
166 const LambdaType& lambda_function)
167{
168 arcaneParallelForeach(items._paddedView(), ForLoopRunInfo(options), lambda_function);
169}
170
171/*!
172 * \ingroup Concurrency
173 * \brief Applique en concurrence la fonction lambda \a lambda_function
174 * \a instance sur la vue \a items_view.
175 */
176template <typename LambdaType> inline void
177arcaneParallelForeach(const ItemVectorView& items_view, const LambdaType& lambda_function)
178{
179 arcaneParallelForeach(items_view, ForLoopRunInfo(), lambda_function);
180}
181
182/*!
183 * \ingroup Concurrency
184 * \brief Applique en concurrence la fonction lambda \a lambda_function
185 * sur le groupe \a items.
186 */
187template <typename LambdaType> inline void
188arcaneParallelForeach(const ItemGroup& items, const LambdaType& lambda_function)
189{
190 arcaneParallelForeach(items._paddedView(), lambda_function);
191}
192
193/*---------------------------------------------------------------------------*/
194/*---------------------------------------------------------------------------*/
195/*!
196 * \ingroup Concurrency
197 * \brief Applique en concurrence la fonction lambda \a lambda_function
198 * sur l'intervalle d'itération [i0,i0+size].
199 */
200template <typename InstanceType> inline void
201arcaneParallelFor(Integer i0, Integer size, InstanceType* itype,
202 void (InstanceType::*lambda_function)(Integer i0, Integer size))
203{
204 RangeFunctorT<InstanceType> ipf(itype, lambda_function);
205 ParallelFor1DLoopInfo loop_info(i0, size, &ipf);
207}
208
209/*!
210 * \ingroup Concurrency
211 * \brief Applique en concurrence la fonction lambda \a lambda_function
212 * sur l'intervalle d'itération [i0,i0+size] avec les options \a options.
213 */
214template <typename LambdaType> inline void
216 const LambdaType& lambda_function)
217{
218 LambdaRangeFunctorT<LambdaType> ipf(lambda_function);
219 ParallelFor1DLoopInfo loop_info(i0, size, &ipf, options);
221}
222
223/*!
224 * \ingroup Concurrency
225 * \brief Applique en concurrence la fonction lambda \a lambda_function
226 * sur l'intervalle d'itération [i0,i0+size] avec les options \a options.
227 */
228template <typename LambdaType> inline void
230 const LambdaType& lambda_function)
231{
232 arcaneParallelFor(i0, size, ForLoopRunInfo(options),lambda_function);
233}
234
235/*!
236 * \brief Applique en concurrence la fonction lambda \a lambda_function
237 * sur l'intervalle d'itération [i0,i0+size]
238 */
239template <typename LambdaType> inline void
240arcaneParallelFor(Integer i0, Integer size, const LambdaType& lambda_function)
241{
242 LambdaRangeFunctorT<LambdaType> ipf(lambda_function);
243 ParallelFor1DLoopInfo loop_info(i0, size, &ipf);
245}
246
247/*---------------------------------------------------------------------------*/
248/*---------------------------------------------------------------------------*/
249/*!
250 * \brief Applique en concurrence la fonction lambda \a lambda_function
251 * \a instance sur les vues des containers \a views avec les options \a options
252 * \ingroup Concurrency
253 */
254template <typename LambdaType, typename... Views> inline void
255arcaneParallelForVa(const ForLoopRunInfo& run_info, const LambdaType& lambda_function, Views... views)
256{
257 // Asserting every views have the size
258 typename std::tuple_element_t<0, std::tuple<Views...>>::size_type sizes[] = {views.size()...};
259 if (!std::all_of(std::begin(sizes), std::end(sizes),[&sizes](auto cur){return cur == sizes[0];}))
260 ARCANE_FATAL("Every views must have the same size");
261
262 LambdaRangeFunctorTVa<LambdaType, Views...> ipf(views..., lambda_function);
263
264 ParallelFor1DLoopInfo loop_info(0, sizes[0], &ipf, run_info);
266}
267
268/*---------------------------------------------------------------------------*/
269/*---------------------------------------------------------------------------*/
270/*!
271 * \brief Implémentation de la concurrence.
272 *
273 * Les méthodes de ce namespace sont obsolètes et doivent être remplacées
274 * par les méthodes équivalentes dans le namespace Arcane.
275 * Par exemple Arcane::Parallel::For() doit être remplacé par Arcane::arcaneParallelFor()
276 * et Arcane::Parallel::Foreach() par Arcane::arcaneParallelForeach().
277 */
278namespace Parallel
279{
280 /*!
281 * \deprecated Use Arcane::arcaneParallelForeach() instead.
282 */
283 template <typename InstanceType, typename ItemType>
284 [[deprecated("Year2021: Use Arcane::arcaneParallelForeach() instead")]] inline void
285 Foreach(const ItemVectorView& items_view, const ParallelLoopOptions& options,
286 InstanceType* instance, void (InstanceType::*function)(ItemVectorViewT<ItemType> items))
287 {
288 ItemRangeFunctorT<InstanceType, ItemType> ipf(items_view, instance, function, options.grainSize());
289 // Recopie \a options et utilise la valeur de 'grain_size' retournée par \a ifp
290 ParallelLoopOptions loop_opt(options);
291 loop_opt.setGrainSize(ipf.blockGrainSize());
292 TaskFactory::executeParallelFor(0, ipf.nbBlock(), loop_opt, &ipf);
293 }
294
295 /*!
296 * \deprecated Use Arcane::arcaneParallelForeach() instead.
297 */
298 template <typename InstanceType, typename ItemType>
299 [[deprecated("Year2021: Use Arcane::arcaneParallelForeach() instead")]] inline void
300 Foreach(const ItemGroup& items, const ParallelLoopOptions& options, InstanceType* instance,
301 void (InstanceType::*function)(ItemVectorViewT<ItemType> items))
302 {
303 Foreach(items._paddedView(), options, instance, function);
304 }
305
306 /*!
307 * \deprecated Use Arcane::arcaneParallelForeach() instead.
308 */
309 template <typename InstanceType, typename ItemType>
310 [[deprecated("Year2021: Use Arcane::arcaneParallelForeach() instead")]] inline void
311 Foreach(const ItemVectorView& items_view, InstanceType* instance, void (InstanceType::*function)(ItemVectorViewT<ItemType> items))
312 {
313 ItemRangeFunctorT<InstanceType, ItemType> ipf(items_view, instance, function);
315 }
316
317 /*!
318 * \deprecated Use Arcane::arcaneParallelForeach() instead.
319 */
320 template <typename InstanceType, typename ItemType>
321 [[deprecated("Year2021: Use Arcane::arcaneParallelForeach() instead")]] inline void
322 Foreach(const ItemGroup& items, InstanceType* instance, void (InstanceType::*function)(ItemVectorViewT<ItemType> items))
323 {
324 Foreach(items._paddedView(), instance, function);
325 }
326
327 /*!
328 * \deprecated Use Arcane::arcaneParallelForeach() instead.
329 */
330 template <typename LambdaType>
331 [[deprecated("Year2021: Use Arcane::arcaneParallelForeach() instead")]] inline void
332 Foreach(const ItemVectorView& items_view, const ParallelLoopOptions& options, const LambdaType& lambda_function)
333 {
334 LambdaItemRangeFunctorT<LambdaType> ipf(items_view, lambda_function, options.grainSize());
335 // Recopie \a options et utilise la valeur de 'grain_size' retournée par \a ifp
336 ParallelLoopOptions loop_opt(options);
337 loop_opt.setGrainSize(ipf.blockGrainSize());
338 TaskFactory::executeParallelFor(0, ipf.nbBlock(), loop_opt, &ipf);
339 }
340
341 /*!
342 * \deprecated Use Arcane::arcaneParallelForeach() instead.
343 */
344 template <typename LambdaType>
345 [[deprecated("Year2021: Use Arcane::arcaneParallelForeach() instead")]] inline void
346 Foreach(const ItemGroup& items, const ParallelLoopOptions& options, const LambdaType& lambda_function)
347 {
348 Foreach(items._paddedView(), options, lambda_function);
349 }
350
351 /*!
352 * \deprecated Use Arcane::arcaneParallelForeach() instead.
353 */
354 template <typename LambdaType>
355 [[deprecated("Year2021: Use Arcane::arcaneParallelForeach() instead")]] inline void
356 Foreach(const ItemVectorView& items_view, const LambdaType& lambda_function)
357 {
358 LambdaItemRangeFunctorT<LambdaType> ipf(items_view, lambda_function);
360 }
361
362 /*!
363 * \deprecated Use Arcane::arcaneParallelForeach() instead.
364 */
365 template <typename LambdaType>
366 [[deprecated("Year2021: Use Arcane::arcaneParallelForeach() instead")]] inline void
367 Foreach(const ItemGroup& items, const LambdaType& lambda_function)
368 {
369 Foreach(items._paddedView(), lambda_function);
370 }
371
372 /*!
373 * \deprecated Utiliser la surcharge For avec ParallelLoopOptions en argument.
374 */
375 template <typename InstanceType> ARCANE_DEPRECATED_122 inline void
376 For(Integer i0, Integer size, Integer grain_size, InstanceType* itype,
377 void (InstanceType::*lambda_function)(Integer i0, Integer size))
378 {
379 RangeFunctorT<InstanceType> ipf(itype, lambda_function);
380 TaskFactory::executeParallelFor(i0, size, grain_size, &ipf);
381 }
382
383 /*!
384 * \deprecated Use Arcane::arcaneParallelFor() instead.
385 */
386 template <typename InstanceType>
387 [[deprecated("Year2021: Use Arcane::arcaneParallelFor() instead")]] inline void
388 For(Integer i0, Integer size, const ParallelLoopOptions& options, InstanceType* itype,
389 void (InstanceType::*lambda_function)(Integer i0, Integer size))
390 {
391 RangeFunctorT<InstanceType> ipf(itype, lambda_function);
392 TaskFactory::executeParallelFor(i0, size, options, &ipf);
393 }
394
395 /*!
396 * \deprecated Utiliser la surcharge For avec ParallelLoopOptions en argument.
397 */
398 template <typename LambdaType> ARCANE_DEPRECATED_122 inline void
399 For(Integer i0, Integer size, Integer grain_size, const LambdaType& lambda_function)
400 {
401 LambdaRangeFunctorT<LambdaType> ipf(lambda_function);
402 TaskFactory::executeParallelFor(i0, size, grain_size, &ipf);
403 }
404
405 /*!
406 * \deprecated Use Arcane::arcaneParallelFor() instead.
407 */
408 template <typename InstanceType>
409 [[deprecated("Year2021: Use Arcane::arcaneParallelFor() instead")]] inline void
410 For(Integer i0, Integer size, InstanceType* itype,
411 void (InstanceType::*lambda_function)(Integer i0, Integer size))
412 {
413 RangeFunctorT<InstanceType> ipf(itype, lambda_function);
414 TaskFactory::executeParallelFor(i0, size, &ipf);
415 }
416
417 /*!
418 * \deprecated Use Arcane::arcaneParallelFor() instead.
419 */
420 template <typename LambdaType>
421 [[deprecated("Year2021: Use Arcane::arcaneParallelFor() instead")]] inline void
422 For(Integer i0, Integer size, const ParallelLoopOptions& options, const LambdaType& lambda_function)
423 {
424 LambdaRangeFunctorT<LambdaType> ipf(lambda_function);
425 TaskFactory::executeParallelFor(i0, size, options, &ipf);
426 }
427
428 /*!
429 * \deprecated Use Arcane::arcaneParallelFor() instead.
430 */
431 template <typename LambdaType>
432 [[deprecated("Year2021: Use Arcane::arcaneParallelFor() instead")]] inline void
433 For(Integer i0, Integer size, const LambdaType& lambda_function)
434 {
435 LambdaRangeFunctorT<LambdaType> ipf(lambda_function);
436 TaskFactory::executeParallelFor(i0, size, &ipf);
437 }
438
439} // End namespace Parallel
440
441/*---------------------------------------------------------------------------*/
442/*---------------------------------------------------------------------------*/
443
444} // End namespace Arcane
445
446/*---------------------------------------------------------------------------*/
447/*---------------------------------------------------------------------------*/
448
449#endif
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
Classes, Types et macros pour gérer la concurrence.
Int32 nbBlock() const
Nombre de blocs.
Definition ItemFunctor.h:52
Int32 blockGrainSize() const
Taille souhaitée d'un intervalle d'itération.
Definition ItemFunctor.h:55
Informations d'exécution d'une boucle.
Groupe d'entités de maillage.
Definition ItemGroup.h:49
ItemVectorView _paddedView() const
Vue sur les entités du groupe avec padding pour la vectorisation.
Definition ItemGroup.cc:592
Fonctor pour itérer sur une liste d'entités.
Definition ItemFunctor.h:79
Vue sur un tableau typé d'entités.
Vue sur un vecteur d'entités.
Fonctor sur un interval d'itération instancié via une lambda fonction.
Fonctor sur un interval d'itération instancié via une lambda fonction.
Fonctor sur un interval d'itération instancié via une lambda fonction.
Caractéristiques d'un boucle 1D multi-thread.
Definition ParallelFor.h:34
Options d'exécution d'une boucle parallèle en multi-thread.
Integer grainSize() const
Taille d'un intervalle d'itération.
void setGrainSize(Integer v)
Positionne la taille (approximative) d'un intervalle d'itération.
Fonctor sur un interval d'itération.
static const ParallelLoopOptions & defaultParallelLoopOptions()
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
void arcaneParallelForeach(const ItemVectorView &items_view, const ForLoopRunInfo &run_info, InstanceType *instance, void(InstanceType::*function)(ItemVectorViewT< ItemType > items))
Applique en concurrence la méthode function de l'instance instance sur la vue items_view avec les opt...
Definition Concurrency.h:56
void arcaneParallelForVa(const ForLoopRunInfo &run_info, const LambdaType &lambda_function, Views... views)
Applique en concurrence la fonction lambda lambda_function instance sur les vues des containers views...
void arcaneParallelFor(Integer i0, Integer size, InstanceType *itype, void(InstanceType::*lambda_function)(Integer i0, Integer size))
Applique en concurrence la fonction lambda lambda_function sur l'intervalle d'itération [i0,...
Implémentation de la concurrence.
void Foreach(const ItemVectorView &items_view, const ParallelLoopOptions &options, InstanceType *instance, void(InstanceType::*function)(ItemVectorViewT< ItemType > items))
ARCANE_DEPRECATED_122 void For(Integer i0, Integer size, Integer grain_size, InstanceType *itype, void(InstanceType::*lambda_function)(Integer i0, Integer size))
-*- 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.