Peano
Loading...
Searching...
No Matches
Tasks.cpp
Go to the documentation of this file.
1// This file is part of the Peano project. For conditions of distribution and
2// use, please see the copyright notice at www.peano-framework.org
4#include "tarch/Assertions.h"
6#include "tarch/mpi/Rank.h"
11
14
15#if defined(SharedTBB)
16#include <tbb/task_arena.h>
17#include <tbb/task_group.h>
18#include <oneapi/tbb/task_arena.h>
19#include <oneapi/tbb/concurrent_hash_map.h>
20#include <atomic>
21
22
24
25
26
27namespace {
28 tarch::logging::Log _log( "tarch::multicore::native" );
29
38 class GlobalTaskGroup : public ::oneapi::tbb::task_group {
39 public:
40 GlobalTaskGroup() : task_group() {}
41 ~GlobalTaskGroup() {
42 wait();
43 }
44 };
45
50
57 GlobalTaskGroup globalTaskGroup;
58
59 #if !defined(SharedTBBExtension) and !defined(TBB_USE_TASK_GROUP_PREVIEW)
60 #else
61 static ::oneapi::tbb::dynamic_task_graph taskGraph(globalTaskGroup);
62 #endif
63}
64
65
113void tarch::multicore::native::spawnAndWaitAsTaskLoop(const std::vector<tarch::multicore::Task*>& tasks) {
114 assertion(not tasks.empty());
115
116 ::tbb::task_group taskGroup;
117 for (auto& p : tasks) {
118 taskGroup.run([&tasks, p]() -> void {
119 p->run();
120 delete p;
121 });
122 }
123
124 taskGroup.wait();
125}
126
127
128void tarch::multicore::native::waitForTasks(const std::set<TaskNumber>& inDependencies) {
129 #if !defined(SharedTBBExtension) and !defined(TBB_USE_TASK_GROUP_PREVIEW)
130 ::oneapi::tbb::task_handle* handle = new ::oneapi::tbb::task_handle(
131 globalTaskGroup.defer(
132 []() -> void {
133 }
134 )
135 );
136 #else
137 ::tbb::dynamic_task_graph_node* handle = new ::tbb::dynamic_task_graph_node(
138 []() -> void {
139 }
140 );
141 #endif
142
143 logDebug( "waitForTasks(std::set)", "create new empty TBB task with in-dependencies for the tasks to wait " << ::tarch::multicore::toString(inDependencies) );
144 taskHandleRepository.addDependencies( handle, inDependencies );
145
146 logDebug( "waitForTasks(std::set)", "run new TBB helper task and wait immediately" );
147 #if !defined(SharedTBBExtension) and !defined(TBB_USE_TASK_GROUP_PREVIEW)
148 //globalTaskGroup.run( std::move(*handle) );
149 globalTaskGroup.run_and_wait( handle );
150 #else
151 taskGraph.put( *handle );
152 taskGraph.wait( *handle );
153 #endif
154
155 logDebug( "waitForTasks(std::set)", "TBB helper task complete, so free handle" );
156 delete handle;
157}
158
159
160void tarch::multicore::native::waitForAllTasks() {
161 #if !defined(SharedTBBExtension) and !defined(TBB_USE_TASK_GROUP_PREVIEW)
162 #else
163 taskGraph.wait_for_all();
164 #endif
165
166 globalTaskGroup.wait();
167}
168
169
170void tarch::multicore::native::spawnTask(
171 Task* job,
172 const std::set<TaskNumber>& inDependencies,
173 const TaskNumber& taskNumber
174) {
175 #if !defined(SharedTBBExtension) and !defined(TBB_USE_TASK_GROUP_PREVIEW)
176 ::oneapi::tbb::task_handle* handle = new ::oneapi::tbb::task_handle(
177 globalTaskGroup.defer(
178 [job]() -> void {
179 job->run();
180 delete job;
181 }
182 )
183 );
184 #elif PeanoDebug>0
185 ::tbb::dynamic_task_graph_node* handle = new ::tbb::dynamic_task_graph_node(
186 [job]() -> void {
187 job->run();
188 delete job;
189 },
190 job->toString()
191 );
192 #else
193 ::tbb::dynamic_task_graph_node* handle = new ::tbb::dynamic_task_graph_node(
194 [job]() -> void {
195 job->run();
196 delete job;
197 }
198 );
199 #endif
200
201 taskHandleRepository.addDependency( handle, taskNumber );
202 taskHandleRepository.addDependencies( handle, inDependencies );
203
204 #if !defined(SharedTBBExtension) and !defined(TBB_USE_TASK_GROUP_PREVIEW)
205 //globalTaskGroup.run( std::move(*handle) );
206 globalTaskGroup.run( handle );
207 #else
208 taskGraph.put( *handle );
209 #endif
210
211 if (taskNumber==NoOutDependencies) {
212 delete handle;
213 }
214 else {
215 taskHandleRepository.registerTask(handle, taskNumber);
216 }
217}
218
219
220#endif // defined(SharedTBB)
#define assertion(expr)
#define logDebug(methodName, logMacroMessageStream)
Definition Log.h:50
tarch::logging::Log _log("::")
Log Device.
Definition Log.h:516
Simple utility class around dynamic task graphs to work with integer task numbers.
Represents one node in task graph.
void wait(MPI_Request &request, const std::string &fullQualifiedClassName, const std::string &functionName)
Wrapper around MPI_Wait.
Definition mpi.cpp:61
void spawnAndWaitAsTaskLoop(const std::vector< tarch::multicore::Task * > &tasks)
Map onto native tasking.
Definition Tasks.cpp:113
std::string toString(const std::set< TaskNumber > &taskNumbers)
Construct string representation.
Definition multicore.cpp:33