11 |
* where cmd is one of |
* where cmd is one of |
12 |
* platforms -- prints a list of OpenCL platforms supported by the |
* platforms -- prints a list of OpenCL platforms supported by the |
13 |
* host. |
* host. |
14 |
* version platform -- prints version of OpenCL supported by the platform |
* version platform -- prints version of OpenCL supported by the platform. The |
15 |
* devices -- prints a list of devices |
* format is "OpenCL major.minor ..." |
16 |
|
* devices platform -- prints a list of devices |
17 |
* extensions device -- prints a list of extensions that are |
* extensions device -- prints a list of extensions that are |
18 |
* supported by the device |
* supported by the device |
19 |
*/ |
*/ |
32 |
# error no cl.h |
# error no cl.h |
33 |
# endif |
# endif |
34 |
#include <stdio.h> |
#include <stdio.h> |
35 |
|
#include <string.h> |
36 |
|
#include <stdlib.h> |
37 |
|
|
38 |
|
#define MAX_PLATFORMS 16 |
39 |
|
|
40 |
|
static void usage (); |
41 |
|
static int GetPlatforms (int max, cl_platform_id *platforms); |
42 |
|
static cl_platform_id GetPlatformByName (const char *name); |
43 |
|
static int GetDevices (cl_platform_id plat, cl_device_type ty, cl_device_id **devs); |
44 |
|
|
45 |
int main (int argc, const char **argv) |
int main (int argc, const char **argv) |
46 |
{ |
{ |
47 |
cl_platform_id platforms[MAX_PLATFORMS]; |
cl_platform_id platforms[MAX_PLATFORMS]; |
|
int numPlatforms; |
|
48 |
|
|
49 |
|
// process command-line options |
50 |
|
if ((strcmp(argv[1], "platforms") == 0) && (argc == 2)) { |
51 |
|
char buf[256]; |
52 |
|
int numPlatforms = GetPlatforms (MAX_PLATFORMS, platforms); |
53 |
|
for (int i = 0; i < numPlatforms; i++) { |
54 |
|
clGetPlatformInfo (platforms[i], CL_PLATFORM_NAME, sizeof(buf), buf, 0); |
55 |
|
printf ("%s\n", buf); |
56 |
|
} |
57 |
|
} |
58 |
|
else if ((strcmp(argv[1], "version") == 0) && (argc == 3)) { |
59 |
|
char buf[256]; |
60 |
|
cl_platform_id id = GetPlatformByName(argv[2]); |
61 |
|
clGetPlatformInfo (id, CL_PLATFORM_VERSION, sizeof(buf), buf, 0); |
62 |
|
printf ("%s\n", buf); |
63 |
|
} |
64 |
|
else if ((strcmp(argv[1], "devices") == 0) && (argc == 3)) { |
65 |
|
cl_platform_id id = GetPlatformByName(argv[2]); |
66 |
|
cl_device_id *devs; |
67 |
|
char buf[256]; |
68 |
|
int numDevs = GetDevices (id, CL_DEVICE_TYPE_ALL, &devs); |
69 |
|
for (int i = 0; i < numDevs; i++) { |
70 |
|
clGetDeviceInfo (devs[i], CL_DEVICE_NAME, sizeof(buf), buf, 0); |
71 |
|
printf ("%s\n", buf); |
72 |
|
} |
73 |
|
} |
74 |
|
else if ((strcmp(argv[1], "extensions") == 0) && (argc == 3)) { |
75 |
|
cl_platform_id id = GetPlatformByName(argv[2]); |
76 |
|
size_t bufSz; |
77 |
|
// get the size of the extensions buffer |
78 |
|
clGetPlatformInfo (id, CL_PLATFORM_EXTENSIONS, 0, 0, &bufSz); |
79 |
|
char *buf = (char *)malloc(bufSz); |
80 |
|
clGetPlatformInfo (id, CL_PLATFORM_EXTENSIONS, bufSz, buf, 0); |
81 |
|
char *p = buf; |
82 |
|
while (p != 0) { |
83 |
|
p = strchr(p, ' '); |
84 |
|
if (p != 0) *p = '\n'; |
85 |
|
} |
86 |
|
printf ("%s", buf); |
87 |
|
free (buf); |
88 |
|
} |
89 |
|
else |
90 |
|
usage(); |
91 |
|
|
92 |
|
} |
93 |
|
|
94 |
|
static void usage () |
95 |
|
{ |
96 |
|
exit (1); |
97 |
|
} |
98 |
|
|
99 |
|
static int GetPlatforms (int max, cl_platform_id *platforms) |
100 |
|
{ |
101 |
|
cl_uint n; |
102 |
// get number of OpenCL platforms |
// get number of OpenCL platforms |
103 |
if (clGetPlatformIDs (MAX_PLATFORMS, platforms, &numPlatforms) != CL_SUCCESS) { |
if (clGetPlatformIDs (max, platforms, &n) != CL_SUCCESS) { |
104 |
fprintf(stderr, "unable to get platform IDs\n"); |
fprintf(stderr, "unable to get platform IDs\n"); |
105 |
return 1; |
exit (1); |
106 |
|
} |
107 |
|
return n; |
108 |
} |
} |
109 |
|
|
110 |
|
static cl_platform_id GetPlatformByName (const char *name) |
111 |
|
{ |
112 |
|
cl_platform_id platforms[MAX_PLATFORMS]; |
113 |
|
char buf[256]; |
114 |
|
|
115 |
#if defined(CL_VERSION_1_1) |
// get number of OpenCL platforms |
116 |
#elif define(CL_VERSION_1_0) |
cl_uint n = GetPlatforms (MAX_PLATFORMS, platforms); |
117 |
#else |
for (cl_uint i = 0; i < n; i++) { |
118 |
# error unable to determine minimum OpenCL version |
clGetPlatformInfo (platforms[i], CL_PLATFORM_NAME, sizeof(buf), buf, 0); |
119 |
#endif |
if (strcmp(buf, name) == 0) return platforms[i]; |
120 |
|
} |
121 |
|
|
122 |
|
fprintf (stderr, "platform %s not found\n", name); |
123 |
|
exit (1); |
124 |
|
|
125 |
|
} |
126 |
|
|
127 |
|
static int GetDevices (cl_platform_id plat, cl_device_type ty, cl_device_id **devs) |
128 |
|
{ |
129 |
|
cl_uint numDevs; |
130 |
|
|
131 |
|
// get number of devices for the platform |
132 |
|
clGetDeviceIDs (plat, ty, 0, 0, &numDevs); |
133 |
|
|
134 |
|
cl_device_id *devices = (cl_device_id *)malloc(numDevs * sizeof(cl_device_id)); |
135 |
|
clGetDeviceIDs (plat, ty, numDevs, devices, 0); |
136 |
|
|
137 |
|
*devs = devices; |
138 |
|
return numDevs; |
139 |
} |
} |