Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
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-2025 */
9/* */
10/* Parallel loop management. */
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
28/*!
29 * \brief Characteristics of a multi-thread 1D loop.
30 *
31 * This class allows specifying the options of a loop to be parallelized
32 * in multi-thread mode.
33 */
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/*---------------------------------------------------------------------------*/
81
82/*!
83 * \brief Applies the lambda function \a lambda_function concurrently
84 * over the iteration interval given by \a loop_ranges.
85 */
86template <int RankValue, typename LambdaType, typename... ReducerArgs> inline void
88 const ForLoopRunInfo& run_info,
89 const LambdaType& lambda_function,
90 const ReducerArgs&... reducer_args)
91{
92 // Modified Arcane 3.7.9 (September 2022)
93 // Performs a copy to privatize the lambda values to the current thread.
94 // This is necessary so that objects like reducers are properly taken
95 // into account.
96 // TODO: check if we could perform the copy only once per thread
97 // if this copy becomes costly.
98 // NOTE: Starting from version 3.12.15 (April 2024), with the new version
99 // of reducers (Reduce2), this privatization is no longer useful. Once
100 // we have removed the old classes managing reductions (Reduce),
101 // we can remove this privatization
102 auto xfunc = [&lambda_function, reducer_args...](const ComplexForLoopRanges<RankValue>& sub_bounds) {
103 using Type = typename std::remove_reference<LambdaType>::type;
104 Type private_lambda(lambda_function);
105 arccoreSequentialFor(sub_bounds, private_lambda, reducer_args...);
106 };
107 LambdaMDRangeFunctor<RankValue, decltype(xfunc)> ipf(xfunc);
108 TaskFactory::executeParallelFor(loop_ranges, run_info, &ipf);
109}
110
111/*---------------------------------------------------------------------------*/
112/*---------------------------------------------------------------------------*/
113
114/*!
115 * \brief Applies the lambda function \a lambda_function concurrently
116 * over the iteration interval given by \a loop_ranges.
117 */
118template <int RankValue, typename LambdaType, typename... ReducerArgs> inline void
120 const ParallelLoopOptions& options,
121 const LambdaType& lambda_function,
122 const ReducerArgs&... reducer_args)
123{
124 arccoreParallelFor(loop_ranges, ForLoopRunInfo(options), lambda_function, reducer_args...);
125}
126
127/*---------------------------------------------------------------------------*/
128/*---------------------------------------------------------------------------*/
129
130/*!
131 * \brief Applies the lambda function \a lambda_function concurrently
132 * over the iteration interval given by \a loop_ranges.
133 */
134template <int RankValue, typename LambdaType, typename... ReducerArgs> inline void
136 const ForLoopRunInfo& run_info,
137 const LambdaType& lambda_function,
138 const ReducerArgs&... reducer_args)
139{
140 ComplexForLoopRanges<RankValue> complex_loop_ranges{ loop_ranges };
141 arccoreParallelFor(complex_loop_ranges, run_info, lambda_function, reducer_args...);
142}
143
144/*---------------------------------------------------------------------------*/
145/*---------------------------------------------------------------------------*/
146
147/*!
148 * \brief Applies the lambda function \a lambda_function concurrently
149 * over the iteration interval given by \a loop_ranges.
150 */
151template <int RankValue, typename LambdaType, typename... ReducerArgs> inline void
153 const ParallelLoopOptions& options,
154 const LambdaType& lambda_function,
155 const ReducerArgs&... reducer_args)
156{
157 ComplexForLoopRanges<RankValue> complex_loop_ranges{ loop_ranges };
158 arccoreParallelFor(complex_loop_ranges, ForLoopRunInfo(options), lambda_function, reducer_args...);
159}
160
161/*---------------------------------------------------------------------------*/
162/*---------------------------------------------------------------------------*/
163
164/*!
165 * \brief Applies the lambda function \a lambda_function concurrently
166 * over the iteration interval given by \a loop_ranges.
167 */
168template <int RankValue, typename LambdaType> inline void
170 const LambdaType& lambda_function)
171{
172 ParallelLoopOptions options;
173 arccoreParallelFor(loop_ranges, options, lambda_function);
174}
175
176/*---------------------------------------------------------------------------*/
177/*---------------------------------------------------------------------------*/
178
179/*!
180 * \brief Applies the lambda function \a lambda_function concurrently
181 * over the iteration interval given by \a loop_ranges.
182 */
183template <int RankValue, typename LambdaType> inline void
185 const LambdaType& lambda_function)
186{
187 ParallelLoopOptions options;
188 ComplexForLoopRanges<RankValue> complex_loop_ranges{ loop_ranges };
189 arccoreParallelFor(complex_loop_ranges, options, lambda_function);
190}
191
192/*---------------------------------------------------------------------------*/
193/*---------------------------------------------------------------------------*/
194
195/*!
196 * \brief Applies the lambda function \a lambda_function concurrently
197 * over the iteration interval [i0,i0+size] with the options \a options.
198 */
199template <typename LambdaType> inline void
201 const LambdaType& lambda_function)
202{
203 LambdaRangeFunctorT<LambdaType> ipf(lambda_function);
204 ParallelFor1DLoopInfo loop_info(i0, size, &ipf, options);
206}
207
208/*---------------------------------------------------------------------------*/
209/*---------------------------------------------------------------------------*/
210
211} // End namespace Arcane
212
213/*---------------------------------------------------------------------------*/
214/*---------------------------------------------------------------------------*/
215
216#endif
Loop execution information.
Interface of a functor on an iteration interval.
Functor over an iteration interval instantiated via a lambda function.
Functor over an iteration interval instantiated via a lambda function.
Characteristics of a multi-thread 1D loop.
Definition ParallelFor.h:35
Execution options for a parallel loop in multi-threading.
void setGrainSize(Integer v)
Sets the size (approximate) of an iteration interval.
static const ParallelLoopOptions & defaultParallelLoopOptions()
Default parallel loop execution options.
static void executeParallelFor(Integer begin, Integer size, const ParallelLoopOptions &options, IRangeFunctor *f)
Executes the functor f in parallel.
Definition TaskFactory.h:97
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
void arccoreSequentialFor(LoopBoundType< 1, IndexType > bounds, const Lambda &func, RemainingArgs... remaining_args)
Applies the functor func over a 1D loop.
void arccoreParallelFor(const ComplexForLoopRanges< RankValue > &loop_ranges, const ForLoopRunInfo &run_info, const LambdaType &lambda_function, const ReducerArgs &... reducer_args)
Applies the lambda function lambda_function concurrently over the iteration interval given by loop_ra...
Definition ParallelFor.h:87
std::int32_t Int32
Signed integer type of 32 bits.