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 {
36 class GlobalTaskGroup : public ::oneapi::tbb::task_group {
37 public:
38 GlobalTaskGroup() : task_group() {}
39 ~GlobalTaskGroup() {
40 wait();
41 }
42 };
43
48
55 GlobalTaskGroup globalTaskGroup;
56
57 #if !defined(SharedTBBExtension) and !defined(TBB_USE_TASK_GROUP_PREVIEW)
58 #else
59 static ::oneapi::tbb::dynamic_task_graph taskGraph(globalTaskGroup);
60 #endif
61}
62
63
111void tarch::multicore::native::spawnAndWaitAsTaskLoop(const std::vector<tarch::multicore::Task*>& tasks) {
112 assertion(not tasks.empty());
113
114 ::tbb::task_group taskGroup;
115 for (auto& p : tasks) {
116 taskGroup.run([&tasks, p]() -> void {
117 while (p->run()) {
119 }
120 delete p;
121 });
122 }
123
124 taskGroup.wait();
125}
126
127
128
142 Task* myTask, const std::list<tarch::multicore::Task*>& tasksOfSameType, int device
143) {
144 std::list<tarch::multicore::Task*> copyOfTasksOfSameType = tasksOfSameType;
145 globalTaskGroup.run([&, myTask, copyOfTasksOfSameType] {
146 bool stillExecuteLocally = myTask->fuse(copyOfTasksOfSameType, device);
147 if (stillExecuteLocally) {
148 tarch::multicore::native::spawnTask(myTask, std::set<TaskNumber>(), NoOutDependencies);
149 } else {
150 delete myTask;
151 }
152 });
153}
154
155
156void tarch::multicore::native::waitForTasks(const std::set<TaskNumber>& inDependencies) {
157 #if !defined(SharedTBBExtension) and !defined(TBB_USE_TASK_GROUP_PREVIEW)
158 ::oneapi::tbb::task_handle* handle = new ::oneapi::tbb::task_handle(
159 globalTaskGroup.defer(
160 []() -> void {
161 }
162 )
163 );
164 #else
165 ::tbb::dynamic_task_graph_node* handle = new ::tbb::dynamic_task_graph_node(
166 []() -> void {
167 }
168 );
169 #endif
170
171 taskHandleRepository.addDependencies( handle, inDependencies );
172
173 #if !defined(SharedTBBExtension) and !defined(TBB_USE_TASK_GROUP_PREVIEW)
174 //globalTaskGroup.run( std::move(*handle) );
175 globalTaskGroup.run_and_wait( handle );
176 #else
177 taskGraph.put( *handle );
178 taskGraph.wait( *handle );
179 #endif
180
181 delete handle;
182}
183
184
185void tarch::multicore::native::waitForAllTasks() {
186 #if !defined(SharedTBBExtension) and !defined(TBB_USE_TASK_GROUP_PREVIEW)
187 #else
188 taskGraph.wait_for_all();
189 #endif
190
191 globalTaskGroup.wait();
192}
193
194
195void tarch::multicore::native::spawnTask(
196 Task* job,
197 const std::set<TaskNumber>& inDependencies,
198 const TaskNumber& taskNumber
199) {
200 #if !defined(SharedTBBExtension) and !defined(TBB_USE_TASK_GROUP_PREVIEW)
201 ::oneapi::tbb::task_handle* handle = new ::oneapi::tbb::task_handle(
202 globalTaskGroup.defer(
203 [job]() -> void {
204 while (job->run()) {
205 }
206 delete job;
207 }
208 )
209 );
210 #elif PeanoDebug>0
211 ::tbb::dynamic_task_graph_node* handle = new ::tbb::dynamic_task_graph_node(
212 [job]() -> void {
213 while (job->run()) {
214 }
215 delete job;
216 },
217 job->toString()
218 );
219 #else
220 ::tbb::dynamic_task_graph_node* handle = new ::tbb::dynamic_task_graph_node(
221 [job]() -> void {
222 while (job->run()) {
223 }
224 delete job;
225 }
226 );
227 #endif
228
229 taskHandleRepository.addDependency( handle, taskNumber );
230 taskHandleRepository.addDependencies( handle, inDependencies );
231
232 #if !defined(SharedTBBExtension) and !defined(TBB_USE_TASK_GROUP_PREVIEW)
233 //globalTaskGroup.run( std::move(*handle) );
234 globalTaskGroup.run( handle );
235 #else
236 taskGraph.put( *handle );
237 #endif
238
239 if (taskNumber==NoOutDependencies) {
240 delete handle;
241 }
242 else {
243 taskHandleRepository.registerTask(handle, taskNumber);
244 }
245}
246
247
248#endif // defined(SharedTBB)
#define assertion(expr)
static Core & getInstance()
Definition Core.cpp:56
void yield()
Wrapper around backend-specific yield.
Definition Core.cpp:80
Abstract super class for a job.
Definition Task.h:21
virtual bool fuse(const std::list< Task * > &otherTasks, int targetDevice=Host)
Fuse multiple tasks.
Definition Task.cpp:42
Simple utility class around dynamic task graphs to work with integer task numbers.
Represents one node in task graph.
void spawnAndWaitAsTaskLoop(const std::vector< tarch::multicore::Task * > &tasks)
Map onto native tasking.
Definition Tasks.cpp:111
void processFusedTask(Task *myTask, const std::list< tarch::multicore::Task * > &tasksOfSameType, int device)
Process a fused task.
Definition Tasks.cpp:141
constexpr TaskNumber NoOutDependencies
Definition multicore.h:159