From b86a78968d9d85db8d9f87fe2bc5d119192c9bed Mon Sep 17 00:00:00 2001 From: vvilpas Date: Wed, 7 Apr 2021 17:29:28 +0200 Subject: [PATCH] Disable GPU methods if no device available --- src/controllers/aer_controller.hpp | 6 ++++++ src/controllers/controller.hpp | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/controllers/aer_controller.hpp b/src/controllers/aer_controller.hpp index 35ff6e4085..a01f8a162a 100755 --- a/src/controllers/aer_controller.hpp +++ b/src/controllers/aer_controller.hpp @@ -502,6 +502,12 @@ void Controller::set_config(const json_t &config) { throw std::runtime_error( "Simulation device \"GPU\" is not supported on this system"); #else + int nDev; + if (cudaGetDeviceCount(&nDev) != cudaSuccess) { + cudaGetLastError(); + throw std::runtime_error("No CUDA device available!"); + } + sim_device_ = Device::GPU; #endif } diff --git a/src/controllers/controller.hpp b/src/controllers/controller.hpp index d5e4da14c0..7ba9157903 100755 --- a/src/controllers/controller.hpp +++ b/src/controllers/controller.hpp @@ -300,6 +300,20 @@ class Controller { //------------------------------------------------------------------------- void Controller::set_config(const json_t &config) { +#ifdef AER_THRUST_CUDA + { + std::string method; + if (JSON::get_value(method, "method", config)) { + if(method.find("gpu") != std::string::npos){ + int nDev; + if (cudaGetDeviceCount(&nDev) != cudaSuccess) { + cudaGetLastError(); + throw std::runtime_error("No CUDA device available!"); + } + } + } + } +#endif // Load validation threshold JSON::get_value(validation_threshold_, "validation_threshold", config);