Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
TaskFactory.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/* TaskFactory.h (C) 2000-2025 */
9/* */
10/* Factory for tasks. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_CONCURRENCY_TASKFACTORY_H
13#define ARCCORE_CONCURRENCY_TASKFACTORY_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arccore/base/ConcurrencyBase.h"
18#include "arccore/concurrency/Task.h"
19#include "arccore/concurrency/ITaskImplementation.h"
20
21/*---------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace Arcane
25{
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30/*!
31 * \ingroup Concurrency
32 * \brief Factory for tasks.
33 */
34class ARCCORE_CONCURRENCY_EXPORT TaskFactory
35{
36 friend TaskFactoryInternal;
37
38 public:
39
40 TaskFactory() = delete;
41
42 public:
43
44 /*!
45 * \brief Creates a task.
46 * During execution, the task will call the method \a function via
47 * the instance \a instance.
48 */
49 template <typename InstanceType> static ITask*
50 createTask(InstanceType* instance, void (InstanceType::*function)(const TaskContext& tc))
51 {
52 TaskFunctorWithContext<InstanceType> functor(instance, function);
53 return m_impl->createRootTask(&functor);
54 }
55
56 /*!
57 * \brief Creates a task.
58 * During execution, the task will call the method \a function via
59 * the instance \a instance.
60 */
61 template <typename InstanceType> static ITask*
62 createTask(InstanceType* instance, void (InstanceType::*function)())
63 {
64 TaskFunctor<InstanceType> functor(instance, function);
65 return m_impl->createRootTask(&functor);
66 }
67
68 /*!
69 * \brief Creates a child task.
70 *
71 * During execution, the task will call the method \a function via
72 * the instance \a instance.
73 */
74 template <typename InstanceType> static ITask*
75 createChildTask(ITask* parent_task, InstanceType* instance, void (InstanceType::*function)(const TaskContext& tc))
76 {
77 ARCCORE_CHECK_POINTER(parent_task);
78 TaskFunctorWithContext<InstanceType> functor(instance, function);
79 return parent_task->_createChildTask(&functor);
80 }
81
82 /*!
83 * \brief Creates a child task.
84 *
85 * During execution, the task will call the method \a function via
86 * the instance \a instance.
87 */
88 template <typename InstanceType> static ITask*
89 createChildTask(ITask* parent_task, InstanceType* instance, void (InstanceType::*function)())
90 {
91 ARCCORE_CHECK_POINTER(parent_task);
92 TaskFunctor<InstanceType> functor(instance, function);
93 return parent_task->_createChildTask(&functor);
94 }
95
96 //! Executes the functor \a f in parallel.
97 static void executeParallelFor(Integer begin, Integer size, const ParallelLoopOptions& options, IRangeFunctor* f)
98 {
99 m_impl->executeParallelFor(begin, size, options, f);
100 }
101
102 //! Executes the functor \a f in parallel.
103 static void executeParallelFor(Integer begin, Integer size, Integer block_size, IRangeFunctor* f)
104 {
105 m_impl->executeParallelFor(begin, size, block_size, f);
106 }
107
108 //! Executes the functor \a f in parallel.
109 static void executeParallelFor(Integer begin, Integer size, IRangeFunctor* f)
110 {
111 m_impl->executeParallelFor(begin, size, f);
112 }
113
114 //! Executes the loop \a loop_info in parallel.
115 static void executeParallelFor(const ParallelFor1DLoopInfo& loop_info)
116 {
117 m_impl->executeParallelFor(loop_info);
118 }
119
120 //! Executes a simple loop
121 static void executeParallelFor(const ComplexForLoopRanges<1>& loop_ranges,
122 const ParallelLoopOptions& options,
123 IMDRangeFunctor<1>* functor)
124 {
125 m_impl->executeParallelFor(loop_ranges, ForLoopRunInfo(options), functor);
126 }
127
128 //! Executes a simple loop
129 static void executeParallelFor(const ComplexForLoopRanges<1>& loop_ranges,
130 const ForLoopRunInfo& run_info,
131 IMDRangeFunctor<1>* functor)
132 {
133 m_impl->executeParallelFor(loop_ranges, run_info, functor);
134 }
135
136 //! Executes a 2D loop
137 static void executeParallelFor(const ComplexForLoopRanges<2>& loop_ranges,
138 const ParallelLoopOptions& options,
139 IMDRangeFunctor<2>* functor)
140 {
141 m_impl->executeParallelFor(loop_ranges, ForLoopRunInfo(options), functor);
142 }
143
144 //! Executes a 2D loop
145 static void executeParallelFor(const ComplexForLoopRanges<2>& loop_ranges,
146 const ForLoopRunInfo& run_info,
147 IMDRangeFunctor<2>* functor)
148 {
149 m_impl->executeParallelFor(loop_ranges, run_info, functor);
150 }
151
152 //! Executes a 3D loop
153 static void executeParallelFor(const ComplexForLoopRanges<3>& loop_ranges,
154 const ParallelLoopOptions& options,
155 IMDRangeFunctor<3>* functor)
156 {
157 m_impl->executeParallelFor(loop_ranges, ForLoopRunInfo(options), functor);
158 }
159
160 //! Executes a 3D loop
161 static void executeParallelFor(const ComplexForLoopRanges<3>& loop_ranges,
162 const ForLoopRunInfo& run_info,
163 IMDRangeFunctor<3>* functor)
164 {
165 m_impl->executeParallelFor(loop_ranges, run_info, functor);
166 }
167
168 //! Executes a 4D loop
169 static void executeParallelFor(const ComplexForLoopRanges<4>& loop_ranges,
170 const ParallelLoopOptions& options,
171 IMDRangeFunctor<4>* functor)
172 {
173 m_impl->executeParallelFor(loop_ranges, ForLoopRunInfo(options), functor);
174 }
175
176 //! Executes a 4D loop
177 static void executeParallelFor(const ComplexForLoopRanges<4>& loop_ranges,
178 const ForLoopRunInfo& run_info,
179 IMDRangeFunctor<4>* functor)
180 {
181 m_impl->executeParallelFor(loop_ranges, run_info, functor);
182 }
183
184 //! Maximum number of threads used to manage tasks.
186
187 /*!
188 * \brief Index (between 0 and nbAllowedThread()-1) of the thread executing
189 * the current task.
190 *
191 * For performance reasons, it is preferable to call this method
192 * as little as possible. Ideally, it should only be called at the beginning
193 * of the task execution and then the returned value should be used.
194 */
196 {
197 return m_impl->currentTaskThreadIndex();
198 }
199
200 /*!
201 * \brief Index (between 0 and nbAllowedThread()-1) of the current task.
202 *
203 * This index is the same as currentTaskThreadIndex() except when we are in
204 * an executeParallelFor() with deterministic partitioning
205 * (ParallelLoopOptions::Partitioner::Deterministic).
206 * In the latter case, the task number is assigned deterministically, depending
207 * only on the number of threads allocated for the task and
208 * ParallelLoopOptions::grainSize().
209 *
210 * If the current thread is not executing a task associated with this implementation,
211 * it returns (-1).
212 */
214 {
215 return m_impl->currentTaskIndex();
216 }
217
218 public:
219
220 // TODO: mark these two methods as obsolete and indicate using
221 // those from ConcurrencyBase instead.
222
223 //! Sets the default parallel loop execution options
228
229 //! Default parallel loop execution options
234
235 public:
236
237 /*!
238 * \brief Indicates whether tasks are active.
239 * Tasks are active if an implementation is available and if the requested number
240 * of threads is strictly greater than 1.
241 */
242 static bool isActive()
243 {
244 return m_impl->isActive();
245 }
246
247 /*!
248 * \brief Prints information about the implementation.
249 *
250 * The information is for example the version number or the name
251 * of the implementation.
252 */
253 static void printInfos(std::ostream& o)
254 {
255 return m_impl->printInfos(o);
256 }
257
258 /*!
259 * \brief Observable called when a thread is created for a task.
260 *
261 * \warning The observable instance is created during the first call
262 * to this method. It is therefore not thread-safe. Similarly,
263 * modifying the observable (adding/removing observers)
264 * is not thread-safe.
265 */
266 ARCCORE_DEPRECATED_REASON("Y2024: This method is internal to Arcane. Do not use it")
267 static IObservable* createThreadObservable();
268
269 /*!
270 * \brief Observable called when a thread is destroyed for a task.
271 *
272 * \warning The observable instance is created during the first call
273 * to this method. It is therefore not thread-safe. Similarly,
274 * modifying the observable (adding/removing observers)
275 * is not thread-safe.
276 */
277 ARCCORE_DEPRECATED_REASON("Y2024: This method is internal to Arcane. Do not use it")
278 static IObservable* destroyThreadObservable();
279
280 /*!
281 * \internal
282 * \brief Indicates that threads will no longer be used.
283 * This method must not be called when tasks are active.
284 */
285 static void terminate();
286
287 public:
288
289 //! Sets the verbosity level (0 for no output, which is the default)
290 static void setVerboseLevel(Integer v) { m_verbose_level = v; }
291
292 //! Verbosity level
293 static Integer verboseLevel() { return m_verbose_level; }
294
295 public:
296
297 //! \internal
298 ARCCORE_DEPRECATED_REASON("Y2024: This method is internal to Arcane. "
299 "Use TaskFactoryInternal::setImplementation() instead")
300 static void _internalSetImplementation(ITaskImplementation* task_impl);
301
302 private:
303
304 static ITaskImplementation* m_impl;
305 static Int32 m_verbose_level;
306};
307
308/*---------------------------------------------------------------------------*/
309/*---------------------------------------------------------------------------*/
310
311} // End namespace Arcane
312
313/*---------------------------------------------------------------------------*/
314/*---------------------------------------------------------------------------*/
315
316#endif
#define ARCCORE_CHECK_POINTER(ptr)
Macro that returns the pointer ptr if it is not null or throws an exception if it is null.
static void setDefaultParallelLoopOptions(const ParallelLoopOptions &v)
Sets the default execution values for a parallel loop.
static Int32 maxAllowedThread()
Maximum number of allowed threads for multi-threading.
static const ParallelLoopOptions & defaultParallelLoopOptions()
Default execution values for a parallel loop.
Loop execution information.
Interface of a functor on a multi-dimensional iteration interval of dimension RankValue.
Interface of a functor on an iteration interval.
Interface for a concurrent task.
Definition Task.h:194
Characteristics of a multi-thread 1D loop.
Definition ParallelFor.h:35
Execution options for a parallel loop in multi-threading.
Execution context of a task.
Definition Task.h:50
static Int32 currentTaskIndex()
Index (between 0 and nbAllowedThread()-1) of the current task.
static void executeParallelFor(const ParallelFor1DLoopInfo &loop_info)
Executes the loop loop_info in parallel.
static void setVerboseLevel(Integer v)
Sets the verbosity level (0 for no output, which is the default).
static Int32 nbAllowedThread()
Maximum number of threads used to manage tasks.
static void executeParallelFor(Integer begin, Integer size, IRangeFunctor *f)
Executes the functor f in parallel.
static ITask * createTask(InstanceType *instance, void(InstanceType::*function)())
Creates a task. During execution, the task will call the method function via the instance instance.
Definition TaskFactory.h:62
static const ParallelLoopOptions & defaultParallelLoopOptions()
Default parallel loop execution options.
static void executeParallelFor(const ComplexForLoopRanges< 1 > &loop_ranges, const ParallelLoopOptions &options, IMDRangeFunctor< 1 > *functor)
Executes a simple loop.
static void executeParallelFor(const ComplexForLoopRanges< 1 > &loop_ranges, const ForLoopRunInfo &run_info, IMDRangeFunctor< 1 > *functor)
Executes a simple loop.
static void executeParallelFor(const ComplexForLoopRanges< 2 > &loop_ranges, const ForLoopRunInfo &run_info, IMDRangeFunctor< 2 > *functor)
Executes a 2D loop.
static Integer verboseLevel()
Verbosity level.
static void executeParallelFor(const ComplexForLoopRanges< 4 > &loop_ranges, const ParallelLoopOptions &options, IMDRangeFunctor< 4 > *functor)
Executes a 4D loop.
static void executeParallelFor(const ComplexForLoopRanges< 2 > &loop_ranges, const ParallelLoopOptions &options, IMDRangeFunctor< 2 > *functor)
Executes a 2D loop.
static void setDefaultParallelLoopOptions(const ParallelLoopOptions &v)
Sets the 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
static void executeParallelFor(Integer begin, Integer size, Integer block_size, IRangeFunctor *f)
Executes the functor f in parallel.
static void executeParallelFor(const ComplexForLoopRanges< 3 > &loop_ranges, const ParallelLoopOptions &options, IMDRangeFunctor< 3 > *functor)
Executes a 3D loop.
static ITask * createChildTask(ITask *parent_task, InstanceType *instance, void(InstanceType::*function)(const TaskContext &tc))
Creates a child task.
Definition TaskFactory.h:75
static Int32 currentTaskThreadIndex()
Index (between 0 and nbAllowedThread()-1) of the thread executing the current task.
static ITask * createChildTask(ITask *parent_task, InstanceType *instance, void(InstanceType::*function)())
Creates a child task.
Definition TaskFactory.h:89
static bool isActive()
Indicates whether tasks are active. Tasks are active if an implementation is available and if the req...
static ITask * createTask(InstanceType *instance, void(InstanceType::*function)(const TaskContext &tc))
Creates a task. During execution, the task will call the method function via the instance instance.
Definition TaskFactory.h:50
static void executeParallelFor(const ComplexForLoopRanges< 4 > &loop_ranges, const ForLoopRunInfo &run_info, IMDRangeFunctor< 4 > *functor)
Executes a 4D loop.
static void printInfos(std::ostream &o)
Prints information about the implementation.
static void executeParallelFor(const ComplexForLoopRanges< 3 > &loop_ranges, const ForLoopRunInfo &run_info, IMDRangeFunctor< 3 > *functor)
Executes a 3D loop.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
std::int32_t Int32
Signed integer type of 32 bits.