Skip to content

Commit ef8573b

Browse files
committed
updated cuda
1 parent f314580 commit ef8573b

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

taskflow/cuda/flow_builder.hpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#pragma once
2+
3+
#include "task.hpp"
4+
5+
namespace tf {
6+
7+
class cudaFlow {
8+
9+
public:
10+
11+
cudaFlow(cudaGraph&);
12+
13+
template <typename F, typename... ArgsT>
14+
cudaTask kernel(dim3 grid, dim3 block, size_t shm, F&& func, ArgsT&&... args);
15+
16+
cudaTask copy(void* tgt, void* src, size_t num, size_t size);
17+
18+
private:
19+
20+
cudaGraph& _graph;
21+
};
22+
23+
// Constructor
24+
inline cudaFlow::cudaFlow(cudaGraph& g) : _graph {g} {
25+
}
26+
27+
// Function: kernel
28+
template <typename F, typename... ArgsT>
29+
cudaTask cudaFlow::kernel(dim3 grid, dim3 block, size_t shm, F&& func, ArgsT&&... args) {
30+
31+
void* arguments[sizeof...(ArgsT)] = { &args... };
32+
33+
auto node = _graph.emplace_back();
34+
35+
auto& param = node->_handle.emplace<cudaNode::Kernel>().param;
36+
37+
param.func = (void*)func;
38+
param.gridDim = grid;
39+
param.blockDim = block;
40+
param.sharedMemBytes = shm;
41+
param.kernelParams = arguments;
42+
param.extra = nullptr;
43+
44+
TF_CHECK_CUDA(
45+
::cudaGraphAddKernelNode(&node->_node, _graph._handle, nullptr, 0, &param),
46+
"failed to create a cudaKernel node"
47+
);
48+
49+
return cudaTask(node);
50+
}
51+
52+
// Function: copy
53+
inline cudaTask cudaFlow::copy(void* tgt, void* src, size_t num, size_t size) {
54+
55+
auto node = _graph.emplace_back();
56+
auto& param = node->_handle.emplace<cudaNode::Copy>().param;
57+
58+
param.srcArray = nullptr;
59+
param.srcPos = ::make_cudaPos(0, 0, 0);
60+
param.srcPtr = ::make_cudaPitchedPtr(src, num*size, num, 1);
61+
param.dstArray = nullptr;
62+
param.dstPos = ::make_cudaPos(0, 0, 0);
63+
param.dstPtr = ::make_cudaPitchedPtr(tgt, num*size, num, 1);
64+
param.extent = ::make_cudaExtent(num*size, 1, 1);
65+
param.kind = cudaMemcpyDefault;
66+
67+
TF_CHECK_CUDA(
68+
cudaGraphAddMemcpyNode(&node->_node, _graph._handle, nullptr, 0, &param),
69+
"failed to create a cudaCopy node"
70+
);
71+
72+
return cudaTask(node);
73+
}
74+
75+
} // end of namespace tf -----------------------------------------------------

taskflow/cuda/task.hpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#pragma once
2+
3+
#include "graph.hpp"
4+
5+
namespace tf {
6+
7+
/**
8+
@class Task
9+
10+
@brief task handle to a node in a cudaGraph
11+
*/
12+
class cudaTask {
13+
14+
friend class cudaFlow;
15+
16+
public:
17+
18+
/**
19+
@brief constructs an empty cudaTask
20+
*/
21+
cudaTask() = default;
22+
23+
/**
24+
@brief copy-constructs a cudaTask
25+
*/
26+
cudaTask(const cudaTask&) = default;
27+
28+
/**
29+
@brief copy-assigns a cudaTask
30+
*/
31+
cudaTask& operator = (const cudaTask&) = default;
32+
33+
/**
34+
@brief adds precedence links from this to other tasks
35+
36+
@tparam Ts... parameter pack
37+
38+
@param tasks one or multiple tasks
39+
40+
@return @c *this
41+
*/
42+
template <typename... Ts>
43+
cudaTask& precede(Ts&&... tasks);
44+
45+
/**
46+
@brief assigns a name to the task
47+
48+
@param name a @std_string acceptable string
49+
50+
@return @c *this
51+
*/
52+
cudaTask& name(const std::string& name);
53+
54+
/**
55+
@brief queries the name of the task
56+
*/
57+
const std::string& name() const;
58+
59+
private:
60+
61+
cudaTask(cudaNode*);
62+
63+
cudaNode* _node {nullptr};
64+
65+
template <typename T>
66+
void _precede(T&&);
67+
68+
template <typename T, typename... Ts>
69+
void _precede(T&&, Ts&&...);
70+
};
71+
72+
// Constructor
73+
inline cudaTask::cudaTask(cudaNode* node) : _node {node} {
74+
}
75+
76+
// Function: precede
77+
template <typename... Ts>
78+
cudaTask& cudaTask::precede(Ts&&... tasks) {
79+
_precede(std::forward<Ts>(tasks)...);
80+
return *this;
81+
}
82+
83+
// Procedure: precede
84+
template <typename T>
85+
void cudaTask::_precede(T&& other) {
86+
_node->_precede(other._node);
87+
}
88+
89+
// Procedure: _precede
90+
template <typename T, typename... Ts>
91+
void cudaTask::_precede(T&& task, Ts&&... others) {
92+
_precede(std::forward<T>(task));
93+
_precede(std::forward<Ts>(others)...);
94+
}
95+
96+
// Function: name
97+
inline cudaTask& cudaTask::name(const std::string& name) {
98+
_node->_name = name;
99+
return *this;
100+
}
101+
102+
// Function: name
103+
inline const std::string& cudaTask::name() const {
104+
return _node->_name;
105+
}
106+
107+
} // end of namespace tf -----------------------------------------------------

0 commit comments

Comments
 (0)