wait_list.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 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_UTILITY_WAIT_LIST_HPP
  11. #define BOOST_COMPUTE_UTILITY_WAIT_LIST_HPP
  12. #include <vector>
  13. #include <boost/compute/config.hpp>
  14. #ifndef BOOST_COMPUTE_NO_HDR_INITIALIZER_LIST
  15. #include <initializer_list>
  16. #endif
  17. #include <boost/compute/event.hpp>
  18. namespace boost {
  19. namespace compute {
  20. template<class T> class future;
  21. /// \class wait_list
  22. /// \brief Stores a list of events.
  23. ///
  24. /// The wait_list class stores a set of event objects and can be used to
  25. /// specify dependencies for OpenCL operations or to wait on the host until
  26. /// all of the events have completed.
  27. ///
  28. /// This class also provides convenience functions for interacting with
  29. /// OpenCL APIs which typically accept event dependencies as a \c cl_event*
  30. /// pointer and a \c cl_uint size. For example:
  31. /// \code
  32. /// wait_list events = ...;
  33. ///
  34. /// clEnqueueNDRangeKernel(..., events.get_event_ptr(), events.size(), ...);
  35. /// \endcode
  36. ///
  37. /// \see event, \ref future "future<T>"
  38. class wait_list
  39. {
  40. public:
  41. typedef std::vector<event>::iterator iterator;
  42. typedef std::vector<event>::const_iterator const_iterator;
  43. /// Creates an empty wait-list.
  44. wait_list()
  45. {
  46. }
  47. /// Creates a wait-list containing \p event.
  48. wait_list(const event &event)
  49. {
  50. insert(event);
  51. }
  52. /// Creates a new wait-list as a copy of \p other.
  53. wait_list(const wait_list &other)
  54. : m_events(other.m_events)
  55. {
  56. }
  57. #ifndef BOOST_COMPUTE_NO_HDR_INITIALIZER_LIST
  58. /// Creates a wait-list from \p events
  59. wait_list(std::initializer_list<event> events)
  60. : m_events(events)
  61. {
  62. }
  63. #endif // BOOST_COMPUTE_NO_HDR_INITIALIZER_LIST
  64. /// Copies the events in the wait-list from \p other.
  65. wait_list& operator=(const wait_list &other)
  66. {
  67. if(this != &other){
  68. m_events = other.m_events;
  69. }
  70. return *this;
  71. }
  72. #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
  73. /// Move-constructs a new wait list object from \p other.
  74. wait_list(wait_list&& other)
  75. : m_events(std::move(other.m_events))
  76. {
  77. }
  78. /// Move-assigns the wait list from \p other to \c *this.
  79. wait_list& operator=(wait_list&& other)
  80. {
  81. m_events = std::move(other.m_events);
  82. return *this;
  83. }
  84. #endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
  85. /// Destroys the wait-list.
  86. ~wait_list()
  87. {
  88. }
  89. /// Returns \c true if the wait-list is empty.
  90. bool empty() const
  91. {
  92. return m_events.empty();
  93. }
  94. /// Returns the number of events in the wait-list.
  95. uint_ size() const
  96. {
  97. return static_cast<uint_>(m_events.size());
  98. }
  99. /// Removes all of the events from the wait-list.
  100. void clear()
  101. {
  102. m_events.clear();
  103. }
  104. /// Returns a cl_event pointer to the first event in the wait-list.
  105. /// Returns \c 0 if the wait-list is empty.
  106. ///
  107. /// This can be used to pass the wait-list to OpenCL functions which
  108. /// expect a \c cl_event pointer to refer to a list of events.
  109. const cl_event* get_event_ptr() const
  110. {
  111. if(empty()){
  112. return 0;
  113. }
  114. return reinterpret_cast<const cl_event *>(&m_events[0]);
  115. }
  116. /// Reserves a minimum length of storage for the wait list object.
  117. void reserve(size_t new_capacity) {
  118. m_events.reserve(new_capacity);
  119. }
  120. /// Inserts \p event into the wait-list.
  121. void insert(const event &event)
  122. {
  123. m_events.push_back(event);
  124. }
  125. /// Inserts the event from \p future into the wait-list.
  126. template<class T>
  127. void insert(const future<T> &future)
  128. {
  129. insert(future.get_event());
  130. }
  131. /// Blocks until all of the events in the wait-list have completed.
  132. ///
  133. /// Does nothing if the wait-list is empty.
  134. void wait() const
  135. {
  136. if(!empty()){
  137. BOOST_COMPUTE_ASSERT_CL_SUCCESS(
  138. clWaitForEvents(size(), get_event_ptr())
  139. );
  140. }
  141. }
  142. /// Returns a reference to the event at specified location \p pos.
  143. const event& operator[](size_t pos) const {
  144. return m_events[pos];
  145. }
  146. /// Returns a reference to the event at specified location \p pos.
  147. event& operator[](size_t pos) {
  148. return m_events[pos];
  149. }
  150. /// Returns an iterator to the first element of the wait-list.
  151. iterator begin() {
  152. return m_events.begin();
  153. }
  154. /// Returns an iterator to the first element of the wait-list.
  155. const_iterator begin() const {
  156. return m_events.begin();
  157. }
  158. /// Returns an iterator to the first element of the wait-list.
  159. const_iterator cbegin() const {
  160. return m_events.begin();
  161. }
  162. /// Returns an iterator to the element following the last element of the wait-list.
  163. iterator end() {
  164. return m_events.end();
  165. }
  166. /// Returns an iterator to the element following the last element of the wait-list.
  167. const_iterator end() const {
  168. return m_events.end();
  169. }
  170. /// Returns an iterator to the element following the last element of the wait-list.
  171. const_iterator cend() const {
  172. return m_events.end();
  173. }
  174. private:
  175. std::vector<event> m_events;
  176. };
  177. } // end compute namespace
  178. } // end boost namespace
  179. #endif // BOOST_COMPUTE_UTILITY_WAIT_LIST_HPP