47 static bool is_cuda_initialized_;
48 static bool is_cuda_available_;
49 static int device_count_;
50 static int device_id_;
51 static bool use_cuda_;
59 static bool UseCuda() {
return use_cuda_; }
61 static void SetUseCuda(
bool use_cuda) { use_cuda_ = use_cuda; }
68#ifdef COVERAGECONTROL_WITH_CUDA
70 return !!GetDeviceCount(device_count);
83 return is_cuda_initialized_;
92#ifdef COVERAGECONTROL_WITH_CUDA
96 if (is_cuda_initialized_) {
99 auto devices = GetEnvironmentCUDA_VISIBLE_DEVICES();
100 if (devices.empty()) {
101 device_id_ = FindDevice();
106 if (device_id_ < 0) {
107 std::cerr <<
"No CUDA device found" << std::endl;
111 if (GPUDeviceInit(device_id_) != device_id_) {
112 std::cerr <<
"Failed to initialize CUDA device" << std::endl;
115 is_cuda_initialized_ =
true;
121#ifdef COVERAGECONTROL_WITH_CUDA
126 static std::vector<int> GetEnvironmentCUDA_VISIBLE_DEVICES() {
127 std::vector<int> cuda_visible_devices;
128 char *env_cuda_visible_devices = std::getenv(
"CUDA_VISIBLE_DEVICES");
129 if (env_cuda_visible_devices !=
nullptr) {
130 std::string str_cuda_visible_devices(env_cuda_visible_devices);
131 std::istringstream ss(str_cuda_visible_devices);
133 while (std::getline(ss, token,
',')) {
134 cuda_visible_devices.push_back(std::stoi(token));
137 return cuda_visible_devices;
145 static bool GetDeviceCount(
int &device_count);
152 static int GPUDeviceInit(
int dev_id);
162 static int FindDevice();
168 static int FindIntegratedGPU();
176 static int GPUGetMaxGflopsDeviceId(std::vector<int> device_list = {});