Itoyori  v0.0.1
checkout_span.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "ityr/common/util.hpp"
4 #include "ityr/ori/ori.hpp"
6 
7 namespace ityr {
8 
9 namespace checkout_mode {
10 
13 
19 inline constexpr read_t read;
20 
23 
29 inline constexpr write_t write;
30 
33 
39 inline constexpr read_write_t read_write;
40 
42 struct no_access_t {};
43 
48 inline constexpr no_access_t no_access;
49 
50 }
51 
65 template <typename T, typename Mode>
67 public:
68  using element_type = T;
69  using value_type = std::remove_cv_t<element_type>;
70  using size_type = std::size_t;
72  using const_pointer = std::add_const_t<element_type>*;
73  using difference_type = typename std::iterator_traits<pointer>::difference_type;
74  using reference = typename std::iterator_traits<pointer>::reference;
75  using const_reference = typename std::iterator_traits<const_pointer>::reference;
76  using iterator = pointer;
78  using reverse_iterator = std::reverse_iterator<iterator>;
79  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
80 
82 
86  explicit checkout_span(ori::global_ptr<T> gptr, std::size_t n, Mode)
87  : ptr_((gptr && n > 0) ? ori::checkout(gptr, n, Mode{}) : nullptr),
88  n_(n) {
89  ITYR_CHECK(((ptr_ && n_ > 0) || (!ptr_ && n == 0)));
90  }
91 
95  ~checkout_span() { destroy(); }
96 
97  checkout_span(const checkout_span&) = delete;
99 
100  checkout_span(checkout_span&& cs) : ptr_(cs.ptr_), n_(cs.n_) { cs.ptr_ = nullptr; cs.n_ = 0; }
102  destroy();
103  ptr_ = cs.ptr_;
104  n_ = cs.n_;
105  cs.ptr_ = nullptr;
106  cs.n_ = 0;
107  return *this;
108  }
109 
110  constexpr pointer data() const noexcept { return ptr_; }
111  constexpr size_type size() const noexcept { return n_; }
112 
113  constexpr iterator begin() const noexcept { return ptr_; }
114  constexpr iterator end() const noexcept { return ptr_ + n_; }
115 
116  constexpr const_iterator cbegin() const noexcept { return ptr_; }
117  constexpr const_iterator cend() const noexcept { return ptr_ + n_; }
118 
119  constexpr reverse_iterator rbegin() const noexcept { return std::make_reverse_iterator(end()); }
120  constexpr reverse_iterator rend() const noexcept { return std::make_reverse_iterator(begin()); }
121 
122  constexpr const_reverse_iterator crbegin() const noexcept { return std::make_reverse_iterator(cend()); }
123  constexpr const_reverse_iterator crend() const noexcept { return std::make_reverse_iterator(begin()); }
124 
125  constexpr reference operator[](size_type i) const { assert(i <= n_); return ptr_[i]; }
126 
127  constexpr reference front() const { return *ptr_; }
128  constexpr reference back() const { return *(ptr_ + n_ - 1); }
129 
130  constexpr bool empty() const noexcept { return n_ == 0; }
131 
135  void checkout(ori::global_ptr<T> gptr, std::size_t n, Mode) {
136  checkin();
137  ptr_ = ori::checkout(gptr, n, Mode{});
138  n_ = n;
139  }
140 
144  void checkout_nb(ori::global_ptr<T> gptr, std::size_t n, Mode) {
145  checkin();
146  ptr_ = ori::checkout_nb(gptr, n, Mode{}).first;
147  n_ = n;
148  }
149 
153  void checkin() {
154  if (ptr_) {
155  ori::checkin(ptr_, n_, Mode{});
156  ptr_ = nullptr;
157  n_ = 0;
158  }
159  }
160 
161 private:
162  void destroy() {
163  if (ptr_) {
164  ori::checkin(ptr_, n_, Mode{});
165  }
166  }
167 
168  pointer ptr_ = nullptr;
169  size_type n_ = 0;
170 };
171 
172 template <typename T, typename Mode>
173 inline constexpr auto data(const checkout_span<T, Mode>& cs) noexcept {
174  return cs.data();
175 }
176 
177 template <typename T, typename Mode>
178 inline constexpr auto size(const checkout_span<T, Mode>& cs) noexcept {
179  return cs.size();
180 }
181 
182 template <typename T, typename Mode>
183 inline constexpr auto begin(const checkout_span<T, Mode>& cs) noexcept {
184  return cs.begin();
185 }
186 
187 template <typename T, typename Mode>
188 inline constexpr auto end(const checkout_span<T, Mode>& cs) noexcept {
189  return cs.end();
190 }
191 
237 template <typename T, typename Mode>
238 inline checkout_span<T, Mode> make_checkout(ori::global_ptr<T> gptr, std::size_t n, Mode mode) {
239  return checkout_span<T, Mode>{gptr, n, mode};
240 }
241 
252 template <typename T, typename Mode>
254  return checkout_span<T, Mode>{gspan.data(), gspan.size(), mode};
255 }
256 
257 namespace internal {
258 
259 inline auto make_checkouts_aux() {
260  return std::make_tuple();
261 }
262 
263 template <typename T, typename Mode, typename... Rest>
264 inline auto make_checkouts_aux(ori::global_ptr<T> gptr, std::size_t n, Mode mode, Rest&&... rest) {
265  checkout_span<T, Mode> cs;
266  cs.checkout_nb(gptr, n, mode);
267  return std::tuple_cat(std::make_tuple(std::move(cs)),
268  make_checkouts_aux(std::forward<Rest>(rest)...));
269 }
270 
271 template <typename T, typename Mode, typename... Rest>
272 inline auto make_checkouts_aux(global_span<T> gspan, Mode mode, Rest&&... rest) {
273  checkout_span<T, Mode> cs;
274  cs.checkout_nb(gspan.data(), gspan.size(), mode);
275  return std::tuple_cat(std::make_tuple(std::move(cs)),
276  make_checkouts_aux(std::forward<Rest>(rest)...));
277 }
278 
279 }
280 
310 template <typename... Args>
311 inline auto make_checkouts(Args&&... args) {
312  auto css = internal::make_checkouts_aux(std::forward<Args>(args)...);
314  return css;
315 }
316 
317 }
Checkout span to automatically manage the lifetime of checked-out memory.
Definition: checkout_span.hpp:66
constexpr reverse_iterator rbegin() const noexcept
Definition: checkout_span.hpp:119
constexpr const_reverse_iterator crend() const noexcept
Definition: checkout_span.hpp:123
std::size_t size_type
Definition: checkout_span.hpp:70
constexpr pointer data() const noexcept
Definition: checkout_span.hpp:110
const_pointer const_iterator
Definition: checkout_span.hpp:77
constexpr reference back() const
Definition: checkout_span.hpp:128
void checkout_nb(ori::global_ptr< T > gptr, std::size_t n, Mode)
Manually perform the nonblocking checkout operation by checking in the previous span.
Definition: checkout_span.hpp:144
std::add_const_t< element_type > * const_pointer
Definition: checkout_span.hpp:72
T element_type
Definition: checkout_span.hpp:68
constexpr reverse_iterator rend() const noexcept
Definition: checkout_span.hpp:120
~checkout_span()
Perform the checkin operation when destroyed.
Definition: checkout_span.hpp:95
constexpr reference operator[](size_type i) const
Definition: checkout_span.hpp:125
constexpr iterator begin() const noexcept
Definition: checkout_span.hpp:113
constexpr size_type size() const noexcept
Definition: checkout_span.hpp:111
checkout_span(ori::global_ptr< T > gptr, std::size_t n, Mode)
Construct a checkout span by checking out a global memory region.
Definition: checkout_span.hpp:86
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: checkout_span.hpp:79
pointer iterator
Definition: checkout_span.hpp:76
element_type * pointer
Definition: checkout_span.hpp:71
typename std::iterator_traits< pointer >::difference_type difference_type
Definition: checkout_span.hpp:73
void checkout(ori::global_ptr< T > gptr, std::size_t n, Mode)
Manually perform the checkout operation by checking in the previous span.
Definition: checkout_span.hpp:135
checkout_span & operator=(const checkout_span &)=delete
constexpr reference front() const
Definition: checkout_span.hpp:127
checkout_span()
Definition: checkout_span.hpp:81
std::reverse_iterator< iterator > reverse_iterator
Definition: checkout_span.hpp:78
checkout_span(checkout_span &&cs)
Definition: checkout_span.hpp:100
checkout_span & operator=(checkout_span &&cs)
Definition: checkout_span.hpp:101
constexpr bool empty() const noexcept
Definition: checkout_span.hpp:130
constexpr const_iterator cbegin() const noexcept
Definition: checkout_span.hpp:116
void checkin()
Manually perform the checkin operation by discarding the current checkout span.
Definition: checkout_span.hpp:153
std::remove_cv_t< element_type > value_type
Definition: checkout_span.hpp:69
checkout_span(const checkout_span &)=delete
typename std::iterator_traits< const_pointer >::reference const_reference
Definition: checkout_span.hpp:75
constexpr const_reverse_iterator crbegin() const noexcept
Definition: checkout_span.hpp:122
constexpr iterator end() const noexcept
Definition: checkout_span.hpp:114
constexpr const_iterator cend() const noexcept
Definition: checkout_span.hpp:117
typename std::iterator_traits< pointer >::reference reference
Definition: checkout_span.hpp:74
Global span to represent a view of a global memory range.
Definition: global_span.hpp:33
constexpr size_type size() const noexcept
Definition: global_span.hpp:74
constexpr pointer data() const noexcept
Definition: global_span.hpp:73
Definition: global_ptr.hpp:19
#define ITYR_CHECK(cond)
Definition: util.hpp:48
constexpr read_write_t read_write
Read+Write checkout mode.
Definition: checkout_span.hpp:39
constexpr read_t read
Read-only checkout mode.
Definition: checkout_span.hpp:19
ori::mode::read_t read_t
See ityr::checkout_mode::read.
Definition: checkout_span.hpp:12
ori::mode::write_t write_t
See ityr::checkout_mode::write.
Definition: checkout_span.hpp:22
constexpr no_access_t no_access
Checkout mode to disable automatic checkout.
Definition: checkout_span.hpp:48
constexpr write_t write
Write-only checkout mode.
Definition: checkout_span.hpp:29
ori::mode::read_write_t read_write_t
See ityr::checkout_mode::read_write.
Definition: checkout_span.hpp:32
ITYR_CONCAT(mode_, ITYR_PROFILER_MODE) mode
Definition: profiler.hpp:257
va_list args
Definition: util.hpp:76
std::pair< T *, bool > checkout_nb(global_ptr< T > ptr, std::size_t count, mode::read_t)
Definition: ori.hpp:108
void checkin(T *raw_ptr, std::size_t count, mode::read_t)
Definition: ori.hpp:168
auto checkout(global_ptr< T > ptr, std::size_t count, Mode mode)
Definition: ori.hpp:148
void checkout_complete()
Definition: ori.hpp:139
Definition: allocator.hpp:16
global_reverse_iterator< global_iterator< T, Mode > > make_reverse_iterator(ori::global_ptr< T > gptr, Mode mode)
Make a reverse iterator for global memory.
Definition: global_iterator.hpp:333
constexpr auto begin(const checkout_span< T, Mode > &cs) noexcept
Definition: checkout_span.hpp:183
constexpr auto data(const checkout_span< T, Mode > &cs) noexcept
Definition: checkout_span.hpp:173
auto make_checkouts(Args &&... args)
Checkout multiple global memory regions.
Definition: checkout_span.hpp:311
checkout_span< T, Mode > make_checkout(ori::global_ptr< T > gptr, std::size_t n, Mode mode)
Checkout a global memory region.
Definition: checkout_span.hpp:238
constexpr auto size(const checkout_span< T, Mode > &cs) noexcept
Definition: checkout_span.hpp:178
constexpr auto end(const checkout_span< T, Mode > &cs) noexcept
Definition: checkout_span.hpp:188
ForwardIteratorD move(const ExecutionPolicy &policy, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIteratorD first_d)
Move a range to another.
Definition: parallel_loop.hpp:934
See ityr::checkout_mode::no_access.
Definition: checkout_span.hpp:42
Definition: util.hpp:10
Definition: util.hpp:14
Definition: util.hpp:12