48 static bool is_cuda_initialized_;
49 static bool is_cuda_available_;
50 static int device_count_;
51 static int device_id_;
52 static bool use_cuda_;
60 static bool UseCuda() {
return use_cuda_; }
62 static void SetUseCuda(
bool use_cuda) { use_cuda_ = use_cuda; }
69#ifdef COVERAGECONTROL_WITH_CUDA
71 return !!GetDeviceCount(device_count);
84 return is_cuda_initialized_;
93#ifdef COVERAGECONTROL_WITH_CUDA
97 if (is_cuda_initialized_) {
100 auto devices = GetEnvironmentCUDA_VISIBLE_DEVICES();
101 if (devices.empty()) {
102 device_id_ = FindDevice();
104 device_id_ = GPUGetMaxGflopsDeviceId(devices);
106 if (device_id_ < 0) {
107 std::cerr <<
"No CUDA device found" << std::endl;
110 std::cout <<
"Initializing CUDA device " << device_id_ << 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 = {});