Skip to content

Commit a4bdb0a

Browse files
committed
Port layerloadtest to C API
1 parent 9755866 commit a4bdb0a

File tree

1 file changed

+63
-38
lines changed

1 file changed

+63
-38
lines changed

tests/loadlayer/layerloadtest.cpp

+63-38
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,74 @@
11
// OpenCL includes
2-
#include <CL/opencl.hpp>
2+
#include <CL/opencl.h>
33

44
// C++ Standard includes
55
#include <vector>
66
#include <iostream>
77
#include <exception>
88

9-
bool platform_supports_spirv(cl::Platform platform)
9+
bool platform_supports_spirv(cl_platform_id platform)
1010
{
11-
return platform.getInfo<CL_PLATFORM_EXTENSIONS>().find("cl_khr_il_program") != std::string::npos;
11+
size_t ret_size;
12+
clGetPlatformInfo(platform, CL_PLATFORM_EXTENSIONS, 0, nullptr, &ret_size);
13+
std::string extensions(ret_size, '\0');
14+
clGetPlatformInfo(platform, CL_PLATFORM_EXTENSIONS, ret_size, extensions.data(), nullptr);
15+
return extensions.find("cl_khr_il_program") != std::string::npos;
1216
}
1317

14-
bool device_supports_spirv(cl::Device device)
18+
bool device_supports_spirv(cl_device_id device)
1519
{
16-
return device.getInfo<CL_DEVICE_EXTENSIONS>().find("cl_khr_il_program") != std::string::npos;
20+
size_t ret_size;
21+
clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, 0, nullptr, &ret_size);
22+
std::string extensions(ret_size, '\0');
23+
clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, ret_size, extensions.data(), nullptr);
24+
return extensions.find("cl_khr_il_program") != std::string::npos;
25+
}
26+
27+
std::vector<cl_platform_id> get_platforms()
28+
{
29+
cl_uint num_platforms;
30+
clGetPlatformIDs(0, nullptr, &num_platforms);
31+
std::vector<cl_platform_id> platforms(num_platforms);
32+
clGetPlatformIDs(num_platforms, platforms.data(), nullptr);
33+
return platforms;
34+
}
35+
36+
std::vector<cl_device_id> get_devices(cl_platform_id platform)
37+
{
38+
cl_uint num_devices;
39+
clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 0, nullptr, &num_devices);
40+
std::vector<cl_device_id> devices(num_devices);
41+
clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, num_devices, devices.data(), nullptr);
42+
return devices;
43+
}
44+
45+
template <int CL_INFO>
46+
std::string get_platform_info(cl_platform_id platform)
47+
{
48+
size_t ret_size;
49+
clGetPlatformInfo(platform, CL_INFO, 0, nullptr, &ret_size);
50+
std::string info(ret_size, '\0');
51+
clGetPlatformInfo(platform, CL_INFO, ret_size, info.data(), nullptr);
52+
info.pop_back(); // pop null-terminator
53+
return info;
54+
}
55+
56+
template <int CL_INFO>
57+
std::string get_device_info(cl_device_id device)
58+
{
59+
size_t ret_size;
60+
clGetDeviceInfo(device, CL_INFO, 0, nullptr, &ret_size);
61+
std::string info(ret_size, '\0');
62+
clGetDeviceInfo(device, CL_INFO, ret_size, info.data(), nullptr);
63+
info.pop_back(); // pop null-terminator
64+
return info;
1765
}
1866

1967
int main(int argc, char* argv[])
2068
{
2169
try // Any error results in program termination
2270
{
23-
std::vector<cl::Platform> platforms;
24-
cl::Platform::get(&platforms);
71+
std::vector<cl_platform_id> platforms = get_platforms();
2572

2673
if (platforms.empty()) throw std::runtime_error{ "No OpenCL platforms found." };
2774

@@ -31,9 +78,9 @@ int main(int argc, char* argv[])
3178
for (const auto& platform : platforms)
3279
{
3380
std::cout <<
34-
"\t" << platform.getInfo<CL_PLATFORM_VENDOR>() <<
81+
"\t" << get_platform_info<CL_PLATFORM_VENDOR>(platform) <<
3582
" (" <<
36-
platform.getInfo<CL_PLATFORM_VERSION>() <<
83+
get_platform_info<CL_PLATFORM_VERSION>(platform) <<
3784
")" <<
3885
std::endl;
3986
std::cout <<
@@ -48,12 +95,12 @@ int main(int argc, char* argv[])
4895
}
4996
else
5097
{
51-
cl::Platform platform = platforms.at(argc > 1 ? std::atoi(argv[1]) : 0);
98+
cl_platform_id platform = platforms.at(argc > 1 ? std::atoi(argv[1]) : 0);
5299
std::cout <<
53100
"Selected platform: " <<
54-
platform.getInfo<CL_PLATFORM_VENDOR>() <<
101+
get_platform_info<CL_PLATFORM_VENDOR>(platform) <<
55102
" (" <<
56-
platform.getInfo<CL_PLATFORM_VERSION>() <<
103+
get_platform_info<CL_PLATFORM_VERSION>(platform) <<
57104
")" <<
58105
std::endl;
59106
std::cout <<
@@ -65,31 +112,14 @@ int main(int argc, char* argv[])
65112
) <<
66113
std::endl;
67114

68-
std::vector<cl::Device> devices;
69-
platform.getDevices(CL_DEVICE_TYPE_ALL, &devices);
115+
std::vector<cl_device_id> devices = get_devices(platform);
70116

71117
if (devices.empty()) throw std::runtime_error{ "No devices found on selected platform." };
72-
/*
73-
std::cout << "Found device" << (devices.size() > 1 ? "s" : "") << ":" << std::endl;
74-
for (const auto& device : devices)
75-
{
76-
std::cout <<
77-
"\t" << device.getInfo<CL_DEVICE_NAME>() <<
78-
std::endl;
79-
std::cout <<
80-
"\t\t" <<
81-
(
82-
device.getInfo<CL_DEVICE_EXTENSIONS>().find("cl_khr_il_program") != std::string::npos ?
83-
"Supports cl_khr_il_program" :
84-
"Doesn't support cl_khr_il_program"
85-
) <<
86-
std::endl;
87-
}
88-
*/
89-
cl::Device device = devices.at(argc > 2 ? std::atoi(argv[2]) : 0);
118+
119+
cl_device_id device = devices.at(argc > 2 ? std::atoi(argv[2]) : 0);
90120
std::cout <<
91121
"Selected device: " <<
92-
device.getInfo<CL_DEVICE_NAME>() <<
122+
get_device_info<CL_DEVICE_NAME>(device) <<
93123
std::endl;
94124
std::cout <<
95125
"\t\t" <<
@@ -101,11 +131,6 @@ int main(int argc, char* argv[])
101131
std::endl;
102132
}
103133
}
104-
catch (cl::Error& error) // If any OpenCL error occurs
105-
{
106-
std::cerr << error.what() << "(" << error.err() << ")" << std::endl;
107-
std::exit(error.err());
108-
}
109134
catch (std::exception& error) // If STL/CRT error occurs
110135
{
111136
std::cerr << error.what() << std::endl;

0 commit comments

Comments
 (0)