Skip to content

Commit 62336a0

Browse files
authored
Check return value of cudaSetDevice (#979)
1 parent 546fca7 commit 62336a0

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

src/torchcodec/_core/CudaDeviceInterface.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,10 @@ UniqueAVBufferRef getHardwareDeviceContext(const torch::Device& device) {
6060

6161
// Create hardware device context
6262
c10::cuda::CUDAGuard deviceGuard(device);
63-
// Valid values for the argument to cudaSetDevice are 0 to maxDevices - 1:
64-
// https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__DEVICE.html#group__CUDART__DEVICE_1g159587909ffa0791bbe4b40187a4c6bb
65-
// So we ensure the deviceIndex is not negative.
6663
// We set the device because we may be called from a different thread than
6764
// the one that initialized the cuda context.
68-
cudaSetDevice(deviceIndex);
65+
TORCH_CHECK(
66+
cudaSetDevice(deviceIndex) == cudaSuccess, "Failed to set CUDA device");
6967
AVBufferRef* hardwareDeviceCtxRaw = nullptr;
7068
std::string deviceOrdinal = std::to_string(deviceIndex);
7169

0 commit comments

Comments
 (0)