|
9 | 9 | #include <CL/cl.h>
|
10 | 10 | #include <CL/cl_ext.h>
|
11 | 11 |
|
| 12 | +#include <cstdlib> |
| 13 | + |
12 | 14 | #ifdef USE_PI_CUDA
|
13 |
| -#include <cuda_driver.h> |
14 |
| -#endif // USE_PI_CUDA |
| 15 | +#include <cuda.h> |
| 16 | +#include <cuda_device_runtime_api.h> |
| 17 | +#endif // USE_PI_CUDA |
15 | 18 |
|
16 | 19 | #include <iostream>
|
| 20 | +#include <sstream> |
17 | 21 | #include <string>
|
18 | 22 | #include <vector>
|
19 | 23 |
|
20 | 24 | static const std::string help =
|
21 |
| -" Help\n" |
22 |
| -" Example: ./get_device_count_by_type cpu opencl\n" |
23 |
| -" Support types: cpu/gpu/accelerator/default/all\n" |
24 |
| -" Support backends: cuda/opencl \n" |
25 |
| -" Output format: <number_of_devices>:<additional_Information>"; |
26 |
| - |
27 |
| -int main(int argc, char* argv[]) { |
28 |
| - if (argc < 3) { |
29 |
| - std::cout |
30 |
| - << "0:Please set a device type and backend to find" << std::endl |
31 |
| - << help << std::endl; |
32 |
| - return 0; |
33 |
| - } |
34 |
| - |
35 |
| - std::string type = argv[1]; |
36 |
| - std::string backend{argv[2]}; |
37 |
| - |
38 |
| - cl_uint deviceCount = 0; |
| 25 | + " Help\n" |
| 26 | + " Example: ./get_device_count_by_type cpu opencl\n" |
| 27 | + " Support types: cpu/gpu/accelerator/default/all\n" |
| 28 | + " Support backends: cuda/opencl \n" |
| 29 | + " Output format: <number_of_devices>:<additional_Information>"; |
| 30 | + |
| 31 | +const char *deviceTypeToString(cl_device_type deviceType) { |
| 32 | + const char *str = "unknown"; |
| 33 | + switch (deviceType) { |
| 34 | + case CL_DEVICE_TYPE_CPU: |
| 35 | + str = "cpu"; |
| 36 | + break; |
| 37 | + case CL_DEVICE_TYPE_GPU: |
| 38 | + str = "gpu"; |
| 39 | + break; |
| 40 | + case CL_DEVICE_TYPE_ACCELERATOR: |
| 41 | + str = "accelerator"; |
| 42 | + break; |
| 43 | + case CL_DEVICE_TYPE_CUSTOM: |
| 44 | + str = "custom"; |
| 45 | + break; |
| 46 | + case CL_DEVICE_TYPE_DEFAULT: |
| 47 | + str = "default"; |
| 48 | + break; |
| 49 | + case CL_DEVICE_TYPE_ALL: |
| 50 | + str = "all"; |
| 51 | + break; |
| 52 | + default: |
| 53 | + // str already set to express unknown device type. |
| 54 | + break; |
| 55 | + } |
| 56 | + |
| 57 | + return str; |
| 58 | +} |
39 | 59 |
|
40 |
| -#ifdef USE_PI_CUDA |
41 |
| - if (backend == "CUDA") { |
42 |
| - std::string msg{""}; |
43 |
| - |
44 |
| - int runtime_version = 0; |
45 |
| - |
46 |
| - cudaError_t err = cuDriverGetVersion(&runtime_version); |
47 |
| - if (runtime_version < 9020 || err != CUDA_SUCCESS) { |
48 |
| - std::cout << deviceCount << " :Unsupported CUDA Runtime " << std::endl; |
49 |
| - } |
50 |
| - |
51 |
| - if (type == "gpu") { |
52 |
| - deviceCount = 1; |
53 |
| - msg = "cuda"; |
54 |
| - } else { |
55 |
| - msg = "Unsupported device type for CUDA backend"; |
56 |
| - msg += " type: "; |
57 |
| - msg += type; |
58 |
| - } |
59 |
| - std::cout << deviceCount << " : " << msg << std::endl; |
60 |
| - return 0; |
| 60 | +static bool queryOpenCL(cl_device_type deviceType, cl_uint &deviceCount, |
| 61 | + std::string &msg) { |
| 62 | + deviceCount = 0u; |
| 63 | + cl_int iRet = CL_SUCCESS; |
| 64 | + cl_uint platformCount = 0; |
| 65 | + |
| 66 | + iRet = clGetPlatformIDs(0, nullptr, &platformCount); |
| 67 | + if (iRet != CL_SUCCESS) { |
| 68 | + if (iRet == CL_PLATFORM_NOT_FOUND_KHR) { |
| 69 | + msg = "OpenCL error runtime not found"; |
| 70 | + } else { |
| 71 | + std::stringstream stream; |
| 72 | + stream << "OpenCL error calling clGetPlatformIDs " << iRet << std::endl; |
| 73 | + msg = stream.str(); |
61 | 74 | }
|
62 |
| -#endif // USE_PI_CUDA |
63 |
| - |
64 |
| - cl_device_type device_type; |
65 |
| - if (type == "cpu") { |
66 |
| - device_type = CL_DEVICE_TYPE_CPU; |
67 |
| - } else if (type == "gpu") { |
68 |
| - device_type = CL_DEVICE_TYPE_GPU; |
69 |
| - } else if (type == "accelerator") { |
70 |
| - device_type = CL_DEVICE_TYPE_ACCELERATOR; |
71 |
| - } else if (type == "default") { |
72 |
| - device_type = CL_DEVICE_TYPE_DEFAULT; |
73 |
| - } else if (type == "all") { |
74 |
| - device_type = CL_DEVICE_TYPE_ALL; |
75 |
| - } else { |
76 |
| - std::cout << "0:Incorrect device type." << std::endl |
77 |
| - << help << std::endl; |
78 |
| - return 0; |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + std::vector<cl_platform_id> platforms(platformCount); |
| 79 | + iRet = clGetPlatformIDs(platformCount, &platforms[0], nullptr); |
| 80 | + if (iRet != CL_SUCCESS) { |
| 81 | + std::stringstream stream; |
| 82 | + stream << "OpenCL error calling clGetPlatformIDs " << iRet << std::endl; |
| 83 | + msg = stream.str(); |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + for (cl_uint i = 0; i < platformCount; i++) { |
| 88 | + cl_uint deviceCountPart = 0; |
| 89 | + iRet = |
| 90 | + clGetDeviceIDs(platforms[i], deviceType, 0, nullptr, &deviceCountPart); |
| 91 | + if (iRet == CL_SUCCESS || iRet == CL_DEVICE_NOT_FOUND) { |
| 92 | + deviceCount += deviceCountPart; |
| 93 | + } else { |
| 94 | + deviceCount = 0u; |
| 95 | + std::stringstream stream; |
| 96 | + stream << "OpenCL error calling clGetDeviceIDs " << iRet << std::endl; |
| 97 | + msg = stream.str(); |
| 98 | + return false; |
79 | 99 | }
|
| 100 | + } |
80 | 101 |
|
81 |
| - cl_int iRet = CL_SUCCESS; |
82 |
| - cl_uint platformCount = 0; |
83 |
| - |
84 |
| - iRet = clGetPlatformIDs(0, nullptr, &platformCount); |
85 |
| - if (iRet != CL_SUCCESS) { |
86 |
| - if (iRet == CL_PLATFORM_NOT_FOUND_KHR) { |
87 |
| - std::cout << "0:OpenCL runtime not found " << std::endl; |
88 |
| - } else { |
89 |
| - std::cout << "0:A problem at calling function clGetPlatformIDs count " |
90 |
| - << iRet << std::endl; |
91 |
| - } |
92 |
| - return 0; |
93 |
| - } |
94 |
| - |
95 |
| - std::vector<cl_platform_id> platforms(platformCount); |
96 |
| - |
97 |
| - iRet = clGetPlatformIDs(platformCount, &platforms[0], nullptr); |
98 |
| - if (iRet != CL_SUCCESS) { |
99 |
| - std::cout << "0:A problem at when calling function clGetPlatformIDs ids " << iRet << std::endl; |
100 |
| - return 0; |
101 |
| - } |
| 102 | + msg = "opencl "; |
| 103 | + msg += deviceTypeToString(deviceType); |
| 104 | + return true; |
| 105 | +} |
102 | 106 |
|
103 |
| - for (cl_uint i = 0; i < platformCount; i++) { |
104 |
| - cl_uint deviceCountPart = 0; |
105 |
| - iRet = clGetDeviceIDs(platforms[i], device_type, 0, nullptr, &deviceCountPart); |
106 |
| - if (iRet == CL_SUCCESS) { |
107 |
| - deviceCount += deviceCountPart; |
108 |
| - } |
| 107 | +static bool queryCUDA(cl_device_type deviceType, cl_uint &deviceCount, |
| 108 | + std::string &msg) { |
| 109 | + deviceCount = 0u; |
| 110 | +#ifdef USE_PI_CUDA |
| 111 | + const unsigned int defaultFlag = 0; |
| 112 | + CUresult err = cuInit(defaultFlag); |
| 113 | + if (err != CUDA_SUCCESS) { |
| 114 | + msg = "CUDA initialization error"; |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + const int minRuntimeVersion = 10010; |
| 119 | + int runtimeVersion = 0; |
| 120 | + err = cuDriverGetVersion(&runtimeVersion); |
| 121 | + if (err != CUDA_SUCCESS) { |
| 122 | + msg = "CUDA error querying driver version"; |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + if (runtimeVersion < minRuntimeVersion) { |
| 127 | + std::stringstream stream; |
| 128 | + stream << "CUDA version not supported " << runtimeVersion; |
| 129 | + msg = stream.str(); |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + switch (deviceType) { |
| 134 | + case CL_DEVICE_TYPE_DEFAULT: // Fall through. |
| 135 | + case CL_DEVICE_TYPE_ALL: // Fall through. |
| 136 | + case CL_DEVICE_TYPE_GPU: { |
| 137 | + int count = 0; |
| 138 | + CUresult err = cuDeviceGetCount(&count); |
| 139 | + if (err != CUDA_SUCCESS || count < 0) { |
| 140 | + msg = "CUDA error querying device count"; |
| 141 | + return false; |
109 | 142 | }
|
110 | 143 |
|
111 |
| - std::cout << deviceCount << ":" << backend << std::endl; |
| 144 | + deviceCount = static_cast<cl_uint>(count); |
| 145 | + msg = "cuda "; |
| 146 | + msg += deviceTypeToString(deviceType); |
| 147 | + return true; |
| 148 | + } break; |
| 149 | + default: |
| 150 | + msg = "CUDA unsupported device type "; |
| 151 | + msg += deviceTypeToString(deviceType); |
| 152 | + return false; |
| 153 | + } |
| 154 | +#else |
| 155 | + msg = "CUDA not supported"; |
| 156 | + deviceCount = 0u; |
| 157 | + |
| 158 | + return false; |
| 159 | +#endif |
| 160 | +} |
112 | 161 |
|
113 |
| - return 0; |
| 162 | +int main(int argc, char *argv[]) { |
| 163 | + if (argc < 3) { |
| 164 | + std::cout << "0:Please set a device type and backend to find" << std::endl |
| 165 | + << help << std::endl; |
| 166 | + return EXIT_FAILURE; |
| 167 | + } |
| 168 | + |
| 169 | + std::string type = argv[1]; |
| 170 | + std::string backend{argv[2]}; |
| 171 | + |
| 172 | + cl_device_type deviceType = CL_DEVICE_TYPE_DEFAULT; |
| 173 | + if (type == "cpu") { |
| 174 | + deviceType = CL_DEVICE_TYPE_CPU; |
| 175 | + } else if (type == "gpu") { |
| 176 | + deviceType = CL_DEVICE_TYPE_GPU; |
| 177 | + } else if (type == "accelerator") { |
| 178 | + deviceType = CL_DEVICE_TYPE_ACCELERATOR; |
| 179 | + } else if (type == "default") { |
| 180 | + deviceType = CL_DEVICE_TYPE_DEFAULT; |
| 181 | + } else if (type == "all") { |
| 182 | + deviceType = CL_DEVICE_TYPE_ALL; |
| 183 | + } else { |
| 184 | + std::cout << "0:Incorrect device type " << type << "\n" |
| 185 | + << help << std::endl; |
| 186 | + return EXIT_FAILURE; |
| 187 | + } |
| 188 | + |
| 189 | + std::string msg; |
| 190 | + cl_uint deviceCount = 0; |
| 191 | + |
| 192 | + bool querySuccess = false; |
| 193 | + |
| 194 | + if (backend == "opencl" || backend == "OpenCL" || backend == "OPENCL" || |
| 195 | + backend == "PI_OPENCL") { |
| 196 | + querySuccess = queryOpenCL(deviceType, deviceCount, msg); |
| 197 | + } else if (backend == "cuda" || backend == "CUDA" || backend == "PI_CUDA") { |
| 198 | + querySuccess = queryCUDA(deviceType, deviceCount, msg); |
| 199 | + } else { |
| 200 | + std::stringstream stream; |
| 201 | + stream << "Unknown backend" << backend << "\n" << help << std::endl; |
| 202 | + msg = stream.str(); |
| 203 | + } |
| 204 | + |
| 205 | + std::cout << deviceCount << ":" << msg << std::endl; |
| 206 | + |
| 207 | + if (!querySuccess) { |
| 208 | + return EXIT_FAILURE; |
| 209 | + } |
| 210 | + |
| 211 | + return EXIT_SUCCESS; |
114 | 212 | }
|
0 commit comments