array.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. #ifndef BOOST_COMPUTE_CONTAINER_ARRAY_HPP
  11. #define BOOST_COMPUTE_CONTAINER_ARRAY_HPP
  12. #include <cstddef>
  13. #include <iterator>
  14. #include <exception>
  15. #include <boost/array.hpp>
  16. #include <boost/throw_exception.hpp>
  17. #include <boost/compute/buffer.hpp>
  18. #include <boost/compute/system.hpp>
  19. #include <boost/compute/algorithm/fill.hpp>
  20. #include <boost/compute/algorithm/swap_ranges.hpp>
  21. #include <boost/compute/iterator/buffer_iterator.hpp>
  22. #include <boost/compute/type_traits/detail/capture_traits.hpp>
  23. #include <boost/compute/detail/buffer_value.hpp>
  24. namespace boost {
  25. namespace compute {
  26. /// \class array
  27. /// \brief A fixed-size container.
  28. ///
  29. /// The array container is very similar to the \ref vector container except
  30. /// its size is fixed at compile-time rather than being dynamically resizable
  31. /// at run-time.
  32. ///
  33. /// For example, to create a fixed-size array with eight values on the device:
  34. /// \code
  35. /// boost::compute::array<int, 8> values(context);
  36. /// \endcode
  37. ///
  38. /// The Boost.Compute \c array class provides a STL-like API and is modeled
  39. /// after the \c std::array class from the C++ standard library.
  40. ///
  41. /// \see \ref vector "vector<T>"
  42. template<class T, std::size_t N>
  43. class array
  44. {
  45. public:
  46. typedef T value_type;
  47. typedef std::size_t size_type;
  48. typedef ptrdiff_t difference_type;
  49. typedef detail::buffer_value<T> reference;
  50. typedef const detail::buffer_value<T> const_reference;
  51. typedef T* pointer;
  52. typedef const T* const_pointer;
  53. typedef buffer_iterator<T> iterator;
  54. typedef buffer_iterator<T> const_iterator;
  55. typedef std::reverse_iterator<iterator> reverse_iterator;
  56. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  57. enum {
  58. static_size = N
  59. };
  60. explicit array(const context &context = system::default_context())
  61. : m_buffer(context, sizeof(T) * N)
  62. {
  63. }
  64. array(const array<T, N> &other)
  65. : m_buffer(other.m_buffer.get_context(), sizeof(T) * N)
  66. {
  67. command_queue queue = default_queue();
  68. boost::compute::copy(other.begin(), other.end(), begin(), queue);
  69. queue.finish();
  70. }
  71. array(const boost::array<T, N> &array,
  72. const context &context = system::default_context())
  73. : m_buffer(context, sizeof(T) * N)
  74. {
  75. command_queue queue = default_queue();
  76. boost::compute::copy(array.begin(), array.end(), begin(), queue);
  77. queue.finish();
  78. }
  79. array(const array<T, N> &other,
  80. const command_queue &queue)
  81. : m_buffer(other.m_buffer.get_context(), sizeof(T) * N)
  82. {
  83. boost::compute::copy(other.begin(), other.end(), begin(), queue);
  84. }
  85. array<T, N>& operator=(const array<T, N> &other)
  86. {
  87. if(this != &other){
  88. command_queue queue = default_queue();
  89. boost::compute::copy(other.begin(), other.end(), begin(), queue);
  90. queue.finish();
  91. }
  92. return *this;
  93. }
  94. array<T, N>& operator=(const boost::array<T, N> &array)
  95. {
  96. command_queue queue = default_queue();
  97. boost::compute::copy(array.begin(), array.end(), begin(), queue);
  98. queue.finish();
  99. return *this;
  100. }
  101. ~array()
  102. {
  103. }
  104. iterator begin()
  105. {
  106. return buffer_iterator<T>(m_buffer, 0);
  107. }
  108. const_iterator begin() const
  109. {
  110. return buffer_iterator<T>(m_buffer, 0);
  111. }
  112. const_iterator cbegin() const
  113. {
  114. return begin();
  115. }
  116. iterator end()
  117. {
  118. return buffer_iterator<T>(m_buffer, N);
  119. }
  120. const_iterator end() const
  121. {
  122. return buffer_iterator<T>(m_buffer, N);
  123. }
  124. const_iterator cend() const
  125. {
  126. return end();
  127. }
  128. reverse_iterator rbegin()
  129. {
  130. return reverse_iterator(end() - 1);
  131. }
  132. const_reverse_iterator rbegin() const
  133. {
  134. return reverse_iterator(end() - 1);
  135. }
  136. const_reverse_iterator crbegin() const
  137. {
  138. return rbegin();
  139. }
  140. reverse_iterator rend()
  141. {
  142. return reverse_iterator(begin() - 1);
  143. }
  144. const_reverse_iterator rend() const
  145. {
  146. return reverse_iterator(begin() - 1);
  147. }
  148. const_reverse_iterator crend() const
  149. {
  150. return rend();
  151. }
  152. size_type size() const
  153. {
  154. return N;
  155. }
  156. bool empty() const
  157. {
  158. return N == 0;
  159. }
  160. size_type max_size() const
  161. {
  162. return N;
  163. }
  164. reference operator[](size_type index)
  165. {
  166. return *(begin() + static_cast<difference_type>(index));
  167. }
  168. const_reference operator[](size_type index) const
  169. {
  170. return *(begin() + static_cast<difference_type>(index));
  171. }
  172. reference at(size_type index)
  173. {
  174. if(index >= N){
  175. BOOST_THROW_EXCEPTION(std::out_of_range("index out of range"));
  176. }
  177. return operator[](index);
  178. }
  179. const_reference at(size_type index) const
  180. {
  181. if(index >= N){
  182. BOOST_THROW_EXCEPTION(std::out_of_range("index out of range"));
  183. }
  184. return operator[](index);
  185. }
  186. reference front()
  187. {
  188. return *begin();
  189. }
  190. const_reference front() const
  191. {
  192. return *begin();
  193. }
  194. reference back()
  195. {
  196. return *(end() - static_cast<difference_type>(1));
  197. }
  198. const_reference back() const
  199. {
  200. return *(end() - static_cast<difference_type>(1));
  201. }
  202. void fill(const value_type &value, const command_queue &queue)
  203. {
  204. ::boost::compute::fill(begin(), end(), value, queue);
  205. }
  206. void swap(array<T, N> &other, const command_queue &queue)
  207. {
  208. ::boost::compute::swap_ranges(begin(), end(), other.begin(), queue);
  209. }
  210. void fill(const value_type &value)
  211. {
  212. command_queue queue = default_queue();
  213. ::boost::compute::fill(begin(), end(), value, queue);
  214. queue.finish();
  215. }
  216. void swap(array<T, N> &other)
  217. {
  218. command_queue queue = default_queue();
  219. ::boost::compute::swap_ranges(begin(), end(), other.begin(), queue);
  220. queue.finish();
  221. }
  222. const buffer& get_buffer() const
  223. {
  224. return m_buffer;
  225. }
  226. private:
  227. buffer m_buffer;
  228. command_queue default_queue() const
  229. {
  230. const context &context = m_buffer.get_context();
  231. command_queue queue(context, context.get_device());
  232. return queue;
  233. }
  234. };
  235. namespace detail {
  236. // set_kernel_arg specialization for array<T, N>
  237. template<class T, std::size_t N>
  238. struct set_kernel_arg<array<T, N> >
  239. {
  240. void operator()(kernel &kernel_, size_t index, const array<T, N> &array)
  241. {
  242. kernel_.set_arg(index, array.get_buffer());
  243. }
  244. };
  245. // for capturing array<T, N> with BOOST_COMPUTE_CLOSURE()
  246. template<class T, size_t N>
  247. struct capture_traits<array<T, N> >
  248. {
  249. static std::string type_name()
  250. {
  251. return std::string("__global ") + ::boost::compute::type_name<T>() + "*";
  252. }
  253. };
  254. // meta_kernel streaming operator for array<T, N>
  255. template<class T, size_t N>
  256. meta_kernel& operator<<(meta_kernel &k, const array<T, N> &array)
  257. {
  258. return k << k.get_buffer_identifier<T>(array.get_buffer());
  259. }
  260. } // end detail namespace
  261. } // end compute namespace
  262. } // end boost namespace
  263. #endif // BOOST_COMPUTE_CONTAINER_ARRAY_HPP