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

SCM Repository

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

Annotation of /branches/pure-cfg/src/clinfo/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1251 - (view) (download) (as text)
Original Path: branches/pure-cfg/src/clinfo/clinfo.c

1 : jhr 1242 /*! \file clinfo.c
2 :     *
3 :     * \author John Reppy
4 :     *
5 :     * \brief This program is used to determine properties of the host machine's
6 :     * OpenCL support.
7 :     *
8 :     * Usage:
9 :     * clinfo cmd [args]
10 :     *
11 :     * where cmd is one of
12 :     * platforms -- prints a list of OpenCL platforms supported by the
13 :     * host.
14 : jhr 1251 * version platform -- prints version of OpenCL supported by the platform. The
15 :     * format is "OpenCL major.minor ..."
16 :     * devices platform -- prints a list of devices
17 : jhr 1242 * extensions device -- prints a list of extensions that are
18 :     * supported by the device
19 :     */
20 :    
21 :     /*
22 :     * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
23 :     * All rights reserved.
24 :     */
25 :    
26 :     #include "Diderot/config.h"
27 :     # ifdef HAVE_CL_CL_H
28 :     # include <CL/cl.h>
29 :     # elif defined(HAVE_OPENCL_CL_H)
30 :     # include <OpenCL/cl.h>
31 :     # else
32 :     # error no cl.h
33 :     # endif
34 :     #include <stdio.h>
35 : jhr 1251 #include <string.h>
36 :     #include <stdlib.h>
37 : jhr 1242
38 : jhr 1251 #define MAX_PLATFORMS 16
39 :    
40 :     static void usage ();
41 :     static int GetPlatforms (int max, cl_platform_id *platforms);
42 :     static cl_platform_id GetPlatformByName (const char *name);
43 :     static int GetDevices (cl_platform_id plat, cl_device_type ty, cl_device_id **devs);
44 :    
45 : jhr 1242 int main (int argc, const char **argv)
46 :     {
47 :     cl_platform_id platforms[MAX_PLATFORMS];
48 :    
49 : jhr 1251 // process command-line options
50 :     if ((strcmp(argv[1], "platforms") == 0) && (argc == 2)) {
51 :     char buf[256];
52 :     int numPlatforms = GetPlatforms (MAX_PLATFORMS, platforms);
53 :     for (int i = 0; i < numPlatforms; i++) {
54 :     clGetPlatformInfo (platforms[i], CL_PLATFORM_NAME, sizeof(buf), buf, 0);
55 :     printf ("%s\n", buf);
56 :     }
57 :     }
58 :     else if ((strcmp(argv[1], "version") == 0) && (argc == 3)) {
59 :     char buf[256];
60 :     cl_platform_id id = GetPlatformByName(argv[2]);
61 :     clGetPlatformInfo (id, CL_PLATFORM_VERSION, sizeof(buf), buf, 0);
62 :     printf ("%s\n", buf);
63 :     }
64 :     else if ((strcmp(argv[1], "devices") == 0) && (argc == 3)) {
65 :     cl_platform_id id = GetPlatformByName(argv[2]);
66 :     cl_device_id *devs;
67 :     char buf[256];
68 :     int numDevs = GetDevices (id, CL_DEVICE_TYPE_ALL, &devs);
69 :     for (int i = 0; i < numDevs; i++) {
70 :     clGetDeviceInfo (devs[i], CL_DEVICE_NAME, sizeof(buf), buf, 0);
71 :     printf ("%s\n", buf);
72 :     }
73 :     }
74 :     else if ((strcmp(argv[1], "extensions") == 0) && (argc == 3)) {
75 :     cl_platform_id id = GetPlatformByName(argv[2]);
76 :     size_t bufSz;
77 :     // get the size of the extensions buffer
78 :     clGetPlatformInfo (id, CL_PLATFORM_EXTENSIONS, 0, 0, &bufSz);
79 :     char *buf = (char *)malloc(bufSz);
80 :     clGetPlatformInfo (id, CL_PLATFORM_EXTENSIONS, bufSz, buf, 0);
81 :     char *p = buf;
82 :     while (p != 0) {
83 :     p = strchr(p, ' ');
84 :     if (p != 0) *p = '\n';
85 :     }
86 :     printf ("%s", buf);
87 :     free (buf);
88 :     }
89 :     else
90 :     usage();
91 :    
92 :     }
93 :    
94 :     static void usage ()
95 :     {
96 :     exit (1);
97 :     }
98 :    
99 :     static int GetPlatforms (int max, cl_platform_id *platforms)
100 :     {
101 :     cl_uint n;
102 : jhr 1242 // get number of OpenCL platforms
103 : jhr 1251 if (clGetPlatformIDs (max, platforms, &n) != CL_SUCCESS) {
104 : jhr 1242 fprintf(stderr, "unable to get platform IDs\n");
105 : jhr 1251 exit (1);
106 : jhr 1242 }
107 : jhr 1251 return n;
108 :     }
109 : jhr 1242
110 : jhr 1251 static cl_platform_id GetPlatformByName (const char *name)
111 :     {
112 :     cl_platform_id platforms[MAX_PLATFORMS];
113 :     char buf[256];
114 : jhr 1242
115 : jhr 1251 // get number of OpenCL platforms
116 :     cl_uint n = GetPlatforms (MAX_PLATFORMS, platforms);
117 :     for (cl_uint i = 0; i < n; i++) {
118 :     clGetPlatformInfo (platforms[i], CL_PLATFORM_NAME, sizeof(buf), buf, 0);
119 :     if (strcmp(buf, name) == 0) return platforms[i];
120 :     }
121 : jhr 1242
122 : jhr 1251 fprintf (stderr, "platform %s not found\n", name);
123 :     exit (1);
124 :    
125 : jhr 1242 }
126 : jhr 1251
127 :     static int GetDevices (cl_platform_id plat, cl_device_type ty, cl_device_id **devs)
128 :     {
129 :     cl_uint numDevs;
130 :    
131 :     // get number of devices for the platform
132 :     clGetDeviceIDs (plat, ty, 0, 0, &numDevs);
133 :    
134 :     cl_device_id *devices = (cl_device_id *)malloc(numDevs * sizeof(cl_device_id));
135 :     clGetDeviceIDs (plat, ty, numDevs, devices, 0);
136 :    
137 :     *devs = devices;
138 :     return numDevs;
139 :     }

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