DPC++ Runtime
Runtime libraries for oneAPI DPC++
leaves_collection.hpp
Go to the documentation of this file.
1 //==---- leaves_collection.hpp - Container for leaves of execution graph ---==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
13 
14 #include <cstddef>
15 #include <list>
16 #include <unordered_map>
17 #include <utility>
18 
20 namespace sycl {
21 namespace detail {
22 
23 struct MemObjRecord;
24 
39 public:
41  using HostAccessorCommandsT = std::list<EmptyCommand *>;
42  using EnqueueListT = std::vector<Command *>;
43 
44  // Make first command depend on the second
45  using AllocateDependencyF =
46  std::function<void(Command *, Command *, MemObjRecord *, EnqueueListT &)>;
47 
48  template <bool IsConst> class IteratorT;
49 
50  using value_type = Command *;
51  using pointer = value_type *;
52  using const_pointer = const value_type *;
53  using reference = value_type &;
54  using const_reference = const value_type &;
55 
58 
59  LeavesCollection(MemObjRecord *Record, std::size_t GenericCommandsCapacity,
60  AllocateDependencyF AllocateDependency)
61  : MRecord{Record}, MGenericCommands{GenericCommandsCapacity},
62  MAllocateDependency{std::move(AllocateDependency)} {}
63 
65  if (MGenericCommands.empty())
66  return iterator{*this, beginHostAccessor()};
67 
68  return iterator{*this, MGenericCommands.begin()};
69  }
70 
71  iterator end() { return iterator{*this, endHostAccessor()}; }
72 
74  if (MGenericCommands.empty())
75  return const_iterator{*this, beginHostAccessor()};
76 
77  return const_iterator{*this, MGenericCommands.begin()};
78  }
79 
80  const_iterator cend() const {
81  return const_iterator{*this, endHostAccessor()};
82  }
83 
85  bool push_back(value_type Cmd, EnqueueListT &ToEnqueue);
86 
90  size_t remove(value_type Cmd);
91 
92  std::vector<value_type> toVector() const;
93 
94  size_t genericCommandsCapacity() const {
95  return MGenericCommands.capacity();
96  };
97 
99  return MGenericCommands;
100  }
101 
103  return MHostAccessorCommands;
104  }
105 
106 private:
107  template <bool IsConst, typename T> struct Iterator;
108 
109  template <typename T> struct Iterator<true, T> {
110  using type = typename T::const_iterator;
111  };
112 
113  template <typename T> struct Iterator<false, T> {
114  using type = typename T::iterator;
115  };
116 
117  using HostAccessorCommandSingleXRefT =
118  typename HostAccessorCommandsT::iterator;
119  using HostAccessorCommandsXRefT =
120  std::unordered_map<EmptyCommand *, HostAccessorCommandSingleXRefT>;
121 
122  MemObjRecord *MRecord;
123  GenericCommandsT MGenericCommands;
124  HostAccessorCommandsT MHostAccessorCommands;
125  HostAccessorCommandsXRefT MHostAccessorCommandsXRef;
126 
127  AllocateDependencyF MAllocateDependency;
128 
129  bool addGenericCommand(value_type Cmd, EnqueueListT &ToEnqueue);
130  bool addHostAccessorCommand(EmptyCommand *Cmd, EnqueueListT &ToEnqueue);
131 
132  // inserts a command to the end of list for its mem object
133  void insertHostAccessorCommand(EmptyCommand *Cmd);
134  // returns number of removed elements
135  size_t eraseHostAccessorCommand(EmptyCommand *Cmd);
136 
137  typename Iterator<false, HostAccessorCommandsT>::type beginHostAccessor() {
138  return MHostAccessorCommands.begin();
139  }
140 
141  typename Iterator<true, HostAccessorCommandsT>::type
142  beginHostAccessor() const {
143  return MHostAccessorCommands.begin();
144  }
145 
146  typename Iterator<false, HostAccessorCommandsT>::type endHostAccessor() {
147  return MHostAccessorCommands.end();
148  }
149 
150  typename Iterator<true, HostAccessorCommandsT>::type endHostAccessor() const {
151  return MHostAccessorCommands.end();
152  }
153 
154  // for access to struct Ref.
155  friend class IteratorT<true>;
156  friend class IteratorT<false>;
157 
158  template <bool IsConst, typename T> struct Ref;
159  template <typename T> struct Ref<true, T> { using type = const T &; };
160  template <typename T> struct Ref<false, T> { using type = T &; };
161 
162  template <bool IsConst, typename T> struct Ptr;
163  template <typename T> struct Ptr<true, T> { using type = const T *; };
164  template <typename T> struct Ptr<false, T> { using type = T *; };
165 
166 public:
167  // iterate over generic commands in the first place and over host accessors
168  // later on
169  template <bool IsConst> class IteratorT {
170  public:
171  using HostT = typename Ref<IsConst, LeavesCollection>::type;
172  using GCItT = typename Iterator<IsConst, GenericCommandsT>::type;
173  using HACItT = typename Iterator<IsConst, HostAccessorCommandsT>::type;
174 
175  private:
176  HostT MHost;
177  GCItT MGCIt;
178  HACItT MHACIt;
179 
180  bool MGenericIsActive;
181 
182  IteratorT(HostT Host, GCItT GCIt, HACItT HACIt, bool GenericIsActive)
183  : MHost(Host), MGCIt(GCIt), MHACIt(HACIt),
184  MGenericIsActive(GenericIsActive) {}
185 
186  public:
188  : IteratorT(Host, std::move(GCIt), Host.beginHostAccessor(), true) {}
189 
191  : IteratorT(Host, Host.MGenericCommands.end(), std::move(HACIt),
192  false) {}
193 
195  : MHost{Other.MHost}, MGCIt(Other.MGCIt), MHACIt(Other.MHACIt),
196  MGenericIsActive(Other.MGenericIsActive) {}
197 
199  : MHost{Other.MHost}, MGCIt(std::move(Other.MGCIt)),
200  MHACIt(std::move(Other.MHACIt)),
201  MGenericIsActive(Other.MGenericIsActive) {}
202 
203  public:
204  bool operator==(const IteratorT<IsConst> &Rhs) const {
205  return &MHost == &Rhs.MHost && MGenericIsActive == Rhs.MGenericIsActive &&
206  ((MGenericIsActive && MGCIt == Rhs.MGCIt) ||
207  (!MGenericIsActive && MHACIt == Rhs.MHACIt));
208  }
209 
210  bool operator!=(const IteratorT<IsConst> &Rhs) const {
211  return &MHost != &Rhs.MHost || MGenericIsActive != Rhs.MGenericIsActive ||
212  ((MGenericIsActive && MGCIt != Rhs.MGCIt) ||
213  (!MGenericIsActive && MHACIt != Rhs.MHACIt));
214  }
215 
216  // pre-increment
218  increment();
219  return *this;
220  }
221 
222  // post-increment
224  IteratorT<IsConst> Other(*this);
225  increment();
226 
227  return Other;
228  }
229 
231  if (MGenericIsActive && MGCIt != MHost.MGenericCommands.end())
232  return *MGCIt;
233 
234  if (!MGenericIsActive && MHACIt != MHost.endHostAccessor())
235  return *MHACIt;
236 
237  assert(false);
238 
239  return nullptr;
240  }
241 
242  private:
243  void increment() {
244  if (MGenericIsActive) {
245  ++MGCIt;
246 
247  if (MGCIt == MHost.MGenericCommands.end()) {
248  MGenericIsActive = false;
249  MHACIt = MHost.MHostAccessorCommands.begin();
250  return;
251  }
252 
253  return;
254  }
255 
256  assert(MGCIt == MHost.MGenericCommands.end());
257 
258  if (MHACIt == MHost.endHostAccessor())
259  return;
260 
261  ++MHACIt;
262  }
263  };
264 };
265 
266 } // namespace detail
267 } // namespace sycl
268 } // __SYCL_INLINE_NAMESPACE(cl)
cl::sycl::detail::LeavesCollection::begin
iterator begin()
Definition: leaves_collection.hpp:64
cl::sycl::detail::LeavesCollection::IteratorT::GCItT
typename Iterator< IsConst, GenericCommandsT >::type GCItT
Definition: leaves_collection.hpp:172
cl::sycl::detail::MemObjRecord
Memory Object Record.
Definition: scheduler.hpp:193
cl::sycl::detail::LeavesCollection::IteratorT
Definition: leaves_collection.hpp:48
sycl
Definition: invoke_simd.hpp:68
cl::sycl::detail::LeavesCollection::getHostAccessorCommands
const HostAccessorCommandsT getHostAccessorCommands() const
Definition: leaves_collection.hpp:102
cl::sycl::detail::LeavesCollection::IteratorT::IteratorT
IteratorT(const IteratorT< IsConst > &Other)
Definition: leaves_collection.hpp:194
cl::sycl::detail::LeavesCollection::IteratorT::operator==
bool operator==(const IteratorT< IsConst > &Rhs) const
Definition: leaves_collection.hpp:204
cl::sycl::detail::LeavesCollection::EnqueueListT
std::vector< Command * > EnqueueListT
Definition: leaves_collection.hpp:42
cl::sycl::detail::LeavesCollection::AllocateDependencyF
std::function< void(Command *, Command *, MemObjRecord *, EnqueueListT &)> AllocateDependencyF
Definition: leaves_collection.hpp:46
cl::sycl::detail::LeavesCollection::IteratorT::operator++
IteratorT< IsConst > & operator++()
Definition: leaves_collection.hpp:217
cl::sycl::detail::LeavesCollection::end
iterator end()
Definition: leaves_collection.hpp:71
commands.hpp
circular_buffer.hpp
cl::sycl::detail::LeavesCollection::HostAccessorCommandsT
std::list< EmptyCommand * > HostAccessorCommandsT
Definition: leaves_collection.hpp:41
cl::sycl::detail::Command
The Command class represents some action that needs to be performed on one or more memory objects.
Definition: commands.hpp:95
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::detail::LeavesCollection::IteratorT::IteratorT
IteratorT(HostT Host, HACItT HACIt)
Definition: leaves_collection.hpp:190
Host
@ Host
Definition: usm_allocator.hpp:14
cl::sycl::detail::LeavesCollection::IteratorT::HostT
typename Ref< IsConst, LeavesCollection >::type HostT
Definition: leaves_collection.hpp:171
cl::sycl::detail::LeavesCollection::IteratorT::operator++
IteratorT< IsConst > operator++(int)
Definition: leaves_collection.hpp:223
cl::sycl::detail::LeavesCollection::IteratorT::IteratorT
IteratorT(IteratorT< IsConst > &&Other)
Definition: leaves_collection.hpp:198
cl::sycl::detail::LeavesCollection::IteratorT::HACItT
typename Iterator< IsConst, HostAccessorCommandsT >::type HACItT
Definition: leaves_collection.hpp:173
cl::sycl::detail::LeavesCollection::cbegin
const_iterator cbegin() const
Definition: leaves_collection.hpp:73
std
Definition: accessor.hpp:2617
cl::sycl::detail::LeavesCollection::IteratorT::IteratorT
IteratorT(HostT Host, GCItT GCIt)
Definition: leaves_collection.hpp:187
cl::sycl::detail::LeavesCollection::IteratorT::operator*
value_type operator*() const
Definition: leaves_collection.hpp:230
cl::sycl::detail::LeavesCollection
A wrapper for CircularBuffer class along with collection for host accessor's EmptyCommands.
Definition: leaves_collection.hpp:38
cl::sycl::detail::LeavesCollection::cend
const_iterator cend() const
Definition: leaves_collection.hpp:80
cl::sycl::detail::LeavesCollection::getGenericCommands
const GenericCommandsT & getGenericCommands() const
Definition: leaves_collection.hpp:98
cl::sycl::detail::LeavesCollection::LeavesCollection
LeavesCollection(MemObjRecord *Record, std::size_t GenericCommandsCapacity, AllocateDependencyF AllocateDependency)
Definition: leaves_collection.hpp:59
cl::sycl::detail::CircularBuffer< Command * >
cl::sycl::detail::LeavesCollection::genericCommandsCapacity
size_t genericCommandsCapacity() const
Definition: leaves_collection.hpp:94
cl::sycl::detail::LeavesCollection::IteratorT::operator!=
bool operator!=(const IteratorT< IsConst > &Rhs) const
Definition: leaves_collection.hpp:210
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12