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_ITERATOR_BUFFER_ITERATOR_HPP
12 #define BOOST_COMPUTE_ITERATOR_BUFFER_ITERATOR_HPP
17 #include <boost/config.hpp>
18 #include <boost/type_traits.hpp>
19 #include <boost/static_assert.hpp>
20 #include <boost/utility/enable_if.hpp>
21 #include <boost/iterator/iterator_facade.hpp>
23 #include <boost/compute/buffer.hpp>
24 #include <boost/compute/detail/buffer_value.hpp>
25 #include <boost/compute/detail/is_buffer_iterator.hpp>
26 #include <boost/compute/detail/meta_kernel.hpp>
27 #include <boost/compute/detail/read_write_single_value.hpp>
28 #include <boost/compute/type_traits/is_device_iterator.hpp>
33 // forward declaration for buffer_iterator<T>
34 template<class T> class buffer_iterator;
38 // helper class which defines the iterator_facade super-class
39 // type for buffer_iterator<T>
41 class buffer_iterator_base
44 typedef ::boost::iterator_facade<
45 ::boost::compute::buffer_iterator<T>,
47 ::std::random_access_iterator_tag,
48 ::boost::compute::detail::buffer_value<T>
52 template<class T, class IndexExpr>
53 struct buffer_iterator_index_expr
55 typedef T result_type;
57 buffer_iterator_index_expr(const buffer &buffer,
59 const memory_object::address_space address_space,
60 const IndexExpr &expr)
61 : m_buffer(buffer.get(), false),
63 m_address_space(address_space),
68 ~buffer_iterator_index_expr()
70 // set buffer to null so that its reference count will
71 // not be decremented when its destructor is called
77 BOOST_STATIC_ASSERT_MSG(boost::is_integral<IndexExpr>::value,
78 "Index expression must be integral");
80 return buffer_value<T>(m_buffer, size_t(m_expr) * sizeof(T));
83 const buffer m_buffer;
85 const memory_object::address_space m_address_space;
86 const IndexExpr m_expr;
89 template<class T, class IndexExpr>
90 inline meta_kernel& operator<<(meta_kernel &kernel,
91 const buffer_iterator_index_expr<T, IndexExpr> &expr)
93 if(expr.m_index == 0){
95 kernel.get_buffer_identifier<T>(expr.m_buffer, expr.m_address_space) <<
96 '[' << expr.m_expr << ']';
100 kernel.get_buffer_identifier<T>(expr.m_buffer, expr.m_address_space) <<
101 '[' << uint_(expr.m_index) << "+(" << expr.m_expr << ")]";
105 } // end detail namespace
107 /// \class buffer_iterator
108 /// \brief An iterator for values in a buffer.
110 /// The buffer_iterator class iterates over values in a memory buffer on a
111 /// compute device. It is the most commonly used iterator in Boost.Compute
112 /// and is used by the \ref vector "vector<T>" and \ref array "array<T, N>"
113 /// container classes.
115 /// Buffer iterators store a reference to a memory buffer along with an index
116 /// into that memory buffer.
118 /// The buffer_iterator class allows for arbitrary OpenCL memory objects
119 /// (including those created outside of Boost.Compute) to be used with the
120 /// Boost.Compute algorithms (such as transform() and sort()). For example,
121 /// to reverse the contents of an OpenCL memory buffer containing a set of
124 /// \snippet test/test_buffer_iterator.cpp reverse_external_buffer
126 /// \see buffer, make_buffer_iterator()
128 class buffer_iterator : public detail::buffer_iterator_base<T>::type
131 typedef typename detail::buffer_iterator_base<T>::type super_type;
132 typedef typename super_type::reference reference;
133 typedef typename super_type::difference_type difference_type;
140 buffer_iterator(const buffer &buffer, size_t index)
141 : m_buffer(buffer.get(), false),
146 buffer_iterator(const buffer_iterator<T> &other)
147 : m_buffer(other.m_buffer.get(), false),
148 m_index(other.m_index)
152 buffer_iterator<T>& operator=(const buffer_iterator<T> &other)
155 m_buffer.get() = other.m_buffer.get();
156 m_index = other.m_index;
164 // set buffer to null so that its reference count will
165 // not be decremented when its destructor is called
169 const buffer& get_buffer() const
174 size_t get_index() const
179 T read(command_queue &queue) const
181 BOOST_ASSERT(m_buffer.get());
182 BOOST_ASSERT(m_index < m_buffer.size() / sizeof(T));
184 return detail::read_single_value<T>(m_buffer, m_index, queue);
187 void write(const T &value, command_queue &queue)
189 BOOST_ASSERT(m_buffer.get());
190 BOOST_ASSERT(m_index < m_buffer.size() / sizeof(T));
192 detail::write_single_value<T>(value, m_buffer, m_index, queue);
197 detail::buffer_iterator_index_expr<T, Expr>
198 operator[](const Expr &expr) const
200 BOOST_ASSERT(m_buffer.get());
202 return detail::buffer_iterator_index_expr<T, Expr>(
203 m_buffer, m_index, memory_object::global_memory, expr
208 friend class ::boost::iterator_core_access;
211 reference dereference() const
213 return detail::buffer_value<T>(m_buffer, m_index * sizeof(T));
217 bool equal(const buffer_iterator<T> &other) const
219 return m_buffer.get() == other.m_buffer.get() &&
220 m_index == other.m_index;
236 void advance(difference_type n)
238 m_index = static_cast<size_t>(static_cast<difference_type>(m_index) + n);
242 difference_type distance_to(const buffer_iterator<T> &other) const
244 return static_cast<difference_type>(other.m_index - m_index);
248 const buffer m_buffer;
252 /// Creates a new \ref buffer_iterator for \p buffer at \p index.
254 /// \param buffer the \ref buffer object
255 /// \param index the index in the buffer
257 /// \return a \c buffer_iterator for \p buffer at \p index
259 inline buffer_iterator<T>
260 make_buffer_iterator(const buffer &buffer, size_t index = 0)
262 return buffer_iterator<T>(buffer, index);
265 /// \internal_ (is_device_iterator specialization for buffer_iterator)
267 struct is_device_iterator<buffer_iterator<T> > : boost::true_type {};
271 // is_buffer_iterator specialization for buffer_iterator
272 template<class Iterator>
273 struct is_buffer_iterator<
275 typename boost::enable_if<
277 buffer_iterator<typename Iterator::value_type>,
278 typename boost::remove_const<Iterator>::type
281 > : public boost::true_type {};
283 } // end detail namespace
284 } // end compute namespace
285 } // end boost namespace
287 #endif // BOOST_COMPUTE_ITERATOR_BUFFER_ITERATOR_HPP