Skip to content

Commit 6e06110

Browse files
committed
Rewrite OpenCL device discovery to SYCL.
Signed-off-by: Cory Levels <[email protected]>
1 parent 5d1d716 commit 6e06110

File tree

2 files changed

+15
-35
lines changed

2 files changed

+15
-35
lines changed

sycl/tools/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ set(CMAKE_CXX_EXTENSIONS OFF)
44

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)
7+
target_include_directories(get_device_count_by_type PRIVATE "${sycl_inc_dir}")
78
target_link_libraries(get_device_count_by_type
9+
PRIVATE sycl
810
PRIVATE OpenCL::Headers
911
PRIVATE ${OpenCL_LIBRARIES}
1012
)

sycl/tools/get_device_count_by_type.cpp

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <CL/cl.h>
1010
#include <CL/cl_ext.h>
11+
#include <CL/sycl.hpp>
1112

1213
#ifdef USE_PI_CUDA
1314
#include <cuda_driver.h>
@@ -17,6 +18,8 @@
1718
#include <string>
1819
#include <vector>
1920

21+
using namespace cl::sycl;
22+
2023
static const std::string help =
2124
" Help\n"
2225
" Example: ./get_device_count_by_type cpu opencl\n"
@@ -61,54 +64,29 @@ int main(int argc, char* argv[]) {
6164
}
6265
#endif // USE_PI_CUDA
6366

64-
cl_device_type device_type;
67+
info::device_type device_type;
6568
if (type == "cpu") {
66-
device_type = CL_DEVICE_TYPE_CPU;
69+
device_type = info::device_type::cpu;
6770
} else if (type == "gpu") {
68-
device_type = CL_DEVICE_TYPE_GPU;
71+
device_type = info::device_type::gpu;
6972
} else if (type == "accelerator") {
70-
device_type = CL_DEVICE_TYPE_ACCELERATOR;
73+
device_type = info::device_type::accelerator;
7174
} else if (type == "default") {
72-
device_type = CL_DEVICE_TYPE_DEFAULT;
75+
device_type = info::device_type::automatic;
7376
} else if (type == "all") {
74-
device_type = CL_DEVICE_TYPE_ALL;
77+
device_type = info::device_type::all;
7578
} else {
7679
std::cout << "0:Incorrect device type." << std::endl
7780
<< help << std::endl;
7881
return 0;
7982
}
8083

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-
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-
}
84+
std::vector<platform> platforms(platform::get_platforms());
85+
for (cl_uint i = 0; i < platforms.size(); i++) {
86+
std::vector<device> result = platforms[i].get_devices(device_type);
87+
deviceCount += result.size();
10988
}
11089

11190
std::cout << deviceCount << ":" << backend << std::endl;
112-
11391
return 0;
11492
}

0 commit comments

Comments
 (0)