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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2074, Fri Nov 2 22:13:59 2012 UTC revision 2075, Sat Nov 3 13:27:02 2012 UTC
# Line 11  Line 11 
11   * All rights reserved.   * All rights reserved.
12   */   */
13    
14  #include "clinfo.h"  #include "Diderot/clinfo.h"
15  #include <stdio.h>  #include <stdio.h>
16  #include <string.h>  #include <string.h>
17  #include <stdlib.h>  #include <stdlib.h>
18    
19  #define MAX_PLATFORMS   16  #define MAX_PLATFORMS   16
20    
21  static bool GetDevices (PlatformInfo_t *plat);  static bool GetDevices (unsigned int platIdx, PlatformInfo_t *plat);
22    
23  #define CHECK(call)     do {                                                    \  #define CHECK(call)     do {                                                    \
24          cl_int __sts = call;                                                    \          cl_int __sts = call;                                                    \
# Line 28  Line 28 
28          }                                                                       \          }                                                                       \
29      } while (0)      } while (0)
30    
31    //! Checked memory allocation
32    STATIC_INLINE void *CheckedAlloc (size_t szb)
33    {
34        void *p = malloc(szb);
35        if (p == 0) {
36            fprintf (stderr, "fatal error: unable to allocate %d bytes of memory\n", (int)szb);
37            exit (1);
38        }
39        return p;
40    }
41    
42    #define NEW(ty)         (ty *)CheckedAlloc(sizeof(ty))
43    #define NEWVEC(ty,n)    (ty *)CheckedAlloc(sizeof(ty) * (n))
44    #define NEWSTR(s)       strcpy((char *)CheckedAlloc(strlen(s)+1), s)
45    #define FREE(p)         do { if (p != 0) free(p); } while(0)
46    
47  CLInfo_t *GetCLInfo ()  CLInfo_t *Diderot_GetCLInfo ()
48  {  {
49      cl_platform_id      platformIDs[MAX_PLATFORMS];      cl_platform_id      platformIDs[MAX_PLATFORMS];
50      char                buf[512];      char                buf[512];
# Line 40  Line 55 
55      CHECK(clGetPlatformIDs (MAX_PLATFORMS, platformIDs, &numPlatforms));      CHECK(clGetPlatformIDs (MAX_PLATFORMS, platformIDs, &numPlatforms));
56    
57      PlatformInfo_t *plats = NEWVEC(PlatformInfo_t, numPlatforms);      PlatformInfo_t *plats = NEWVEC(PlatformInfo_t, numPlatforms);
58      for (int i = 0;  i < numPlatforms;  i++) {      for (unsigned int i = 0;  i < numPlatforms;  i++) {
59          clGetPlatformInfo (platformIDs[i], CL_PLATFORM_NAME, sizeof(buf), buf, 0);          clGetPlatformInfo (platformIDs[i], CL_PLATFORM_NAME, sizeof(buf), buf, 0);
60          plats[i].name = NEWSTR(buf);          plats[i].name = NEWSTR(buf);
61          plats[i].id = platformIDs[i];          plats[i].id = platformIDs[i];
62        // get OpenCL version info        // get OpenCL version info
63          clGetPlatformInfo (platformIDs[i], CL_PLATFORM_VERSION, sizeof(buf), buf, 0);          clGetPlatformInfo (platformIDs[i], CL_PLATFORM_VERSION, sizeof(buf), buf, 0);
 // FIXME: parser version info  
64        // get extension info        // get extension info
65  // FIXME: TODO  // FIXME: TODO
66        // get device info        // get device info
67          if (! GetDevices (&(plats[i]))) {          if (! GetDevices (i, &(plats[i]))) {
68            // free storage            // free storage
69              for (int j = 0;  j <= i;  j++) {              for (unsigned int j = 0;  j <= i;  j++) {
70                  free (plats[i].name);                  free (plats[i].name);
71              }              }
72              free (plats);              free (plats);
# Line 74  Line 88 
88      return NEWSTR(buf);      return NEWSTR(buf);
89  }  }
90    
91  static bool GetDevices (PlatformInfo_t *plat)  static bool GetDevices (unsigned int platIdx, PlatformInfo_t *plat)
92  {  {
93      cl_uint             numDevs;      cl_uint             numDevs;
94      char                buf[512];      char                buf[512];
# Line 94  Line 108 
108      cl_device_id *devices = NEWVEC(cl_device_id, numDevs);      cl_device_id *devices = NEWVEC(cl_device_id, numDevs);
109      CHECK(clGetDeviceIDs (plat->id, CL_DEVICE_TYPE_ALL, numDevs, devices, 0));      CHECK(clGetDeviceIDs (plat->id, CL_DEVICE_TYPE_ALL, numDevs, devices, 0));
110    
111      for (int i = 0;  i < numDevs;  i++) {      for (unsigned int i = 0;  i < numDevs;  i++) {
112          DeviceInfo_t *dev = &(plat->devices[i]);          DeviceInfo_t *dev = &(plat->devices[i]);
113            dev->index[0] = platIdx;
114            dev->index[1] = i;
115          dev->name = GetStringInfo(devices[i], CL_DEVICE_NAME);          dev->name = GetStringInfo(devices[i], CL_DEVICE_NAME);
116          dev->vendor = GetStringInfo(devices[i], CL_DEVICE_VENDOR);          dev->vendor = GetStringInfo(devices[i], CL_DEVICE_VENDOR);
117          dev->id = devices[i];          dev->id = devices[i];
# Line 185  Line 201 
201      return true;      return true;
202  }  }
203    
204  void PrintCLInfo (FILE *outS, CLInfo_t *clinfo)  /*! \brief return the device with the given index.
205     */
206    DeviceInfo_t *Diderot_GetDeviceByIndex (CLInfo_t *clinfo, unsigned int platIdx, unsigned int devIdx)
207    {
208        if ((clinfo == 0)
209        ||  (clinfo->numPlatforms <= platIdx)
210        ||  (clinfo->platforms[platIdx].numDevices <= devIdx))
211            return 0;
212        else
213            return &(clinfo->platforms[platIdx].devices[devIdx]);
214    }
215    
216    void Diderot_PrintCLInfo (FILE *outS, CLInfo_t *clinfo)
217  {  {
218      if (clinfo->numPlatforms == 0) {      if (clinfo->numPlatforms == 0) {
219          fprintf(outS, "No OpenCL platforms\n");          fprintf(outS, "No OpenCL platforms\n");
# Line 193  Line 221 
221      }      }
222    
223      fprintf(outS, "OpenCL profile:\n");      fprintf(outS, "OpenCL profile:\n");
224      for (int i = 0;  i < clinfo->numPlatforms;  i++) {      for (unsigned int i = 0;  i < clinfo->numPlatforms;  i++) {
225          PlatformInfo_t *plat = &(clinfo->platforms[i]);          PlatformInfo_t *plat = &(clinfo->platforms[i]);
226          fprintf (outS, "  Platform %d (%s)\n", i, plat->name);          fprintf (outS, "  Platform %d (%s)\n", i, plat->name);
227          for (int j = 0;  j < plat->numDevices;  j++) {          for (unsigned int j = 0;  j < plat->numDevices;  j++) {
228              DeviceInfo_t *dev = &(plat->devices[j]);              DeviceInfo_t *dev = &(plat->devices[j]);
229              if (dev->isAvail)              if (dev->isAvail)
230                  fprintf (outS, "    Device %d.%d (%s):\n", i, j, dev->name);                  fprintf (outS, "    Device %d.%d (%s):\n", i, j, dev->name);
# Line 245  Line 273 
273      }      }
274    
275  }  }
276    
277    DeviceInfo_t *Diderot_DefaultCLDevice (CLInfo_t *clinfo)
278    {
279        if ((clinfo == 0) || (clinfo->numPlatforms == 0))
280            return 0;
281    
282      // we pick the first GPU device that we find
283        for (unsigned int i = 0;  i < clinfo->numPlatforms;  i++) {
284            PlatformInfo_t *plat = &(clinfo->platforms[i]);
285            for (unsigned int j = 0;  j < plat->numDevices;  j++) {
286                if (isGPUDevice(&(plat->devices[j])))
287                    return &(plat->devices[j]);
288            }
289        }
290    
291      // otherwise we pick a CPU device
292        for (unsigned int i = 0;  i < clinfo->numPlatforms;  i++) {
293            PlatformInfo_t *plat = &(clinfo->platforms[i]);
294            for (unsigned int j = 0;  j < plat->numDevices;  j++) {
295                if (isCPUDevice(&(plat->devices[j])))
296                    return &(plat->devices[j]);
297            }
298        }
299    
300      // otherwise return 0
301        return 0;
302    
303    }

Legend:
Removed from v.2074  
changed lines
  Added in v.2075

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