vector.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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_VECTOR_HPP
  11. #define BOOST_COMPUTE_CONTAINER_VECTOR_HPP
  12. #include <vector>
  13. #include <cstddef>
  14. #include <iterator>
  15. #include <exception>
  16. #include <boost/throw_exception.hpp>
  17. #include <boost/compute/config.hpp>
  18. #ifndef BOOST_COMPUTE_NO_HDR_INITIALIZER_LIST
  19. #include <initializer_list>
  20. #endif
  21. #include <boost/compute/buffer.hpp>
  22. #include <boost/compute/device.hpp>
  23. #include <boost/compute/system.hpp>
  24. #include <boost/compute/context.hpp>
  25. #include <boost/compute/command_queue.hpp>
  26. #include <boost/compute/algorithm/copy.hpp>
  27. #include <boost/compute/algorithm/copy_n.hpp>
  28. #include <boost/compute/algorithm/fill_n.hpp>
  29. #include <boost/compute/allocator/buffer_allocator.hpp>
  30. #include <boost/compute/iterator/buffer_iterator.hpp>
  31. #include <boost/compute/type_traits/detail/capture_traits.hpp>
  32. #include <boost/compute/detail/buffer_value.hpp>
  33. #include <boost/compute/detail/iterator_range_size.hpp>
  34. namespace boost {
  35. namespace compute {
  36. /// \class vector
  37. /// \brief A resizable array of values.
  38. ///
  39. /// The vector<T> class stores a dynamic array of values. Internally, the data
  40. /// is stored in an OpenCL buffer object.
  41. ///
  42. /// The vector class is the prefered container for storing and accessing data
  43. /// on a compute device. In most cases it should be used instead of directly
  44. /// dealing with buffer objects. If the undelying buffer is needed, it can be
  45. /// accessed with the get_buffer() method.
  46. ///
  47. /// The internal storage is allocated in a specific OpenCL context which is
  48. /// passed as an argument to the constructor when the vector is created.
  49. ///
  50. /// For example, to create a vector on the device containing space for ten
  51. /// \c int values:
  52. /// \code
  53. /// boost::compute::vector<int> vec(10, context);
  54. /// \endcode
  55. ///
  56. /// Allocation and data transfer can also be performed in a single step:
  57. /// \code
  58. /// // values on the host
  59. /// int data[] = { 1, 2, 3, 4 };
  60. ///
  61. /// // create a vector of size four and copy the values from data
  62. /// boost::compute::vector<int> vec(data, data + 4, queue);
  63. /// \endcode
  64. ///
  65. /// The Boost.Compute \c vector class provides a STL-like API and is modeled
  66. /// after the \c std::vector class from the C++ standard library. It can be
  67. /// used with any of the STL-like algorithms provided by Boost.Compute
  68. /// including \c copy(), \c transform(), and \c sort() (among many others).
  69. ///
  70. /// For example:
  71. /// \code
  72. /// // a vector on a compute device
  73. /// boost::compute::vector<float> vec = ...
  74. ///
  75. /// // copy data to the vector from a host std:vector
  76. /// boost::compute::copy(host_vec.begin(), host_vec.end(), vec.begin(), queue);
  77. ///
  78. /// // copy data from the vector to a host std::vector
  79. /// boost::compute::copy(vec.begin(), vec.end(), host_vec.begin(), queue);
  80. ///
  81. /// // sort the values in the vector
  82. /// boost::compute::sort(vec.begin(), vec.end(), queue);
  83. ///
  84. /// // calculate the sum of the values in the vector (also see reduce())
  85. /// float sum = boost::compute::accumulate(vec.begin(), vec.end(), 0, queue);
  86. ///
  87. /// // reverse the values in the vector
  88. /// boost::compute::reverse(vec.begin(), vec.end(), queue);
  89. ///
  90. /// // fill the vector with ones
  91. /// boost::compute::fill(vec.begin(), vec.end(), 1, queue);
  92. /// \endcode
  93. ///
  94. /// \see \ref array "array<T, N>", buffer
  95. template<class T, class Alloc = buffer_allocator<T> >
  96. class vector
  97. {
  98. public:
  99. typedef T value_type;
  100. typedef Alloc allocator_type;
  101. typedef typename allocator_type::size_type size_type;
  102. typedef typename allocator_type::difference_type difference_type;
  103. typedef detail::buffer_value<T> reference;
  104. typedef const detail::buffer_value<T> const_reference;
  105. typedef typename allocator_type::pointer pointer;
  106. typedef typename allocator_type::const_pointer const_pointer;
  107. typedef buffer_iterator<T> iterator;
  108. typedef buffer_iterator<T> const_iterator;
  109. typedef std::reverse_iterator<iterator> reverse_iterator;
  110. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  111. /// Creates an empty vector in \p context.
  112. explicit vector(const context &context = system::default_context())
  113. : m_size(0),
  114. m_allocator(context)
  115. {
  116. m_data = m_allocator.allocate(_minimum_capacity());
  117. }
  118. /// Creates a vector with space for \p count elements in \p context.
  119. ///
  120. /// Note that unlike \c std::vector's constructor, this will not initialize
  121. /// the values in the container. Either call the vector constructor which
  122. /// takes a value to initialize with or use the fill() algorithm to set
  123. /// the initial values.
  124. ///
  125. /// For example:
  126. /// \code
  127. /// // create a vector on the device with space for ten ints
  128. /// boost::compute::vector<int> vec(10, context);
  129. /// \endcode
  130. explicit vector(size_type count,
  131. const context &context = system::default_context())
  132. : m_size(count),
  133. m_allocator(context)
  134. {
  135. m_data = m_allocator.allocate((std::max)(count, _minimum_capacity()));
  136. }
  137. /// Creates a vector with space for \p count elements and sets each equal
  138. /// to \p value.
  139. ///
  140. /// For example:
  141. /// \code
  142. /// // creates a vector with four values set to nine (e.g. [9, 9, 9, 9]).
  143. /// boost::compute::vector<int> vec(4, 9, queue);
  144. /// \endcode
  145. vector(size_type count,
  146. const T &value,
  147. command_queue &queue = system::default_queue())
  148. : m_size(count),
  149. m_allocator(queue.get_context())
  150. {
  151. m_data = m_allocator.allocate((std::max)(count, _minimum_capacity()));
  152. ::boost::compute::fill_n(begin(), count, value, queue);
  153. }
  154. /// Creates a vector with space for the values in the range [\p first,
  155. /// \p last) and copies them into the vector with \p queue.
  156. ///
  157. /// For example:
  158. /// \code
  159. /// // values on the host
  160. /// int data[] = { 1, 2, 3, 4 };
  161. ///
  162. /// // create a vector of size four and copy the values from data
  163. /// boost::compute::vector<int> vec(data, data + 4, queue);
  164. /// \endcode
  165. template<class InputIterator>
  166. vector(InputIterator first,
  167. InputIterator last,
  168. command_queue &queue = system::default_queue())
  169. : m_size(detail::iterator_range_size(first, last)),
  170. m_allocator(queue.get_context())
  171. {
  172. m_data = m_allocator.allocate((std::max)(m_size, _minimum_capacity()));
  173. ::boost::compute::copy(first, last, begin(), queue);
  174. }
  175. /// Creates a new vector and copies the values from \p other.
  176. vector(const vector &other,
  177. command_queue &queue = system::default_queue())
  178. : m_size(other.m_size),
  179. m_allocator(other.m_allocator)
  180. {
  181. m_data = m_allocator.allocate((std::max)(m_size, _minimum_capacity()));
  182. if(!other.empty()){
  183. if(other.get_buffer().get_context() != queue.get_context()){
  184. command_queue other_queue = other.default_queue();
  185. ::boost::compute::copy(other.begin(), other.end(), begin(), other_queue);
  186. other_queue.finish();
  187. }
  188. else {
  189. ::boost::compute::copy(other.begin(), other.end(), begin(), queue);
  190. queue.finish();
  191. }
  192. }
  193. }
  194. /// Creates a new vector and copies the values from \p other.
  195. template<class OtherAlloc>
  196. vector(const vector<T, OtherAlloc> &other,
  197. command_queue &queue = system::default_queue())
  198. : m_size(other.size()),
  199. m_allocator(queue.get_context())
  200. {
  201. m_data = m_allocator.allocate((std::max)(m_size, _minimum_capacity()));
  202. if(!other.empty()){
  203. ::boost::compute::copy(other.begin(), other.end(), begin(), queue);
  204. queue.finish();
  205. }
  206. }
  207. /// Creates a new vector and copies the values from \p vector.
  208. template<class OtherAlloc>
  209. vector(const std::vector<T, OtherAlloc> &vector,
  210. command_queue &queue = system::default_queue())
  211. : m_size(vector.size()),
  212. m_allocator(queue.get_context())
  213. {
  214. m_data = m_allocator.allocate((std::max)(m_size, _minimum_capacity()));
  215. ::boost::compute::copy(vector.begin(), vector.end(), begin(), queue);
  216. }
  217. #ifndef BOOST_COMPUTE_NO_HDR_INITIALIZER_LIST
  218. vector(std::initializer_list<T> list,
  219. command_queue &queue = system::default_queue())
  220. : m_size(list.size()),
  221. m_allocator(queue.get_context())
  222. {
  223. m_data = m_allocator.allocate((std::max)(m_size, _minimum_capacity()));
  224. ::boost::compute::copy(list.begin(), list.end(), begin(), queue);
  225. }
  226. #endif // BOOST_COMPUTE_NO_HDR_INITIALIZER_LIST
  227. vector& operator=(const vector &other)
  228. {
  229. if(this != &other){
  230. command_queue queue = default_queue();
  231. resize(other.size(), queue);
  232. ::boost::compute::copy(other.begin(), other.end(), begin(), queue);
  233. queue.finish();
  234. }
  235. return *this;
  236. }
  237. template<class OtherAlloc>
  238. vector& operator=(const vector<T, OtherAlloc> &other)
  239. {
  240. command_queue queue = default_queue();
  241. resize(other.size(), queue);
  242. ::boost::compute::copy(other.begin(), other.end(), begin(), queue);
  243. queue.finish();
  244. return *this;
  245. }
  246. template<class OtherAlloc>
  247. vector& operator=(const std::vector<T, OtherAlloc> &vector)
  248. {
  249. command_queue queue = default_queue();
  250. resize(vector.size(), queue);
  251. ::boost::compute::copy(vector.begin(), vector.end(), begin(), queue);
  252. queue.finish();
  253. return *this;
  254. }
  255. #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
  256. /// Move-constructs a new vector from \p other.
  257. vector(vector&& other)
  258. : m_data(std::move(other.m_data)),
  259. m_size(other.m_size),
  260. m_allocator(std::move(other.m_allocator))
  261. {
  262. other.m_size = 0;
  263. }
  264. /// Move-assigns the data from \p other to \c *this.
  265. vector& operator=(vector&& other)
  266. {
  267. if(capacity() > 0){
  268. m_allocator.deallocate(m_data, capacity());
  269. }
  270. m_data = std::move(other.m_data);
  271. m_size = other.m_size;
  272. m_allocator = std::move(other.m_allocator);
  273. other.m_size = 0;
  274. return *this;
  275. }
  276. #endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
  277. /// Destroys the vector object.
  278. ~vector()
  279. {
  280. if(capacity() > 0){
  281. m_allocator.deallocate(m_data, capacity());
  282. }
  283. }
  284. iterator begin()
  285. {
  286. return ::boost::compute::make_buffer_iterator<T>(m_data.get_buffer(), 0);
  287. }
  288. const_iterator begin() const
  289. {
  290. return ::boost::compute::make_buffer_iterator<T>(m_data.get_buffer(), 0);
  291. }
  292. const_iterator cbegin() const
  293. {
  294. return begin();
  295. }
  296. iterator end()
  297. {
  298. return ::boost::compute::make_buffer_iterator<T>(m_data.get_buffer(), m_size);
  299. }
  300. const_iterator end() const
  301. {
  302. return ::boost::compute::make_buffer_iterator<T>(m_data.get_buffer(), m_size);
  303. }
  304. const_iterator cend() const
  305. {
  306. return end();
  307. }
  308. reverse_iterator rbegin()
  309. {
  310. return reverse_iterator(end() - 1);
  311. }
  312. const_reverse_iterator rbegin() const
  313. {
  314. return reverse_iterator(end() - 1);
  315. }
  316. const_reverse_iterator crbegin() const
  317. {
  318. return rbegin();
  319. }
  320. reverse_iterator rend()
  321. {
  322. return reverse_iterator(begin() - 1);
  323. }
  324. const_reverse_iterator rend() const
  325. {
  326. return reverse_iterator(begin() - 1);
  327. }
  328. const_reverse_iterator crend() const
  329. {
  330. return rend();
  331. }
  332. /// Returns the number of elements in the vector.
  333. size_type size() const
  334. {
  335. return m_size;
  336. }
  337. size_type max_size() const
  338. {
  339. return m_allocator.max_size();
  340. }
  341. /// Resizes the vector to \p size.
  342. void resize(size_type size, command_queue &queue)
  343. {
  344. if(size <= capacity()){
  345. m_size = size;
  346. }
  347. else {
  348. // allocate new buffer
  349. pointer new_data =
  350. m_allocator.allocate(
  351. static_cast<size_type>(
  352. static_cast<float>(size) * _growth_factor()
  353. )
  354. );
  355. if(capacity() > 0)
  356. {
  357. // copy old values to the new buffer
  358. ::boost::compute::copy(m_data, m_data + m_size, new_data, queue);
  359. // free old memory
  360. m_allocator.deallocate(m_data, capacity());
  361. }
  362. // set new data and size
  363. m_data = new_data;
  364. m_size = size;
  365. }
  366. }
  367. /// \overload
  368. void resize(size_type size)
  369. {
  370. command_queue queue = default_queue();
  371. resize(size, queue);
  372. queue.finish();
  373. }
  374. /// Returns \c true if the vector is empty.
  375. bool empty() const
  376. {
  377. return m_size == 0;
  378. }
  379. /// Returns the capacity of the vector.
  380. size_type capacity() const
  381. {
  382. if(m_data == pointer()) // null pointer check
  383. {
  384. return 0;
  385. }
  386. return m_data.get_buffer().size() / sizeof(T);
  387. }
  388. void reserve(size_type size, command_queue &queue)
  389. {
  390. if(size > max_size()){
  391. throw std::length_error("vector::reserve");
  392. }
  393. if(capacity() < size){
  394. // allocate new buffer
  395. pointer new_data =
  396. m_allocator.allocate(
  397. static_cast<size_type>(
  398. static_cast<float>(size) * _growth_factor()
  399. )
  400. );
  401. if(capacity() > 0)
  402. {
  403. // copy old values to the new buffer
  404. ::boost::compute::copy(m_data, m_data + m_size, new_data, queue);
  405. // free old memory
  406. m_allocator.deallocate(m_data, capacity());
  407. }
  408. // set new data
  409. m_data = new_data;
  410. }
  411. }
  412. void reserve(size_type size)
  413. {
  414. command_queue queue = default_queue();
  415. reserve(size, queue);
  416. queue.finish();
  417. }
  418. void shrink_to_fit(command_queue &queue)
  419. {
  420. pointer old_data = m_data;
  421. m_data = pointer(); // null pointer
  422. if(m_size > 0)
  423. {
  424. // allocate new buffer
  425. m_data = m_allocator.allocate(m_size);
  426. // copy old values to the new buffer
  427. ::boost::compute::copy(old_data, old_data + m_size, m_data, queue);
  428. }
  429. if(capacity() > 0)
  430. {
  431. // free old memory
  432. m_allocator.deallocate(old_data, capacity());
  433. }
  434. }
  435. void shrink_to_fit()
  436. {
  437. command_queue queue = default_queue();
  438. shrink_to_fit(queue);
  439. queue.finish();
  440. }
  441. reference operator[](size_type index)
  442. {
  443. return *(begin() + static_cast<difference_type>(index));
  444. }
  445. const_reference operator[](size_type index) const
  446. {
  447. return *(begin() + static_cast<difference_type>(index));
  448. }
  449. reference at(size_type index)
  450. {
  451. if(index >= size()){
  452. BOOST_THROW_EXCEPTION(std::out_of_range("index out of range"));
  453. }
  454. return operator[](index);
  455. }
  456. const_reference at(size_type index) const
  457. {
  458. if(index >= size()){
  459. BOOST_THROW_EXCEPTION(std::out_of_range("index out of range"));
  460. }
  461. return operator[](index);
  462. }
  463. reference front()
  464. {
  465. return *begin();
  466. }
  467. const_reference front() const
  468. {
  469. return *begin();
  470. }
  471. reference back()
  472. {
  473. return *(end() - static_cast<difference_type>(1));
  474. }
  475. const_reference back() const
  476. {
  477. return *(end() - static_cast<difference_type>(1));
  478. }
  479. template<class InputIterator>
  480. void assign(InputIterator first,
  481. InputIterator last,
  482. command_queue &queue)
  483. {
  484. // resize vector for new contents
  485. resize(detail::iterator_range_size(first, last), queue);
  486. // copy values into the vector
  487. ::boost::compute::copy(first, last, begin(), queue);
  488. }
  489. template<class InputIterator>
  490. void assign(InputIterator first, InputIterator last)
  491. {
  492. command_queue queue = default_queue();
  493. assign(first, last, queue);
  494. queue.finish();
  495. }
  496. void assign(size_type n, const T &value, command_queue &queue)
  497. {
  498. // resize vector for new contents
  499. resize(n, queue);
  500. // fill vector with value
  501. ::boost::compute::fill_n(begin(), n, value, queue);
  502. }
  503. void assign(size_type n, const T &value)
  504. {
  505. command_queue queue = default_queue();
  506. assign(n, value, queue);
  507. queue.finish();
  508. }
  509. /// Inserts \p value at the end of the vector (resizing if neccessary).
  510. ///
  511. /// Note that calling \c push_back() to insert data values one at a time
  512. /// is inefficient as there is a non-trivial overhead in performing a data
  513. /// transfer to the device. It is usually better to store a set of values
  514. /// on the host (for example, in a \c std::vector) and then transfer them
  515. /// in bulk using the \c insert() method or the copy() algorithm.
  516. void push_back(const T &value, command_queue &queue)
  517. {
  518. insert(end(), value, queue);
  519. }
  520. /// \overload
  521. void push_back(const T &value)
  522. {
  523. command_queue queue = default_queue();
  524. push_back(value, queue);
  525. queue.finish();
  526. }
  527. void pop_back(command_queue &queue)
  528. {
  529. resize(size() - 1, queue);
  530. }
  531. void pop_back()
  532. {
  533. command_queue queue = default_queue();
  534. pop_back(queue);
  535. queue.finish();
  536. }
  537. iterator insert(iterator position, const T &value, command_queue &queue)
  538. {
  539. if(position == end()){
  540. resize(m_size + 1, queue);
  541. position = begin() + position.get_index();
  542. ::boost::compute::copy_n(&value, 1, position, queue);
  543. }
  544. else {
  545. ::boost::compute::vector<T, Alloc> tmp(position, end(), queue);
  546. resize(m_size + 1, queue);
  547. position = begin() + position.get_index();
  548. ::boost::compute::copy_n(&value, 1, position, queue);
  549. ::boost::compute::copy(tmp.begin(), tmp.end(), position + 1, queue);
  550. }
  551. return position + 1;
  552. }
  553. iterator insert(iterator position, const T &value)
  554. {
  555. command_queue queue = default_queue();
  556. iterator iter = insert(position, value, queue);
  557. queue.finish();
  558. return iter;
  559. }
  560. void insert(iterator position,
  561. size_type count,
  562. const T &value,
  563. command_queue &queue)
  564. {
  565. ::boost::compute::vector<T, Alloc> tmp(position, end(), queue);
  566. resize(size() + count, queue);
  567. position = begin() + position.get_index();
  568. ::boost::compute::fill_n(position, count, value, queue);
  569. ::boost::compute::copy(
  570. tmp.begin(),
  571. tmp.end(),
  572. position + static_cast<difference_type>(count),
  573. queue
  574. );
  575. }
  576. void insert(iterator position, size_type count, const T &value)
  577. {
  578. command_queue queue = default_queue();
  579. insert(position, count, value, queue);
  580. queue.finish();
  581. }
  582. /// Inserts the values in the range [\p first, \p last) into the vector at
  583. /// \p position using \p queue.
  584. template<class InputIterator>
  585. void insert(iterator position,
  586. InputIterator first,
  587. InputIterator last,
  588. command_queue &queue)
  589. {
  590. ::boost::compute::vector<T, Alloc> tmp(position, end(), queue);
  591. size_type count = detail::iterator_range_size(first, last);
  592. resize(size() + count, queue);
  593. position = begin() + position.get_index();
  594. ::boost::compute::copy(first, last, position, queue);
  595. ::boost::compute::copy(
  596. tmp.begin(),
  597. tmp.end(),
  598. position + static_cast<difference_type>(count),
  599. queue
  600. );
  601. }
  602. /// \overload
  603. template<class InputIterator>
  604. void insert(iterator position, InputIterator first, InputIterator last)
  605. {
  606. command_queue queue = default_queue();
  607. insert(position, first, last, queue);
  608. queue.finish();
  609. }
  610. iterator erase(iterator position, command_queue &queue)
  611. {
  612. return erase(position, position + 1, queue);
  613. }
  614. iterator erase(iterator position)
  615. {
  616. command_queue queue = default_queue();
  617. iterator iter = erase(position, queue);
  618. queue.finish();
  619. return iter;
  620. }
  621. iterator erase(iterator first, iterator last, command_queue &queue)
  622. {
  623. if(last != end()){
  624. ::boost::compute::vector<T, Alloc> tmp(last, end(), queue);
  625. ::boost::compute::copy(tmp.begin(), tmp.end(), first, queue);
  626. }
  627. difference_type count = std::distance(first, last);
  628. resize(size() - static_cast<size_type>(count), queue);
  629. return begin() + first.get_index() + count;
  630. }
  631. iterator erase(iterator first, iterator last)
  632. {
  633. command_queue queue = default_queue();
  634. iterator iter = erase(first, last, queue);
  635. queue.finish();
  636. return iter;
  637. }
  638. /// Swaps the contents of \c *this with \p other.
  639. void swap(vector &other)
  640. {
  641. std::swap(m_data, other.m_data);
  642. std::swap(m_size, other.m_size);
  643. std::swap(m_allocator, other.m_allocator);
  644. }
  645. /// Removes all elements from the vector.
  646. void clear()
  647. {
  648. m_size = 0;
  649. }
  650. allocator_type get_allocator() const
  651. {
  652. return m_allocator;
  653. }
  654. /// Returns the underlying buffer.
  655. const buffer& get_buffer() const
  656. {
  657. return m_data.get_buffer();
  658. }
  659. /// \internal_
  660. ///
  661. /// Returns a command queue usable to issue commands for the vector's
  662. /// memory buffer. This is used when a member function is called without
  663. /// specifying an existing command queue to use.
  664. command_queue default_queue() const
  665. {
  666. const context &context = m_allocator.get_context();
  667. command_queue queue(context, context.get_device());
  668. return queue;
  669. }
  670. private:
  671. /// \internal_
  672. BOOST_CONSTEXPR size_type _minimum_capacity() const { return 4; }
  673. /// \internal_
  674. BOOST_CONSTEXPR float _growth_factor() const { return 1.5; }
  675. private:
  676. pointer m_data;
  677. size_type m_size;
  678. allocator_type m_allocator;
  679. };
  680. namespace detail {
  681. // set_kernel_arg specialization for vector<T>
  682. template<class T, class Alloc>
  683. struct set_kernel_arg<vector<T, Alloc> >
  684. {
  685. void operator()(kernel &kernel_, size_t index, const vector<T, Alloc> &vector)
  686. {
  687. kernel_.set_arg(index, vector.get_buffer());
  688. }
  689. };
  690. // for capturing vector<T> with BOOST_COMPUTE_CLOSURE()
  691. template<class T, class Alloc>
  692. struct capture_traits<vector<T, Alloc> >
  693. {
  694. static std::string type_name()
  695. {
  696. return std::string("__global ") + ::boost::compute::type_name<T>() + "*";
  697. }
  698. };
  699. // meta_kernel streaming operator for vector<T>
  700. template<class T, class Alloc>
  701. meta_kernel& operator<<(meta_kernel &k, const vector<T, Alloc> &vector)
  702. {
  703. return k << k.get_buffer_identifier<T>(vector.get_buffer());
  704. }
  705. } // end detail namespace
  706. } // end compute namespace
  707. } // end boost namespace
  708. #endif // BOOST_COMPUTE_CONTAINER_VECTOR_HPP