Arcane  v4.1.0.0
Documentation développeur
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/*---------------------------------------------------------------------------*/
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/*---------------------------------------------------------------------------*/
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
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
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
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
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
152template <typename LambdaType> inline void
154 const LambdaType& lambda_function)
155{
156 arcaneParallelForeach(items_view, ForLoopRunInfo(options), lambda_function);
157}
158
164template <typename LambdaType> inline void
166 const LambdaType& lambda_function)
167{
168 arcaneParallelForeach(items._paddedView(), ForLoopRunInfo(options), lambda_function);
169}
170
176template <typename LambdaType> inline void
177arcaneParallelForeach(const ItemVectorView& items_view, const LambdaType& lambda_function)
178{
179 arcaneParallelForeach(items_view, ForLoopRunInfo(), lambda_function);
180}
181
187template <typename LambdaType> inline void
188arcaneParallelForeach(const ItemGroup& items, const LambdaType& lambda_function)
189{
190 arcaneParallelForeach(items._paddedView(), lambda_function);
191}
192
193/*---------------------------------------------------------------------------*/
194/*---------------------------------------------------------------------------*/
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
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
228template <typename LambdaType> inline void
230 const LambdaType& lambda_function)
231{
232 arcaneParallelFor(i0, size, ForLoopRunInfo(options),lambda_function);
233}
234
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/*---------------------------------------------------------------------------*/
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/*---------------------------------------------------------------------------*/
278namespace Parallel
279{
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
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
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
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
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
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
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
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
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
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
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
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
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
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:96
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.