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(const buffer_iterator_index_expr& other)
69 : m_buffer(other.m_buffer.get(), false),
70 m_index(other.m_index),
71 m_address_space(other.m_address_space),
76 ~buffer_iterator_index_expr()
78 // set buffer to null so that its reference count will
79 // not be decremented when its destructor is called
85 BOOST_STATIC_ASSERT_MSG(boost::is_integral<IndexExpr>::value,
86 "Index expression must be integral");
88 return buffer_value<T>(m_buffer, size_t(m_expr) * sizeof(T));
91 const buffer m_buffer;
93 const memory_object::address_space m_address_space;
94 const IndexExpr m_expr;
97 template<class T, class IndexExpr>
98 inline meta_kernel& operator<<(meta_kernel &kernel,
99 const buffer_iterator_index_expr<T, IndexExpr> &expr)
101 if(expr.m_index == 0){
103 kernel.get_buffer_identifier<T>(expr.m_buffer, expr.m_address_space) <<
104 '[' << expr.m_expr << ']';
108 kernel.get_buffer_identifier<T>(expr.m_buffer, expr.m_address_space) <<
109 '[' << uint_(expr.m_index) << "+(" << expr.m_expr << ")]";
113 } // end detail namespace
115 /// \class buffer_iterator
116 /// \brief An iterator for values in a buffer.
118 /// The buffer_iterator class iterates over values in a memory buffer on a
119 /// compute device. It is the most commonly used iterator in Boost.Compute
120 /// and is used by the \ref vector "vector<T>" and \ref array "array<T, N>"
121 /// container classes.
123 /// Buffer iterators store a reference to a memory buffer along with an index
124 /// into that memory buffer.
126 /// The buffer_iterator class allows for arbitrary OpenCL memory objects
127 /// (including those created outside of Boost.Compute) to be used with the
128 /// Boost.Compute algorithms (such as transform() and sort()). For example,
129 /// to reverse the contents of an OpenCL memory buffer containing a set of
132 /// \snippet test/test_buffer_iterator.cpp reverse_external_buffer
134 /// \see buffer, make_buffer_iterator()
136 class buffer_iterator : public detail::buffer_iterator_base<T>::type
139 typedef typename detail::buffer_iterator_base<T>::type super_type;
140 typedef typename super_type::reference reference;
141 typedef typename super_type::difference_type difference_type;
148 buffer_iterator(const buffer &buffer, size_t index)
149 : m_buffer(buffer.get(), false),
154 buffer_iterator(const buffer_iterator<T> &other)
155 : m_buffer(other.m_buffer.get(), false),
156 m_index(other.m_index)
160 buffer_iterator<T>& operator=(const buffer_iterator<T> &other)
163 m_buffer.get() = other.m_buffer.get();
164 m_index = other.m_index;
172 // set buffer to null so that its reference count will
173 // not be decremented when its destructor is called
177 const buffer& get_buffer() const
182 size_t get_index() const
187 T read(command_queue &queue) const
189 BOOST_ASSERT(m_buffer.get());
190 BOOST_ASSERT(m_index < m_buffer.size() / sizeof(T));
192 return detail::read_single_value<T>(m_buffer, m_index, queue);
195 void write(const T &value, command_queue &queue)
197 BOOST_ASSERT(m_buffer.get());
198 BOOST_ASSERT(m_index < m_buffer.size() / sizeof(T));
200 detail::write_single_value<T>(value, m_buffer, m_index, queue);
205 detail::buffer_iterator_index_expr<T, Expr>
206 operator[](const Expr &expr) const
208 BOOST_ASSERT(m_buffer.get());
210 return detail::buffer_iterator_index_expr<T, Expr>(
211 m_buffer, m_index, memory_object::global_memory, expr
216 friend class ::boost::iterator_core_access;
219 reference dereference() const
221 return detail::buffer_value<T>(m_buffer, m_index * sizeof(T));
225 bool equal(const buffer_iterator<T> &other) const
227 return m_buffer.get() == other.m_buffer.get() &&
228 m_index == other.m_index;
244 void advance(difference_type n)
246 m_index = static_cast<size_t>(static_cast<difference_type>(m_index) + n);
250 difference_type distance_to(const buffer_iterator<T> &other) const
252 return static_cast<difference_type>(other.m_index - m_index);
256 const buffer m_buffer;
260 /// Creates a new \ref buffer_iterator for \p buffer at \p index.
262 /// \param buffer the \ref buffer object
263 /// \param index the index in the buffer
265 /// \return a \c buffer_iterator for \p buffer at \p index
267 inline buffer_iterator<T>
268 make_buffer_iterator(const buffer &buffer, size_t index = 0)
270 return buffer_iterator<T>(buffer, index);
273 /// \internal_ (is_device_iterator specialization for buffer_iterator)
275 struct is_device_iterator<buffer_iterator<T> > : boost::true_type {};
279 // is_buffer_iterator specialization for buffer_iterator
280 template<class Iterator>
281 struct is_buffer_iterator<
283 typename boost::enable_if<
285 buffer_iterator<typename Iterator::value_type>,
286 typename boost::remove_const<Iterator>::type
289 > : public boost::true_type {};
291 } // end detail namespace
292 } // end compute namespace
293 } // end boost namespace
295 #endif // BOOST_COMPUTE_ITERATOR_BUFFER_ITERATOR_HPP