Skip to content

Commit f709eaa

Browse files
committed
Add source code to check OpenCL platforms and devices
1 parent 69b7b0a commit f709eaa

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

devices.c

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#ifdef __APPLE__
4+
#include <OpenCL/opencl.h>
5+
#else
6+
#include <CL/cl.h>
7+
#endif
8+
9+
int main() {
10+
11+
int i, j;
12+
char* value;
13+
size_t valueSize;
14+
cl_uint platformCount;
15+
cl_platform_id* platforms;
16+
cl_uint deviceCount;
17+
cl_device_id* devices;
18+
cl_uint maxComputeUnits;
19+
20+
// get all platforms
21+
clGetPlatformIDs(0, NULL, &platformCount);
22+
platforms = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount);
23+
clGetPlatformIDs(platformCount, platforms, NULL);
24+
25+
for (i = 0; i < platformCount; i++) {
26+
27+
// get all devices
28+
clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &deviceCount);
29+
devices = (cl_device_id*) malloc(sizeof(cl_device_id) * deviceCount);
30+
clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, deviceCount, devices, NULL);
31+
32+
// for each device print critical attributes
33+
for (j = 0; j < deviceCount; j++) {
34+
35+
// print device name
36+
clGetDeviceInfo(devices[j], CL_DEVICE_NAME, 0, NULL, &valueSize);
37+
value = (char*) malloc(valueSize);
38+
clGetDeviceInfo(devices[j], CL_DEVICE_NAME, valueSize, value, NULL);
39+
printf("%d. Device: %s\n", j+1, value);
40+
free(value);
41+
42+
// print hardware device version
43+
clGetDeviceInfo(devices[j], CL_DEVICE_VERSION, 0, NULL, &valueSize);
44+
value = (char*) malloc(valueSize);
45+
clGetDeviceInfo(devices[j], CL_DEVICE_VERSION, valueSize, value, NULL);
46+
printf(" %d.%d Hardware version: %s\n", j+1, 1, value);
47+
free(value);
48+
49+
// print software driver version
50+
clGetDeviceInfo(devices[j], CL_DRIVER_VERSION, 0, NULL, &valueSize);
51+
value = (char*) malloc(valueSize);
52+
clGetDeviceInfo(devices[j], CL_DRIVER_VERSION, valueSize, value, NULL);
53+
printf(" %d.%d Software version: %s\n", j+1, 2, value);
54+
free(value);
55+
56+
// print c version supported by compiler for device
57+
clGetDeviceInfo(devices[j], CL_DEVICE_OPENCL_C_VERSION, 0, NULL, &valueSize);
58+
value = (char*) malloc(valueSize);
59+
clGetDeviceInfo(devices[j], CL_DEVICE_OPENCL_C_VERSION, valueSize, value, NULL);
60+
printf(" %d.%d OpenCL C version: %s\n", j+1, 3, value);
61+
free(value);
62+
63+
// print parallel compute units
64+
clGetDeviceInfo(devices[j], CL_DEVICE_MAX_COMPUTE_UNITS,
65+
sizeof(maxComputeUnits), &maxComputeUnits, NULL);
66+
printf(" %d.%d Parallel compute units: %d\n", j+1, 4, maxComputeUnits);
67+
68+
}
69+
70+
free(devices);
71+
72+
}
73+
74+
free(platforms);
75+
return 0;
76+
77+
}

platforms.c

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#ifdef __APPLE__
4+
#include <OpenCL/opencl.h>
5+
#else
6+
#include <CL/cl.h>
7+
#endif
8+
9+
int main() {
10+
11+
int i, j;
12+
char* info;
13+
size_t infoSize;
14+
cl_uint platformCount;
15+
cl_platform_id *platforms;
16+
const char* attributeNames[5] = { "Name", "Vendor",
17+
"Version", "Profile", "Extensions" };
18+
const cl_platform_info attributeTypes[5] = { CL_PLATFORM_NAME, CL_PLATFORM_VENDOR,
19+
CL_PLATFORM_VERSION, CL_PLATFORM_PROFILE, CL_PLATFORM_EXTENSIONS };
20+
const int attributeCount = sizeof(attributeNames) / sizeof(char*);
21+
22+
// get platform count
23+
clGetPlatformIDs(5, NULL, &platformCount);
24+
25+
// get all platforms
26+
platforms = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount);
27+
clGetPlatformIDs(platformCount, platforms, NULL);
28+
29+
// for each platform print all attributes
30+
for (i = 0; i < platformCount; i++) {
31+
32+
printf("\n %d. Platform \n", i+1);
33+
34+
for (j = 0; j < attributeCount; j++) {
35+
36+
// get platform attribute value size
37+
clGetPlatformInfo(platforms[i], attributeTypes[j], 0, NULL, &infoSize);
38+
info = (char*) malloc(infoSize);
39+
40+
// get platform attribute value
41+
clGetPlatformInfo(platforms[i], attributeTypes[j], infoSize, info, NULL);
42+
43+
printf(" %d.%d %-11s: %s\n", i+1, j+1, attributeNames[j], info);
44+
free(info);
45+
46+
}
47+
48+
printf("\n");
49+
50+
}
51+
52+
free(platforms);
53+
return 0;
54+
55+
}

0 commit comments

Comments
 (0)