context.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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_CONTEXT_HPP
  11. #define BOOST_COMPUTE_CONTEXT_HPP
  12. #include <vector>
  13. #include <boost/throw_exception.hpp>
  14. #include <boost/compute/config.hpp>
  15. #include <boost/compute/device.hpp>
  16. #include <boost/compute/exception/opencl_error.hpp>
  17. #include <boost/compute/detail/assert_cl_success.hpp>
  18. namespace boost {
  19. namespace compute {
  20. /// \class context
  21. /// \brief A compute context.
  22. ///
  23. /// The context class represents a compute context.
  24. ///
  25. /// A context object manages a set of OpenCL resources including memory
  26. /// buffers and program objects. Before allocating memory on the device or
  27. /// executing kernels you must set up a context object.
  28. ///
  29. /// To create a context for the default device on the system:
  30. /// \code
  31. /// // get the default compute device
  32. /// boost::compute::device gpu = boost::compute::system::default_device();
  33. ///
  34. /// // create a context for the device
  35. /// boost::compute::context context(gpu);
  36. /// \endcode
  37. ///
  38. /// Once a context is created, memory can be allocated using the buffer class
  39. /// and kernels can be executed using the command_queue class.
  40. ///
  41. /// \see device, command_queue
  42. class context
  43. {
  44. public:
  45. /// Create a null context object.
  46. context()
  47. : m_context(0)
  48. {
  49. }
  50. /// Creates a new context for \p device with \p properties.
  51. ///
  52. /// \see_opencl_ref{clCreateContext}
  53. explicit context(const device &device,
  54. const cl_context_properties *properties = 0)
  55. {
  56. BOOST_ASSERT(device.id() != 0);
  57. cl_device_id device_id = device.id();
  58. cl_int error = 0;
  59. m_context = clCreateContext(properties, 1, &device_id, 0, 0, &error);
  60. if(!m_context){
  61. BOOST_THROW_EXCEPTION(opencl_error(error));
  62. }
  63. }
  64. /// Creates a new context for \p devices with \p properties.
  65. ///
  66. /// \see_opencl_ref{clCreateContext}
  67. explicit context(const std::vector<device> &devices,
  68. const cl_context_properties *properties = 0)
  69. {
  70. BOOST_ASSERT(!devices.empty());
  71. cl_int error = 0;
  72. m_context = clCreateContext(
  73. properties,
  74. static_cast<cl_uint>(devices.size()),
  75. reinterpret_cast<const cl_device_id *>(&devices[0]),
  76. 0,
  77. 0,
  78. &error
  79. );
  80. if(!m_context){
  81. BOOST_THROW_EXCEPTION(opencl_error(error));
  82. }
  83. }
  84. /// Creates a new context object for \p context. If \p retain is
  85. /// \c true, the reference count for \p context will be incremented.
  86. explicit context(cl_context context, bool retain = true)
  87. : m_context(context)
  88. {
  89. if(m_context && retain){
  90. clRetainContext(m_context);
  91. }
  92. }
  93. /// Creates a new context object as a copy of \p other.
  94. context(const context &other)
  95. : m_context(other.m_context)
  96. {
  97. if(m_context){
  98. clRetainContext(m_context);
  99. }
  100. }
  101. /// Copies the context object from \p other to \c *this.
  102. context& operator=(const context &other)
  103. {
  104. if(this != &other){
  105. if(m_context){
  106. clReleaseContext(m_context);
  107. }
  108. m_context = other.m_context;
  109. if(m_context){
  110. clRetainContext(m_context);
  111. }
  112. }
  113. return *this;
  114. }
  115. #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
  116. /// Move-constructs a new context object from \p other.
  117. context(context&& other) BOOST_NOEXCEPT
  118. : m_context(other.m_context)
  119. {
  120. other.m_context = 0;
  121. }
  122. /// Move-assigns the context from \p other to \c *this.
  123. context& operator=(context&& other) BOOST_NOEXCEPT
  124. {
  125. if(m_context){
  126. clReleaseContext(m_context);
  127. }
  128. m_context = other.m_context;
  129. other.m_context = 0;
  130. return *this;
  131. }
  132. #endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
  133. /// Destroys the context object.
  134. ~context()
  135. {
  136. if(m_context){
  137. BOOST_COMPUTE_ASSERT_CL_SUCCESS(
  138. clReleaseContext(m_context)
  139. );
  140. }
  141. }
  142. /// Returns the underlying OpenCL context.
  143. cl_context& get() const
  144. {
  145. return const_cast<cl_context &>(m_context);
  146. }
  147. /// Returns the device for the context. If the context contains multiple
  148. /// devices, the first is returned.
  149. device get_device() const
  150. {
  151. std::vector<device> devices = get_devices();
  152. if(devices.empty()) {
  153. return device();
  154. }
  155. return devices.front();
  156. }
  157. /// Returns a vector of devices for the context.
  158. std::vector<device> get_devices() const
  159. {
  160. return get_info<std::vector<device> >(CL_CONTEXT_DEVICES);
  161. }
  162. /// Returns information about the context.
  163. ///
  164. /// \see_opencl_ref{clGetContextInfo}
  165. template<class T>
  166. T get_info(cl_context_info info) const
  167. {
  168. return detail::get_object_info<T>(clGetContextInfo, m_context, info);
  169. }
  170. /// \overload
  171. template<int Enum>
  172. typename detail::get_object_info_type<context, Enum>::type
  173. get_info() const;
  174. /// Returns \c true if the context is the same as \p other.
  175. bool operator==(const context &other) const
  176. {
  177. return m_context == other.m_context;
  178. }
  179. /// Returns \c true if the context is different from \p other.
  180. bool operator!=(const context &other) const
  181. {
  182. return m_context != other.m_context;
  183. }
  184. /// \internal_
  185. operator cl_context() const
  186. {
  187. return m_context;
  188. }
  189. private:
  190. cl_context m_context;
  191. };
  192. /// \internal_ define get_info() specializations for context
  193. BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(context,
  194. ((cl_uint, CL_CONTEXT_REFERENCE_COUNT))
  195. ((std::vector<cl_device_id>, CL_CONTEXT_DEVICES))
  196. ((std::vector<cl_context_properties>, CL_CONTEXT_PROPERTIES))
  197. )
  198. #ifdef BOOST_COMPUTE_CL_VERSION_1_1
  199. BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(context,
  200. ((cl_uint, CL_CONTEXT_NUM_DEVICES))
  201. )
  202. #endif // BOOST_COMPUTE_CL_VERSION_1_1
  203. } // end compute namespace
  204. } // end boost namespace
  205. #endif // BOOST_COMPUTE_CONTEXT_HPP