From f42f59d68528f426bcafc77893ebc92ae00f5acc Mon Sep 17 00:00:00 2001 From: Kenneth Moreland Date: Fri, 3 May 2024 17:03:20 -0400 Subject: [PATCH] Fix crash when CUDA device is disabled There was an issue where if VTK-m was compiled with CUDA support but then run on a computer where no CUDA device was available, an inappropriate exception was thrown (instead of just disabling the device). The initialization code should now properly check for the existance of a CUDA device. --- docs/changelog/safe-cuda-runtime-config.md | 7 +++++ .../internal/RuntimeDeviceConfigurationCuda.h | 28 +++++++++++++++---- 2 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 docs/changelog/safe-cuda-runtime-config.md diff --git a/docs/changelog/safe-cuda-runtime-config.md b/docs/changelog/safe-cuda-runtime-config.md new file mode 100644 index 000000000..a9a0880cc --- /dev/null +++ b/docs/changelog/safe-cuda-runtime-config.md @@ -0,0 +1,7 @@ +# Fix crash when CUDA device is disabled + +There was an issue where if VTK-m was compiled with CUDA support but then +run on a computer where no CUDA device was available, an inappropriate +exception was thrown (instead of just disabling the device). The +initialization code should now properly check for the existance of a CUDA +device. diff --git a/vtkm/cont/cuda/internal/RuntimeDeviceConfigurationCuda.h b/vtkm/cont/cuda/internal/RuntimeDeviceConfigurationCuda.h index 0e589a260..afd4505ca 100644 --- a/vtkm/cont/cuda/internal/RuntimeDeviceConfigurationCuda.h +++ b/vtkm/cont/cuda/internal/RuntimeDeviceConfigurationCuda.h @@ -10,6 +10,7 @@ #ifndef vtk_m_cont_cuda_internal_RuntimeDeviceConfigurationCuda_h #define vtk_m_cont_cuda_internal_RuntimeDeviceConfigurationCuda_h +#include #include #include @@ -36,13 +37,28 @@ class RuntimeDeviceConfiguration public: RuntimeDeviceConfiguration() { - int tmp; - VTKM_CUDA_CALL(cudaGetDeviceCount(&tmp)); - this->CudaDeviceCount = tmp; - this->CudaProp.resize(this->CudaDeviceCount); - for (int i = 0; i < this->CudaDeviceCount; ++i) + this->CudaDeviceCount = 0; + this->CudaProp.clear(); + vtkm::cont::DeviceAdapterRuntimeDetector detector; + if (detector.Exists()) { - VTKM_CUDA_CALL(cudaGetDeviceProperties(&this->CudaProp[i], i)); + try + { + int tmp; + VTKM_CUDA_CALL(cudaGetDeviceCount(&tmp)); + this->CudaDeviceCount = tmp; + this->CudaProp.resize(this->CudaDeviceCount); + for (int i = 0; i < this->CudaDeviceCount; ++i) + { + VTKM_CUDA_CALL(cudaGetDeviceProperties(&this->CudaProp[i], i)); + } + } + catch (...) + { + VTKM_LOG_F(vtkm::cont::LogLevel::Error, + "Error retrieving CUDA device information. Disabling."); + this->CudaDeviceCount = 0; + } } }