platform.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 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. #ifndef BOOST_COMPUTE_PLATFORM_HPP
  11. #define BOOST_COMPUTE_PLATFORM_HPP
  12. #include <algorithm>
  13. #include <string>
  14. #include <vector>
  15. #include <boost/algorithm/string/split.hpp>
  16. #include <boost/algorithm/string/classification.hpp>
  17. #include <boost/compute/cl.hpp>
  18. #include <boost/compute/device.hpp>
  19. #include <boost/compute/detail/get_object_info.hpp>
  20. namespace boost {
  21. namespace compute {
  22. /// \class platform
  23. /// \brief A compute platform.
  24. ///
  25. /// The platform class provides an interface to an OpenCL platform.
  26. ///
  27. /// To obtain a list of all platforms on the system use the
  28. /// system::platforms() method.
  29. ///
  30. /// \see device, context
  31. class platform
  32. {
  33. public:
  34. /// Creates a new platform object for \p id.
  35. explicit platform(cl_platform_id id)
  36. : m_platform(id)
  37. {
  38. }
  39. /// Creates a new platform as a copy of \p other.
  40. platform(const platform &other)
  41. : m_platform(other.m_platform)
  42. {
  43. }
  44. /// Copies the platform id from \p other.
  45. platform& operator=(const platform &other)
  46. {
  47. if(this != &other){
  48. m_platform = other.m_platform;
  49. }
  50. return *this;
  51. }
  52. /// Destroys the platform object.
  53. ~platform()
  54. {
  55. }
  56. /// Returns the ID of the platform.
  57. cl_platform_id id() const
  58. {
  59. return m_platform;
  60. }
  61. /// Returns the name of the platform.
  62. std::string name() const
  63. {
  64. return get_info<std::string>(CL_PLATFORM_NAME);
  65. }
  66. /// Returns the name of the vendor for the platform.
  67. std::string vendor() const
  68. {
  69. return get_info<std::string>(CL_PLATFORM_VENDOR);
  70. }
  71. /// Returns the profile string for the platform.
  72. std::string profile() const
  73. {
  74. return get_info<std::string>(CL_PLATFORM_PROFILE);
  75. }
  76. /// Returns the version string for the platform.
  77. std::string version() const
  78. {
  79. return get_info<std::string>(CL_PLATFORM_VERSION);
  80. }
  81. /// Returns a list of extensions supported by the platform.
  82. std::vector<std::string> extensions() const
  83. {
  84. std::string extensions_string =
  85. get_info<std::string>(CL_PLATFORM_EXTENSIONS);
  86. std::vector<std::string> extensions_vector;
  87. boost::split(extensions_vector,
  88. extensions_string,
  89. boost::is_any_of("\t "),
  90. boost::token_compress_on);
  91. return extensions_vector;
  92. }
  93. /// Returns \c true if the platform supports the extension with
  94. /// \p name.
  95. bool supports_extension(const std::string &name) const
  96. {
  97. const std::vector<std::string> extensions = this->extensions();
  98. return std::find(
  99. extensions.begin(), extensions.end(), name) != extensions.end();
  100. }
  101. /// Returns a list of devices on the platform.
  102. std::vector<device> devices(cl_device_type type = CL_DEVICE_TYPE_ALL) const
  103. {
  104. size_t count = device_count(type);
  105. if(count == 0){
  106. // no devices for this platform
  107. return std::vector<device>();
  108. }
  109. std::vector<cl_device_id> device_ids(count);
  110. cl_int ret = clGetDeviceIDs(m_platform,
  111. type,
  112. static_cast<cl_uint>(count),
  113. &device_ids[0],
  114. 0);
  115. if(ret != CL_SUCCESS){
  116. BOOST_THROW_EXCEPTION(opencl_error(ret));
  117. }
  118. std::vector<device> devices;
  119. for(cl_uint i = 0; i < count; i++){
  120. devices.push_back(device(device_ids[i]));
  121. }
  122. return devices;
  123. }
  124. /// Returns the number of devices on the platform.
  125. size_t device_count(cl_device_type type = CL_DEVICE_TYPE_ALL) const
  126. {
  127. cl_uint count = 0;
  128. cl_int ret = clGetDeviceIDs(m_platform, type, 0, 0, &count);
  129. if(ret != CL_SUCCESS){
  130. if(ret == CL_DEVICE_NOT_FOUND){
  131. // no devices for this platform
  132. return 0;
  133. }
  134. else {
  135. // something else went wrong
  136. BOOST_THROW_EXCEPTION(opencl_error(ret));
  137. }
  138. }
  139. return count;
  140. }
  141. /// Returns information about the platform.
  142. ///
  143. /// \see_opencl_ref{clGetPlatformInfo}
  144. template<class T>
  145. T get_info(cl_platform_info info) const
  146. {
  147. return detail::get_object_info<T>(clGetPlatformInfo, m_platform, info);
  148. }
  149. /// \overload
  150. template<int Enum>
  151. typename detail::get_object_info_type<platform, Enum>::type
  152. get_info() const;
  153. /// Returns the address of the \p function_name extension
  154. /// function. Returns \c 0 if \p function_name is invalid.
  155. void* get_extension_function_address(const char *function_name) const
  156. {
  157. #ifdef BOOST_COMPUTE_CL_VERSION_1_2
  158. return clGetExtensionFunctionAddressForPlatform(m_platform,
  159. function_name);
  160. #else
  161. return clGetExtensionFunctionAddress(function_name);
  162. #endif
  163. }
  164. /// Requests that the platform unload any compiler resources.
  165. void unload_compiler()
  166. {
  167. #ifdef BOOST_COMPUTE_CL_VERSION_1_2
  168. clUnloadPlatformCompiler(m_platform);
  169. #else
  170. clUnloadCompiler();
  171. #endif
  172. }
  173. /// Returns \c true if the platform is the same at \p other.
  174. bool operator==(const platform &other) const
  175. {
  176. return m_platform == other.m_platform;
  177. }
  178. /// Returns \c true if the platform is different from \p other.
  179. bool operator!=(const platform &other) const
  180. {
  181. return m_platform != other.m_platform;
  182. }
  183. /// Returns \c true if the platform OpenCL version is major.minor
  184. /// or newer; otherwise returns \c false.
  185. bool check_version(int major, int minor) const
  186. {
  187. std::stringstream stream;
  188. stream << version();
  189. int actual_major, actual_minor;
  190. stream.ignore(7); // 'OpenCL '
  191. stream >> actual_major;
  192. stream.ignore(1); // '.'
  193. stream >> actual_minor;
  194. return actual_major > major ||
  195. (actual_major == major && actual_minor >= minor);
  196. }
  197. private:
  198. cl_platform_id m_platform;
  199. };
  200. /// \internal_ define get_info() specializations for platform
  201. BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(platform,
  202. ((std::string, CL_PLATFORM_PROFILE))
  203. ((std::string, CL_PLATFORM_VERSION))
  204. ((std::string, CL_PLATFORM_NAME))
  205. ((std::string, CL_PLATFORM_VENDOR))
  206. ((std::string, CL_PLATFORM_EXTENSIONS))
  207. )
  208. #ifdef BOOST_COMPUTE_CL_VERSION_2_1
  209. BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(platform,
  210. ((cl_ulong, CL_PLATFORM_HOST_TIMER_RESOLUTION))
  211. )
  212. #endif // BOOST_COMPUTE_CL_VERSION_2_1
  213. inline boost::compute::platform device::platform() const
  214. {
  215. return boost::compute::platform(get_info<CL_DEVICE_PLATFORM>());
  216. }
  217. } // end compute namespace
  218. } // end boost namespace
  219. #endif // BOOST_COMPUTE_PLATFORM_HPP