SCM Repository
Annotation of /trunk/src/clinfo/clinfo.c
Parent Directory
|
Revision Log
Revision 1640 - (view) (download) (as text)
1 : | jhr | 1640 | /*! \file clinfo.c |
2 : | * | ||
3 : | * \author John Reppy | ||
4 : | * | ||
5 : | * \brief This program is used to determine properties of the host machine's | ||
6 : | * OpenCL support. | ||
7 : | * | ||
8 : | * Usage: | ||
9 : | * clinfo cmd [args] | ||
10 : | * | ||
11 : | * where cmd is one of | ||
12 : | * platforms -- prints a list of OpenCL platforms supported by the | ||
13 : | * host. | ||
14 : | * version platform -- prints version of OpenCL supported by the platform. The | ||
15 : | * format is "OpenCL major.minor ..." | ||
16 : | * devices platform -- prints a list of devices | ||
17 : | * extensions device -- prints a list of extensions that are | ||
18 : | * supported by the device | ||
19 : | */ | ||
20 : | |||
21 : | /* | ||
22 : | * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu) | ||
23 : | * All rights reserved. | ||
24 : | */ | ||
25 : | |||
26 : | #include "Diderot/config.h" | ||
27 : | # ifdef HAVE_CL_CL_H | ||
28 : | # include <CL/cl.h> | ||
29 : | # elif defined(HAVE_OPENCL_CL_H) | ||
30 : | # include <OpenCL/cl.h> | ||
31 : | # else | ||
32 : | # error no cl.h | ||
33 : | # endif | ||
34 : | #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) | ||
46 : | { | ||
47 : | cl_platform_id platforms[MAX_PLATFORMS]; | ||
48 : | |||
49 : | if (argc < 2) { | ||
50 : | usage(); | ||
51 : | } | ||
52 : | |||
53 : | // process command-line options | ||
54 : | if ((strcmp(argv[1], "platforms") == 0) && (argc == 2)) { | ||
55 : | char buf[256]; | ||
56 : | int numPlatforms = GetPlatforms (MAX_PLATFORMS, platforms); | ||
57 : | for (int i = 0; i < numPlatforms; i++) { | ||
58 : | clGetPlatformInfo (platforms[i], CL_PLATFORM_NAME, sizeof(buf), buf, 0); | ||
59 : | printf ("%s\n", buf); | ||
60 : | } | ||
61 : | } | ||
62 : | else if ((strcmp(argv[1], "version") == 0) && (argc == 3)) { | ||
63 : | char buf[256]; | ||
64 : | cl_platform_id id = GetPlatformByName(argv[2]); | ||
65 : | clGetPlatformInfo (id, CL_PLATFORM_VERSION, sizeof(buf), buf, 0); | ||
66 : | printf ("%s\n", buf); | ||
67 : | } | ||
68 : | else if ((strcmp(argv[1], "devices") == 0) && (argc == 3)) { | ||
69 : | cl_platform_id id = GetPlatformByName(argv[2]); | ||
70 : | cl_device_id *devs; | ||
71 : | char buf[256]; | ||
72 : | int numDevs = GetDevices (id, CL_DEVICE_TYPE_ALL, &devs); | ||
73 : | for (int i = 0; i < numDevs; i++) { | ||
74 : | clGetDeviceInfo (devs[i], CL_DEVICE_NAME, sizeof(buf), buf, 0); | ||
75 : | printf ("%s\n", buf); | ||
76 : | } | ||
77 : | } | ||
78 : | else if ((strcmp(argv[1], "extensions") == 0) && (argc == 3)) { | ||
79 : | cl_platform_id id = GetPlatformByName(argv[2]); | ||
80 : | size_t bufSz; | ||
81 : | // get the size of the extensions buffer | ||
82 : | clGetPlatformInfo (id, CL_PLATFORM_EXTENSIONS, 0, 0, &bufSz); | ||
83 : | char *buf = (char *)malloc(bufSz); | ||
84 : | clGetPlatformInfo (id, CL_PLATFORM_EXTENSIONS, bufSz, buf, 0); | ||
85 : | char *p = buf; | ||
86 : | while (p != 0) { | ||
87 : | p = strchr(p, ' '); | ||
88 : | if (p != 0) *p = '\n'; | ||
89 : | } | ||
90 : | printf ("%s", buf); | ||
91 : | free (buf); | ||
92 : | } | ||
93 : | else | ||
94 : | usage(); | ||
95 : | |||
96 : | } | ||
97 : | |||
98 : | static void usage () | ||
99 : | { | ||
100 : | exit (1); | ||
101 : | } | ||
102 : | |||
103 : | static int GetPlatforms (int max, cl_platform_id *platforms) | ||
104 : | { | ||
105 : | cl_uint n; | ||
106 : | // get number of OpenCL platforms | ||
107 : | if (clGetPlatformIDs (max, platforms, &n) != CL_SUCCESS) { | ||
108 : | fprintf(stderr, "unable to get platform IDs\n"); | ||
109 : | exit (1); | ||
110 : | } | ||
111 : | return n; | ||
112 : | } | ||
113 : | |||
114 : | static cl_platform_id GetPlatformByName (const char *name) | ||
115 : | { | ||
116 : | cl_platform_id platforms[MAX_PLATFORMS]; | ||
117 : | char buf[256]; | ||
118 : | |||
119 : | // get number of OpenCL platforms | ||
120 : | cl_uint n = GetPlatforms (MAX_PLATFORMS, platforms); | ||
121 : | for (cl_uint i = 0; i < n; i++) { | ||
122 : | clGetPlatformInfo (platforms[i], CL_PLATFORM_NAME, sizeof(buf), buf, 0); | ||
123 : | if (strcmp(buf, name) == 0) return platforms[i]; | ||
124 : | } | ||
125 : | |||
126 : | fprintf (stderr, "platform %s not found\n", name); | ||
127 : | exit (1); | ||
128 : | |||
129 : | } | ||
130 : | |||
131 : | static int GetDevices (cl_platform_id plat, cl_device_type ty, cl_device_id **devs) | ||
132 : | { | ||
133 : | cl_uint numDevs; | ||
134 : | |||
135 : | // get number of devices for the platform | ||
136 : | clGetDeviceIDs (plat, ty, 0, 0, &numDevs); | ||
137 : | |||
138 : | cl_device_id *devices = (cl_device_id *)malloc(numDevs * sizeof(cl_device_id)); | ||
139 : | clGetDeviceIDs (plat, ty, numDevs, devices, 0); | ||
140 : | |||
141 : | *devs = devices; | ||
142 : | return numDevs; | ||
143 : | } |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |