Arcane  4.2.1.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
ParallelFor.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 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/* ParallelFor.h (C) 2000-2026 */
9/* */
10/* Gestion des boucles parallèles. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_BASE_PARALLELFOR_H
13#define ARCCORE_BASE_PARALLELFOR_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arccore/concurrency/TaskFactory.h"
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22namespace Arcane
23{
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
34class ARCCORE_CONCURRENCY_EXPORT ParallelFor1DLoopInfo
35{
36 public:
37
38 using ThatClass = ParallelFor1DLoopInfo;
39
40 public:
41
42 ParallelFor1DLoopInfo(Int32 begin, Int32 size, IRangeFunctor* functor)
43 : m_begin(begin)
44 , m_size(size)
45 , m_functor(functor)
46 {}
47 ParallelFor1DLoopInfo(Int32 begin, Int32 size, IRangeFunctor* functor, const ForLoopRunInfo& run_info)
48 : m_run_info(run_info)
49 , m_begin(begin)
50 , m_size(size)
51 , m_functor(functor)
52 {}
53 ParallelFor1DLoopInfo(Int32 begin, Int32 size, Int32 block_size, IRangeFunctor* functor)
54 : m_begin(begin)
55 , m_size(size)
56 , m_functor(functor)
57 {
59 opts.setGrainSize(block_size);
60 m_run_info.addOptions(opts);
61 }
62
63 public:
64
65 Int32 beginIndex() const { return m_begin; }
66 Int32 size() const { return m_size; }
67 IRangeFunctor* functor() const { return m_functor; }
68 ForLoopRunInfo& runInfo() { return m_run_info; }
69 const ForLoopRunInfo& runInfo() const { return m_run_info; }
70
71 private:
72
73 ForLoopRunInfo m_run_info;
74 Int32 m_begin = 0;
75 Int32 m_size = 0;
76 IRangeFunctor* m_functor = nullptr;
77};
78
79/*---------------------------------------------------------------------------*/
80/*---------------------------------------------------------------------------*/
85template <int RankValue, typename IndexType_, typename LambdaType, typename... ReducerArgs> inline void
87 const ForLoopRunInfo& run_info,
88 const LambdaType& lambda_function,
89 const ReducerArgs&... reducer_args)
90{
91 // Modif Arcane 3.7.9 (septembre 2022)
92 // Effectue une copie pour privatiser au thread courant les valeurs de la lambda.
93 // Cela est nécessaire pour que objets comme les reducers soient bien pris
94 // en compte.
95 // TODO: regarder si on pourrait faire la copie uniquement une fois par thread
96 // si cette copie devient couteuse.
97 // NOTE: A partir de la version 3.12.15 (avril 2024), avec la nouvelle version
98 // des réducteurs (Reduce2), cette privatisation n'est plus utile. Une fois
99 // qu'on aura supprimer les anciennes classes gérant les réductions (Reduce),
100 // on pourra supprimer cette privatisation
101
102 // La boucle finale est toujours avec un index de type 'Int32'
103 // (car ITaskImplementation ne supporte que cela) nous convertissons donc la boucle
104 // si nécessaire.
105 // TODO: Ne faire la conversion que si nécessaire
106 // TODO: Ajouter la prise en charge de la boucle Int64 dans TaskFactory
107 auto final_loop_ranges = ComplexForLoopRanges<RankValue, Int32>::fromOther(loop_ranges);
108 auto xfunc = [&lambda_function, reducer_args...](const ComplexForLoopRanges<RankValue>& sub_bounds) {
109 using Type = typename std::remove_reference<LambdaType>::type;
110 Type private_lambda(lambda_function);
111 arccoreSequentialFor(sub_bounds, private_lambda, reducer_args...);
112 };
113 LambdaMDRangeFunctor<RankValue, decltype(xfunc)> ipf(xfunc);
114 TaskFactory::executeParallelFor(final_loop_ranges, run_info, &ipf);
115}
116
117/*---------------------------------------------------------------------------*/
118/*---------------------------------------------------------------------------*/
119
124template <int RankValue, typename IndexType_, typename LambdaType, typename... ReducerArgs> inline void
126 const ParallelLoopOptions& options,
127 const LambdaType& lambda_function,
128 const ReducerArgs&... reducer_args)
129{
130 arccoreParallelFor(loop_ranges, ForLoopRunInfo(options), lambda_function, reducer_args...);
131}
132
133/*---------------------------------------------------------------------------*/
134/*---------------------------------------------------------------------------*/
139template <int RankValue, typename IndexType_, typename LambdaType, typename... ReducerArgs> inline void
141 const ForLoopRunInfo& run_info,
142 const LambdaType& lambda_function,
143 const ReducerArgs&... reducer_args)
144{
145 ComplexForLoopRanges<RankValue, IndexType_> complex_loop_ranges{ loop_ranges };
146 arccoreParallelFor(complex_loop_ranges, run_info, lambda_function, reducer_args...);
147}
148
149/*---------------------------------------------------------------------------*/
150/*---------------------------------------------------------------------------*/
155template <int RankValue, typename IndexType_, typename LambdaType, typename... ReducerArgs> inline void
157 const ParallelLoopOptions& options,
158 const LambdaType& lambda_function,
159 const ReducerArgs&... reducer_args)
160{
161 ComplexForLoopRanges<RankValue, IndexType_> complex_loop_ranges{ loop_ranges };
162 arccoreParallelFor(complex_loop_ranges, ForLoopRunInfo(options), lambda_function, reducer_args...);
163}
164
165/*---------------------------------------------------------------------------*/
166/*---------------------------------------------------------------------------*/
171template <int RankValue, typename IndexType_, typename LambdaType> inline void
173 const LambdaType& lambda_function)
174{
175 ParallelLoopOptions options;
176 arccoreParallelFor(loop_ranges, options, lambda_function);
177}
178
179/*---------------------------------------------------------------------------*/
180/*---------------------------------------------------------------------------*/
185template <int RankValue, typename IndexType_, typename LambdaType> inline void
187 const LambdaType& lambda_function)
188{
189 ParallelLoopOptions options;
190 ComplexForLoopRanges<RankValue, IndexType_> complex_loop_ranges{ loop_ranges };
191 arccoreParallelFor(complex_loop_ranges, options, lambda_function);
192}
193
194/*---------------------------------------------------------------------------*/
195/*---------------------------------------------------------------------------*/
196
201template <typename LambdaType> inline void
203 const LambdaType& lambda_function)
204{
205 LambdaRangeFunctorT<LambdaType> ipf(lambda_function);
206 ParallelFor1DLoopInfo loop_info(i0, size, &ipf, options);
208}
209
210/*---------------------------------------------------------------------------*/
211/*---------------------------------------------------------------------------*/
212
213} // End namespace Arcane
214
215/*---------------------------------------------------------------------------*/
216/*---------------------------------------------------------------------------*/
217
218#endif
Informations d'exécution d'une boucle.
Interface d'un fonctor sur un interval d'itération.
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:35
Options d'exécution d'une boucle parallèle en multi-thread.
void setGrainSize(Integer v)
Positionne la taille (approximative) d'un intervalle 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
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
void arccoreParallelFor(const ComplexForLoopRanges< RankValue, IndexType_ > &loop_ranges, const ForLoopRunInfo &run_info, const LambdaType &lambda_function, const ReducerArgs &... reducer_args)
Applique en concurrence la fonction lambda lambda_function sur l'intervalle d'itération donné par loo...
Definition ParallelFor.h:86
Int32 Integer
Type représentant un entier.
void arccoreSequentialFor(LoopBoundType< 1, IndexType > bounds, Lambda func, RemainingArgs... remaining_args)
Applique le functor func sur une boucle 1D.
std::int32_t Int32
Type entier signé sur 32 bits.
Type
Type of JSON value.
Definition rapidjson.h:730