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

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