1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 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_UTILITY_WAIT_LIST_HPP
12 #define BOOST_COMPUTE_UTILITY_WAIT_LIST_HPP
16 #include <boost/compute/config.hpp>
18 #ifndef BOOST_COMPUTE_NO_HDR_INITIALIZER_LIST
19 #include <initializer_list>
22 #include <boost/compute/event.hpp>
27 template<class T> class future;
30 /// \brief Stores a list of events.
32 /// The wait_list class stores a set of event objects and can be used to
33 /// specify dependencies for OpenCL operations or to wait on the host until
34 /// all of the events have completed.
36 /// This class also provides convenience functions for interacting with
37 /// OpenCL APIs which typically accept event dependencies as a \c cl_event*
38 /// pointer and a \c cl_uint size. For example:
40 /// wait_list events = ...;
42 /// clEnqueueNDRangeKernel(..., events.get_event_ptr(), events.size(), ...);
45 /// \see event, \ref future "future<T>"
49 typedef std::vector<event>::iterator iterator;
50 typedef std::vector<event>::const_iterator const_iterator;
52 /// Creates an empty wait-list.
57 /// Creates a wait-list containing \p event.
58 wait_list(const event &event)
63 /// Creates a new wait-list as a copy of \p other.
64 wait_list(const wait_list &other)
65 : m_events(other.m_events)
69 #ifndef BOOST_COMPUTE_NO_HDR_INITIALIZER_LIST
70 /// Creates a wait-list from \p events
71 wait_list(std::initializer_list<event> events)
75 #endif // BOOST_COMPUTE_NO_HDR_INITIALIZER_LIST
77 /// Copies the events in the wait-list from \p other.
78 wait_list& operator=(const wait_list &other)
81 m_events = other.m_events;
87 #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
88 /// Move-constructs a new wait list object from \p other.
89 wait_list(wait_list&& other)
90 : m_events(std::move(other.m_events))
94 /// Move-assigns the wait list from \p other to \c *this.
95 wait_list& operator=(wait_list&& other)
97 m_events = std::move(other.m_events);
101 #endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
103 /// Destroys the wait-list.
108 /// Returns \c true if the wait-list is empty.
111 return m_events.empty();
114 /// Returns the number of events in the wait-list.
117 return static_cast<uint_>(m_events.size());
120 /// Removes all of the events from the wait-list.
126 /// Returns a cl_event pointer to the first event in the wait-list.
127 /// Returns \c 0 if the wait-list is empty.
129 /// This can be used to pass the wait-list to OpenCL functions which
130 /// expect a \c cl_event pointer to refer to a list of events.
131 const cl_event* get_event_ptr() const
137 return reinterpret_cast<const cl_event *>(&m_events[0]);
140 /// Reserves a minimum length of storage for the wait list object.
141 void reserve(size_t new_capacity) {
142 m_events.reserve(new_capacity);
145 /// Inserts \p event into the wait-list.
146 void insert(const event &event)
148 m_events.push_back(event);
151 /// Inserts the event from \p future into the wait-list.
153 void insert(const future<T> &future)
155 insert(future.get_event());
158 /// Blocks until all of the events in the wait-list have completed.
160 /// Does nothing if the wait-list is empty.
164 BOOST_COMPUTE_ASSERT_CL_SUCCESS(
165 clWaitForEvents(size(), get_event_ptr())
170 /// Returns a reference to the event at specified location \p pos.
171 const event& operator[](size_t pos) const {
172 return m_events[pos];
175 /// Returns a reference to the event at specified location \p pos.
176 event& operator[](size_t pos) {
177 return m_events[pos];
180 /// Returns an iterator to the first element of the wait-list.
182 return m_events.begin();
185 /// Returns an iterator to the first element of the wait-list.
186 const_iterator begin() const {
187 return m_events.begin();
190 /// Returns an iterator to the first element of the wait-list.
191 const_iterator cbegin() const {
192 return m_events.begin();
195 /// Returns an iterator to the element following the last element of the wait-list.
197 return m_events.end();
200 /// Returns an iterator to the element following the last element of the wait-list.
201 const_iterator end() const {
202 return m_events.end();
205 /// Returns an iterator to the element following the last element of the wait-list.
206 const_iterator cend() const {
207 return m_events.end();
211 std::vector<event> m_events;
214 } // end compute namespace
215 } // end boost namespace
217 #endif // BOOST_COMPUTE_UTILITY_WAIT_LIST_HPP