Arcane  4.1.15.0
Developer 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-2026 */
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
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 // Modified Arcane 3.7.9 (September 2022)
92 // Performs a copy to privatize the lambda values to the current thread.
93 // This is necessary so that objects like reducers are properly taken
94 // into account.
95 // TODO: check if we could perform the copy only once per thread
96 // if this copy becomes costly.
97 // NOTE: Starting from version 3.12.15 (April 2024), with the new version
98 // of reducers (Reduce2), this privatization is no longer useful. Once
99 // we have removed the old classes managing reductions (Reduce),
100 // we can remove this privatization
101
102 // The final loop is always with index of type 'Int32'
103 // (because ITaskImplementation only support that) so we convert the loop
104 // if needed.
105 // TODO: Only do the conversion if needed
106 // TODO: Add support for Int64 loop in 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
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 --
void arccoreParallelFor(const ComplexForLoopRanges< RankValue, IndexType_ > &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:86
Int32 Integer
Type representing an integer.
void arccoreSequentialFor(LoopBoundType< 1, IndexType > bounds, Lambda func, RemainingArgs... remaining_args)
Applies the functor func over a 1D loop.
std::int32_t Int32
Signed integer type of 32 bits.
Type
Type of JSON value.
Definition rapidjson.h:730