-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cuda_graph.hpp
723 lines (581 loc) · 17.5 KB
/
cuda_graph.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
#pragma once
#include "cuda_memory.hpp"
#include "cuda_stream.hpp"
#include "../utility/object_pool.hpp"
#include "../utility/traits.hpp"
#include "../utility/passive_vector.hpp"
namespace tf {
// ----------------------------------------------------------------------------
// cudaGraph_t routines
// ----------------------------------------------------------------------------
/**
@brief gets the memcpy node parameter of a copy task
*/
template <typename T,
std::enable_if_t<!std::is_same_v<T, void>, void>* = nullptr
>
cudaMemcpy3DParms cuda_get_copy_parms(T* tgt, const T* src, size_t num) {
using U = std::decay_t<T>;
cudaMemcpy3DParms p;
p.srcArray = nullptr;
p.srcPos = ::make_cudaPos(0, 0, 0);
p.srcPtr = ::make_cudaPitchedPtr(const_cast<T*>(src), num*sizeof(U), num, 1);
p.dstArray = nullptr;
p.dstPos = ::make_cudaPos(0, 0, 0);
p.dstPtr = ::make_cudaPitchedPtr(tgt, num*sizeof(U), num, 1);
p.extent = ::make_cudaExtent(num*sizeof(U), 1, 1);
p.kind = cudaMemcpyDefault;
return p;
}
/**
@brief gets the memcpy node parameter of a memcpy task (untyped)
*/
inline cudaMemcpy3DParms cuda_get_memcpy_parms(
void* tgt, const void* src, size_t bytes
) {
// Parameters in cudaPitchedPtr
// d - Pointer to allocated memory
// p - Pitch of allocated memory in bytes
// xsz - Logical width of allocation in elements
// ysz - Logical height of allocation in elements
cudaMemcpy3DParms p;
p.srcArray = nullptr;
p.srcPos = ::make_cudaPos(0, 0, 0);
p.srcPtr = ::make_cudaPitchedPtr(const_cast<void*>(src), bytes, bytes, 1);
p.dstArray = nullptr;
p.dstPos = ::make_cudaPos(0, 0, 0);
p.dstPtr = ::make_cudaPitchedPtr(tgt, bytes, bytes, 1);
p.extent = ::make_cudaExtent(bytes, 1, 1);
p.kind = cudaMemcpyDefault;
return p;
}
/**
@brief gets the memset node parameter of a memcpy task (untyped)
*/
inline cudaMemsetParams cuda_get_memset_parms(void* dst, int ch, size_t count) {
cudaMemsetParams p;
p.dst = dst;
p.value = ch;
p.pitch = 0;
//p.elementSize = (count & 1) == 0 ? ((count & 3) == 0 ? 4 : 2) : 1;
//p.width = (count & 1) == 0 ? ((count & 3) == 0 ? count >> 2 : count >> 1) : count;
p.elementSize = 1; // either 1, 2, or 4
p.width = count;
p.height = 1;
return p;
}
/**
@brief gets the memset node parameter of a fill task (typed)
*/
template <typename T, std::enable_if_t<
is_pod_v<T> && (sizeof(T)==1 || sizeof(T)==2 || sizeof(T)==4), void>* = nullptr
>
cudaMemsetParams cuda_get_fill_parms(T* dst, T value, size_t count) {
cudaMemsetParams p;
p.dst = dst;
// perform bit-wise copy
p.value = 0; // crucial
static_assert(sizeof(T) <= sizeof(p.value), "internal error");
std::memcpy(&p.value, &value, sizeof(T));
p.pitch = 0;
p.elementSize = sizeof(T); // either 1, 2, or 4
p.width = count;
p.height = 1;
return p;
}
/**
@brief gets the memset node parameter of a zero task (typed)
*/
template <typename T, std::enable_if_t<
is_pod_v<T> && (sizeof(T)==1 || sizeof(T)==2 || sizeof(T)==4), void>* = nullptr
>
cudaMemsetParams cuda_get_zero_parms(T* dst, size_t count) {
cudaMemsetParams p;
p.dst = dst;
p.value = 0;
p.pitch = 0;
p.elementSize = sizeof(T); // either 1, 2, or 4
p.width = count;
p.height = 1;
return p;
}
/**
@brief queries the number of root nodes in a native CUDA graph
*/
inline size_t cuda_get_graph_num_root_nodes(cudaGraph_t graph) {
size_t num_nodes;
TF_CHECK_CUDA(
cudaGraphGetRootNodes(graph, nullptr, &num_nodes),
"failed to get native graph root nodes"
);
return num_nodes;
}
/**
@brief queries the number of nodes in a native CUDA graph
*/
inline size_t cuda_get_graph_num_nodes(cudaGraph_t graph) {
size_t num_nodes;
TF_CHECK_CUDA(
cudaGraphGetNodes(graph, nullptr, &num_nodes),
"failed to get native graph nodes"
);
return num_nodes;
}
/**
@brief queries the number of edges in a native CUDA graph
*/
inline size_t cuda_get_graph_num_edges(cudaGraph_t graph) {
size_t num_edges;
TF_CHECK_CUDA(
cudaGraphGetEdges(graph, nullptr, nullptr, &num_edges),
"failed to get native graph edges"
);
return num_edges;
}
/**
@brief acquires the nodes in a native CUDA graph
*/
inline std::vector<cudaGraphNode_t> cuda_get_graph_nodes(cudaGraph_t graph) {
size_t num_nodes = cuda_get_graph_num_nodes(graph);
std::vector<cudaGraphNode_t> nodes(num_nodes);
TF_CHECK_CUDA(
cudaGraphGetNodes(graph, nodes.data(), &num_nodes),
"failed to get native graph nodes"
);
return nodes;
}
/**
@brief acquires the root nodes in a native CUDA graph
*/
inline std::vector<cudaGraphNode_t> cuda_get_graph_root_nodes(cudaGraph_t graph) {
size_t num_nodes = cuda_get_graph_num_root_nodes(graph);
std::vector<cudaGraphNode_t> nodes(num_nodes);
TF_CHECK_CUDA(
cudaGraphGetRootNodes(graph, nodes.data(), &num_nodes),
"failed to get native graph nodes"
);
return nodes;
}
/**
@brief acquires the edges in a native CUDA graph
*/
inline std::vector<std::pair<cudaGraphNode_t, cudaGraphNode_t>>
cuda_get_graph_edges(cudaGraph_t graph) {
size_t num_edges = cuda_get_graph_num_edges(graph);
std::vector<cudaGraphNode_t> froms(num_edges), tos(num_edges);
TF_CHECK_CUDA(
cudaGraphGetEdges(graph, froms.data(), tos.data(), &num_edges),
"failed to get native graph edges"
);
std::vector<std::pair<cudaGraphNode_t, cudaGraphNode_t>> edges(num_edges);
for(size_t i=0; i<num_edges; i++) {
edges[i] = std::make_pair(froms[i], tos[i]);
}
return edges;
}
/**
@brief queries the type of a native CUDA graph node
valid type values are:
+ cudaGraphNodeTypeKernel = 0x00
+ cudaGraphNodeTypeMemcpy = 0x01
+ cudaGraphNodeTypeMemset = 0x02
+ cudaGraphNodeTypeHost = 0x03
+ cudaGraphNodeTypeGraph = 0x04
+ cudaGraphNodeTypeEmpty = 0x05
+ cudaGraphNodeTypeWaitEvent = 0x06
+ cudaGraphNodeTypeEventRecord = 0x07
*/
inline cudaGraphNodeType cuda_get_graph_node_type(cudaGraphNode_t node) {
cudaGraphNodeType type;
TF_CHECK_CUDA(
cudaGraphNodeGetType(node, &type), "failed to get native graph node type"
);
return type;
}
/**
@brief convert the type of a native CUDA graph node to a readable string
*/
inline const char* cuda_graph_node_type_to_string(cudaGraphNodeType type) {
switch(type) {
case cudaGraphNodeTypeKernel : return "kernel";
case cudaGraphNodeTypeMemcpy : return "memcpy";
case cudaGraphNodeTypeMemset : return "memset";
case cudaGraphNodeTypeHost : return "host";
case cudaGraphNodeTypeGraph : return "graph";
case cudaGraphNodeTypeEmpty : return "empty";
case cudaGraphNodeTypeWaitEvent : return "event_wait";
case cudaGraphNodeTypeEventRecord : return "event_record";
default : return "undefined";
}
}
/**
@brief dumps a native CUDA graph and all associated child graphs to a DOT format
@tparam T output stream target
@param os target output stream
@param graph native CUDA graph
*/
template <typename T>
void cuda_dump_graph(T& os, cudaGraph_t graph) {
os << "digraph cudaGraph {\n";
std::stack<std::tuple<cudaGraph_t, cudaGraphNode_t, int>> stack;
stack.push(std::make_tuple(graph, nullptr, 1));
int pl = 0;
while(stack.empty() == false) {
auto [graph, parent, l] = stack.top();
stack.pop();
for(int i=0; i<pl-l+1; i++) {
os << "}\n";
}
os << "subgraph cluster_p" << graph << " {\n"
<< "label=\"cudaGraph-L" << l << "\";\n"
<< "color=\"purple\";\n";
auto nodes = cuda_get_graph_nodes(graph);
auto edges = cuda_get_graph_edges(graph);
for(auto& [from, to] : edges) {
os << 'p' << from << " -> " << 'p' << to << ";\n";
}
for(auto& node : nodes) {
auto type = cuda_get_graph_node_type(node);
if(type == cudaGraphNodeTypeGraph) {
cudaGraph_t graph;
TF_CHECK_CUDA(cudaGraphChildGraphNodeGetGraph(node, &graph), "");
stack.push(std::make_tuple(graph, node, l+1));
os << 'p' << node << "["
<< "shape=folder, style=filled, fontcolor=white, fillcolor=purple, "
<< "label=\"cudaGraph-L" << l+1
<< "\"];\n";
}
else {
os << 'p' << node << "[label=\""
<< cuda_graph_node_type_to_string(type)
<< "\"];\n";
}
}
// precede to parent
if(parent != nullptr) {
std::unordered_set<cudaGraphNode_t> successors;
for(const auto& p : edges) {
successors.insert(p.first);
}
for(auto node : nodes) {
if(successors.find(node) == successors.end()) {
os << 'p' << node << " -> " << 'p' << parent << ";\n";
}
}
}
// set the previous level
pl = l;
}
for(int i=0; i<=pl; i++) {
os << "}\n";
}
}
// ----------------------------------------------------------------------------
// cudaGraph class
// ----------------------------------------------------------------------------
// class: cudaGraph
class cudaGraph : public CustomGraphBase {
friend class cudaNode;
friend class cudaTask;
friend class cudaFlowCapturerBase;
friend class cudaFlowCapturer;
friend class cudaFlow;
friend class cudaCapturingBase;
friend class cudaSequentialCapturing;
friend class cudaRoundRobinCapturing;
friend class Taskflow;
friend class Executor;
public:
cudaGraph() = default;
~cudaGraph();
cudaGraph(const cudaGraph&) = delete;
cudaGraph(cudaGraph&&);
cudaGraph& operator = (const cudaGraph&) = delete;
cudaGraph& operator = (cudaGraph&&);
template <typename... ArgsT>
cudaNode* emplace_back(ArgsT&&...);
bool empty() const;
void clear();
void dump(std::ostream&, const void*, const std::string&) const override final;
private:
cudaGraph_t _native_handle {nullptr};
// TODO: nvcc complains deleter of unique_ptr
//std::vector<std::unique_ptr<cudaNode>> _nodes;
std::vector<cudaNode*> _nodes;
};
// ----------------------------------------------------------------------------
// cudaNode class
// ----------------------------------------------------------------------------
// class: cudaNode
// each create_native_node is wrapped in a function to call at runtime
// in order to work with gpu context
class cudaNode {
friend class cudaGraph;
friend class cudaTask;
friend class cudaFlow;
friend class cudaFlowCapturer;
friend class cudaFlowCapturerBase;
friend class cudaCapturingBase;
friend class cudaSequentialCapturing;
friend class cudaRoundRobinCapturing;
friend class Taskflow;
friend class Executor;
// Empty handle
struct Empty {
};
// Host handle
struct Host {
template <typename C>
Host(C&&);
std::function<void()> func;
static void callback(void*);
};
// Memset handle
struct Memset {
};
// Memcpy handle
struct Memcpy {
};
// Kernel handle
struct Kernel {
template <typename F>
Kernel(F&& f);
void* func {nullptr};
};
// Subflow handle
struct Subflow {
cudaGraph graph;
};
// Capture
struct Capture {
template <typename C>
Capture(C&&);
std::function<void(cudaStream_t)> work;
cudaEvent_t event {nullptr};
size_t level;
size_t idx;
};
using handle_t = std::variant<
Empty,
Host,
Memset,
Memcpy,
Kernel,
Subflow,
Capture
>;
constexpr static auto STATE_VISITED = 0x1;
public:
// variant index
constexpr static auto EMPTY = get_index_v<Empty, handle_t>;
constexpr static auto HOST = get_index_v<Host, handle_t>;
constexpr static auto MEMSET = get_index_v<Memset, handle_t>;
constexpr static auto MEMCPY = get_index_v<Memcpy, handle_t>;
constexpr static auto KERNEL = get_index_v<Kernel, handle_t>;
constexpr static auto SUBFLOW = get_index_v<Subflow, handle_t>;
constexpr static auto CAPTURE = get_index_v<Capture, handle_t>;
cudaNode() = delete;
template <typename... ArgsT>
cudaNode(cudaGraph&, ArgsT&&...);
private:
cudaGraph& _graph;
std::string _name;
handle_t _handle;
cudaGraphNode_t _native_handle {nullptr};
std::vector<cudaNode*> _successors;
std::vector<cudaNode*> _dependents;
void _precede(cudaNode*);
//void _set_state(int);
//void _unset_state(int);
//void _clear_state();
//bool _has_state(int) const;
};
// ----------------------------------------------------------------------------
// cudaNode definitions
// ----------------------------------------------------------------------------
// Host handle constructor
template <typename C>
cudaNode::Host::Host(C&& c) : func {std::forward<C>(c)} {
}
// Host callback
inline void cudaNode::Host::callback(void* data) {
static_cast<Host*>(data)->func();
};
// Kernel handle constructor
template <typename F>
cudaNode::Kernel::Kernel(F&& f) :
func {std::forward<F>(f)} {
}
// Capture handle constructor
template <typename C>
cudaNode::Capture::Capture(C&& work) :
work {std::forward<C>(work)} {
}
// Constructor
template <typename... ArgsT>
cudaNode::cudaNode(cudaGraph& graph, ArgsT&&... args) :
_graph {graph},
_handle {std::forward<ArgsT>(args)...} {
}
// Procedure: _precede
inline void cudaNode::_precede(cudaNode* v) {
_successors.push_back(v);
v->_dependents.push_back(this);
// capture node doesn't have the native graph yet
if(_handle.index() != cudaNode::CAPTURE) {
TF_CHECK_CUDA(
::cudaGraphAddDependencies(
_graph._native_handle, &_native_handle, &v->_native_handle, 1
),
"failed to add a preceding link ", this, "->", v
);
}
}
//// Procedure: _set_state
//inline void cudaNode::_set_state(int flag) {
// _state |= flag;
//}
//
//// Procedure: _unset_state
//inline void cudaNode::_unset_state(int flag) {
// _state &= ~flag;
//}
//
//// Procedure: _clear_state
//inline void cudaNode::_clear_state() {
// _state = 0;
//}
//
//// Function: _has_state
//inline bool cudaNode::_has_state(int flag) const {
// return _state & flag;
//}
// ----------------------------------------------------------------------------
// cudaGraph definitions
// ----------------------------------------------------------------------------
// Destructor
inline cudaGraph::~cudaGraph() {
clear();
assert(_native_handle == nullptr);
}
// Move constructor
inline cudaGraph::cudaGraph(cudaGraph&& g) :
_native_handle {g._native_handle},
_nodes {std::move(g._nodes)} {
g._native_handle = nullptr;
assert(g._nodes.empty());
}
// Move assignment
inline cudaGraph& cudaGraph::operator = (cudaGraph&& rhs) {
clear();
// lhs
_native_handle = rhs._native_handle;
_nodes = std::move(rhs._nodes);
assert(rhs._nodes.empty());
// rhs
rhs._native_handle = nullptr;
return *this;
}
// Function: empty
inline bool cudaGraph::empty() const {
return _nodes.empty();
}
// Procedure: clear
inline void cudaGraph::clear() {
for(auto n : _nodes) {
delete n;
}
_nodes.clear();
}
// Function: emplace_back
template <typename... ArgsT>
cudaNode* cudaGraph::emplace_back(ArgsT&&... args) {
//auto node = std::make_unique<cudaNode>(std::forward<ArgsT>(args)...);
//_nodes.emplace_back(std::move(node));
//return _nodes.back().get();
// TODO: object pool
auto node = new cudaNode(std::forward<ArgsT>(args)...);
_nodes.push_back(node);
return node;
}
// Procedure: dump the graph to a DOT format
inline void cudaGraph::dump(
std::ostream& os, const void* root, const std::string& root_name
) const {
// recursive dump with stack
std::stack<std::tuple<const cudaGraph*, const cudaNode*, int>> stack;
stack.push(std::make_tuple(this, nullptr, 1));
int pl = 0;
while(!stack.empty()) {
auto [graph, parent, l] = stack.top();
stack.pop();
for(int i=0; i<pl-l+1; i++) {
os << "}\n";
}
if(parent == nullptr) {
if(root) {
os << "subgraph cluster_p" << root << " {\nlabel=\"cudaFlow: ";
if(root_name.empty()) os << 'p' << root;
else os << root_name;
os << "\";\n" << "color=\"purple\"\n";
}
else {
os << "digraph cudaFlow {\n";
}
}
else {
os << "subgraph cluster_p" << parent << " {\nlabel=\"cudaSubflow: ";
if(parent->_name.empty()) os << 'p' << parent;
else os << parent->_name;
os << "\";\n" << "color=\"purple\"\n";
}
for(auto& v : graph->_nodes) {
os << 'p' << v << "[label=\"";
if(v->_name.empty()) {
os << 'p' << v << "\"";
}
else {
os << v->_name << "\"";
}
switch(v->_handle.index()) {
case cudaNode::KERNEL:
os << " style=\"filled\""
<< " color=\"white\" fillcolor=\"black\""
<< " fontcolor=\"white\""
<< " shape=\"box3d\"";
break;
case cudaNode::SUBFLOW:
stack.push(std::make_tuple(
&std::get<cudaNode::Subflow>(v->_handle).graph, v, l+1)
);
os << " style=\"filled\""
<< " color=\"black\" fillcolor=\"purple\""
<< " fontcolor=\"white\""
<< " shape=\"folder\"";
break;
default:
break;
}
os << "];\n";
for(const auto s : v->_successors) {
os << 'p' << v << " -> " << 'p' << s << ";\n";
}
if(v->_successors.size() == 0) {
if(parent == nullptr) {
if(root) {
os << 'p' << v << " -> p" << root << ";\n";
}
}
else {
os << 'p' << v << " -> p" << parent << ";\n";
}
}
}
// set the previous level
pl = l;
}
for(int i=0; i<pl; i++) {
os << "}\n";
}
}
} // end of namespace tf -----------------------------------------------------