Shortcuts

Struct OptionalCUDAGuard

Page Contents

Struct Documentation

struct OptionalCUDAGuard

A variant of OptionalDeviceGuard that is specialized for CUDA.

See CUDAGuard for when you can use this.

Public Functions

inline explicit OptionalCUDAGuard()

Create an uninitialized OptionalCUDAGuard.

inline explicit OptionalCUDAGuard(std::optional<Device> device_opt)

Set the current CUDA device to the passed Device, if it is not nullopt.

inline explicit OptionalCUDAGuard(std::optional<DeviceIndex> device_index_opt)

Set the current CUDA device to the passed device index, if it is not nullopt.

OptionalCUDAGuard(const OptionalCUDAGuard&) = delete
OptionalCUDAGuard &operator=(const OptionalCUDAGuard&) = delete
OptionalCUDAGuard(OptionalCUDAGuard &&other) = delete
OptionalCUDAGuard &operator=(OptionalCUDAGuard &&other) = delete
~OptionalCUDAGuard() = default
inline void set_device(Device device)

Sets the CUDA device to the given device, initializing the guard if it is not already initialized.

Errors if the given device is not a CUDA device.

inline void reset_device(Device device)

Sets the CUDA device to the given device, initializing the guard if it is not already initialized.

Errors if the given device is not a CUDA device. (This method is provided for uniformity with OptionalDeviceGuard).

inline void set_index(DeviceIndex device_index)

Sets the CUDA device to the given device index, initializing the guard if it is not already initialized.

inline std::optional<Device> original_device() const

Returns the device that was set immediately prior to initialization of the guard, or nullopt if the guard is uninitialized.

inline std::optional<Device> current_device() const

Returns the most recent device that was set using this device guard, either from construction, or via set_device, if the guard is initialized, or nullopt if the guard is uninitialized.

inline void reset()

Restore the original CUDA device, resetting this guard to uninitialized state.

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