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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : jhr 1671 /*! \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 : jhr 3291 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
11 :     *
12 :     * COPYRIGHT (c) 2015 The University of Chicago
13 : jhr 1671 * All rights reserved.
14 :     */
15 :    
16 : jhr 2075 #include "Diderot/clinfo.h"
17 : jhr 1671 #include <stdio.h>
18 :     #include <string.h>
19 :     #include <stdlib.h>
20 :    
21 :     #define MAX_PLATFORMS 16
22 :    
23 : jhr 2075 static bool GetDevices (unsigned int platIdx, PlatformInfo_t *plat);
24 : jhr 1671
25 :     #define CHECK(call) do { \
26 :     cl_int __sts = call; \
27 :     if (__sts != CL_SUCCESS) { \
28 :     fprintf(stderr, "error %d at %s:%d\n", __sts, __FILE__, __LINE__); \
29 :     exit (1); \
30 :     } \
31 :     } while (0)
32 :    
33 : jhr 2075 //! Checked memory allocation
34 :     STATIC_INLINE void *CheckedAlloc (size_t szb)
35 :     {
36 :     void *p = malloc(szb);
37 :     if (p == 0) {
38 :     fprintf (stderr, "fatal error: unable to allocate %d bytes of memory\n", (int)szb);
39 :     exit (1);
40 :     }
41 :     return p;
42 :     }
43 : jhr 1671
44 : jhr 2075 #define NEW(ty) (ty *)CheckedAlloc(sizeof(ty))
45 :     #define NEWVEC(ty,n) (ty *)CheckedAlloc(sizeof(ty) * (n))
46 :     #define NEWSTR(s) strcpy((char *)CheckedAlloc(strlen(s)+1), s)
47 :     #define FREE(p) do { if (p != 0) free(p); } while(0)
48 :    
49 :     CLInfo_t *Diderot_GetCLInfo ()
50 : jhr 1671 {
51 :     cl_platform_id platformIDs[MAX_PLATFORMS];
52 :     char buf[512];
53 :    
54 :     // get platform info
55 :     // get number of OpenCL platforms
56 :     cl_uint numPlatforms;
57 :     CHECK(clGetPlatformIDs (MAX_PLATFORMS, platformIDs, &numPlatforms));
58 :    
59 :     PlatformInfo_t *plats = NEWVEC(PlatformInfo_t, numPlatforms);
60 : jhr 2075 for (unsigned int i = 0; i < numPlatforms; i++) {
61 : jhr 1671 clGetPlatformInfo (platformIDs[i], CL_PLATFORM_NAME, sizeof(buf), buf, 0);
62 :     plats[i].name = NEWSTR(buf);
63 :     plats[i].id = platformIDs[i];
64 :     // get OpenCL version info
65 :     clGetPlatformInfo (platformIDs[i], CL_PLATFORM_VERSION, sizeof(buf), buf, 0);
66 :     // get extension info
67 :     // FIXME: TODO
68 :     // get device info
69 : jhr 2075 if (! GetDevices (i, &(plats[i]))) {
70 : jhr 1671 // free storage
71 : jhr 2075 for (unsigned int j = 0; j <= i; j++) {
72 : jhr 1671 free (plats[i].name);
73 :     }
74 :     free (plats);
75 :     }
76 :     }
77 :    
78 :     CLInfo_t *info = NEW(CLInfo_t);
79 :     info->numPlatforms = numPlatforms;
80 :     info->platforms = plats;
81 :    
82 :     return info;
83 :    
84 :     }
85 :    
86 :     static char *GetStringInfo (cl_device_id dev, cl_device_info param)
87 :     {
88 :     char buf[1024];
89 :     CHECK(clGetDeviceInfo (dev, param, sizeof(buf), buf, 0));
90 :     return NEWSTR(buf);
91 :     }
92 :    
93 : jhr 2075 static bool GetDevices (unsigned int platIdx, PlatformInfo_t *plat)
94 : jhr 1671 {
95 :     cl_uint numDevs;
96 :     char buf[512];
97 :    
98 :     // get number of devices for the platform
99 :     clGetDeviceIDs (plat->id, CL_DEVICE_TYPE_ALL, 0, 0, &numDevs);
100 :    
101 :     if (numDevs == 0) {
102 :     plat->numDevices = 0;
103 :     plat->devices = 0;
104 :     return false;
105 :     }
106 :    
107 :     plat->numDevices = numDevs;
108 :     plat->devices = NEWVEC(DeviceInfo_t, numDevs);
109 :    
110 :     cl_device_id *devices = NEWVEC(cl_device_id, numDevs);
111 :     CHECK(clGetDeviceIDs (plat->id, CL_DEVICE_TYPE_ALL, numDevs, devices, 0));
112 :    
113 : jhr 2075 for (unsigned int i = 0; i < numDevs; i++) {
114 : jhr 1671 DeviceInfo_t *dev = &(plat->devices[i]);
115 : jhr 2075 dev->index[0] = platIdx;
116 :     dev->index[1] = i;
117 : jhr 1671 dev->name = GetStringInfo(devices[i], CL_DEVICE_NAME);
118 :     dev->vendor = GetStringInfo(devices[i], CL_DEVICE_VENDOR);
119 :     dev->id = devices[i];
120 :     CHECK(clGetDeviceInfo (
121 :     devices[i], CL_DEVICE_VERSION, sizeof(buf), buf, 0));
122 :     if (sscanf (buf, "OpenCL %d.%d", &(dev->majorVersion), &(dev->minorVersion)) != 2) {
123 :     fprintf(stderr, "error scanning version string: \"%s\"\n", buf);
124 :     exit (1);
125 :     }
126 :     CHECK(clGetDeviceInfo (
127 :     devices[i], CL_DEVICE_TYPE, sizeof(cl_device_type), &(dev->ty), 0));
128 :     CHECK(clGetDeviceInfo (
129 :     devices[i], CL_DEVICE_AVAILABLE, sizeof(cl_bool), &(dev->isAvail), 0));
130 :     CHECK(clGetDeviceInfo (
131 :     devices[i], CL_DEVICE_ADDRESS_BITS, sizeof(cl_uint), &(dev->addrBits), 0));
132 :     CHECK(clGetDeviceInfo (
133 :     devices[i], CL_DEVICE_ENDIAN_LITTLE, sizeof(cl_bool), &(dev->littleEndian), 0));
134 :     CHECK(clGetDeviceInfo (
135 :     devices[i], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cl_uint), &(dev->numCUs), 0));
136 :     CHECK(clGetDeviceInfo (
137 :     devices[i], CL_DEVICE_MAX_CONSTANT_ARGS, sizeof(cl_uint), &(dev->maxConstArgs), 0));
138 :     CHECK(clGetDeviceInfo (
139 :     devices[i], CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(cl_uint), &(dev->maxWIDims), 0));
140 :     CHECK(clGetDeviceInfo (
141 :     devices[i], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), &(dev->maxWGSize), 0));
142 :     size_t szb = sizeof(size_t) * dev->maxWIDims;
143 :     dev->maxWISize = (size_t *) CheckedAlloc (szb);
144 :     CHECK(clGetDeviceInfo (devices[i], CL_DEVICE_MAX_WORK_ITEM_SIZES, szb, dev->maxWISize, 0));
145 :     CHECK(clGetDeviceInfo (
146 :     devices[i], CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(cl_ulong), &(dev->globalMemSzb), 0));
147 :     CHECK(clGetDeviceInfo (
148 :     devices[i], CL_DEVICE_LOCAL_MEM_SIZE, sizeof(cl_ulong), &(dev->localMemSzb), 0));
149 :     CHECK(clGetDeviceInfo (
150 :     devices[i], CL_DEVICE_MAX_PARAMETER_SIZE, sizeof(size_t), &(dev->maxParamSzb), 0));
151 :     CHECK(clGetDeviceInfo (
152 :     devices[i], CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE, sizeof(cl_ulong), &(dev->maxConstBufSzb), 0));
153 :     CHECK(clGetDeviceInfo (
154 :     devices[i], CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(cl_ulong), &(dev->maxAllocSzb), 0));
155 :     cl_bool imagesSupported;
156 :     CHECK(clGetDeviceInfo (
157 :     devices[i], CL_DEVICE_IMAGE_SUPPORT, sizeof(cl_bool), &imagesSupported, 0));
158 :     if (imagesSupported) {
159 :     CHECK(clGetDeviceInfo (
160 :     devices[i], CL_DEVICE_IMAGE2D_MAX_WIDTH, sizeof(size_t), &(dev->maxImg2D[0]), 0));
161 :     CHECK(clGetDeviceInfo (
162 :     devices[i], CL_DEVICE_IMAGE3D_MAX_DEPTH, sizeof(size_t), &(dev->maxImg2D[1]), 0));
163 :     CHECK(clGetDeviceInfo (
164 :     devices[i], CL_DEVICE_IMAGE3D_MAX_WIDTH, sizeof(size_t), &(dev->maxImg3D[0]), 0));
165 :     CHECK(clGetDeviceInfo (
166 :     devices[i], CL_DEVICE_IMAGE3D_MAX_HEIGHT, sizeof(size_t), &(dev->maxImg3D[1]), 0));
167 :     CHECK(clGetDeviceInfo (
168 :     devices[i], CL_DEVICE_IMAGE3D_MAX_DEPTH, sizeof(size_t), &(dev->maxImg3D[2]), 0));
169 :     }
170 :     else {
171 :     dev->maxImg2D[0] = dev->maxImg2D[1] = 0;
172 :     dev->maxImg3D[0] = dev->maxImg3D[1] = dev->maxImg3D[2] = 0;
173 :     }
174 :     CHECK(clGetDeviceInfo (
175 :     devices[i], CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, sizeof(cl_uint), &(dev->charWid), 0));
176 :     CHECK(clGetDeviceInfo (
177 :     devices[i], CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, sizeof(cl_uint), &(dev->shortWid), 0));
178 :     CHECK(clGetDeviceInfo (
179 :     devices[i], CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, sizeof(cl_uint), &(dev->intWid), 0));
180 :     CHECK(clGetDeviceInfo (
181 :     devices[i], CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, sizeof(cl_uint), &(dev->longWid), 0));
182 :     CHECK(clGetDeviceInfo (
183 :     devices[i], CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, sizeof(cl_uint), &(dev->floatWid), 0));
184 :     CHECK(clGetDeviceInfo (
185 :     devices[i], CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, sizeof(cl_uint), &(dev->doubleWid), 0));
186 :     // determine the compute-unit width
187 :     if (isCPUDevice(dev))
188 :     dev->cuWidth = 1;
189 :     else if (isGPUDevice(dev))
190 :     if (strcasecmp("AMD", dev->vendor) == 0)
191 :     dev->cuWidth = 64; // an AMD wavefront is 64-wide
192 :     else if (strcasecmp("NVIDIA", dev->vendor) == 0)
193 :     dev->cuWidth = 32; // an NVIDIA warp is 32-wide
194 :     else
195 :     dev->cuWidth = 32; // FIXME: not sure what this should be?
196 :     else
197 :     dev->cuWidth = 1; // FIXME: not sure what this should be?
198 :    
199 :     }
200 :    
201 :     free(devices);
202 :    
203 :     return true;
204 :     }
205 :    
206 : jhr 2075 /*! \brief return the device with the given index.
207 :     */
208 :     DeviceInfo_t *Diderot_GetDeviceByIndex (CLInfo_t *clinfo, unsigned int platIdx, unsigned int devIdx)
209 : jhr 1671 {
210 : jhr 2075 if ((clinfo == 0)
211 :     || (clinfo->numPlatforms <= platIdx)
212 :     || (clinfo->platforms[platIdx].numDevices <= devIdx))
213 :     return 0;
214 :     else
215 :     return &(clinfo->platforms[platIdx].devices[devIdx]);
216 :     }
217 :    
218 :     void Diderot_PrintCLInfo (FILE *outS, CLInfo_t *clinfo)
219 :     {
220 : jhr 1671 if (clinfo->numPlatforms == 0) {
221 :     fprintf(outS, "No OpenCL platforms\n");
222 :     return;
223 :     }
224 :    
225 :     fprintf(outS, "OpenCL profile:\n");
226 : jhr 2075 for (unsigned int i = 0; i < clinfo->numPlatforms; i++) {
227 : jhr 1671 PlatformInfo_t *plat = &(clinfo->platforms[i]);
228 :     fprintf (outS, " Platform %d (%s)\n", i, plat->name);
229 : jhr 2075 for (unsigned int j = 0; j < plat->numDevices; j++) {
230 : jhr 1671 DeviceInfo_t *dev = &(plat->devices[j]);
231 :     if (dev->isAvail)
232 :     fprintf (outS, " Device %d.%d (%s):\n", i, j, dev->name);
233 :     else
234 :     fprintf (outS, " Device %d.%d (%s): **UNAVAILABLE**\n", i, j, dev->name);
235 :     fprintf (outS, " Vendor: %s\n", dev->vendor);
236 :     fprintf (outS, " OpenCL version: %d.%d\n",
237 :     dev->majorVersion, dev->minorVersion);
238 :     fprintf (outS, " Type: ");
239 :     if (isCPUDevice(dev)) fprintf (outS, " CPU");
240 :     if (isGPUDevice(dev)) fprintf (outS, " GPU");
241 :     if (dev->ty & CL_DEVICE_TYPE_ACCELERATOR) fprintf (outS, " ACCELERATOR");
242 :     if (dev->ty & CL_DEVICE_TYPE_DEFAULT) fprintf (outS, " DEFAULT");
243 :     fprintf (outS, "\n");
244 :     fprintf (outS, " Address size: %d\n", dev->addrBits);
245 :     fprintf (outS, " Endianess: %s\n", dev->littleEndian ? "LITTLE" : "BIG");
246 :     fprintf (outS, " Num. compute units: %d", dev->numCUs);
247 :     if (dev->cuWidth > 1)
248 :     fprintf (outS, " * %d\n", dev->cuWidth);
249 :     else
250 :     fprintf (outS, "\n");
251 :     fprintf (outS, " Max. dimensions: %d\n", dev->maxWIDims);
252 :     fprintf (outS, " Max. work group size: %ld\n", (long)(dev->maxWGSize));
253 :     fprintf (outS, " Max. work items: ");
254 :     for (int k = 0; k < dev->maxWIDims; k++)
255 :     fprintf (outS, "%s%ld", (k > 0) ? " x " : "", (long)(dev->maxWISize[k]));
256 :     fprintf (outS, "\n");
257 :     fprintf (outS, " Global memory size: %ld\n", (long)(dev->globalMemSzb));
258 :     fprintf (outS, " Local memory size: %ld\n", (long)(dev->localMemSzb));
259 :     fprintf (outS, " Max. parameter size: %ld\n", (long)(dev->maxParamSzb));
260 :     fprintf (outS, " Max. allocation size: %ld\n", (long)(dev->maxAllocSzb));
261 :     fprintf (outS, " Max. const. buffer size: %ld\n", (long)(dev->maxConstBufSzb));
262 :     fprintf (outS, " Max. const. arguments: %d\n", dev->maxConstArgs);
263 :     fprintf (outS, " Max. 2D image size: %ld x %ld\n",
264 :     (long)(dev->maxImg2D[0]), (long)(dev->maxImg2D[1]));
265 :     fprintf (outS, " Max. 3D image size: %ld x %ld x %ld\n",
266 :     (long)(dev->maxImg3D[0]), (long)(dev->maxImg3D[1]), (long)(dev->maxImg3D[2]));
267 :     fprintf (outS, " Prefered vector width: char%d, short%d, int%d, long%d\n",
268 :     dev->charWid, dev->shortWid, dev->intWid, dev->longWid);
269 :     fprintf (outS, " float%d", dev->floatWid);
270 :     if (dev->doubleWid > 0)
271 :     fprintf (outS, ", double%d\n", dev->doubleWid);
272 :     else
273 :     fprintf (outS, "\n");
274 :     }
275 :     }
276 :    
277 :     }
278 : jhr 2075
279 :     DeviceInfo_t *Diderot_DefaultCLDevice (CLInfo_t *clinfo)
280 :     {
281 :     if ((clinfo == 0) || (clinfo->numPlatforms == 0))
282 :     return 0;
283 :    
284 :     // we pick the first GPU device that we find
285 :     for (unsigned int i = 0; i < clinfo->numPlatforms; i++) {
286 :     PlatformInfo_t *plat = &(clinfo->platforms[i]);
287 :     for (unsigned int j = 0; j < plat->numDevices; j++) {
288 :     if (isGPUDevice(&(plat->devices[j])))
289 :     return &(plat->devices[j]);
290 :     }
291 :     }
292 :    
293 :     // otherwise we pick a CPU device
294 :     for (unsigned int i = 0; i < clinfo->numPlatforms; i++) {
295 :     PlatformInfo_t *plat = &(clinfo->platforms[i]);
296 :     for (unsigned int j = 0; j < plat->numDevices; j++) {
297 :     if (isCPUDevice(&(plat->devices[j])))
298 :     return &(plat->devices[j]);
299 :     }
300 :     }
301 :    
302 :     // otherwise return 0
303 :     return 0;
304 :    
305 :     }

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