Arcane  v4.1.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-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/* ParallelFor.h (C) 2000-2025 */
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/*---------------------------------------------------------------------------*/
33class ARCCORE_CONCURRENCY_EXPORT ParallelFor1DLoopInfo
34{
35 public:
36
37 using ThatClass = ParallelFor1DLoopInfo;
38
39 public:
40
41 ParallelFor1DLoopInfo(Int32 begin, Int32 size, IRangeFunctor* functor)
42 : m_begin(begin)
43 , m_size(size)
44 , m_functor(functor)
45 {}
46 ParallelFor1DLoopInfo(Int32 begin, Int32 size, IRangeFunctor* functor, const ForLoopRunInfo& run_info)
47 : m_run_info(run_info)
48 , m_begin(begin)
49 , m_size(size)
50 , m_functor(functor)
51 {}
52 ParallelFor1DLoopInfo(Int32 begin, Int32 size, Int32 block_size, IRangeFunctor* functor)
53 : m_begin(begin)
54 , m_size(size)
55 , m_functor(functor)
56 {
58 opts.setGrainSize(block_size);
59 m_run_info.addOptions(opts);
60 }
61
62 public:
63
64 Int32 beginIndex() const { return m_begin; }
65 Int32 size() const { return m_size; }
66 IRangeFunctor* functor() const { return m_functor; }
67 ForLoopRunInfo& runInfo() { return m_run_info; }
68 const ForLoopRunInfo& runInfo() const { return m_run_info; }
69
70 private:
71
72 ForLoopRunInfo m_run_info;
73 Int32 m_begin = 0;
74 Int32 m_size = 0;
75 IRangeFunctor* m_functor = nullptr;
76};
77
78/*---------------------------------------------------------------------------*/
79/*---------------------------------------------------------------------------*/
84template <int RankValue, typename LambdaType, typename... ReducerArgs> inline void
86 const ForLoopRunInfo& run_info,
87 const LambdaType& lambda_function,
88 const ReducerArgs&... reducer_args)
89{
90 // Modif Arcane 3.7.9 (septembre 2022)
91 // Effectue une copie pour privatiser au thread courant les valeurs de la lambda.
92 // Cela est nécessaire pour que objets comme les reducers soient bien pris
93 // en compte.
94 // TODO: regarder si on pourrait faire la copie uniquement une fois par thread
95 // si cette copie devient couteuse.
96 // NOTE: A partir de la version 3.12.15 (avril 2024), avec la nouvelle version
97 // des réducteurs (Reduce2), cette privatisation n'est plus utile. Une fois
98 // qu'on aura supprimer les anciennes classes gérant les réductions (Reduce),
99 // on pourra supprimer cette privatisation
100 auto xfunc = [&lambda_function, reducer_args...](const ComplexForLoopRanges<RankValue>& sub_bounds) {
101 using Type = typename std::remove_reference<LambdaType>::type;
102 Type private_lambda(lambda_function);
103 arccoreSequentialFor(sub_bounds, private_lambda, reducer_args...);
104 };
105 LambdaMDRangeFunctor<RankValue, decltype(xfunc)> ipf(xfunc);
106 TaskFactory::executeParallelFor(loop_ranges, run_info, &ipf);
107}
108
109/*---------------------------------------------------------------------------*/
110/*---------------------------------------------------------------------------*/
115template <int RankValue, typename LambdaType, typename... ReducerArgs> inline void
117 const ParallelLoopOptions& options,
118 const LambdaType& lambda_function,
119 const ReducerArgs&... reducer_args)
120{
121 arccoreParallelFor(loop_ranges, ForLoopRunInfo(options), lambda_function, reducer_args...);
122}
123
124/*---------------------------------------------------------------------------*/
125/*---------------------------------------------------------------------------*/
130template <int RankValue, typename LambdaType, typename... ReducerArgs> inline void
132 const ForLoopRunInfo& run_info,
133 const LambdaType& lambda_function,
134 const ReducerArgs&... reducer_args)
135{
136 ComplexForLoopRanges<RankValue> complex_loop_ranges{ loop_ranges };
137 arccoreParallelFor(complex_loop_ranges, run_info, lambda_function, reducer_args...);
138}
139
140/*---------------------------------------------------------------------------*/
141/*---------------------------------------------------------------------------*/
146template <int RankValue, typename LambdaType, typename... ReducerArgs> inline void
148 const ParallelLoopOptions& options,
149 const LambdaType& lambda_function,
150 const ReducerArgs&... reducer_args)
151{
152 ComplexForLoopRanges<RankValue> complex_loop_ranges{ loop_ranges };
153 arccoreParallelFor(complex_loop_ranges, ForLoopRunInfo(options), lambda_function, reducer_args...);
154}
155
156/*---------------------------------------------------------------------------*/
157/*---------------------------------------------------------------------------*/
162template <int RankValue, typename LambdaType> inline void
164 const LambdaType& lambda_function)
165{
166 ParallelLoopOptions options;
167 arccoreParallelFor(loop_ranges, options, lambda_function);
168}
169
170/*---------------------------------------------------------------------------*/
171/*---------------------------------------------------------------------------*/
176template <int RankValue, typename LambdaType> inline void
178 const LambdaType& lambda_function)
179{
180 ParallelLoopOptions options;
181 ComplexForLoopRanges<RankValue> complex_loop_ranges{ loop_ranges };
182 arccoreParallelFor(complex_loop_ranges, options, lambda_function);
183}
184
185/*---------------------------------------------------------------------------*/
186/*---------------------------------------------------------------------------*/
191template <typename LambdaType> inline void
193 const LambdaType& lambda_function)
194{
195 LambdaRangeFunctorT<LambdaType> ipf(lambda_function);
196 ParallelFor1DLoopInfo loop_info(i0, size, &ipf, options);
198}
199
200/*---------------------------------------------------------------------------*/
201/*---------------------------------------------------------------------------*/
202
203} // End namespace Arcane
204
205/*---------------------------------------------------------------------------*/
206/*---------------------------------------------------------------------------*/
207
208#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:34
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
void arccoreParallelFor(WorkGroupLoopRange bounds, ForLoopRunInfo run_info, const Lambda &func, const RemainingArgs &... remaining_args)
Applique le fonctor func sur une boucle parallèle.
void arccoreSequentialFor(WorkGroupLoopRange bounds, const Lambda &func, const RemainingArgs &... remaining_args)
Applique le fonctor func sur une boucle séqentielle.
-*- 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.
Type
Type of JSON value.
Definition rapidjson.h:730