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_DEVICE_PTR_HPP
12 #define BOOST_COMPUTE_DEVICE_PTR_HPP
14 #include <boost/type_traits.hpp>
15 #include <boost/static_assert.hpp>
17 #include <boost/compute/buffer.hpp>
18 #include <boost/compute/config.hpp>
19 #include <boost/compute/detail/is_buffer_iterator.hpp>
20 #include <boost/compute/detail/read_write_single_value.hpp>
21 #include <boost/compute/type_traits/is_device_iterator.hpp>
27 template<class T, class IndexExpr>
28 struct device_ptr_index_expr
30 typedef T result_type;
32 device_ptr_index_expr(const buffer &buffer,
34 const IndexExpr &expr)
43 BOOST_STATIC_ASSERT_MSG(boost::is_integral<IndexExpr>::value,
44 "Index expression must be integral");
46 BOOST_ASSERT(m_buffer.get());
48 const context &context = m_buffer.get_context();
49 const device &device = context.get_device();
50 command_queue queue(context, device);
52 return detail::read_single_value<T>(m_buffer, m_expr, queue);
55 const buffer &m_buffer;
65 typedef std::size_t size_type;
66 typedef std::ptrdiff_t difference_type;
67 typedef std::random_access_iterator_tag iterator_category;
76 device_ptr(const buffer &buffer, size_t index = 0)
77 : m_buffer(buffer.get(), false),
82 device_ptr(const device_ptr<T> &other)
83 : m_buffer(other.m_buffer.get(), false),
84 m_index(other.m_index)
88 device_ptr<T>& operator=(const device_ptr<T> &other)
91 m_buffer.get() = other.m_buffer.get();
92 m_index = other.m_index;
98 #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
99 device_ptr(device_ptr<T>&& other) BOOST_NOEXCEPT
100 : m_buffer(other.m_buffer.get(), false),
101 m_index(other.m_index)
103 other.m_buffer.get() = 0;
106 device_ptr<T>& operator=(device_ptr<T>&& other) BOOST_NOEXCEPT
108 m_buffer.get() = other.m_buffer.get();
109 m_index = other.m_index;
111 other.m_buffer.get() = 0;
115 #endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
119 // set buffer to null so that its reference count will
120 // not be decremented when its destructor is called
124 size_type get_index() const
129 const buffer& get_buffer() const
135 device_ptr<OT> cast() const
137 return device_ptr<OT>(m_buffer, m_index);
140 device_ptr<T> operator+(difference_type n) const
142 return device_ptr<T>(m_buffer, m_index + n);
145 device_ptr<T> operator+(const device_ptr<T> &other) const
147 return device_ptr<T>(m_buffer, m_index + other.m_index);
150 device_ptr<T>& operator+=(difference_type n)
152 m_index += static_cast<size_t>(n);
156 difference_type operator-(const device_ptr<T> &other) const
158 return static_cast<difference_type>(m_index - other.m_index);
161 device_ptr<T>& operator-=(difference_type n)
167 bool operator==(const device_ptr<T> &other) const
169 return m_buffer.get() == other.m_buffer.get() &&
170 m_index == other.m_index;
173 bool operator!=(const device_ptr<T> &other) const
175 return !(*this == other);
179 detail::device_ptr_index_expr<T, Expr>
180 operator[](const Expr &expr) const
182 BOOST_ASSERT(m_buffer.get());
184 return detail::device_ptr_index_expr<T, Expr>(m_buffer,
190 const buffer m_buffer;
194 // is_buffer_iterator specialization for device_ptr
195 template<class Iterator>
196 struct is_buffer_iterator<
198 typename boost::enable_if<
200 device_ptr<typename Iterator::value_type>,
201 typename boost::remove_const<Iterator>::type
204 > : public boost::true_type {};
206 } // end detail namespace
208 // is_device_iterator specialization for device_ptr
210 struct is_device_iterator<detail::device_ptr<T> > : boost::true_type {};
212 } // end compute namespace
213 } // end boost namespace
215 #endif // BOOST_COMPUTE_DEVICE_PTR_HPP