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_CONTAINER_ARRAY_HPP
12 #define BOOST_COMPUTE_CONTAINER_ARRAY_HPP
18 #include <boost/array.hpp>
19 #include <boost/throw_exception.hpp>
21 #include <boost/compute/buffer.hpp>
22 #include <boost/compute/system.hpp>
23 #include <boost/compute/algorithm/fill.hpp>
24 #include <boost/compute/algorithm/swap_ranges.hpp>
25 #include <boost/compute/iterator/buffer_iterator.hpp>
26 #include <boost/compute/type_traits/detail/capture_traits.hpp>
27 #include <boost/compute/detail/buffer_value.hpp>
33 /// \brief A fixed-size container.
35 /// The array container is very similar to the \ref vector container except
36 /// its size is fixed at compile-time rather than being dynamically resizable
39 /// For example, to create a fixed-size array with eight values on the device:
41 /// boost::compute::array<int, 8> values(context);
44 /// The Boost.Compute \c array class provides a STL-like API and is modeled
45 /// after the \c std::array class from the C++ standard library.
47 /// \see \ref vector "vector<T>"
48 template<class T, std::size_t N>
53 typedef std::size_t size_type;
54 typedef ptrdiff_t difference_type;
55 typedef detail::buffer_value<T> reference;
56 typedef const detail::buffer_value<T> const_reference;
58 typedef const T* const_pointer;
59 typedef buffer_iterator<T> iterator;
60 typedef buffer_iterator<T> const_iterator;
61 typedef std::reverse_iterator<iterator> reverse_iterator;
62 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
68 explicit array(const context &context = system::default_context())
69 : m_buffer(context, sizeof(T) * N)
73 array(const array<T, N> &other)
74 : m_buffer(other.m_buffer.get_context(), sizeof(T) * N)
76 command_queue queue = default_queue();
77 boost::compute::copy(other.begin(), other.end(), begin(), queue);
81 array(const boost::array<T, N> &array,
82 const context &context = system::default_context())
83 : m_buffer(context, sizeof(T) * N)
85 command_queue queue = default_queue();
86 boost::compute::copy(array.begin(), array.end(), begin(), queue);
90 array(const array<T, N> &other,
91 const command_queue &queue)
92 : m_buffer(other.m_buffer.get_context(), sizeof(T) * N)
94 boost::compute::copy(other.begin(), other.end(), begin(), queue);
97 array<T, N>& operator=(const array<T, N> &other)
100 command_queue queue = default_queue();
101 boost::compute::copy(other.begin(), other.end(), begin(), queue);
108 array<T, N>& operator=(const boost::array<T, N> &array)
110 command_queue queue = default_queue();
111 boost::compute::copy(array.begin(), array.end(), begin(), queue);
123 return buffer_iterator<T>(m_buffer, 0);
126 const_iterator begin() const
128 return buffer_iterator<T>(m_buffer, 0);
131 const_iterator cbegin() const
138 return buffer_iterator<T>(m_buffer, N);
141 const_iterator end() const
143 return buffer_iterator<T>(m_buffer, N);
146 const_iterator cend() const
151 reverse_iterator rbegin()
153 return reverse_iterator(end() - 1);
156 const_reverse_iterator rbegin() const
158 return reverse_iterator(end() - 1);
161 const_reverse_iterator crbegin() const
166 reverse_iterator rend()
168 return reverse_iterator(begin() - 1);
171 const_reverse_iterator rend() const
173 return reverse_iterator(begin() - 1);
176 const_reverse_iterator crend() const
181 size_type size() const
191 size_type max_size() const
196 reference operator[](size_type index)
198 return *(begin() + static_cast<difference_type>(index));
201 const_reference operator[](size_type index) const
203 return *(begin() + static_cast<difference_type>(index));
206 reference at(size_type index)
209 BOOST_THROW_EXCEPTION(std::out_of_range("index out of range"));
212 return operator[](index);
215 const_reference at(size_type index) const
218 BOOST_THROW_EXCEPTION(std::out_of_range("index out of range"));
221 return operator[](index);
229 const_reference front() const
236 return *(end() - static_cast<difference_type>(1));
239 const_reference back() const
241 return *(end() - static_cast<difference_type>(1));
244 void fill(const value_type &value, const command_queue &queue)
246 ::boost::compute::fill(begin(), end(), value, queue);
249 void swap(array<T, N> &other, const command_queue &queue)
251 ::boost::compute::swap_ranges(begin(), end(), other.begin(), queue);
254 void fill(const value_type &value)
256 command_queue queue = default_queue();
257 ::boost::compute::fill(begin(), end(), value, queue);
261 void swap(array<T, N> &other)
263 command_queue queue = default_queue();
264 ::boost::compute::swap_ranges(begin(), end(), other.begin(), queue);
268 const buffer& get_buffer() const
276 command_queue default_queue() const
278 const context &context = m_buffer.get_context();
279 command_queue queue(context, context.get_device());
286 // set_kernel_arg specialization for array<T, N>
287 template<class T, std::size_t N>
288 struct set_kernel_arg<array<T, N> >
290 void operator()(kernel &kernel_, size_t index, const array<T, N> &array)
292 kernel_.set_arg(index, array.get_buffer());
296 // for capturing array<T, N> with BOOST_COMPUTE_CLOSURE()
297 template<class T, size_t N>
298 struct capture_traits<array<T, N> >
300 static std::string type_name()
302 return std::string("__global ") + ::boost::compute::type_name<T>() + "*";
306 // meta_kernel streaming operator for array<T, N>
307 template<class T, size_t N>
308 meta_kernel& operator<<(meta_kernel &k, const array<T, N> &array)
310 return k << k.get_buffer_identifier<T>(array.get_buffer());
313 } // end detail namespace
314 } // end compute namespace
315 } // end boost namespace
317 #endif // BOOST_COMPUTE_CONTAINER_ARRAY_HPP