Advertisement
Ucurrent

Cpuid_cuda.h

Jan 17th, 2023
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | Source Code | 0 0
  1. // cpuid.h CUDA attempt
  2. #include <cuda_runtime.h>
  3.  
  4. int GetCUDACapability() {
  5.     int deviceCount;
  6.     cudaError_t error_id = cudaGetDeviceCount(&deviceCount);
  7.     if (error_id != cudaSuccess) {
  8.         return 0;
  9.     }
  10.     int maxCapability = 0;
  11.     for (int i = 0; i < deviceCount; i++) {
  12.         cudaDeviceProp deviceProperties;
  13.         cudaGetDeviceProperties(&deviceProperties, i);
  14.         maxCapability = max(maxCapability, deviceProperties.major);
  15.     }
  16.     return maxCapability;
  17. }
  18.  
  19. void SetMaxCUDA(int major) {
  20.     int deviceCount;
  21.     cudaError_t error_id = cudaGetDeviceCount(&deviceCount);
  22.     if (error_id != cudaSuccess) {
  23.         return;
  24.     }
  25.     int maxCapability = 0;
  26.     int maxCapabilityDevice = 0;
  27.     for (int i = 0; i < deviceCount; i++) {
  28.         cudaDeviceProp deviceProperties;
  29.         cudaGetDeviceProperties(&deviceProperties, i);
  30.         if (deviceProperties.major > maxCapability) {
  31.             maxCapability = deviceProperties.major;
  32.             maxCapabilityDevice = i;
  33.         }
  34.     }
  35.     if (major <= maxCapability) {
  36.         cudaSetDevice(maxCapabilityDevice);
  37.     }
  38. }
  39.  
  40. /* ai comment###
  41. This code uses the cuda runtime library to get the CUDA capabilities of the host GPU, and sets the highest CUDA instruction set to use at runtime. You would need to include this new code in your project and call the GetCUDACapability() and SetMaxCUDA() functions as appropriate.
  42. */
Tags: CUDA
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement