File size: 9,809 Bytes
d1be154
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#pragma once

#include "cuda_error.hpp"

/**
@file cuda_stream.hpp
@brief CUDA stream utilities include file
*/

namespace tf {


// ----------------------------------------------------------------------------
// cudaEventBase
// ----------------------------------------------------------------------------
  
/**
@class cudaEventCreator

@brief class to create functors that construct CUDA events
*/
class cudaEventCreator {
    
  public:

  /**
  @brief creates a new `cudaEvent_t` object using `cudaEventCreate`
  */
  cudaEvent_t operator () () const {
    cudaEvent_t event;
    TF_CHECK_CUDA(cudaEventCreate(&event), "failed to create a CUDA event");
    return event;
  }
  
  /**
  @brief creates a new `cudaEvent_t` object using `cudaEventCreate` with the given `flag`
  */
  cudaEvent_t operator () (unsigned int flag) const {
    cudaEvent_t event;
    TF_CHECK_CUDA(
      cudaEventCreateWithFlags(&event, flag),
      "failed to create a CUDA event with flag=", flag
    );
    return event;
  }
  
  /**
  @brief returns the given `cudaEvent_t` object
  */
  cudaEvent_t operator () (cudaEvent_t event) const {
    return event;
  }
};

/**
@class cudaEventDeleter

@brief class to create a functor that deletes a CUDA event
*/
class cudaEventDeleter {
  public:
  /**
  @brief deletes the given `cudaEvent_t` object using `cudaEventDestroy`
  */
  void operator () (cudaEvent_t event) const {
    cudaEventDestroy(event);
  }
};

/**
@class cudaEventBase

@brief class to create a CUDA event with unique ownership

@tparam Creator functor to create the stream (used in constructor)
@tparam Deleter functor to delete the stream (used in destructor)

The `cudaEventBase` class encapsulates a `cudaEvent_t` using `std::unique_ptr`, ensuring that
CUDA events are properly created and destroyed with a unique ownership.
*/
template <typename Creator, typename Deleter>
class cudaEventBase : public std::unique_ptr<std::remove_pointer_t<cudaEvent_t>, Deleter> {

  static_assert(std::is_pointer_v<cudaEvent_t>, "cudaEvent_t is not a pointer type");

  public:
  
  /**
  @brief base type for the underlying unique pointer

  This alias provides a shorthand for the underlying `std::unique_ptr` type that manages
  CUDA event resources with an associated deleter.
  */
  using base_type = std::unique_ptr<std::remove_pointer_t<cudaEvent_t>, Deleter>;

  /**
  @brief constructs a `cudaEvent` object by passing the given arguments to the event creator

  Constructs a `cudaEvent` object by passing the given arguments to the event creator

  @param args arguments to pass to the event creator
  */
  template <typename... ArgsT>
  explicit cudaEventBase(ArgsT&& ... args) : base_type(
    Creator{}(std::forward<ArgsT>(args)...), Deleter()
  ) {
  }  
  
  /**
  @brief constructs a `cudaEvent` from the given rhs using move semantics
  */
  cudaEventBase(cudaEventBase&&) = default;

  /**
  @brief assign the rhs to `*this` using move semantics
  */
  cudaEventBase& operator = (cudaEventBase&&) = default;
  
  private:

  cudaEventBase(const cudaEventBase&) = delete;
  cudaEventBase& operator = (const cudaEventBase&) = delete;
};

/**
@brief default smart pointer type to manage a `cudaEvent_t` object with unique ownership
*/
using cudaEvent = cudaEventBase<cudaEventCreator, cudaEventDeleter>;

// ----------------------------------------------------------------------------
// cudaStream
// ----------------------------------------------------------------------------

/**
@class cudaStreamCreator 

@brief class to create functors that construct CUDA streams
*/
class cudaStreamCreator {
  
  public:

  /**
  @brief constructs a new `cudaStream_t` object using `cudaStreamCreate`
  */
  cudaStream_t operator () () const {
    cudaStream_t stream;
    TF_CHECK_CUDA(cudaStreamCreate(&stream), "failed to create a CUDA stream");
    return stream;
  }
  
  /**
  @brief returns the given `cudaStream_t` object
  */
  cudaStream_t operator () (cudaStream_t stream) const {
    return stream;
  }
};

/**
@class cudaStreamDeleter

@brief class to create a functor that deletes a CUDA stream
*/
class cudaStreamDeleter {

  public:

  /**
  @brief deletes the given `cudaStream_t` object
  */
  void operator () (cudaStream_t stream) const {
    cudaStreamDestroy(stream);
  }
};

/**
@class cudaStreamBase

@brief class to create a CUDA stream with unique ownership

@tparam Creator functor to create the stream (used in constructor)
@tparam Deleter functor to delete the stream (used in destructor)

The `cudaStream` class encapsulates a `cudaStream_t` using `std::unique_ptr`, ensuring that
CUDA events are properly created and destroyed with a unique ownership.
*/
template <typename Creator, typename Deleter>
class cudaStreamBase : public std::unique_ptr<std::remove_pointer_t<cudaStream_t>, Deleter> {

  static_assert(std::is_pointer_v<cudaStream_t>, "cudaStream_t is not a pointer type");
  
  public:
  
  /**
  @brief base type for the underlying unique pointer

  This alias provides a shorthand for the underlying `std::unique_ptr` type that manages
  CUDA stream resources with an associated deleter.
  */
  using base_type = std::unique_ptr<std::remove_pointer_t<cudaStream_t>, Deleter>;

  /**
  @brief constructs a `cudaStream` object by passing the given arguments to the stream creator

  Constructs a `cudaStream` object by passing the given arguments to the stream creator

  @param args arguments to pass to the stream creator
  */
  template <typename... ArgsT>
  explicit cudaStreamBase(ArgsT&& ... args) : base_type(
    Creator{}(std::forward<ArgsT>(args)...), Deleter()
  ) {
  }  
  
  /**
  @brief constructs a `cudaStream` from the given rhs using move semantics
  */
  cudaStreamBase(cudaStreamBase&&) = default;

  /**
  @brief assign the rhs to `*this` using move semantics
  */
  cudaStreamBase& operator = (cudaStreamBase&&) = default;
  
  /**
  @brief synchronizes the associated stream

  Equivalently calling @c cudaStreamSynchronize to block 
  until this stream has completed all operations.
  */
  cudaStreamBase& synchronize() {
    TF_CHECK_CUDA(
      cudaStreamSynchronize(this->get()), "failed to synchronize a CUDA stream"
    );
    return *this;
  }
  
  /**
  @brief begins graph capturing on the stream

  When a stream is in capture mode, all operations pushed into the stream 
  will not be executed, but will instead be captured into a graph, 
  which will be returned via cudaStream::end_capture. 

  A thread's mode can be one of the following:
  + @c cudaStreamCaptureModeGlobal: This is the default mode. 
    If the local thread has an ongoing capture sequence that was not initiated 
    with @c cudaStreamCaptureModeRelaxed at @c cuStreamBeginCapture, 
    or if any other thread has a concurrent capture sequence initiated with 
    @c cudaStreamCaptureModeGlobal, this thread is prohibited from potentially 
    unsafe API calls.

  + @c cudaStreamCaptureModeThreadLocal: If the local thread has an ongoing capture 
    sequence not initiated with @c cudaStreamCaptureModeRelaxed, 
    it is prohibited from potentially unsafe API calls. 
    Concurrent capture sequences in other threads are ignored.

  + @c cudaStreamCaptureModeRelaxed: The local thread is not prohibited 
    from potentially unsafe API calls. Note that the thread is still prohibited 
    from API calls which necessarily conflict with stream capture, for example, 
    attempting @c cudaEventQuery on an event that was last recorded 
    inside a capture sequence.
  */
  void begin_capture(cudaStreamCaptureMode m = cudaStreamCaptureModeGlobal) const {
    TF_CHECK_CUDA(
      cudaStreamBeginCapture(this->get(), m), 
      "failed to begin capture on stream ", this->get(), " with thread mode ", m
    );
  }

  /**
  @brief ends graph capturing on the stream
  
  Equivalently calling @c cudaStreamEndCapture to
  end capture on stream and returning the captured graph. 
  Capture must have been initiated on stream via a call to cudaStream::begin_capture. 
  If capture was invalidated, due to a violation of the rules of stream capture, 
  then a NULL graph will be returned.
  */
  cudaGraph_t end_capture() const {
    cudaGraph_t native_g;
    TF_CHECK_CUDA(
      cudaStreamEndCapture(this->get(), &native_g), 
      "failed to end capture on stream ", this->get()
    );
    return native_g;
  }
  
  /**
  @brief records an event on the stream

  Equivalently calling @c cudaEventRecord to record an event on this stream,
  both of which must be on the same CUDA context.
  */
  void record(cudaEvent_t event) const {
    TF_CHECK_CUDA(
      cudaEventRecord(event, this->get()), 
      "failed to record event ", event, " on stream ", this->get()
    );
  }

  /**
  @brief waits on an event

  Equivalently calling @c cudaStreamWaitEvent to make all future work 
  submitted to stream wait for all work captured in event.
  */
  void wait(cudaEvent_t event) const {
    TF_CHECK_CUDA(
      cudaStreamWaitEvent(this->get(), event, 0), 
      "failed to wait for event ", event, " on stream ", this->get()
    );
  }

  /**
  @brief runs the given executable CUDA graph

  @param exec the given `cudaGraphExec`
  */
  template <typename C, typename D>
  cudaStreamBase& run(const cudaGraphExecBase<C, D>& exec);

  /**
  @brief runs the given executable CUDA graph
  
  @param exec the given `cudaGraphExec_t`
  */
  cudaStreamBase& run(cudaGraphExec_t exec);

  private:

  cudaStreamBase(const cudaStreamBase&) = delete;
  cudaStreamBase& operator = (const cudaStreamBase&) = delete;
};

/**
@brief default smart pointer type to manage a `cudaStream_t` object with unique ownership
*/
using cudaStream = cudaStreamBase<cudaStreamCreator, cudaStreamDeleter>;

}  // end of namespace tf -----------------------------------------------------