30#include <cuda_runtime.h>
35#include "CoverageControl/extern/cuda_helpers/helper_cuda.h"
36#include "CoverageControl/extern/cuda_helpers/helper_string.h"
41 if (result != cudaSuccess) {
42 std::cerr <<
"CUDA error: [" <<
static_cast<unsigned int>(result) <<
"] "
43 << cudaGetErrorString(result) << std::endl;
49bool CudaUtils::GetDeviceCount(
int &device_count) {
51 if (device_count == 0) {
58int CudaUtils::GPUDeviceInit(
int dev_id) {
60 bool device_count_success = GetDeviceCount(device_count);
61 if (!device_count_success) {
69 if (dev_id > device_count - 1) {
70 std::cerr <<
"Invalid GPU device ID" << std::endl;
71 std::cerr <<
"Device ID: " << dev_id <<
" Device Count: " << device_count
76 int compute_mode = -1, major = 0, minor = 0;
78 cudaDeviceGetAttribute(&compute_mode, cudaDevAttrComputeMode, dev_id));
80 &major, cudaDevAttrComputeCapabilityMajor, dev_id));
82 &minor, cudaDevAttrComputeCapabilityMinor, dev_id));
83 if (compute_mode == cudaComputeModeProhibited) {
84 std::cerr <<
"Error: device is running in <Compute Mode Prohibited>, no "
85 "threads can use cudaSetDevice()."
91 std::cerr <<
"GPUDeviceInit(): GPU device does not support CUDA."
100 is_cuda_initialized_ =
true;
105int CudaUtils::FindDevice() {
return GPUGetMaxGflopsDeviceId(); }
107int CudaUtils::FindIntegratedGPU() {
108 int current_device = 0;
109 int device_count = 0;
110 int devices_prohibited = 0;
114 if (device_count == 0) {
120 while (current_device < device_count) {
121 int compute_mode = -1, integrated = -1;
123 &compute_mode, cudaDevAttrComputeMode, current_device));
124 CheckCudaErrors(cudaDeviceGetAttribute(&integrated, cudaDevAttrIntegrated,
128 if (integrated && (compute_mode != cudaComputeModeProhibited)) {
131 int major = 0, minor = 0;
133 &major, cudaDevAttrComputeCapabilityMajor, current_device));
135 &minor, cudaDevAttrComputeCapabilityMinor, current_device));
140 return current_device;
142 devices_prohibited++;
148 if (devices_prohibited == device_count) {
149 std::cerr <<
"CUDA error: No Integrated GPU found that supports CUDA."
163int CudaUtils::GPUGetMaxGflopsDeviceId(std::vector<int> device_list) {
164 int sm_per_multiproc = 0;
165 int max_perf_device = 0;
166 int device_count = 0;
167 int devices_prohibited = 0;
169 uint64_t max_compute_perf = 0;
171 if (GetDeviceCount(device_count) ==
false) {
175 if (device_count == 0) {
180 if (device_list.size() == 0) {
181 for (
int i = 0; i < device_count; i++) {
182 device_list.push_back(i);
186 while (device_list.size() > 0) {
187 int current_device = device_list.back();
188 device_list.pop_back();
189 int compute_mode = -1, major = 0, minor = 0;
191 &compute_mode, cudaDevAttrComputeMode, current_device));
193 &major, cudaDevAttrComputeCapabilityMajor, current_device));
195 &minor, cudaDevAttrComputeCapabilityMinor, current_device));
199 if (compute_mode != cudaComputeModeProhibited) {
200 if (major == 9999 && minor == 9999) {
201 sm_per_multiproc = 1;
203 sm_per_multiproc = _ConvertSMVer2Cores(major, minor);
205 int multiProcessorCount = 0, clockRate = 0;
207 cudaDevAttrMultiProcessorCount,
209 cudaError_t result = cudaDeviceGetAttribute(
210 &clockRate, cudaDevAttrClockRate, current_device);
211 if (result != cudaSuccess) {
214 if (result == cudaErrorInvalidValue) {
217 fprintf(stderr,
"CUDA error at %s:%d code=%d(%s) \n", __FILE__,
218 __LINE__,
static_cast<unsigned int>(result),
219 _cudaGetErrorEnum(result));
223 uint64_t compute_perf =
224 (uint64_t)multiProcessorCount * sm_per_multiproc * clockRate;
226 if (compute_perf > max_compute_perf) {
227 max_compute_perf = compute_perf;
228 max_perf_device = current_device;
231 devices_prohibited++;
235 if (devices_prohibited == device_count) {
236 std::cerr <<
"GPUGetMaxGflopsDeviceId() CUDA error:"
237 <<
" all devices have compute mode prohibited." << std::endl;
241 return max_perf_device;
Utility functions for CUDA.
Namespace for the CoverageControl library.
bool CheckCudaErrors(cudaError_t result)