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

SCM Repository

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

Annotation of /trunk/src/lib/cl-target/clinfo.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2060 - (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 : lamonts 2060 devices[i], CL_DEVICE_VENDOR_ID, sizeof(cl_uint), &(dev->vendorId), 0));
110 :     CHECK(clGetDeviceInfo (
111 : jhr 1671 devices[i], CL_DEVICE_TYPE, sizeof(cl_device_type), &(dev->ty), 0));
112 :     CHECK(clGetDeviceInfo (
113 :     devices[i], CL_DEVICE_AVAILABLE, sizeof(cl_bool), &(dev->isAvail), 0));
114 :     CHECK(clGetDeviceInfo (
115 :     devices[i], CL_DEVICE_ADDRESS_BITS, sizeof(cl_uint), &(dev->addrBits), 0));
116 :     CHECK(clGetDeviceInfo (
117 :     devices[i], CL_DEVICE_ENDIAN_LITTLE, sizeof(cl_bool), &(dev->littleEndian), 0));
118 :     CHECK(clGetDeviceInfo (
119 :     devices[i], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cl_uint), &(dev->numCUs), 0));
120 :     CHECK(clGetDeviceInfo (
121 :     devices[i], CL_DEVICE_MAX_CONSTANT_ARGS, sizeof(cl_uint), &(dev->maxConstArgs), 0));
122 :     CHECK(clGetDeviceInfo (
123 :     devices[i], CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(cl_uint), &(dev->maxWIDims), 0));
124 :     CHECK(clGetDeviceInfo (
125 :     devices[i], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), &(dev->maxWGSize), 0));
126 :     size_t szb = sizeof(size_t) * dev->maxWIDims;
127 :     dev->maxWISize = (size_t *) CheckedAlloc (szb);
128 :     CHECK(clGetDeviceInfo (devices[i], CL_DEVICE_MAX_WORK_ITEM_SIZES, szb, dev->maxWISize, 0));
129 :     CHECK(clGetDeviceInfo (
130 :     devices[i], CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(cl_ulong), &(dev->globalMemSzb), 0));
131 :     CHECK(clGetDeviceInfo (
132 :     devices[i], CL_DEVICE_LOCAL_MEM_SIZE, sizeof(cl_ulong), &(dev->localMemSzb), 0));
133 :     CHECK(clGetDeviceInfo (
134 :     devices[i], CL_DEVICE_MAX_PARAMETER_SIZE, sizeof(size_t), &(dev->maxParamSzb), 0));
135 :     CHECK(clGetDeviceInfo (
136 :     devices[i], CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE, sizeof(cl_ulong), &(dev->maxConstBufSzb), 0));
137 :     CHECK(clGetDeviceInfo (
138 :     devices[i], CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(cl_ulong), &(dev->maxAllocSzb), 0));
139 :     cl_bool imagesSupported;
140 :     CHECK(clGetDeviceInfo (
141 :     devices[i], CL_DEVICE_IMAGE_SUPPORT, sizeof(cl_bool), &imagesSupported, 0));
142 :     if (imagesSupported) {
143 :     CHECK(clGetDeviceInfo (
144 :     devices[i], CL_DEVICE_IMAGE2D_MAX_WIDTH, sizeof(size_t), &(dev->maxImg2D[0]), 0));
145 :     CHECK(clGetDeviceInfo (
146 :     devices[i], CL_DEVICE_IMAGE3D_MAX_DEPTH, sizeof(size_t), &(dev->maxImg2D[1]), 0));
147 :     CHECK(clGetDeviceInfo (
148 :     devices[i], CL_DEVICE_IMAGE3D_MAX_WIDTH, sizeof(size_t), &(dev->maxImg3D[0]), 0));
149 :     CHECK(clGetDeviceInfo (
150 :     devices[i], CL_DEVICE_IMAGE3D_MAX_HEIGHT, sizeof(size_t), &(dev->maxImg3D[1]), 0));
151 :     CHECK(clGetDeviceInfo (
152 :     devices[i], CL_DEVICE_IMAGE3D_MAX_DEPTH, sizeof(size_t), &(dev->maxImg3D[2]), 0));
153 :     }
154 :     else {
155 :     dev->maxImg2D[0] = dev->maxImg2D[1] = 0;
156 :     dev->maxImg3D[0] = dev->maxImg3D[1] = dev->maxImg3D[2] = 0;
157 :     }
158 :     CHECK(clGetDeviceInfo (
159 :     devices[i], CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, sizeof(cl_uint), &(dev->charWid), 0));
160 :     CHECK(clGetDeviceInfo (
161 :     devices[i], CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, sizeof(cl_uint), &(dev->shortWid), 0));
162 :     CHECK(clGetDeviceInfo (
163 :     devices[i], CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, sizeof(cl_uint), &(dev->intWid), 0));
164 :     CHECK(clGetDeviceInfo (
165 :     devices[i], CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, sizeof(cl_uint), &(dev->longWid), 0));
166 :     CHECK(clGetDeviceInfo (
167 :     devices[i], CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, sizeof(cl_uint), &(dev->floatWid), 0));
168 :     CHECK(clGetDeviceInfo (
169 :     devices[i], CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, sizeof(cl_uint), &(dev->doubleWid), 0));
170 :     // determine the compute-unit width
171 : lamonts 2060
172 : jhr 1671 if (isCPUDevice(dev))
173 :     dev->cuWidth = 1;
174 :     else if (isGPUDevice(dev))
175 : lamonts 2060 if (dev->vendorId == AMD_VENDOR_ID)
176 : jhr 1671 dev->cuWidth = 64; // an AMD wavefront is 64-wide
177 : lamonts 2060 else if (dev->vendorId == NVIDIA_VENDOR_ID)
178 : jhr 1671 dev->cuWidth = 32; // an NVIDIA warp is 32-wide
179 :     else
180 :     dev->cuWidth = 32; // FIXME: not sure what this should be?
181 :     else
182 :     dev->cuWidth = 1; // FIXME: not sure what this should be?
183 :    
184 :     }
185 :    
186 :     free(devices);
187 :    
188 :     return true;
189 :     }
190 :    
191 :     void PrintCLInfo (FILE *outS, CLInfo_t *clinfo)
192 :     {
193 :     if (clinfo->numPlatforms == 0) {
194 :     fprintf(outS, "No OpenCL platforms\n");
195 :     return;
196 :     }
197 :    
198 :     fprintf(outS, "OpenCL profile:\n");
199 :     for (int i = 0; i < clinfo->numPlatforms; i++) {
200 :     PlatformInfo_t *plat = &(clinfo->platforms[i]);
201 :     fprintf (outS, " Platform %d (%s)\n", i, plat->name);
202 :     for (int j = 0; j < plat->numDevices; j++) {
203 :     DeviceInfo_t *dev = &(plat->devices[j]);
204 :     if (dev->isAvail)
205 :     fprintf (outS, " Device %d.%d (%s):\n", i, j, dev->name);
206 :     else
207 :     fprintf (outS, " Device %d.%d (%s): **UNAVAILABLE**\n", i, j, dev->name);
208 :     fprintf (outS, " Vendor: %s\n", dev->vendor);
209 : lamonts 2060 fprintf (outS, " Vendor Id: %d\n", dev->vendorId);
210 : jhr 1671 fprintf (outS, " OpenCL version: %d.%d\n",
211 :     dev->majorVersion, dev->minorVersion);
212 :     fprintf (outS, " Type: ");
213 :     if (isCPUDevice(dev)) fprintf (outS, " CPU");
214 :     if (isGPUDevice(dev)) fprintf (outS, " GPU");
215 :     if (dev->ty & CL_DEVICE_TYPE_ACCELERATOR) fprintf (outS, " ACCELERATOR");
216 :     if (dev->ty & CL_DEVICE_TYPE_DEFAULT) fprintf (outS, " DEFAULT");
217 :     fprintf (outS, "\n");
218 :     fprintf (outS, " Address size: %d\n", dev->addrBits);
219 :     fprintf (outS, " Endianess: %s\n", dev->littleEndian ? "LITTLE" : "BIG");
220 :     fprintf (outS, " Num. compute units: %d", dev->numCUs);
221 :     if (dev->cuWidth > 1)
222 :     fprintf (outS, " * %d\n", dev->cuWidth);
223 :     else
224 :     fprintf (outS, "\n");
225 :     fprintf (outS, " Max. dimensions: %d\n", dev->maxWIDims);
226 :     fprintf (outS, " Max. work group size: %ld\n", (long)(dev->maxWGSize));
227 :     fprintf (outS, " Max. work items: ");
228 :     for (int k = 0; k < dev->maxWIDims; k++)
229 :     fprintf (outS, "%s%ld", (k > 0) ? " x " : "", (long)(dev->maxWISize[k]));
230 :     fprintf (outS, "\n");
231 :     fprintf (outS, " Global memory size: %ld\n", (long)(dev->globalMemSzb));
232 :     fprintf (outS, " Local memory size: %ld\n", (long)(dev->localMemSzb));
233 :     fprintf (outS, " Max. parameter size: %ld\n", (long)(dev->maxParamSzb));
234 :     fprintf (outS, " Max. allocation size: %ld\n", (long)(dev->maxAllocSzb));
235 :     fprintf (outS, " Max. const. buffer size: %ld\n", (long)(dev->maxConstBufSzb));
236 :     fprintf (outS, " Max. const. arguments: %d\n", dev->maxConstArgs);
237 :     fprintf (outS, " Max. 2D image size: %ld x %ld\n",
238 :     (long)(dev->maxImg2D[0]), (long)(dev->maxImg2D[1]));
239 :     fprintf (outS, " Max. 3D image size: %ld x %ld x %ld\n",
240 :     (long)(dev->maxImg3D[0]), (long)(dev->maxImg3D[1]), (long)(dev->maxImg3D[2]));
241 :     fprintf (outS, " Prefered vector width: char%d, short%d, int%d, long%d\n",
242 :     dev->charWid, dev->shortWid, dev->intWid, dev->longWid);
243 :     fprintf (outS, " float%d", dev->floatWid);
244 :     if (dev->doubleWid > 0)
245 :     fprintf (outS, ", double%d\n", dev->doubleWid);
246 :     else
247 :     fprintf (outS, "\n");
248 :     }
249 :     }
250 :    
251 :     }

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