diff --git a/openmp/libomptarget/plugins/cuda/dynamic_cuda/cuda.h b/openmp/libomptarget/plugins/cuda/dynamic_cuda/cuda.h --- a/openmp/libomptarget/plugins/cuda/dynamic_cuda/cuda.h +++ b/openmp/libomptarget/plugins/cuda/dynamic_cuda/cuda.h @@ -27,6 +27,7 @@ typedef enum cudaError_enum { CUDA_SUCCESS = 0, CUDA_ERROR_INVALID_VALUE = 1, + CUDA_ERROR_NO_DEVICE = 100, CUDA_ERROR_INVALID_HANDLE = 400, } CUresult; diff --git a/openmp/libomptarget/plugins/cuda/src/rtl.cpp b/openmp/libomptarget/plugins/cuda/src/rtl.cpp --- a/openmp/libomptarget/plugins/cuda/src/rtl.cpp +++ b/openmp/libomptarget/plugins/cuda/src/rtl.cpp @@ -507,6 +507,10 @@ DP("Failed to load CUDA shared library\n"); return; } + if (Err == CUDA_ERROR_NO_DEVICE) { + DP("There are no devices supporting CUDA.\n"); + return; + } if (!checkResult(Err, "Error returned from cuInit\n")) { return; } diff --git a/openmp/libomptarget/test/offloading/cuda_no_devices.c b/openmp/libomptarget/test/offloading/cuda_no_devices.c new file mode 100644 --- /dev/null +++ b/openmp/libomptarget/test/offloading/cuda_no_devices.c @@ -0,0 +1,20 @@ +// The CUDA plugin used to complain on stderr when no CUDA devices were enabled, +// and then it let the application run anyway. Check that there's no such +// complaint anymore, especially when the user isn't targeting CUDA. + +// RUN: %libomptarget-compile-generic +// RUN: env CUDA_VISIBLE_DEVICES= \ +// RUN: %libomptarget-run-generic 2>&1 | %fcheck-generic + +#include + +// CHECK-NOT: {{.}} +// CHECK: Hello World: 4 +// CHECK-NOT: {{.}} +int main() { + int x = 0; + #pragma omp target teams num_teams(2) reduction(+:x) + x += 2; + printf("Hello World: %d\n", x); + return 0; +}