Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/pure-cfg/src/lib/cl-target/clinfo.c
ViewVC logotype

Annotation of /branches/pure-cfg/src/lib/cl-target/clinfo.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1436 - (view) (download) (as text)

1 : jhr 1267 /*! \file clinfo.c
2 :     *
3 :     * \author John Reppy
4 :     *
5 :     * \brief This file contains functions that can be used to determine properties of the
6 :     * host machine's OpenCL support.
7 :     */
8 :    
9 :     /*
10 :     * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
11 :     * All rights reserved.
12 :     */
13 :    
14 :     #include "clinfo.h"
15 :     #include <stdio.h>
16 :     #include <string.h>
17 :     #include <stdlib.h>
18 :    
19 :     #define MAX_PLATFORMS 16
20 :    
21 :     static bool GetDevices (PlatformInfo_t *plat);
22 :    
23 :     STATIC_INLINE void *alloc (size_t szb)
24 :     {
25 :     void *p = malloc(szb);
26 :     if (p == 0) {
27 :     fprintf (stderr, "fatal error: unable to allocate %d bytes of memory\n", (int)szb);
28 :     exit (1);
29 :     }
30 :     return p;
31 :     }
32 :    
33 :     #define NEW(ty) (ty *)alloc(sizeof(ty))
34 :     #define NEWVEC(ty,n) (ty *)alloc(sizeof(ty) * (n))
35 :     #define NEWSTR(s) strcpy((char *)alloc(strlen(s)+1), s)
36 :    
37 :     #define CHECK(call) do { \
38 :     cl_int __sts = call; \
39 :     if (__sts != CL_SUCCESS) { \
40 : jhr 1269 fprintf(stderr, "error %d at %s:%d\n", __sts, __FILE__, __LINE__); \
41 : jhr 1267 exit (1); \
42 :     } \
43 :     } while (0)
44 :    
45 :    
46 :     CLInfo_t *GetCLInfo ()
47 :     {
48 :     cl_platform_id platformIDs[MAX_PLATFORMS];
49 :     char buf[512];
50 :    
51 :     // get platform info
52 : jhr 1269 // get number of OpenCL platforms
53 :     cl_uint numPlatforms;
54 :     CHECK(clGetPlatformIDs (MAX_PLATFORMS, platformIDs, &numPlatforms));
55 : jhr 1267
56 :     PlatformInfo_t *plats = NEWVEC(PlatformInfo_t, numPlatforms);
57 :     for (int i = 0; i < numPlatforms; i++) {
58 :     clGetPlatformInfo (platformIDs[i], CL_PLATFORM_NAME, sizeof(buf), buf, 0);
59 :     plats[i].name = NEWSTR(buf);
60 :     plats[i].id = platformIDs[i];
61 :     // get OpenCL version info
62 : jhr 1269 clGetPlatformInfo (platformIDs[i], CL_PLATFORM_VERSION, sizeof(buf), buf, 0);
63 : jhr 1267 // FIXME: parser version info
64 :     // get extension info
65 :     // FIXME: TODO
66 :     // get device info
67 :     if (! GetDevices (&(plats[i]))) {
68 :     // free storage
69 : jhr 1269 for (int j = 0; j <= i; j++) {
70 : jhr 1267 free (plats[i].name);
71 :     }
72 :     free (plats);
73 :     }
74 :     }
75 :    
76 :     CLInfo_t *info = NEW(CLInfo_t);
77 :     info->numPlatforms = numPlatforms;
78 : jhr 1269 info->platforms = plats;
79 : jhr 1267
80 :     return info;
81 :    
82 :     }
83 :    
84 : jhr 1436 static char *GetStringInfo (cl_device_id dev, cl_device_info param)
85 :     {
86 :     char buf[1024];
87 :     CHECK(clGetDeviceInfo (dev, param, sizeof(buf), buf, 0));
88 :     return NEWSTR(buf);
89 :     }
90 :    
91 : jhr 1267 static bool GetDevices (PlatformInfo_t *plat)
92 :     {
93 :     cl_uint numDevs;
94 :     char buf[512];
95 :    
96 :     // get number of devices for the platform
97 : jhr 1269 clGetDeviceIDs (plat->id, CL_DEVICE_TYPE_ALL, 0, 0, &numDevs);
98 : jhr 1267
99 :     if (numDevs == 0) {
100 :     plat->numDevices = 0;
101 :     plat->devices = 0;
102 :     return false;
103 :     }
104 :    
105 :     plat->numDevices = numDevs;
106 :     plat->devices = NEWVEC(DeviceInfo_t, numDevs);
107 :    
108 :     cl_device_id *devices = NEWVEC(cl_device_id, numDevs);
109 : jhr 1269 CHECK(clGetDeviceIDs (plat->id, CL_DEVICE_TYPE_ALL, numDevs, devices, 0));
110 : jhr 1267
111 :     for (int i = 0; i < numDevs; i++) {
112 : jhr 1377 DeviceInfo_t *dev = &(plat->devices[i]);
113 : jhr 1436 dev->name = GetStringInfo(devices[i], CL_DEVICE_NAME);
114 :     dev->vendor = GetStringInfo(devices[i], CL_DEVICE_VENDOR);
115 : jhr 1377 dev->id = devices[i];
116 : jhr 1267 CHECK(clGetDeviceInfo (
117 : jhr 1415 devices[i], CL_DEVICE_VERSION, sizeof(buf), buf, 0));
118 :     if (sscanf (buf, "OpenCL %d.%d", &(dev->majorVersion), &(dev->minorVersion)) != 2) {
119 :     fprintf(stderr, "error scanning version string: \"%s\"\n", buf);
120 :     exit (1);
121 :     }
122 :     CHECK(clGetDeviceInfo (
123 : jhr 1377 devices[i], CL_DEVICE_TYPE, sizeof(cl_device_type), &(dev->ty), 0));
124 :     CHECK(clGetDeviceInfo (
125 : jhr 1379 devices[i], CL_DEVICE_AVAILABLE, sizeof(cl_bool), &(dev->isAvail), 0));
126 :     CHECK(clGetDeviceInfo (
127 : jhr 1377 devices[i], CL_DEVICE_ADDRESS_BITS, sizeof(cl_uint), &(dev->addrBits), 0));
128 :     CHECK(clGetDeviceInfo (
129 :     devices[i], CL_DEVICE_ENDIAN_LITTLE, sizeof(cl_bool), &(dev->littleEndian), 0));
130 :     CHECK(clGetDeviceInfo (
131 :     devices[i], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cl_uint), &(dev->numCUs), 0));
132 :     CHECK(clGetDeviceInfo (
133 : jhr 1384 devices[i], CL_DEVICE_MAX_CONSTANT_ARGS, sizeof(cl_uint), &(dev->maxConstArgs), 0));
134 :     CHECK(clGetDeviceInfo (
135 : jhr 1377 devices[i], CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(cl_uint), &(dev->maxWIDims), 0));
136 :     CHECK(clGetDeviceInfo (
137 :     devices[i], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), &(dev->maxWGSize), 0));
138 :     size_t szb = sizeof(size_t) * dev->maxWIDims;
139 : jhr 1379 dev->maxWISize = (size_t *) alloc (szb);
140 :     CHECK(clGetDeviceInfo (devices[i], CL_DEVICE_MAX_WORK_ITEM_SIZES, szb, dev->maxWISize, 0));
141 : jhr 1377 CHECK(clGetDeviceInfo (
142 :     devices[i], CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(cl_ulong), &(dev->globalMemSzb), 0));
143 :     CHECK(clGetDeviceInfo (
144 :     devices[i], CL_DEVICE_LOCAL_MEM_SIZE, sizeof(cl_ulong), &(dev->localMemSzb), 0));
145 :     CHECK(clGetDeviceInfo (
146 : jhr 1384 devices[i], CL_DEVICE_MAX_PARAMETER_SIZE, sizeof(size_t), &(dev->maxParamSzb), 0));
147 :     CHECK(clGetDeviceInfo (
148 :     devices[i], CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE, sizeof(cl_ulong), &(dev->maxConstBufSzb), 0));
149 :     CHECK(clGetDeviceInfo (
150 : jhr 1377 devices[i], CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(cl_ulong), &(dev->maxAllocSzb), 0));
151 :     cl_bool imagesSupported;
152 :     CHECK(clGetDeviceInfo (
153 :     devices[i], CL_DEVICE_IMAGE_SUPPORT, sizeof(cl_bool), &imagesSupported, 0));
154 :     if (imagesSupported) {
155 :     CHECK(clGetDeviceInfo (
156 :     devices[i], CL_DEVICE_IMAGE2D_MAX_WIDTH, sizeof(size_t), &(dev->maxImg2D[0]), 0));
157 :     CHECK(clGetDeviceInfo (
158 :     devices[i], CL_DEVICE_IMAGE3D_MAX_DEPTH, sizeof(size_t), &(dev->maxImg2D[1]), 0));
159 :     CHECK(clGetDeviceInfo (
160 :     devices[i], CL_DEVICE_IMAGE3D_MAX_WIDTH, sizeof(size_t), &(dev->maxImg3D[0]), 0));
161 :     CHECK(clGetDeviceInfo (
162 :     devices[i], CL_DEVICE_IMAGE3D_MAX_HEIGHT, sizeof(size_t), &(dev->maxImg3D[1]), 0));
163 :     CHECK(clGetDeviceInfo (
164 :     devices[i], CL_DEVICE_IMAGE3D_MAX_DEPTH, sizeof(size_t), &(dev->maxImg3D[2]), 0));
165 :     }
166 :     else {
167 :     dev->maxImg2D[0] = dev->maxImg2D[1] = 0;
168 :     dev->maxImg3D[0] = dev->maxImg3D[1] = dev->maxImg3D[2] = 0;
169 :     }
170 : jhr 1436 CHECK(clGetDeviceInfo (
171 :     devices[i], CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, sizeof(cl_uint), &(dev->charWid), 0));
172 :     CHECK(clGetDeviceInfo (
173 :     devices[i], CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, sizeof(cl_uint), &(dev->shortWid), 0));
174 :     CHECK(clGetDeviceInfo (
175 :     devices[i], CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, sizeof(cl_uint), &(dev->intWid), 0));
176 :     CHECK(clGetDeviceInfo (
177 :     devices[i], CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, sizeof(cl_uint), &(dev->longWid), 0));
178 :     CHECK(clGetDeviceInfo (
179 :     devices[i], CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, sizeof(cl_uint), &(dev->floatWid), 0));
180 :     CHECK(clGetDeviceInfo (
181 :     devices[i], CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, sizeof(cl_uint), &(dev->doubleWid), 0));
182 : jhr 1267 }
183 :    
184 : jhr 1379 free(devices);
185 :    
186 : jhr 1267 return true;
187 :     }
188 : jhr 1379
189 :     void PrintCLInfo (FILE *outS, CLInfo_t *clinfo)
190 :     {
191 :     if (clinfo->numPlatforms == 0) {
192 :     fprintf(outS, "No OpenCL platforms\n");
193 :     return;
194 :     }
195 :    
196 :     fprintf(outS, "OpenCL profile:\n");
197 :     for (int i = 0; i < clinfo->numPlatforms; i++) {
198 :     PlatformInfo_t *plat = &(clinfo->platforms[i]);
199 :     fprintf (outS, " Platform %d (%s)\n", i, plat->name);
200 :     for (int j = 0; j < plat->numDevices; j++) {
201 :     DeviceInfo_t *dev = &(plat->devices[j]);
202 :     if (dev->isAvail)
203 :     fprintf (outS, " Device %d.%d (%s):\n", i, j, dev->name);
204 :     else
205 :     fprintf (outS, " Device %d.%d (%s): **UNAVAILABLE**\n", i, j, dev->name);
206 : jhr 1436 fprintf (outS, " Vendor: %s\n", dev->vendor);
207 : jhr 1415 fprintf (outS, " OpenCL version: %d.%d\n",
208 :     dev->majorVersion, dev->minorVersion);
209 : jhr 1417 fprintf (outS, " Type: ");
210 :     if (isCPUDevice(dev)) fprintf (outS, " CPU");
211 :     if (isGPUDevice(dev)) fprintf (outS, " GPU");
212 :     if (dev->ty & CL_DEVICE_TYPE_ACCELERATOR) fprintf (outS, " ACCELERATOR");
213 :     if (dev->ty & CL_DEVICE_TYPE_DEFAULT) fprintf (outS, " DEFAULT");
214 :     fprintf (outS, "\n");
215 : jhr 1384 fprintf (outS, " Address size: %d\n", dev->addrBits);
216 :     fprintf (outS, " Endianess: %s\n", dev->littleEndian ? "LITTLE" : "BIG");
217 :     fprintf (outS, " Num. compute units: %d\n", dev->numCUs);
218 :     fprintf (outS, " Max. dimensions: %d\n", dev->maxWIDims);
219 :     fprintf (outS, " Max. work group size: %ld\n", (long)(dev->maxWGSize));
220 :     fprintf (outS, " Max. work items: ");
221 : jhr 1379 for (int k = 0; k < dev->maxWIDims; k++)
222 :     fprintf (outS, "%s%ld", (k > 0) ? " x " : "", (long)(dev->maxWISize[k]));
223 :     fprintf (outS, "\n");
224 : jhr 1384 fprintf (outS, " Global memory size: %ld\n", (long)(dev->globalMemSzb));
225 :     fprintf (outS, " Local memory size: %ld\n", (long)(dev->localMemSzb));
226 :     fprintf (outS, " Max. parameter size: %ld\n", (long)(dev->maxParamSzb));
227 :     fprintf (outS, " Max. allocation size: %ld\n", (long)(dev->maxAllocSzb));
228 :     fprintf (outS, " Max. const. buffer size: %ld\n", (long)(dev->maxConstBufSzb));
229 :     fprintf (outS, " Max. const. arguments: %d\n", dev->maxConstArgs);
230 :     fprintf (outS, " Max. 2D image size: %ld x %ld\n",
231 : jhr 1379 (long)(dev->maxImg2D[0]), (long)(dev->maxImg2D[1]));
232 : jhr 1384 fprintf (outS, " Max. 3D image size: %ld x %ld x %ld\n",
233 : jhr 1379 (long)(dev->maxImg3D[0]), (long)(dev->maxImg3D[1]), (long)(dev->maxImg3D[2]));
234 : jhr 1436 fprintf (outS, " Prefered vector width: char%d, short%d, int%d, long%d\n",
235 :     dev->charWid, dev->shortWid, dev->intWid, dev->longWid);
236 :     fprintf (outS, " float%d", dev->floatWid);
237 :     if (dev->doubleWid > 0)
238 :     fprintf (outS, ", double%d\n", dev->doubleWid);
239 :     else
240 :     fprintf (outS, "\n");
241 : jhr 1379 }
242 :     }
243 :    
244 :     }

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0