buffer_iterator.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <[email protected]>
  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_ITERATOR_BUFFER_ITERATOR_HPP
  11. #define BOOST_COMPUTE_ITERATOR_BUFFER_ITERATOR_HPP
  12. #include <cstddef>
  13. #include <iterator>
  14. #include <boost/config.hpp>
  15. #include <boost/type_traits.hpp>
  16. #include <boost/static_assert.hpp>
  17. #include <boost/utility/enable_if.hpp>
  18. #include <boost/iterator/iterator_facade.hpp>
  19. #include <boost/compute/buffer.hpp>
  20. #include <boost/compute/detail/buffer_value.hpp>
  21. #include <boost/compute/detail/is_buffer_iterator.hpp>
  22. #include <boost/compute/detail/meta_kernel.hpp>
  23. #include <boost/compute/detail/read_write_single_value.hpp>
  24. #include <boost/compute/type_traits/is_device_iterator.hpp>
  25. namespace boost {
  26. namespace compute {
  27. // forward declaration for buffer_iterator<T>
  28. template<class T> class buffer_iterator;
  29. namespace detail {
  30. // helper class which defines the iterator_facade super-class
  31. // type for buffer_iterator<T>
  32. template<class T>
  33. class buffer_iterator_base
  34. {
  35. public:
  36. typedef ::boost::iterator_facade<
  37. ::boost::compute::buffer_iterator<T>,
  38. T,
  39. ::std::random_access_iterator_tag,
  40. ::boost::compute::detail::buffer_value<T>
  41. > type;
  42. };
  43. template<class T, class IndexExpr>
  44. struct buffer_iterator_index_expr
  45. {
  46. typedef T result_type;
  47. buffer_iterator_index_expr(const buffer &buffer,
  48. size_t index,
  49. const memory_object::address_space address_space,
  50. const IndexExpr &expr)
  51. : m_buffer(buffer.get(), false),
  52. m_index(index),
  53. m_address_space(address_space),
  54. m_expr(expr)
  55. {
  56. }
  57. buffer_iterator_index_expr(const buffer_iterator_index_expr& other)
  58. : m_buffer(other.m_buffer.get(), false),
  59. m_index(other.m_index),
  60. m_address_space(other.m_address_space),
  61. m_expr(other.m_expr)
  62. {
  63. }
  64. ~buffer_iterator_index_expr()
  65. {
  66. // set buffer to null so that its reference count will
  67. // not be decremented when its destructor is called
  68. m_buffer.get() = 0;
  69. }
  70. operator T() const
  71. {
  72. BOOST_STATIC_ASSERT_MSG(boost::is_integral<IndexExpr>::value,
  73. "Index expression must be integral");
  74. return buffer_value<T>(m_buffer, size_t(m_expr) * sizeof(T));
  75. }
  76. const buffer m_buffer;
  77. const size_t m_index;
  78. const memory_object::address_space m_address_space;
  79. const IndexExpr m_expr;
  80. };
  81. template<class T, class IndexExpr>
  82. inline meta_kernel& operator<<(meta_kernel &kernel,
  83. const buffer_iterator_index_expr<T, IndexExpr> &expr)
  84. {
  85. if(expr.m_index == 0){
  86. return kernel <<
  87. kernel.get_buffer_identifier<T>(expr.m_buffer, expr.m_address_space) <<
  88. '[' << expr.m_expr << ']';
  89. }
  90. else {
  91. return kernel <<
  92. kernel.get_buffer_identifier<T>(expr.m_buffer, expr.m_address_space) <<
  93. '[' << uint_(expr.m_index) << "+(" << expr.m_expr << ")]";
  94. }
  95. }
  96. } // end detail namespace
  97. /// \class buffer_iterator
  98. /// \brief An iterator for values in a buffer.
  99. ///
  100. /// The buffer_iterator class iterates over values in a memory buffer on a
  101. /// compute device. It is the most commonly used iterator in Boost.Compute
  102. /// and is used by the \ref vector "vector<T>" and \ref array "array<T, N>"
  103. /// container classes.
  104. ///
  105. /// Buffer iterators store a reference to a memory buffer along with an index
  106. /// into that memory buffer.
  107. ///
  108. /// The buffer_iterator class allows for arbitrary OpenCL memory objects
  109. /// (including those created outside of Boost.Compute) to be used with the
  110. /// Boost.Compute algorithms (such as transform() and sort()). For example,
  111. /// to reverse the contents of an OpenCL memory buffer containing a set of
  112. /// integers:
  113. ///
  114. /// \snippet test/test_buffer_iterator.cpp reverse_external_buffer
  115. ///
  116. /// \see buffer, make_buffer_iterator()
  117. template<class T>
  118. class buffer_iterator : public detail::buffer_iterator_base<T>::type
  119. {
  120. public:
  121. typedef typename detail::buffer_iterator_base<T>::type super_type;
  122. typedef typename super_type::reference reference;
  123. typedef typename super_type::difference_type difference_type;
  124. buffer_iterator()
  125. : m_index(0)
  126. {
  127. }
  128. buffer_iterator(const buffer &buffer, size_t index)
  129. : m_buffer(buffer.get(), false),
  130. m_index(index)
  131. {
  132. }
  133. buffer_iterator(const buffer_iterator<T> &other)
  134. : m_buffer(other.m_buffer.get(), false),
  135. m_index(other.m_index)
  136. {
  137. }
  138. buffer_iterator<T>& operator=(const buffer_iterator<T> &other)
  139. {
  140. if(this != &other){
  141. m_buffer.get() = other.m_buffer.get();
  142. m_index = other.m_index;
  143. }
  144. return *this;
  145. }
  146. ~buffer_iterator()
  147. {
  148. // set buffer to null so that its reference count will
  149. // not be decremented when its destructor is called
  150. m_buffer.get() = 0;
  151. }
  152. const buffer& get_buffer() const
  153. {
  154. return m_buffer;
  155. }
  156. size_t get_index() const
  157. {
  158. return m_index;
  159. }
  160. T read(command_queue &queue) const
  161. {
  162. BOOST_ASSERT(m_buffer.get());
  163. BOOST_ASSERT(m_index < m_buffer.size() / sizeof(T));
  164. return detail::read_single_value<T>(m_buffer, m_index, queue);
  165. }
  166. void write(const T &value, command_queue &queue)
  167. {
  168. BOOST_ASSERT(m_buffer.get());
  169. BOOST_ASSERT(m_index < m_buffer.size() / sizeof(T));
  170. detail::write_single_value<T>(value, m_buffer, m_index, queue);
  171. }
  172. /// \internal_
  173. template<class Expr>
  174. detail::buffer_iterator_index_expr<T, Expr>
  175. operator[](const Expr &expr) const
  176. {
  177. BOOST_ASSERT(m_buffer.get());
  178. return detail::buffer_iterator_index_expr<T, Expr>(
  179. m_buffer, m_index, memory_object::global_memory, expr
  180. );
  181. }
  182. private:
  183. friend class ::boost::iterator_core_access;
  184. /// \internal_
  185. reference dereference() const
  186. {
  187. return detail::buffer_value<T>(m_buffer, m_index * sizeof(T));
  188. }
  189. /// \internal_
  190. bool equal(const buffer_iterator<T> &other) const
  191. {
  192. return m_buffer.get() == other.m_buffer.get() &&
  193. m_index == other.m_index;
  194. }
  195. /// \internal_
  196. void increment()
  197. {
  198. m_index++;
  199. }
  200. /// \internal_
  201. void decrement()
  202. {
  203. m_index--;
  204. }
  205. /// \internal_
  206. void advance(difference_type n)
  207. {
  208. m_index = static_cast<size_t>(static_cast<difference_type>(m_index) + n);
  209. }
  210. /// \internal_
  211. difference_type distance_to(const buffer_iterator<T> &other) const
  212. {
  213. return static_cast<difference_type>(other.m_index - m_index);
  214. }
  215. private:
  216. const buffer m_buffer;
  217. size_t m_index;
  218. };
  219. /// Creates a new \ref buffer_iterator for \p buffer at \p index.
  220. ///
  221. /// \param buffer the \ref buffer object
  222. /// \param index the index in the buffer
  223. ///
  224. /// \return a \c buffer_iterator for \p buffer at \p index
  225. template<class T>
  226. inline buffer_iterator<T>
  227. make_buffer_iterator(const buffer &buffer, size_t index = 0)
  228. {
  229. return buffer_iterator<T>(buffer, index);
  230. }
  231. /// \internal_ (is_device_iterator specialization for buffer_iterator)
  232. template<class T>
  233. struct is_device_iterator<buffer_iterator<T> > : boost::true_type {};
  234. namespace detail {
  235. // is_buffer_iterator specialization for buffer_iterator
  236. template<class Iterator>
  237. struct is_buffer_iterator<
  238. Iterator,
  239. typename boost::enable_if<
  240. boost::is_same<
  241. buffer_iterator<typename Iterator::value_type>,
  242. typename boost::remove_const<Iterator>::type
  243. >
  244. >::type
  245. > : public boost::true_type {};
  246. } // end detail namespace
  247. } // end compute namespace
  248. } // end boost namespace
  249. #endif // BOOST_COMPUTE_ITERATOR_BUFFER_ITERATOR_HPP