• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
3 //
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
10 
11 // See boost/compute/detail/diagnostic.hpp
12 // GCC
13 #if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
14 #define BOOST_COMPUTE_GCC_DIAG_STR(s) #s
15 #define BOOST_COMPUTE_GCC_DIAG_JOINSTR(x,y) BOOST_COMPUTE_GCC_DIAG_STR(x ## y)
16 # define BOOST_COMPUTE_GCC_DIAG_DO_PRAGMA(x) _Pragma (#x)
17 # define BOOST_COMPUTE_GCC_DIAG_PRAGMA(x) BOOST_COMPUTE_GCC_DIAG_DO_PRAGMA(GCC diagnostic x)
18 # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
19 #  define BOOST_COMPUTE_GCC_DIAG_OFF(x) BOOST_COMPUTE_GCC_DIAG_PRAGMA(push) \
20       BOOST_COMPUTE_GCC_DIAG_PRAGMA(ignored BOOST_COMPUTE_GCC_DIAG_JOINSTR(-W,x))
21 #  define BOOST_COMPUTE_GCC_DIAG_ON(x) BOOST_COMPUTE_GCC_DIAG_PRAGMA(pop)
22 # else
23 #  define BOOST_COMPUTE_GCC_DIAG_OFF(x) \
24       BOOST_COMPUTE_GCC_DIAG_PRAGMA(ignored BOOST_COMPUTE_GCC_DIAG_JOINSTR(-W,x))
25 #  define BOOST_COMPUTE_GCC_DIAG_ON(x) \
26       BOOST_COMPUTE_GCC_DIAG_PRAGMA(warning BOOST_COMPUTE_GCC_DIAG_JOINSTR(-W,x))
27 # endif
28 #else // Ensure these macros do nothing for other compilers.
29 # define BOOST_COMPUTE_GCC_DIAG_OFF(x)
30 # define BOOST_COMPUTE_GCC_DIAG_ON(x)
31 #endif
32 
33 // Clang
34 #ifdef __clang__
35 #  define BOOST_COMPUTE_CLANG_DIAG_STR(s) # s
36 // stringize s to "no-sign-compare"
37 #  define BOOST_COMPUTE_CLANG_DIAG_JOINSTR(x,y) BOOST_COMPUTE_CLANG_DIAG_STR(x ## y)
38 //  join -W with no-unused-variable to "-Wno-sign-compare"
39 #  define BOOST_COMPUTE_CLANG_DIAG_DO_PRAGMA(x) _Pragma (#x)
40 // _Pragma is unary operator  #pragma ("")
41 #  define BOOST_COMPUTE_CLANG_DIAG_PRAGMA(x) \
42       BOOST_COMPUTE_CLANG_DIAG_DO_PRAGMA(clang diagnostic x)
43 #  define BOOST_COMPUTE_CLANG_DIAG_OFF(x) BOOST_COMPUTE_CLANG_DIAG_PRAGMA(push) \
44       BOOST_COMPUTE_CLANG_DIAG_PRAGMA(ignored BOOST_COMPUTE_CLANG_DIAG_JOINSTR(-W,x))
45 // For example: #pragma clang diagnostic ignored "-Wno-sign-compare"
46 #  define BOOST_COMPUTE_CLANG_DIAG_ON(x) BOOST_COMPUTE_CLANG_DIAG_PRAGMA(pop)
47 // For example: #pragma clang diagnostic warning "-Wno-sign-compare"
48 #else // Ensure these macros do nothing for other compilers.
49 #  define BOOST_COMPUTE_CLANG_DIAG_OFF(x)
50 #  define BOOST_COMPUTE_CLANG_DIAG_ON(x)
51 #  define BOOST_COMPUTE_CLANG_DIAG_PRAGMA(x)
52 #endif
53 
54 // MSVC
55 #if defined(_MSC_VER)
56 #  define BOOST_COMPUTE_MSVC_DIAG_DO_PRAGMA(x) __pragma(x)
57 #  define BOOST_COMPUTE_MSVC_DIAG_PRAGMA(x) \
58           BOOST_COMPUTE_MSVC_DIAG_DO_PRAGMA(warning(x))
59 #  define BOOST_COMPUTE_MSVC_DIAG_OFF(x) BOOST_COMPUTE_MSVC_DIAG_PRAGMA(push) \
60           BOOST_COMPUTE_MSVC_DIAG_PRAGMA(disable: x)
61 #  define BOOST_COMPUTE_MSVC_DIAG_ON(x) BOOST_COMPUTE_MSVC_DIAG_PRAGMA(pop)
62 #else // Ensure these macros do nothing for other compilers.
63 #  define BOOST_COMPUTE_MSVC_DIAG_OFF(x)
64 #  define BOOST_COMPUTE_MSVC_DIAG_ON(x)
65 #endif
66 
67 #include <iostream>
68 
69 // include the proper opencl header for the system
70 #if defined(__APPLE__)
71 #include <OpenCL/cl.h>
72 #else
73 #include <CL/cl.h>
74 #endif
75 
76 // the opencl_test example displays the opencl platforms and devices found
77 // on the system using the opencl api directly. if this test fails to compile
78 // and/or run, there is a problem with the opencl implementation found on the
79 // system. users should ensure this test runs successfuly before using any of
80 // the boost.compute apis (which depend on a working opencl implementation).
main()81 int main()
82 {
83     // Suppress deprecated declarations warning
84     BOOST_COMPUTE_MSVC_DIAG_OFF(4996); // MSVC
85     BOOST_COMPUTE_GCC_DIAG_OFF(deprecated-declarations); // GCC
86     BOOST_COMPUTE_CLANG_DIAG_OFF(deprecated-declarations); // Clang
87 
88     // query number of opencl platforms
89     cl_uint num_platforms = 0;
90     cl_int ret = clGetPlatformIDs(0, NULL, &num_platforms);
91     if(ret != CL_SUCCESS){
92         std::cerr << "failed to query platforms: " << ret << std::endl;
93         return -1;
94     }
95 
96     // check that at least one platform was found
97     if(num_platforms == 0){
98         std::cerr << "found 0 platforms" << std::endl;
99         return 0;
100     }
101 
102     // get platform ids
103     cl_platform_id *platforms = new cl_platform_id[num_platforms];
104     clGetPlatformIDs(num_platforms, platforms, NULL);
105 
106     // iterate through each platform and query its devices
107     for(cl_uint i = 0; i < num_platforms; i++){
108         cl_platform_id platform = platforms[i];
109 
110         // query number of opencl devices
111         cl_uint num_devices = 0;
112         ret = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices);
113         if(ret != CL_SUCCESS){
114             std::cerr << "failed to lookup devices for platform " << i << std::endl;
115             continue;
116         }
117 
118         // print number of devices found
119         std::cout << "platform " << i << " has " << num_devices << " devices:" << std::endl;
120 
121         // get device ids for the platform
122         cl_device_id *devices = new cl_device_id[num_devices];
123         ret = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, num_devices, devices, NULL);
124         if(ret != CL_SUCCESS){
125             std::cerr << "failed to query platform devices" << std::endl;
126             delete[] devices;
127             continue;
128         }
129 
130         // iterate through each device on the platform and print its name
131         for(cl_uint j = 0; j < num_devices; j++){
132             cl_device_id device = devices[j];
133 
134             // get length of the device name string
135             size_t name_length = 0;
136             ret = clGetDeviceInfo(device, CL_DEVICE_NAME, 0, NULL, &name_length);
137             if(ret != CL_SUCCESS){
138                 std::cerr << "failed to query device name length for device " << j << std::endl;
139                 continue;
140             }
141 
142             // get the device name string
143             char *name = new char[name_length];
144             ret = clGetDeviceInfo(device, CL_DEVICE_NAME, name_length, name, NULL);
145             if(ret != CL_SUCCESS){
146                 std::cerr << "failed to query device name string for device " << j << std::endl;
147                 delete[] name;
148                 continue;
149             }
150 
151             // print out the device name
152             std::cout << "  device: " << name << std::endl;
153 
154             delete[] name;
155         }
156         delete[] devices;
157     }
158     delete[] platforms;
159 
160     BOOST_COMPUTE_CLANG_DIAG_ON(deprecated-declarations); // Clang
161     BOOST_COMPUTE_GCC_DIAG_ON(deprecated-declarations); // GCC
162     BOOST_COMPUTE_MSVC_DIAG_OFF(4996); // MSVC
163 
164     return 0;
165 }
166