Cpp-Taskflow  2.3.1
cuda_task.hpp
1 #pragma once
2 
3 #include "cuda_graph.hpp"
4 
5 namespace tf {
6 
12 template <typename C>
13 constexpr bool is_cudaflow_task_v = is_invocable_r_v<void, C, cudaFlow&>;
14 
20 class cudaTask {
21 
22  friend class cudaFlow;
23 
24  public:
25 
29  cudaTask() = default;
30 
34  cudaTask(const cudaTask&) = default;
35 
39  cudaTask& operator = (const cudaTask&) = default;
40 
50  template <typename... Ts>
51  cudaTask& precede(Ts&&... tasks);
52 
62  template <typename... Ts>
63  cudaTask& succeed(Ts&&... tasks);
64 
72  cudaTask& name(const std::string& name);
73 
77  const std::string& name() const;
78 
82  size_t num_successors() const;
83 
87  bool empty() const;
88 
89  private:
90 
91  cudaTask(cudaNode*);
92 
93  cudaNode* _node {nullptr};
94 
96  template <typename T>
97  void _precede(T&&);
98 
100  template <typename T, typename... Ts>
101  void _precede(T&&, Ts&&...);
102 
104  template <typename T>
105  void _succeed(T&&);
106 
107  // @private
108  template <typename T, typename... Ts>
109  void _succeed(T&&, Ts&&...);
110 };
111 
112 // Constructor
113 inline cudaTask::cudaTask(cudaNode* node) : _node {node} {
114 }
115 
116 // Function: precede
117 template <typename... Ts>
118 cudaTask& cudaTask::precede(Ts&&... tasks) {
119  _precede(std::forward<Ts>(tasks)...);
120  return *this;
121 }
122 
124 // Procedure: precede
125 template <typename T>
126 void cudaTask::_precede(T&& other) {
127  _node->_precede(other._node);
128 }
129 
131 // Procedure: _precede
132 template <typename T, typename... Ts>
133 void cudaTask::_precede(T&& task, Ts&&... others) {
134  _precede(std::forward<T>(task));
135  _precede(std::forward<Ts>(others)...);
136 }
137 
138 // Function: succeed
139 template <typename... Ts>
140 cudaTask& cudaTask::succeed(Ts&&... tasks) {
141  _succeed(std::forward<Ts>(tasks)...);
142  return *this;
143 }
144 
146 // Procedure: _succeed
147 template <typename T>
148 void cudaTask::_succeed(T&& other) {
149  other._node->_precede(_node);
150 }
151 
153 // Procedure: _succeed
154 template <typename T, typename... Ts>
155 void cudaTask::_succeed(T&& task, Ts&&... others) {
156  _succeed(std::forward<T>(task));
157  _succeed(std::forward<Ts>(others)...);
158 }
159 
160 // Function: empty
161 inline bool cudaTask::empty() const {
162  return _node == nullptr;
163 }
164 
165 // Function: name
166 inline cudaTask& cudaTask::name(const std::string& name) {
167  _node->_name = name;
168  return *this;
169 }
170 
171 // Function: name
172 inline const std::string& cudaTask::name() const {
173  return _node->_name;
174 }
175 
176 // Function: num_successors
177 inline size_t cudaTask::num_successors() const {
178  return _node->_successors.size();
179 }
180 
182 //template <typename F, typename... ArgsT>
183 //cudaTask& cudaTask::kernel(
184 // dim3 grid, dim3 block, size_t shm, F&& func, ArgsT&&... args
185 //) {
186 //
187 // using traits = function_traits<F>;
188 //
189 // static_assert(traits::arity == sizeof...(ArgsT), "arity mismatches");
190 //
191 // void* arguments[sizeof...(ArgsT)] = { &args... };
192 //
193 // auto& p = _node->_handle.emplace<cudaNode::Kernel>().param;
194 //
195 // p.func = (void*)func;
196 // p.gridDim = grid;
197 // p.blockDim = block;
198 // p.sharedMemBytes = shm;
199 // p.kernelParams = arguments;
200 // p.extra = nullptr;
201 //
202 // TF_CHECK_CUDA(
203 // ::cudaGraphAddKernelNode(&_node->_node, _node->_graph._handle, nullptr, 0, &p),
204 // "failed to create a cudaKernel node"
205 // );
206 //
207 // return *this;
208 //}
209 //
211 //template <
212 // typename T,
213 // std::enable_if_t<!std::is_same<T, void>::value, void>*
214 //>
215 //cudaTask& cudaTask::copy(T* tgt, T* src, size_t num) {
216 //
217 // using U = std::decay_t<T>;
218 //
219 // auto& p = _node->_handle.emplace<cudaNode::Copy>().param;
220 //
221 // p.srcArray = nullptr;
222 // p.srcPos = ::make_cudaPos(0, 0, 0);
223 // p.srcPtr = ::make_cudaPitchedPtr(src, num*sizeof(U), num, 1);
224 // p.dstArray = nullptr;
225 // p.dstPos = ::make_cudaPos(0, 0, 0);
226 // p.dstPtr = ::make_cudaPitchedPtr(tgt, num*sizeof(U), num, 1);
227 // p.extent = ::make_cudaExtent(num*sizeof(U), 1, 1);
228 // p.kind = cudaMemcpyDefault;
229 //
230 // TF_CHECK_CUDA(
231 // cudaGraphAddMemcpyNode(&_node->_node, _node->_graph._handle, nullptr, 0, &p),
232 // "failed to create a cudaCopy node"
233 // );
234 //
235 // return *this;
236 //}
237 
238 } // end of namespace tf -----------------------------------------------------
cudaTask & precede(Ts &&... tasks)
adds precedence links from this to other tasks
Definition: cuda_task.hpp:118
cudaTask & succeed(Ts &&... tasks)
adds precedence links from other tasks to this
Definition: cuda_task.hpp:140
Definition: error.hpp:9
Building methods of a cuda task dependency graph.
Definition: cuda_flow_builder.hpp:12
bool empty() const
queries if the task is associated with a cudaNode
Definition: cuda_task.hpp:161
handle to a node in a cudaGraph
Definition: cuda_task.hpp:20
cudaTask()=default
constructs an empty cudaTask
size_t num_successors() const
queries the number of successors
Definition: cuda_task.hpp:177
cudaTask & operator=(const cudaTask &)=default
copy-assigns a cudaTask
const std::string & name() const
queries the name of the task
Definition: cuda_task.hpp:172