35 |
std::cerr << "Error: No CUDA GPUs found." << std::endl; |
std::cerr << "Error: No CUDA GPUs found." << std::endl; |
36 |
exit(1); |
exit(1); |
37 |
} |
} |
38 |
|
int defaultGpuIdx; |
39 |
|
cudaGetDevice(&defaultGpuIdx); |
40 |
if (!validGpuId && interactive) { |
if (!validGpuId && interactive) { |
41 |
std::cout << "Available GPUs:" << std::endl; |
std::cout << "Available GPUs:" << std::endl; |
42 |
std::cout << "| id | name | memory | max grid | max threads per block |\n"; |
std::cout << "| id | name | memory | max grid | max threads per block |\n"; |
63 |
} while (gpuIdx <= 0 || gpuIdx >= devicesCount); |
} while (gpuIdx <= 0 || gpuIdx >= devicesCount); |
64 |
} |
} |
65 |
validGpuId = gpuIdx >= 0 && gpuIdx <= devicesCount; |
validGpuId = gpuIdx >= 0 && gpuIdx <= devicesCount; |
66 |
|
cudaError_t gpuSelectionErr = cudaSuccess; |
67 |
if (validGpuId) { |
if (validGpuId) { |
68 |
cudaDeviceProp deviceProperties{}; |
cudaDeviceProp deviceProperties{}; |
69 |
cudaGetDeviceProperties(&deviceProperties, gpuIdx); |
cudaGetDeviceProperties(&deviceProperties, gpuIdx); |
73 |
return; |
return; |
74 |
} |
} |
75 |
std::cout << "Set GPU to: " << deviceProperties.name << " (id:" << gpuIdx << ")" << std::endl; |
std::cout << "Set GPU to: " << deviceProperties.name << " (id:" << gpuIdx << ")" << std::endl; |
76 |
cudaSetDevice(gpuIdx); |
gpuSelectionErr = cudaSetDevice(gpuIdx); |
77 |
} else if (devicesCount > 1) { // No need to display selection when signle GPU |
if (gpuSelectionErr == cudaErrorDeviceAlreadyInUse) { |
78 |
|
std::cerr << "Warning: GPU already in use - falling back to default" << std::endl; |
79 |
|
} else if (gpuSelectionErr == cudaErrorInvalidDevice) { // Should technically not happen |
80 |
|
std::cerr << "Warning: GPU invalid - falling back to default" << std::endl; |
81 |
|
} |
82 |
|
} |
83 |
|
if ((!validGpuId || gpuSelectionErr != cudaSuccess) && |
84 |
|
devicesCount > 1) { // No need to display selection when single GPU |
85 |
cudaDeviceProp deviceProperties{}; |
cudaDeviceProp deviceProperties{}; |
86 |
cudaGetDeviceProperties(&deviceProperties, 0); |
cudaGetDeviceProperties(&deviceProperties, defaultGpuIdx); |
87 |
std::cout << "Using default GPU: " << deviceProperties.name << std::endl; |
std::cout << "Using default GPU: " << deviceProperties.name << " (id:" << defaultGpuIdx << ")" << std::endl; |
88 |
cudaSetDevice(0); |
gpuSelectionErr = cudaSetDevice(defaultGpuIdx); |
89 |
|
if (gpuSelectionErr == cudaErrorDeviceAlreadyInUse) { |
90 |
|
std::cerr << "Error: Default/Fallback GPU already in use" << std::endl; |
91 |
|
exit(1); |
92 |
|
} else if (gpuSelectionErr == cudaErrorInvalidDevice) { // Should technically not happen |
93 |
|
std::cerr << "Warning: Default/Fallback GPU invalid" << std::endl; |
94 |
|
exit(1); |
95 |
|
} |
96 |
} |
} |
97 |
} |
} |
98 |
}; |
}; |