Struct CUDADeviceGuard#

Struct Documentation#

struct CUDADeviceGuard#

CUDA Device Guard. On construction, it calls cudaGetDevice to set the CUDA device to target index, and stores the original current device index. And on destruction, it sets the current CUDA device back to original device index.

Example usage:

void kernel(ffi::TensorView x) {
  ffi::CUDADeviceGuard guard(x.device().device_id);
  ...
}

Public Functions

CUDADeviceGuard() = delete#
inline explicit CUDADeviceGuard(int device_index)#

Constructor from a device index, and store the original device index.

Parameters:

device_index – The device index to guard.

inline ~CUDADeviceGuard() noexcept(false)#

Destructor to set the current device index back to original one if different.