Skip to content

Commit f7d5ba4

Browse files
authored
add pure C and C++ versions of the enumopencl sample (#67)
* switch enumopencl to pure C * add a C++ version of the enumopencl sample * fix extraneous whitespace in CMakeLists.txt * switch to cout instead of printf * add clang formatting * remove null assignment after free
1 parent 52bc990 commit f7d5ba4

3 files changed

Lines changed: 287 additions & 234 deletions

File tree

samples/core/enumopencl/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ add_sample(
1616
TEST
1717
TARGET enumopencl
1818
VERSION 120
19+
SOURCES main.c)
20+
21+
add_sample(
22+
TEST
23+
TARGET enumopenclcpp
24+
VERSION 120
1925
SOURCES main.cpp)

samples/core/enumopencl/main.c

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
/*
2+
* Copyright (c) 2020 The Khronos Group Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <stdio.h>
18+
#include <stdlib.h>
19+
20+
#include <CL/cl.h>
21+
22+
static cl_int AllocateAndGetPlatformInfoString(cl_platform_id platformId,
23+
cl_platform_info param_name,
24+
char** pStr)
25+
{
26+
cl_int errorCode = CL_SUCCESS;
27+
28+
size_t size = 0;
29+
char* str = NULL;
30+
31+
if (errorCode == CL_SUCCESS)
32+
{
33+
errorCode = clGetPlatformInfo(platformId, param_name, 0, NULL, &size);
34+
}
35+
36+
if (errorCode == CL_SUCCESS)
37+
{
38+
if (size != 0)
39+
{
40+
str = (char*)malloc(size);
41+
if (str == NULL)
42+
{
43+
errorCode = CL_OUT_OF_HOST_MEMORY;
44+
}
45+
}
46+
}
47+
48+
if (errorCode == CL_SUCCESS)
49+
{
50+
errorCode = clGetPlatformInfo(platformId, param_name, size, str, NULL);
51+
}
52+
53+
if (errorCode != CL_SUCCESS)
54+
{
55+
free(str);
56+
}
57+
else
58+
{
59+
*pStr = str;
60+
}
61+
62+
return errorCode;
63+
}
64+
65+
static cl_int PrintPlatformInfoSummary(cl_platform_id platformId)
66+
{
67+
cl_int errorCode = CL_SUCCESS;
68+
69+
char* platformName = NULL;
70+
char* platformVendor = NULL;
71+
char* platformVersion = NULL;
72+
73+
errorCode |= AllocateAndGetPlatformInfoString(platformId, CL_PLATFORM_NAME,
74+
&platformName);
75+
errorCode |= AllocateAndGetPlatformInfoString(
76+
platformId, CL_PLATFORM_VENDOR, &platformVendor);
77+
errorCode |= AllocateAndGetPlatformInfoString(
78+
platformId, CL_PLATFORM_VERSION, &platformVersion);
79+
80+
printf("\tName: %s\n", platformName);
81+
printf("\tVendor: %s\n", platformVendor);
82+
printf("\tDriver Version: %s\n", platformVersion);
83+
84+
free(platformName);
85+
free(platformVendor);
86+
free(platformVersion);
87+
88+
return errorCode;
89+
}
90+
91+
static cl_int AllocateAndGetDeviceInfoString(cl_device_id device,
92+
cl_device_info param_name,
93+
char** pStr)
94+
{
95+
cl_int errorCode = CL_SUCCESS;
96+
97+
size_t size = 0;
98+
char* str = NULL;
99+
100+
if (errorCode == CL_SUCCESS)
101+
{
102+
errorCode = clGetDeviceInfo(device, param_name, 0, NULL, &size);
103+
}
104+
105+
if (errorCode == CL_SUCCESS)
106+
{
107+
if (size != 0)
108+
{
109+
str = (char*)malloc(size);
110+
if (str == NULL)
111+
{
112+
errorCode = CL_OUT_OF_HOST_MEMORY;
113+
}
114+
}
115+
}
116+
117+
if (errorCode == CL_SUCCESS)
118+
{
119+
errorCode = clGetDeviceInfo(device, param_name, size, str, NULL);
120+
}
121+
122+
if (errorCode != CL_SUCCESS)
123+
{
124+
free(str);
125+
}
126+
else
127+
{
128+
*pStr = str;
129+
}
130+
131+
return errorCode;
132+
}
133+
134+
static void PrintDeviceType(const char* label, cl_device_type type)
135+
{
136+
printf("%s%s%s%s%s%s\n", label,
137+
(type & CL_DEVICE_TYPE_DEFAULT) ? "DEFAULT " : "",
138+
(type & CL_DEVICE_TYPE_CPU) ? "CPU " : "",
139+
(type & CL_DEVICE_TYPE_GPU) ? "GPU " : "",
140+
(type & CL_DEVICE_TYPE_ACCELERATOR) ? "ACCELERATOR " : "",
141+
(type & CL_DEVICE_TYPE_CUSTOM) ? "CUSTOM " : "");
142+
}
143+
144+
static cl_int PrintDeviceInfoSummary(cl_device_id* devices, cl_uint numDevices)
145+
{
146+
cl_int errorCode = CL_SUCCESS;
147+
148+
cl_device_type deviceType;
149+
char* deviceName = NULL;
150+
char* deviceVendor = NULL;
151+
char* deviceVersion = NULL;
152+
char* deviceProfile = NULL;
153+
char* driverVersion = NULL;
154+
155+
cl_uint i = 0;
156+
for (i = 0; i < numDevices; i++)
157+
{
158+
errorCode |= clGetDeviceInfo(devices[i], CL_DEVICE_TYPE,
159+
sizeof(deviceType), &deviceType, NULL);
160+
errorCode |= AllocateAndGetDeviceInfoString(devices[i], CL_DEVICE_NAME,
161+
&deviceName);
162+
errorCode |= AllocateAndGetDeviceInfoString(
163+
devices[i], CL_DEVICE_VENDOR, &deviceVendor);
164+
errorCode |= AllocateAndGetDeviceInfoString(
165+
devices[i], CL_DEVICE_VERSION, &deviceVersion);
166+
errorCode |= AllocateAndGetDeviceInfoString(
167+
devices[i], CL_DEVICE_PROFILE, &deviceProfile);
168+
errorCode |= AllocateAndGetDeviceInfoString(
169+
devices[i], CL_DRIVER_VERSION, &driverVersion);
170+
171+
if (errorCode == CL_SUCCESS)
172+
{
173+
printf("Device[%u]:\n", i);
174+
175+
PrintDeviceType("\tType: ", deviceType);
176+
177+
printf("\tName: %s\n", deviceName);
178+
printf("\tVendor: %s\n", deviceVendor);
179+
printf("\tDevice Version: %s\n", deviceVersion);
180+
printf("\tDevice Profile: %s\n", deviceProfile);
181+
printf("\tDriver Version: %s\n", driverVersion);
182+
}
183+
else
184+
{
185+
fprintf(stderr, "Error getting device info for device %u.\n", i);
186+
}
187+
188+
free(deviceName);
189+
free(deviceVendor);
190+
free(deviceVersion);
191+
free(deviceProfile);
192+
free(driverVersion);
193+
}
194+
195+
return errorCode;
196+
}
197+
198+
int main(int argc, char** argv)
199+
{
200+
cl_uint numPlatforms = 0;
201+
clGetPlatformIDs(0, NULL, &numPlatforms);
202+
printf("Enumerated %u platforms.\n\n", numPlatforms);
203+
204+
cl_platform_id* platforms =
205+
(cl_platform_id*)malloc(numPlatforms * sizeof(cl_platform_id));
206+
clGetPlatformIDs(numPlatforms, platforms, NULL);
207+
208+
cl_uint i = 0;
209+
for (i = 0; i < numPlatforms; i++)
210+
{
211+
printf("Platform[%u]:\n", i);
212+
PrintPlatformInfoSummary(platforms[i]);
213+
214+
cl_uint numDevices = 0;
215+
clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &numDevices);
216+
217+
cl_device_id* devices =
218+
(cl_device_id*)malloc(numDevices * sizeof(cl_device_id));
219+
clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, numDevices, devices,
220+
NULL);
221+
222+
PrintDeviceInfoSummary(devices, numDevices);
223+
printf("\n");
224+
225+
free(devices);
226+
}
227+
228+
free(platforms);
229+
230+
printf("Done.\n");
231+
232+
return 0;
233+
}

0 commit comments

Comments
 (0)