9
3

system.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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_SYSTEM_HPP
  11. #define BOOST_COMPUTE_SYSTEM_HPP
  12. #include <string>
  13. #include <vector>
  14. #include <cstdlib>
  15. #include <boost/throw_exception.hpp>
  16. #include <boost/compute/cl.hpp>
  17. #include <boost/compute/device.hpp>
  18. #include <boost/compute/context.hpp>
  19. #include <boost/compute/platform.hpp>
  20. #include <boost/compute/command_queue.hpp>
  21. #include <boost/compute/detail/getenv.hpp>
  22. #include <boost/compute/exception/no_device_found.hpp>
  23. namespace boost {
  24. namespace compute {
  25. /// \class system
  26. /// \brief Provides access to platforms and devices on the system.
  27. ///
  28. /// The system class contains a set of static functions which provide access to
  29. /// the OpenCL platforms and compute devices on the host system.
  30. ///
  31. /// The default_device() convenience method automatically selects and returns
  32. /// the "best" compute device for the system following a set of heuristics and
  33. /// environment variables. This simplifies setup of the OpenCL enviornment.
  34. ///
  35. /// \see platform, device, context
  36. class system
  37. {
  38. public:
  39. /// Returns the default compute device for the system.
  40. ///
  41. /// The default device is selected based on a set of heuristics and can be
  42. /// influenced using one of the following environment variables:
  43. ///
  44. /// \li \c BOOST_COMPUTE_DEFAULT_DEVICE -
  45. /// name of the compute device (e.g. "GTX TITAN")
  46. /// \li \c BOOST_COMPUTE_DEFAULT_DEVICE_TYPE
  47. /// type of the compute device (e.g. "GPU" or "CPU")
  48. /// \li \c BOOST_COMPUTE_DEFAULT_PLATFORM -
  49. /// name of the platform (e.g. "NVIDIA CUDA")
  50. /// \li \c BOOST_COMPUTE_DEFAULT_VENDOR -
  51. /// name of the device vendor (e.g. "NVIDIA")
  52. /// \li \c BOOST_COMPUTE_DEFAULT_ENFORCE -
  53. /// If this is set to "1", then throw a no_device_found() exception
  54. /// if any of the above environment variables is set, but a matching
  55. /// device was not found.
  56. ///
  57. /// The default device is determined once on the first time this function
  58. /// is called. Calling this function multiple times will always result in
  59. /// the same device being returned.
  60. ///
  61. /// If no OpenCL device is found on the system, a no_device_found exception
  62. /// is thrown.
  63. ///
  64. /// For example, to print the name of the default compute device on the
  65. /// system:
  66. /// \code
  67. /// // get the default compute device
  68. /// boost::compute::device device = boost::compute::system::default_device();
  69. ///
  70. /// // print the name of the device
  71. /// std::cout << "default device: " << device.name() << std::endl;
  72. /// \endcode
  73. static device default_device()
  74. {
  75. static device default_device = find_default_device();
  76. return default_device;
  77. }
  78. /// Returns the device with \p name.
  79. ///
  80. /// \throws no_device_found if no device with \p name is found.
  81. static device find_device(const std::string &name)
  82. {
  83. const std::vector<device> devices = system::devices();
  84. for(size_t i = 0; i < devices.size(); i++){
  85. const device& device = devices[i];
  86. if(device.name() == name){
  87. return device;
  88. }
  89. }
  90. BOOST_THROW_EXCEPTION(no_device_found());
  91. }
  92. /// Returns a vector containing all of the compute devices on
  93. /// the system.
  94. ///
  95. /// For example, to print out the name of each OpenCL-capable device
  96. /// available on the system:
  97. /// \code
  98. /// for(const auto &device : boost::compute::system::devices()){
  99. /// std::cout << device.name() << std::endl;
  100. /// }
  101. /// \endcode
  102. static std::vector<device> devices()
  103. {
  104. std::vector<device> devices;
  105. const std::vector<platform> platforms = system::platforms();
  106. for(size_t i = 0; i < platforms.size(); i++){
  107. const std::vector<device> platform_devices = platforms[i].devices();
  108. devices.insert(
  109. devices.end(), platform_devices.begin(), platform_devices.end()
  110. );
  111. }
  112. return devices;
  113. }
  114. /// Returns the number of compute devices on the system.
  115. static size_t device_count()
  116. {
  117. size_t count = 0;
  118. const std::vector<platform> platforms = system::platforms();
  119. for(size_t i = 0; i < platforms.size(); i++){
  120. count += platforms[i].device_count();
  121. }
  122. return count;
  123. }
  124. /// Returns the default context for the system.
  125. ///
  126. /// The default context is created for the default device on the system
  127. /// (as returned by default_device()).
  128. ///
  129. /// The default context is created once on the first time this function is
  130. /// called. Calling this function multiple times will always result in the
  131. /// same context object being returned.
  132. static context default_context()
  133. {
  134. static context default_context(default_device());
  135. return default_context;
  136. }
  137. /// Returns the default command queue for the system.
  138. static command_queue& default_queue()
  139. {
  140. static command_queue queue(default_context(), default_device());
  141. return queue;
  142. }
  143. /// Blocks until all outstanding computations on the default
  144. /// command queue are complete.
  145. ///
  146. /// This is equivalent to:
  147. /// \code
  148. /// system::default_queue().finish();
  149. /// \endcode
  150. static void finish()
  151. {
  152. default_queue().finish();
  153. }
  154. /// Returns a vector containing each of the OpenCL platforms on the system.
  155. ///
  156. /// For example, to print out the name of each OpenCL platform present on
  157. /// the system:
  158. /// \code
  159. /// for(const auto &platform : boost::compute::system::platforms()){
  160. /// std::cout << platform.name() << std::endl;
  161. /// }
  162. /// \endcode
  163. static std::vector<platform> platforms()
  164. {
  165. cl_uint count = 0;
  166. clGetPlatformIDs(0, 0, &count);
  167. std::vector<platform> platforms;
  168. if(count > 0)
  169. {
  170. std::vector<cl_platform_id> platform_ids(count);
  171. clGetPlatformIDs(count, &platform_ids[0], 0);
  172. for(size_t i = 0; i < platform_ids.size(); i++){
  173. platforms.push_back(platform(platform_ids[i]));
  174. }
  175. }
  176. return platforms;
  177. }
  178. /// Returns the number of compute platforms on the system.
  179. static size_t platform_count()
  180. {
  181. cl_uint count = 0;
  182. clGetPlatformIDs(0, 0, &count);
  183. return static_cast<size_t>(count);
  184. }
  185. private:
  186. /// \internal_
  187. static device find_default_device()
  188. {
  189. // get a list of all devices on the system
  190. const std::vector<device> devices_ = devices();
  191. if(devices_.empty()){
  192. BOOST_THROW_EXCEPTION(no_device_found());
  193. }
  194. // check for device from environment variable
  195. const char *name = detail::getenv("BOOST_COMPUTE_DEFAULT_DEVICE");
  196. const char *type = detail::getenv("BOOST_COMPUTE_DEFAULT_DEVICE_TYPE");
  197. const char *platform = detail::getenv("BOOST_COMPUTE_DEFAULT_PLATFORM");
  198. const char *vendor = detail::getenv("BOOST_COMPUTE_DEFAULT_VENDOR");
  199. const char *enforce = detail::getenv("BOOST_COMPUTE_DEFAULT_ENFORCE");
  200. if(name || type || platform || vendor){
  201. for(size_t i = 0; i < devices_.size(); i++){
  202. const device& device = devices_[i];
  203. if (name && !matches(device.name(), name))
  204. continue;
  205. if (type && matches(std::string("GPU"), type))
  206. if (!(device.type() & device::gpu))
  207. continue;
  208. if (type && matches(std::string("CPU"), type))
  209. if (!(device.type() & device::cpu))
  210. continue;
  211. if (platform && !matches(device.platform().name(), platform))
  212. continue;
  213. if (vendor && !matches(device.vendor(), vendor))
  214. continue;
  215. return device;
  216. }
  217. if(enforce && enforce[0] == '1')
  218. BOOST_THROW_EXCEPTION(no_device_found());
  219. }
  220. // find the first gpu device
  221. for(size_t i = 0; i < devices_.size(); i++){
  222. const device& device = devices_[i];
  223. if(device.type() & device::gpu){
  224. return device;
  225. }
  226. }
  227. // find the first cpu device
  228. for(size_t i = 0; i < devices_.size(); i++){
  229. const device& device = devices_[i];
  230. if(device.type() & device::cpu){
  231. return device;
  232. }
  233. }
  234. // return the first device found
  235. return devices_[0];
  236. }
  237. /// \internal_
  238. static bool matches(const std::string &str, const std::string &pattern)
  239. {
  240. return str.find(pattern) != std::string::npos;
  241. }
  242. };
  243. } // end compute namespace
  244. } // end boost namespace
  245. #endif // BOOST_COMPUTE_SYSTEM_HPP