Back to Taskflow

Taskflow: A General

docs/cuda__stream_8hpp_source.html

4.1.016.2 KB
Original Source

| | Taskflow: A General-purpose Task-parallel Programming System |

Loading...

Searching...

No Matches

cuda_stream.hpp

1#pragma once

2

3#include "cuda_error.hpp"

4

9

10namespace tf {

11

12

13// ----------------------------------------------------------------------------

14// cudaEventBase

15// ----------------------------------------------------------------------------

16

22class cudaEventCreator {

23

24public:

25

29 cudaEvent_t operator ()() const {

30 cudaEvent_t event;

31 TF_CHECK_CUDA(cudaEventCreate(&event), "failed to create a CUDA event");

32return event;

33 }

34

38 cudaEvent_t operator ()(unsigned int flag) const {

39 cudaEvent_t event;

40 TF_CHECK_CUDA(

41 cudaEventCreateWithFlags(&event, flag),

42"failed to create a CUDA event with flag=", flag

43 );

44return event;

45 }

46

50 cudaEvent_t operator ()(cudaEvent_t event) const {

51return event;

52 }

53};

54

60class cudaEventDeleter {

61public:

65void operator ()(cudaEvent_t event) const {

66 cudaEventDestroy(event);

67 }

68};

69

81template <typename Creator, typename Deleter>

82class cudaEventBase : public std::unique_ptr<std::remove_pointer_t<cudaEvent_t>, Deleter> {

83

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

85

86public:

87

94using base_type = std::unique_ptr<std::remove_pointer_t<cudaEvent_t>, Deleter>;

95

103template <typename... ArgsT>

104explicit cudaEventBase(ArgsT&& ... args) : base_type(

105 Creator{}(std::forward<ArgsT>(args)...), Deleter()

106 ) {

107 }

108

112cudaEventBase(cudaEventBase&&) = default;

113

117cudaEventBase& operator =(cudaEventBase&&) = default;

118

119private:

120

121cudaEventBase(const cudaEventBase&) = delete;

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

123};

124

128using cudaEvent = cudaEventBase<cudaEventCreator, cudaEventDeleter>;

129

130// ----------------------------------------------------------------------------

131// cudaStream

132// ----------------------------------------------------------------------------

133

139class cudaStreamCreator {

140

141public:

142

146 cudaStream_t operator ()() const {

147 cudaStream_t stream;

148 TF_CHECK_CUDA(cudaStreamCreate(&stream), "failed to create a CUDA stream");

149return stream;

150 }

151

155 cudaStream_t operator ()(cudaStream_t stream) const {

156return stream;

157 }

158};

159

165class cudaStreamDeleter {

166

167public:

168

172void operator ()(cudaStream_t stream) const {

173 cudaStreamDestroy(stream);

174 }

175};

176

188template <typename Creator, typename Deleter>

189class cudaStreamBase : public std::unique_ptr<std::remove_pointer_t<cudaStream_t>, Deleter> {

190

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

192

193public:

194

201using base_type = std::unique_ptr<std::remove_pointer_t<cudaStream_t>, Deleter>;

202

210template <typename... ArgsT>

211explicit cudaStreamBase(ArgsT&& ... args) : base_type(

212 Creator{}(std::forward<ArgsT>(args)...), Deleter()

213 ) {

214 }

215

219cudaStreamBase(cudaStreamBase&&) = default;

220

224cudaStreamBase& operator =(cudaStreamBase&&) = default;

225

232cudaStreamBase& synchronize() {

233 TF_CHECK_CUDA(

234 cudaStreamSynchronize(this->get()), "failed to synchronize a CUDA stream"

235 );

236return *this;

237 }

238

265void begin_capture(cudaStreamCaptureMode m = cudaStreamCaptureModeGlobal) const {

266 TF_CHECK_CUDA(

267 cudaStreamBeginCapture(this->get(), m),

268"failed to begin capture on stream ", this->get(), " with thread mode ", m

269 );

270 }

271

281 cudaGraph_t end_capture() const {

282 cudaGraph_t native_g;

283 TF_CHECK_CUDA(

284 cudaStreamEndCapture(this->get(), &native_g),

285"failed to end capture on stream ", this->get()

286 );

287return native_g;

288 }

289

296void record(cudaEvent_t event) const {

297 TF_CHECK_CUDA(

298 cudaEventRecord(event, this->get()),

299"failed to record event ", event, " on stream ", this->get()

300 );

301 }

302

309void wait(cudaEvent_t event) const {

310 TF_CHECK_CUDA(

311 cudaStreamWaitEvent(this->get(), event, 0),

312"failed to wait for event ", event, " on stream ", this->get()

313 );

314 }

315

321template <typename C, typename D>

322cudaStreamBase& run(const cudaGraphExecBase<C, D>& exec);

323

329cudaStreamBase& run(cudaGraphExec_t exec);

330

331private:

332

333cudaStreamBase(const cudaStreamBase&) = delete;

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

335};

336

340using cudaStream = cudaStreamBase<cudaStreamCreator, cudaStreamDeleter>;

341

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

343

344

345

tf::cudaEventBase

class to create a CUDA event with unique ownership

Definition cuda_stream.hpp:82

tf::cudaEventBase::base_type

std::unique_ptr< std::remove_pointer_t< cudaEvent_t >, Deleter > base_type

base type for the underlying unique pointer

Definition cuda_stream.hpp:94

tf::cudaEventBase::cudaEventBase

cudaEventBase(cudaEventBase &&)=default

constructs a cudaEvent from the given rhs using move semantics

tf::cudaEventBase::operator=

cudaEventBase & operator=(cudaEventBase &&)=default

assign the rhs to *this using move semantics

tf::cudaEventBase::cudaEventBase

cudaEventBase(ArgsT &&... args)

constructs a cudaEvent object by passing the given arguments to the event creator

Definition cuda_stream.hpp:104

tf::cudaEventCreator

class to create functors that construct CUDA events

Definition cuda_stream.hpp:22

tf::cudaEventCreator::operator()

cudaEvent_t operator()() const

creates a new cudaEvent_t object using cudaEventCreate

Definition cuda_stream.hpp:29

tf::cudaEventDeleter

class to create a functor that deletes a CUDA event

Definition cuda_stream.hpp:60

tf::cudaEventDeleter::operator()

void operator()(cudaEvent_t event) const

deletes the given cudaEvent_t object using cudaEventDestroy

Definition cuda_stream.hpp:65

tf::cudaGraphExecBase

class to create an executable CUDA graph with unique ownership

Definition cuda_graph_exec.hpp:93

tf::cudaStreamBase

class to create a CUDA stream with unique ownership

Definition cuda_stream.hpp:189

tf::cudaStreamBase::cudaStreamBase

cudaStreamBase(cudaStreamBase &&)=default

constructs a cudaStream from the given rhs using move semantics

tf::cudaStreamBase::synchronize

cudaStreamBase & synchronize()

synchronizes the associated stream

Definition cuda_stream.hpp:232

tf::cudaStreamBase::begin_capture

void begin_capture(cudaStreamCaptureMode m=cudaStreamCaptureModeGlobal) const

begins graph capturing on the stream

Definition cuda_stream.hpp:265

tf::cudaStreamBase::end_capture

cudaGraph_t end_capture() const

ends graph capturing on the stream

Definition cuda_stream.hpp:281

tf::cudaStreamBase::cudaStreamBase

cudaStreamBase(ArgsT &&... args)

constructs a cudaStream object by passing the given arguments to the stream creator

Definition cuda_stream.hpp:211

tf::cudaStreamBase::record

void record(cudaEvent_t event) const

records an event on the stream

Definition cuda_stream.hpp:296

tf::cudaStreamBase::run

cudaStreamBase & run(const cudaGraphExecBase< C, D > &exec)

runs the given executable CUDA graph

tf::cudaStreamBase::wait

void wait(cudaEvent_t event) const

waits on an event

Definition cuda_stream.hpp:309

tf::cudaStreamBase::operator=

cudaStreamBase & operator=(cudaStreamBase &&)=default

assign the rhs to *this using move semantics

tf::cudaStreamBase::run

cudaStreamBase & run(cudaGraphExec_t exec)

runs the given executable CUDA graph

Definition cuda_graph_exec.hpp:366

tf::cudaStreamBase::base_type

std::unique_ptr< std::remove_pointer_t< cudaStream_t >, Deleter > base_type

base type for the underlying unique pointer

Definition cuda_stream.hpp:201

tf::cudaStreamCreator

class to create functors that construct CUDA streams

Definition cuda_stream.hpp:139

tf::cudaStreamCreator::operator()

cudaStream_t operator()() const

constructs a new cudaStream_t object using cudaStreamCreate

Definition cuda_stream.hpp:146

tf::cudaStreamDeleter

class to create a functor that deletes a CUDA stream

Definition cuda_stream.hpp:165

tf::cudaStreamDeleter::operator()

void operator()(cudaStream_t stream) const

deletes the given cudaStream_t object

Definition cuda_stream.hpp:172

tf

taskflow namespace

Definition small_vector.hpp:20

tf::cudaEvent

cudaEventBase< cudaEventCreator, cudaEventDeleter > cudaEvent

default smart pointer type to manage a cudaEvent_t object with unique ownership

Definition cuda_stream.hpp:128

tf::cudaStream

cudaStreamBase< cudaStreamCreator, cudaStreamDeleter > cudaStream

default smart pointer type to manage a cudaStream_t object with unique ownership

Definition cuda_stream.hpp:340