Taskflow  3.2.0-Master-Branch
Loading...
Searching...
No Matches
cuda_task.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "cuda_graph.hpp"
4
10namespace tf {
11
12// ----------------------------------------------------------------------------
13// cudaTask Types
14// ----------------------------------------------------------------------------
15
21enum class cudaTaskType : int {
23 EMPTY = 0,
25 HOST,
27 MEMSET,
29 MEMCPY,
31 KERNEL,
33 SUBFLOW,
35 CAPTURE,
38};
39
43constexpr const char* to_string(cudaTaskType type) {
44 switch(type) {
45 case cudaTaskType::EMPTY: return "empty";
46 case cudaTaskType::HOST: return "host";
47 case cudaTaskType::MEMSET: return "memset";
48 case cudaTaskType::MEMCPY: return "memcpy";
49 case cudaTaskType::KERNEL: return "kernel";
50 case cudaTaskType::SUBFLOW: return "subflow";
51 case cudaTaskType::CAPTURE: return "capture";
52 default: return "undefined";
53 }
54}
55
56// ----------------------------------------------------------------------------
57// cudaTask
58// ----------------------------------------------------------------------------
59
65class cudaTask {
66
67 friend class cudaFlow;
68 friend class cudaFlowCapturer;
69 friend class cudaFlowCapturerBase;
70
72
73 public:
74
78 cudaTask() = default;
79
83 cudaTask(const cudaTask&) = default;
84
88 cudaTask& operator = (const cudaTask&) = default;
89
99 template <typename... Ts>
100 cudaTask& precede(Ts&&... tasks);
101
111 template <typename... Ts>
112 cudaTask& succeed(Ts&&... tasks);
113
121 cudaTask& name(const std::string& name);
122
126 const std::string& name() const;
127
131 size_t num_successors() const;
132
136 size_t num_dependents() const;
137
141 bool empty() const;
142
146 cudaTaskType type() const;
147
154 template <typename T>
155 void dump(T& ostream) const;
156
160 template <typename V>
161 void for_each_successor(V&& visitor) const;
162
166 template <typename V>
167 void for_each_dependent(V&& visitor) const;
168
169 private:
170
171 cudaTask(cudaNode*);
172
173 cudaNode* _node {nullptr};
174};
175
176// Constructor
177inline cudaTask::cudaTask(cudaNode* node) : _node {node} {
178}
179
180// Function: precede
181template <typename... Ts>
183 (_node->_precede(tasks._node), ...);
184 return *this;
185}
186
187// Function: succeed
188template <typename... Ts>
190 (tasks._node->_precede(_node), ...);
191 return *this;
192}
193
194// Function: empty
195inline bool cudaTask::empty() const {
196 return _node == nullptr;
197}
198
199// Function: name
200inline cudaTask& cudaTask::name(const std::string& name) {
201 _node->_name = name;
202 return *this;
203}
204
205// Function: name
206inline const std::string& cudaTask::name() const {
207 return _node->_name;
208}
209
210// Function: num_successors
211inline size_t cudaTask::num_successors() const {
212 return _node->_successors.size();
213}
214
215// Function: num_dependents
216inline size_t cudaTask::num_dependents() const {
217 return _node->_dependents.size();
218}
219
220// Function: type
222 switch(_node->_handle.index()) {
223 case cudaNode::EMPTY: return cudaTaskType::EMPTY;
224 case cudaNode::HOST: return cudaTaskType::HOST;
225 case cudaNode::MEMSET: return cudaTaskType::MEMSET;
226 case cudaNode::MEMCPY: return cudaTaskType::MEMCPY;
227 case cudaNode::KERNEL: return cudaTaskType::KERNEL;
228 case cudaNode::SUBFLOW: return cudaTaskType::SUBFLOW;
229 case cudaNode::CAPTURE: return cudaTaskType::CAPTURE;
230 default: return cudaTaskType::UNDEFINED;
231 }
232}
233
234// Procedure: dump
235template <typename T>
236void cudaTask::dump(T& os) const {
237 os << "cudaTask ";
238 if(_node->_name.empty()) os << _node;
239 else os << _node->_name;
240 os << " [type=" << to_string(type()) << ']';
241}
242
243// Function: for_each_successor
244template <typename V>
245void cudaTask::for_each_successor(V&& visitor) const {
246 for(size_t i=0; i<_node->_successors.size(); ++i) {
247 visitor(cudaTask(_node->_successors[i]));
248 }
249}
250
251// Function: for_each_dependent
252template <typename V>
253void cudaTask::for_each_dependent(V&& visitor) const {
254 for(size_t i=0; i<_node->_dependents.size(); ++i) {
255 visitor(cudaTask(_node->_dependents[i]));
256 }
257}
258
259// ----------------------------------------------------------------------------
260// global ostream
261// ----------------------------------------------------------------------------
262
267 ct.dump(os);
268 return os;
269}
270
271} // end of namespace tf -----------------------------------------------------
272
273
274
class to create a cudaFlow graph using stream capture
Definition cuda_capturer.hpp:57
class to create a cudaFlow task dependency graph
Definition cudaflow.hpp:56
class to create a task handle over an internal node of a cudaFlow graph
Definition cuda_task.hpp:65
bool empty() const
queries if the task is associated with a cudaNode
Definition cuda_task.hpp:195
cudaTask(const cudaTask &)=default
copy-constructs a cudaTask
cudaTask & succeed(Ts &&... tasks)
adds precedence links from other tasks to this
Definition cuda_task.hpp:189
size_t num_successors() const
queries the number of successors
Definition cuda_task.hpp:211
cudaTask()=default
constructs an empty cudaTask
cudaTaskType type() const
queries the task type
Definition cuda_task.hpp:221
const std::string & name() const
queries the name of the task
Definition cuda_task.hpp:206
cudaTask & operator=(const cudaTask &)=default
copy-assigns a cudaTask
cudaTask & precede(Ts &&... tasks)
adds precedence links from this to other tasks
Definition cuda_task.hpp:182
void for_each_successor(V &&visitor) const
applies an visitor callable to each successor of the task
Definition cuda_task.hpp:245
void dump(T &ostream) const
dumps the task through an output stream
Definition cuda_task.hpp:236
friend std::ostream & operator<<(std::ostream &, const cudaTask &)
overload of ostream inserter operator for cudaTask
Definition cuda_task.hpp:266
void for_each_dependent(V &&visitor) const
applies an visitor callable to each dependents of the task
Definition cuda_task.hpp:253
size_t num_dependents() const
queries the number of dependents
Definition cuda_task.hpp:216
taskflow namespace
Definition small_vector.hpp:27
@ UNDEFINED
undefined task type (for internal use only)
const char * to_string(TaskType type)
convert a task type to a human-readable string
Definition task.hpp:81
std::ostream & operator<<(std::ostream &os, const Task &task)
overload of ostream inserter operator for cudaTask
Definition task.hpp:626
cudaTaskType
enumeration of all cudaTask types
Definition cuda_task.hpp:21
@ UNDEFINED
undefined task type
@ KERNEL
memory copy task type
@ MEMSET
memory set task type
@ SUBFLOW
subflow (child graph) task type
@ CAPTURE
capture task type
@ HOST
host task type
@ EMPTY
empty task type
@ MEMCPY
memory copy task type