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