Skip to content

Commit 1a33ab8

Browse files
committed
[SYCL][CUDA] Fix LIT testing with CUDA devices
Build the `get_device_count_by_type` tool with the required preprocessor definitions to enable CUDA, and link with CUDA. Passed correct backend name to the `get_device_count_by_type` tool to is required to run tests with PI OpenCL instead of PI CUDA. Clean up the `get_device_count_by_type` tool code to make it harder to call it with the wrong backend and get unexpected query results back. Signed-off-by: Bjoern Knafla <[email protected]>
1 parent eb373c4 commit 1a33ab8

File tree

4 files changed

+207
-97
lines changed

4 files changed

+207
-97
lines changed

sycl/plugins/cuda/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ message(STATUS "Including the PI API CUDA backend.")
66

77
find_package(CUDA 10.0 REQUIRED)
88

9-
add_library(cudadrv SHARED IMPORTED)
9+
# Make imported library global to use it within the project.
10+
add_library(cudadrv SHARED IMPORTED GLOBAL)
1011

1112
set_target_properties(
1213
cudadrv PROPERTIES

sycl/test/lit.cfg.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ def getDeviceCount(device_type):
106106
if len(result) > 1 and len(result[1]):
107107
print("getDeviceCount {TYPE}:{MSG}".format(
108108
TYPE=device_type, MSG=result[1]))
109-
if re.match(r".*cuda", result[1]):
109+
if re.match(r".*cuda.*", result[1]):
110110
is_cuda = True;
111111
if err:
112112
print("getDeviceCount {TYPE}:{ERR}".format(
113113
TYPE=device_type, ERR=err))
114114
return [value,is_cuda]
115-
return 0
115+
return [0, False]
116116

117117
# Every SYCL implementation provides a host implementation.
118118
config.available_features.add('host')
@@ -150,6 +150,8 @@ def getDeviceCount(device_type):
150150
config.available_features.add('gpu')
151151
if cuda:
152152
config.available_features.add('cuda')
153+
gpu_run_substitute += " SYCL_BE=PI_CUDA "
154+
153155

154156
if platform.system() == "Linux":
155157
gpu_run_on_linux_substitute = "env SYCL_DEVICE_TYPE=GPU "

sycl/tools/CMakeLists.txt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,29 @@ set(CMAKE_CXX_EXTENSIONS OFF)
55
add_executable(get_device_count_by_type get_device_count_by_type.cpp)
66
add_dependencies(get_device_count_by_type ocl-headers ocl-icd)
77
target_link_libraries(get_device_count_by_type
8-
PRIVATE OpenCL::Headers
9-
PRIVATE ${OpenCL_LIBRARIES}
8+
PRIVATE
9+
OpenCL::Headers
10+
${OpenCL_LIBRARIES}
11+
$<$<BOOL:${SYCL_BUILD_PI_CUDA}>:cudadrv>
12+
13+
)
14+
target_compile_definitions(get_device_count_by_type
15+
PRIVATE
16+
$<$<BOOL:${SYCL_BUILD_PI_CUDA}>:USE_PI_CUDA>
1017
)
1118

1219
add_executable(sycl-check sycl-check.cpp)
1320
add_dependencies(sycl-check sycl)
1421
target_include_directories(sycl-check PRIVATE "${sycl_inc_dir}")
1522
target_link_libraries(sycl-check
16-
PRIVATE sycl
17-
PRIVATE OpenCL::Headers
18-
PRIVATE ${OpenCL_LIBRARIES})
23+
PRIVATE
24+
sycl
25+
OpenCL::Headers
26+
${OpenCL_LIBRARIES})
1927

2028
#Minimum supported version of Intel's OCL GPU and CPU devices
2129
target_compile_definitions(sycl-check
22-
PRIVATE MIN_INTEL_OCL_GPU_VERSION=\"18.47.11882\"
23-
PRIVATE MIN_INTEL_OCL_CPU_VERSION=\"18.1.0.0901\",\"7.6.0.1202\"
30+
PRIVATE
31+
MIN_INTEL_OCL_GPU_VERSION=\"18.47.11882\"
32+
MIN_INTEL_OCL_CPU_VERSION=\"18.1.0.0901\",\"7.6.0.1202\"
2433
)

sycl/tools/get_device_count_by_type.cpp

Lines changed: 185 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -9,106 +9,204 @@
99
#include <CL/cl.h>
1010
#include <CL/cl_ext.h>
1111

12+
#include <cstdlib>
13+
1214
#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
1518

1619
#include <iostream>
20+
#include <sstream>
1721
#include <string>
1822
#include <vector>
1923

2024
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+
}
3959

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();
6174
}
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;
7999
}
100+
}
80101

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+
}
102106

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;
109142
}
110143

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+
}
112161

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;
114212
}

0 commit comments

Comments
 (0)