DASH  0.3.0
GlobAsyncRef.h
1 #ifndef DASH__GLOB_ASYNC_REF_H__
2 #define DASH__GLOB_ASYNC_REF_H__
3 
4 #include <iostream>
5 
6 #include <dash/GlobPtr.h>
7 #include <dash/Onesided.h>
8 
9 #include <dash/iterator/internal/GlobRefBase.h>
10 
11 
12 namespace dash {
13 
14 // Forward declarations
15 template <class T>
16 class GlobRef;
17 
42 template<typename T>
43 class GlobAsyncRef
44 {
45  template<typename U>
46  friend std::ostream & operator<<(
47  std::ostream & os,
48  const GlobAsyncRef<U> & gar);
49 
50  template <
51  typename ElementT >
52  friend class GlobAsyncRef;
53 
54  struct ReleaseHandle {
55  void operator()(dart_handle_t handle)
56  {
57  DASH_ASSERT_RETURNS(
58  dart_wait_local(&handle),
59  DART_OK
60  );
61  }
62  };
63 
64 public:
65  using value_type = T;
66  using const_value_type = typename std::add_const<T>::type;
67  using nonconst_value_type = typename std::remove_const<T>::type;
68  using self_t = GlobAsyncRef<T>;
69  using const_type = GlobAsyncRef<const_value_type>;
70  using nonconst_type = GlobAsyncRef<nonconst_value_type>;
71 
72 private:
74  dart_gptr_t _gptr{};
76  mutable nonconst_value_type _value;
78  mutable std::
79  unique_ptr<std::remove_pointer<dart_handle_t>::type, ReleaseHandle>
80  _handle{DART_HANDLE_NULL};
81 
82 private:
83 
88  template<typename ParentT>
89  GlobAsyncRef(
91  const GlobAsyncRef<ParentT> & parent,
93  size_t offset)
94  : _gptr(parent._gptr)
95  {
96  DASH_ASSERT_RETURNS(
97  dart_gptr_incaddr(&_gptr, offset),
98  DART_OK);
99  }
100 
105  template<class ElementT, class MemSpaceT>
106  explicit GlobAsyncRef(
108  GlobPtr<ElementT, MemSpaceT> & gptr)
109  : _gptr(gptr.dart_gptr())
110  { }
111 
112 public:
113 
118  explicit GlobAsyncRef(
121  : _gptr(dart_gptr)
122  { }
123 
132  template<typename _T,
133  int = internal::enable_implicit_copy_ctor<value_type, _T>::value>
135  : GlobAsyncRef(gref.dart_gptr())
136  { }
137 
144  template <
145  typename _T,
146  long = internal::enable_explicit_copy_ctor<value_type, _T>::value>
147  explicit GlobAsyncRef(const GlobAsyncRef<_T>& gref)
148  : GlobAsyncRef(gref.dart_gptr())
149  {
150  }
151 
152  template <
153  typename _T,
154  int = internal::enable_implicit_copy_ctor<value_type, _T>::value>
155  GlobAsyncRef(const GlobRef<_T>& gref)
156  : GlobAsyncRef(gref.dart_gptr())
157  {
158  }
159 
160  template <
161  typename _T,
162  long = internal::enable_explicit_copy_ctor<value_type, _T>::value>
163  explicit GlobAsyncRef(const GlobRef<_T>& gref)
164  : GlobAsyncRef(gref.dart_gptr())
165  {
166  }
167 
168  ~GlobAsyncRef() = default;
169 
170  GlobAsyncRef(self_t&& other) = default;
171 
175  self_t& operator=(self_t&& other) = default;
176 
180  self_t& operator=(const self_t& other) = delete;
181 
185  inline bool is_local() const noexcept
186  {
187  return dash::internal::is_local(this->_gptr);
188  }
189 
194  template<typename MEMTYPE>
196  member(size_t offs) const {
198  }
199 
203  template<class MEMTYPE, class P=T>
206  const MEMTYPE P::*mem) const {
207  auto offs = (size_t) & (reinterpret_cast<P*>(0)->*mem);
208  return member<typename internal::add_const_from_type<T, MEMTYPE>::type>(offs);
209  }
210 
214  friend void swap(self_t & a, self_t & b) {
215  static_assert(std::is_same<value_type, nonconst_value_type>::value,
216  "Cannot swap GlobAsyncRef<const T>!");
217  nonconst_value_type temp = a->get();
218  a = b->get();
219  b = temp;
220  }
221 
226  nonconst_value_type get() const {
227  nonconst_value_type value;
228  DASH_LOG_TRACE_VAR("GlobAsyncRef.T()", _gptr);
229  dash::internal::get_blocking(_gptr, &value, 1);
230  return value;
231  }
232 
239  void get(nonconst_value_type *tptr) const {
240  dash::internal::get(_gptr, tptr, 1);
241  }
242 
249  void get(nonconst_value_type& tref) const {
250  get(&tref);
251  }
252 
259  void set(const_value_type* tptr) const {
260  static_assert(std::is_same<value_type, nonconst_value_type>::value,
261  "Cannot modify value through GlobAsyncRef<const T>!");
262  DASH_LOG_TRACE_VAR("GlobAsyncRef.set()", *tptr);
263  DASH_LOG_TRACE_VAR("GlobAsyncRef.set()", _gptr);
264  dash::internal::put(_gptr, tptr, 1);
265  }
266 
273  void set(const_value_type& new_value) const {
274  static_assert(std::is_same<value_type, nonconst_value_type>::value,
275  "Cannot modify value through GlobAsyncRef<const T>!");
276  DASH_LOG_TRACE_VAR("GlobAsyncRef.set()", new_value);
277  DASH_LOG_TRACE_VAR("GlobAsyncRef.set()", _gptr);
278 
279  _value = new_value;
280 
281  dart_handle_t new_handle;
282  dash::internal::put_handle(_gptr, &_value, 1, &new_handle);
283 
284  _handle.reset(new_handle);
285  }
286 
292  const self_t &
293  operator=(const_value_type & new_value) const
294  {
295  set(new_value);
296  return *this;
297  }
298 
303  return this->_gptr;
304  }
305 
309  template <class ValueT>
310  bool operator==(const GlobAsyncRef<ValueT> & other) = delete;
311 
312  template <class ValueT>
313  bool operator!=(const GlobAsyncRef<ValueT> & other) = delete;
314 
318  void flush() const
319  {
320  DASH_ASSERT_RETURNS(
321  dart_flush(_gptr),
322  DART_OK
323  );
324  }
325 
326 }; // class GlobAsyncRef
327 
328 template<typename T>
329 std::ostream & operator<<(
330  std::ostream & os,
331  const GlobAsyncRef<T> & gar) {
332  os << "dash::GlobAsyncRef(" << gar._gptr << ")";
333  return os;
334 }
335 
336 } // namespace dash
337 
338 #endif // DASH__GLOB_ASYNC_REF_H__
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
Signals success.
Definition: dart_types.h:33
GlobAsyncRef(const GlobAsyncRef< _T > &gref)
Copy constructor, implicit if at least one of the following conditions is satisfied: 1) value_type an...
Definition: GlobAsyncRef.h:134
friend void swap(self_t &a, self_t &b)
Swap values with synchronous reads and asynchronous writes.
Definition: GlobAsyncRef.h:214
GlobAsyncRef< typename internal::add_const_from_type< T, MEMTYPE >::type > member(size_t offs) const
Get a global ref to a member of a certain type at the specified offset.
Definition: GlobAsyncRef.h:196
const self_t & operator=(const_value_type &new_value) const
Value assignment operator, calls non-blocking put.
Definition: GlobAsyncRef.h:293
void flush() const
Flush all pending asynchronous operations on this asynchronous reference.
Definition: GlobAsyncRef.h:318
GlobAsyncRef(dart_gptr_t dart_gptr)
Conctructor, creates an GlobRefAsync object referencing an element in global memory.
Definition: GlobAsyncRef.h:118
bool operator==(const GlobAsyncRef< ValueT > &other)=delete
Disallow implicit comparison with other global references.
dart_gptr_t dart_gptr() const
Returns the underlying DART global pointer.
Definition: GlobAsyncRef.h:302
DART Global pointer type.
Definition: dart_globmem.h:77
struct dart_handle_struct * dart_handle_t
Handle returned by dart_get_handle and the like used to wait for a specific operation to complete usi...
#define DART_HANDLE_NULL
Handle returned by dart_get_handle and the like used to wait for a specific operation to complete usi...
dart_ret_t dart_wait_local(dart_handle_t *handle)
Wait for the local completion of an operation.
Global value reference for asynchronous / non-blocking operations.
Definition: GlobAtomicRef.h:17
self_t & operator=(self_t &&other)=default
MOVE Assignment.
nonconst_value_type get() const
Return the value referenced by this GlobAsyncRef.
Definition: GlobAsyncRef.h:226
bool is_local() const noexcept
Whether the referenced element is located in local memory.
Definition: GlobAsyncRef.h:185
GlobAsyncRef< typename internal::add_const_from_type< T, MEMTYPE >::type > member(const MEMTYPE P::*mem) const
Get the member via pointer to member.
Definition: GlobAsyncRef.h:205
dart_ret_t dart_flush(dart_gptr_t gptr)
Guarantee completion of all outstanding operations involving a segment on a certain unit...