]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/compute/iterator/buffer_iterator.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / 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.get(), false),
62 m_index(index),
63 m_address_space(address_space),
64 m_expr(expr)
65 {
66 }
67
68 ~buffer_iterator_index_expr()
69 {
70 // set buffer to null so that its reference count will
71 // not be decremented when its destructor is called
72 m_buffer.get() = 0;
73 }
74
75 operator T() const
76 {
77 BOOST_STATIC_ASSERT_MSG(boost::is_integral<IndexExpr>::value,
78 "Index expression must be integral");
79
80 return buffer_value<T>(m_buffer, size_t(m_expr) * sizeof(T));
81 }
82
83 const buffer m_buffer;
84 const size_t m_index;
85 const memory_object::address_space m_address_space;
86 const IndexExpr m_expr;
87 };
88
89 template<class T, class IndexExpr>
90 inline meta_kernel& operator<<(meta_kernel &kernel,
91 const buffer_iterator_index_expr<T, IndexExpr> &expr)
92 {
93 if(expr.m_index == 0){
94 return kernel <<
95 kernel.get_buffer_identifier<T>(expr.m_buffer, expr.m_address_space) <<
96 '[' << expr.m_expr << ']';
97 }
98 else {
99 return kernel <<
100 kernel.get_buffer_identifier<T>(expr.m_buffer, expr.m_address_space) <<
101 '[' << uint_(expr.m_index) << "+(" << expr.m_expr << ")]";
102 }
103 }
104
105 } // end detail namespace
106
107 /// \class buffer_iterator
108 /// \brief An iterator for values in a buffer.
109 ///
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.
114 ///
115 /// Buffer iterators store a reference to a memory buffer along with an index
116 /// into that memory buffer.
117 ///
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
122 /// integers:
123 ///
124 /// \snippet test/test_buffer_iterator.cpp reverse_external_buffer
125 ///
126 /// \see buffer, make_buffer_iterator()
127 template<class T>
128 class buffer_iterator : public detail::buffer_iterator_base<T>::type
129 {
130 public:
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;
134
135 buffer_iterator()
136 : m_index(0)
137 {
138 }
139
140 buffer_iterator(const buffer &buffer, size_t index)
141 : m_buffer(buffer.get(), false),
142 m_index(index)
143 {
144 }
145
146 buffer_iterator(const buffer_iterator<T> &other)
147 : m_buffer(other.m_buffer.get(), false),
148 m_index(other.m_index)
149 {
150 }
151
152 buffer_iterator<T>& operator=(const buffer_iterator<T> &other)
153 {
154 if(this != &other){
155 m_buffer.get() = other.m_buffer.get();
156 m_index = other.m_index;
157 }
158
159 return *this;
160 }
161
162 ~buffer_iterator()
163 {
164 // set buffer to null so that its reference count will
165 // not be decremented when its destructor is called
166 m_buffer.get() = 0;
167 }
168
169 const buffer& get_buffer() const
170 {
171 return m_buffer;
172 }
173
174 size_t get_index() const
175 {
176 return m_index;
177 }
178
179 T read(command_queue &queue) const
180 {
181 BOOST_ASSERT(m_buffer.get());
182 BOOST_ASSERT(m_index < m_buffer.size() / sizeof(T));
183
184 return detail::read_single_value<T>(m_buffer, m_index, queue);
185 }
186
187 void write(const T &value, command_queue &queue)
188 {
189 BOOST_ASSERT(m_buffer.get());
190 BOOST_ASSERT(m_index < m_buffer.size() / sizeof(T));
191
192 detail::write_single_value<T>(value, m_buffer, m_index, queue);
193 }
194
195 /// \internal_
196 template<class Expr>
197 detail::buffer_iterator_index_expr<T, Expr>
198 operator[](const Expr &expr) const
199 {
200 BOOST_ASSERT(m_buffer.get());
201
202 return detail::buffer_iterator_index_expr<T, Expr>(
203 m_buffer, m_index, memory_object::global_memory, expr
204 );
205 }
206
207 private:
208 friend class ::boost::iterator_core_access;
209
210 /// \internal_
211 reference dereference() const
212 {
213 return detail::buffer_value<T>(m_buffer, m_index * sizeof(T));
214 }
215
216 /// \internal_
217 bool equal(const buffer_iterator<T> &other) const
218 {
219 return m_buffer.get() == other.m_buffer.get() &&
220 m_index == other.m_index;
221 }
222
223 /// \internal_
224 void increment()
225 {
226 m_index++;
227 }
228
229 /// \internal_
230 void decrement()
231 {
232 m_index--;
233 }
234
235 /// \internal_
236 void advance(difference_type n)
237 {
238 m_index = static_cast<size_t>(static_cast<difference_type>(m_index) + n);
239 }
240
241 /// \internal_
242 difference_type distance_to(const buffer_iterator<T> &other) const
243 {
244 return static_cast<difference_type>(other.m_index - m_index);
245 }
246
247 private:
248 const buffer m_buffer;
249 size_t m_index;
250 };
251
252 /// Creates a new \ref buffer_iterator for \p buffer at \p index.
253 ///
254 /// \param buffer the \ref buffer object
255 /// \param index the index in the buffer
256 ///
257 /// \return a \c buffer_iterator for \p buffer at \p index
258 template<class T>
259 inline buffer_iterator<T>
260 make_buffer_iterator(const buffer &buffer, size_t index = 0)
261 {
262 return buffer_iterator<T>(buffer, index);
263 }
264
265 /// \internal_ (is_device_iterator specialization for buffer_iterator)
266 template<class T>
267 struct is_device_iterator<buffer_iterator<T> > : boost::true_type {};
268
269 namespace detail {
270
271 // is_buffer_iterator specialization for buffer_iterator
272 template<class Iterator>
273 struct is_buffer_iterator<
274 Iterator,
275 typename boost::enable_if<
276 boost::is_same<
277 buffer_iterator<typename Iterator::value_type>,
278 typename boost::remove_const<Iterator>::type
279 >
280 >::type
281 > : public boost::true_type {};
282
283 } // end detail namespace
284 } // end compute namespace
285 } // end boost namespace
286
287 #endif // BOOST_COMPUTE_ITERATOR_BUFFER_ITERATOR_HPP