1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_PLATFORM_HPP
12 #define BOOST_COMPUTE_PLATFORM_HPP
18 #include <boost/algorithm/string/split.hpp>
19 #include <boost/algorithm/string/classification.hpp>
21 #include <boost/compute/cl.hpp>
22 #include <boost/compute/device.hpp>
23 #include <boost/compute/detail/get_object_info.hpp>
29 /// \brief A compute platform.
31 /// The platform class provides an interface to an OpenCL platform.
33 /// To obtain a list of all platforms on the system use the
34 /// system::platforms() method.
36 /// \see device, context
40 /// Creates a new platform object for \p id.
41 explicit platform(cl_platform_id id)
46 /// Creates a new platform as a copy of \p other.
47 platform(const platform &other)
48 : m_platform(other.m_platform)
52 /// Copies the platform id from \p other.
53 platform& operator=(const platform &other)
56 m_platform = other.m_platform;
62 /// Destroys the platform object.
67 /// Returns the ID of the platform.
68 cl_platform_id id() const
73 /// Returns the name of the platform.
74 std::string name() const
76 return get_info<std::string>(CL_PLATFORM_NAME);
79 /// Returns the name of the vendor for the platform.
80 std::string vendor() const
82 return get_info<std::string>(CL_PLATFORM_VENDOR);
85 /// Returns the profile string for the platform.
86 std::string profile() const
88 return get_info<std::string>(CL_PLATFORM_PROFILE);
91 /// Returns the version string for the platform.
92 std::string version() const
94 return get_info<std::string>(CL_PLATFORM_VERSION);
97 /// Returns a list of extensions supported by the platform.
98 std::vector<std::string> extensions() const
100 std::string extensions_string =
101 get_info<std::string>(CL_PLATFORM_EXTENSIONS);
102 std::vector<std::string> extensions_vector;
103 boost::split(extensions_vector,
105 boost::is_any_of("\t "),
106 boost::token_compress_on);
107 return extensions_vector;
110 /// Returns \c true if the platform supports the extension with
112 bool supports_extension(const std::string &name) const
114 const std::vector<std::string> extensions = this->extensions();
117 extensions.begin(), extensions.end(), name) != extensions.end();
120 /// Returns a list of devices on the platform.
121 std::vector<device> devices(cl_device_type type = CL_DEVICE_TYPE_ALL) const
123 size_t count = device_count(type);
125 // no devices for this platform
126 return std::vector<device>();
129 std::vector<cl_device_id> device_ids(count);
130 cl_int ret = clGetDeviceIDs(m_platform,
132 static_cast<cl_uint>(count),
135 if(ret != CL_SUCCESS){
136 BOOST_THROW_EXCEPTION(opencl_error(ret));
139 std::vector<device> devices;
140 for(cl_uint i = 0; i < count; i++){
141 devices.push_back(device(device_ids[i]));
147 /// Returns the number of devices on the platform.
148 size_t device_count(cl_device_type type = CL_DEVICE_TYPE_ALL) const
151 cl_int ret = clGetDeviceIDs(m_platform, type, 0, 0, &count);
152 if(ret != CL_SUCCESS){
153 if(ret == CL_DEVICE_NOT_FOUND){
154 // no devices for this platform
158 // something else went wrong
159 BOOST_THROW_EXCEPTION(opencl_error(ret));
166 /// Returns information about the platform.
168 /// \see_opencl_ref{clGetPlatformInfo}
170 T get_info(cl_platform_info info) const
172 return detail::get_object_info<T>(clGetPlatformInfo, m_platform, info);
177 typename detail::get_object_info_type<platform, Enum>::type
180 /// Returns the address of the \p function_name extension
181 /// function. Returns \c 0 if \p function_name is invalid.
182 void* get_extension_function_address(const char *function_name) const
184 #ifdef BOOST_COMPUTE_CL_VERSION_1_2
185 return clGetExtensionFunctionAddressForPlatform(m_platform,
188 return clGetExtensionFunctionAddress(function_name);
192 /// Requests that the platform unload any compiler resources.
193 void unload_compiler()
195 #ifdef BOOST_COMPUTE_CL_VERSION_1_2
196 clUnloadPlatformCompiler(m_platform);
202 /// Returns \c true if the platform is the same at \p other.
203 bool operator==(const platform &other) const
205 return m_platform == other.m_platform;
208 /// Returns \c true if the platform is different from \p other.
209 bool operator!=(const platform &other) const
211 return m_platform != other.m_platform;
214 /// Returns \c true if the platform OpenCL version is major.minor
215 /// or newer; otherwise returns \c false.
216 bool check_version(int major, int minor) const
218 std::stringstream stream;
221 int actual_major, actual_minor;
222 stream.ignore(7); // 'OpenCL '
223 stream >> actual_major;
224 stream.ignore(1); // '.'
225 stream >> actual_minor;
227 return actual_major > major ||
228 (actual_major == major && actual_minor >= minor);
232 cl_platform_id m_platform;
235 /// \internal_ define get_info() specializations for platform
236 BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(platform,
237 ((std::string, CL_PLATFORM_PROFILE))
238 ((std::string, CL_PLATFORM_VERSION))
239 ((std::string, CL_PLATFORM_NAME))
240 ((std::string, CL_PLATFORM_VENDOR))
241 ((std::string, CL_PLATFORM_EXTENSIONS))
244 #ifdef BOOST_COMPUTE_CL_VERSION_2_1
245 BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(platform,
246 ((cl_ulong, CL_PLATFORM_HOST_TIMER_RESOLUTION))
248 #endif // BOOST_COMPUTE_CL_VERSION_2_1
250 inline boost::compute::platform device::platform() const
252 return boost::compute::platform(get_info<CL_DEVICE_PLATFORM>());
255 } // end compute namespace
256 } // end boost namespace
258 #endif // BOOST_COMPUTE_PLATFORM_HPP