Shortcuts

Struct CUDAStreamGuard

Page Contents

Struct Documentation

struct CUDAStreamGuard

A variant of StreamGuard that is specialized for CUDA.

See CUDAGuard for when you can use this.

Public Functions

explicit CUDAStreamGuard() = delete

No default constructor, see Note [Omitted default constructor from RAII].

inline explicit CUDAStreamGuard(Stream stream)

Set the current CUDA device to the device associated with the passed stream, and set the current CUDA stream on that device to the passed stream.

Errors if the Stream is not a CUDA stream.

~CUDAStreamGuard() = default
CUDAStreamGuard(const CUDAStreamGuard&) = delete

Copy is disallowed.

CUDAStreamGuard &operator=(const CUDAStreamGuard&) = delete
CUDAStreamGuard(CUDAStreamGuard &&other) = delete

Move is disallowed, as CUDAStreamGuard does not have an uninitialized state, which is required for moves on types with nontrivial destructors.

CUDAStreamGuard &operator=(CUDAStreamGuard &&other) = delete
inline void reset_stream(Stream stream)

Resets the currently set stream to the original stream and the currently set device to the original device.

Then, set the current device to the device associated with the passed stream, and set the current stream on that device to the passed stream. Errors if the stream passed is not a CUDA stream.

NOTE: this implementation may skip some stream/device setting if it can prove that it is unnecessary.

WARNING: reset_stream does NOT preserve previously set streams on different devices. If you need to set streams on multiple devices on CUDA, use CUDAMultiStreamGuard instead.

inline CUDAStream original_stream() const

Returns the CUDA stream that was set at the time the guard was constructed.

inline CUDAStream current_stream() const

Returns the most recent CUDA stream that was set using this device guard, either from construction, or via set_stream.

inline Device current_device() const

Returns the most recent CUDA device that was set using this device guard, either from construction, or via set_device/reset_device/set_index.

inline Device original_device() const

Returns the CUDA device that was set at the most recent reset_stream(), or otherwise the device at construction time.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources