Arcane  4.1.12.0
Developer 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
34class ARCCORE_CONCURRENCY_EXPORT TaskFactory
35{
36 friend TaskFactoryInternal;
37
38 public:
39
40 TaskFactory() = delete;
41
42 public:
43
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
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
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
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
97 static void executeParallelFor(Integer begin, Integer size, const ParallelLoopOptions& options, IRangeFunctor* f)
98 {
99 m_impl->executeParallelFor(begin, size, options, f);
100 }
101
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
109 static void executeParallelFor(Integer begin, Integer size, IRangeFunctor* f)
110 {
111 m_impl->executeParallelFor(begin, size, f);
112 }
113
115 static void executeParallelFor(const ParallelFor1DLoopInfo& loop_info)
116 {
117 m_impl->executeParallelFor(loop_info);
118 }
119
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
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
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
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
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
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
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
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
186
196 {
197 return m_impl->currentTaskThreadIndex();
198 }
199
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
228
234
235 public:
236
242 static bool isActive()
243 {
244 return m_impl->isActive();
245 }
246
253 static void printInfos(std::ostream& o)
254 {
255 return m_impl->printInfos(o);
256 }
257
266 ARCCORE_DEPRECATED_REASON("Y2024: This method is internal to Arcane. Do not use it")
267 static IObservable* createThreadObservable();
268
277 ARCCORE_DEPRECATED_REASON("Y2024: This method is internal to Arcane. Do not use it")
278 static IObservable* destroyThreadObservable();
279
285 static void terminate();
286
287 public:
288
290 static void setVerboseLevel(Integer v) { m_verbose_level = v; }
291
293 static Integer verboseLevel() { return m_verbose_level; }
294
295 public:
296
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.
Implementation of a task factory.
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.
Functor for a task taking a TaskContext as an argument.
Definition Task.h:150
Functor without arguments for a task.
Definition Task.h:104
-- 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.