DASH  0.3.0
GlobPtr.h
1 #ifndef DASH__GLOB_PTR_H_
2 #define DASH__GLOB_PTR_H_
3 
4 #include <cstddef>
5 #include <iostream>
6 #include <sstream>
7 
8 #include <dash/dart/if/dart.h>
9 
10 #include <dash/Exception.h>
11 #include <dash/Init.h>
12 
13 #include <dash/atomic/Type_traits.h>
14 
15 #include <dash/internal/Logging.h>
16 #include <dash/iterator/internal/GlobPtrBase.h>
17 
18 #include <dash/memory/MemorySpaceBase.h>
19 
20 std::ostream &operator<<(std::ostream &os, const dart_gptr_t &dartptr);
21 
22 bool operator==(const dart_gptr_t &lhs, const dart_gptr_t &rhs);
23 
24 bool operator!=(const dart_gptr_t &lhs, const dart_gptr_t &rhs);
25 
26 namespace dash {
27 
28  // Forward-declarations
29 template <typename T>
30 class GlobRef;
31 
32 template <class T, class MemSpaceT>
33 class GlobPtr;
34 
35 template <class T, class MemSpaceT>
38 
46 template <typename ElementType, class GlobMemT>
47 class GlobPtr {
48 private:
50 
51  using local_pointer_traits =
52  std::pointer_traits<typename GlobMemT::local_void_pointer>;
53 
55 
56 public:
57  typedef ElementType value_type;
59  typedef typename GlobMemT::index_type index_type;
60  typedef typename GlobMemT::size_type size_type;
61  typedef index_type gptrdiff_t;
62 
63  typedef typename local_pointer_traits::template rebind<value_type>
64  local_type;
65 
66  typedef typename local_pointer_traits::template rebind<value_type const>
67  const_local_type;
68 
69  typedef GlobMemT memory_type;
70 
74  template <typename T>
76 
77 public:
78  template <typename T, class MemSpaceT>
79  friend class GlobPtr;
80 
81  template <typename T, class MemSpaceT>
82  friend std::ostream &operator<<(
83  std::ostream &os, const GlobPtr<T, MemSpaceT> &gptr);
84 
85  template <class T, class MemSpaceT>
88 
89 private:
90  // Raw global pointer used to initialize this pointer instance
91  dart_gptr_t m_dart_pointer = DART_GPTR_NULL;
92 public:
96  constexpr GlobPtr() = default;
97 
101  explicit constexpr GlobPtr(dart_gptr_t gptr) DASH_NOEXCEPT
102  : m_dart_pointer(gptr)
103  {
104  }
105 
109  explicit constexpr GlobPtr(std::nullptr_t) DASH_NOEXCEPT
110  : m_dart_pointer(DART_GPTR_NULL)
111  {
112  }
113 
117  constexpr GlobPtr(const self_t &other) = default;
118 
122  self_t &operator=(const self_t &rhs) = default;
123 
127  self_t &operator=(std::nullptr_t) {
128  m_dart_pointer = DART_GPTR_NULL;
129 
130  return *this;
131  }
132 
133  //clang-format off
144  //clang-format on
145  template <
146  typename From,
147  typename = typename std::enable_if<
148  // We always allow GlobPtr<T> -> GlobPtr<void> or the other way)
149  // or if From is assignable to To (value_type)
150  dash::internal::is_pointer_assignable<
151  typename dash::remove_atomic<From>::type,
152  typename dash::remove_atomic<value_type>::type>::value>
153 
154  ::type>
155  constexpr GlobPtr(const GlobPtr<From, GlobMemT> &other) DASH_NOEXCEPT
156  : m_dart_pointer(other.m_dart_pointer)
157  {
158  }
159 
163  constexpr GlobPtr(self_t &&other) DASH_NOEXCEPT = default;
164 
168  self_t &operator=(self_t &&rhs) DASH_NOEXCEPT = default;
169 
175  explicit operator value_type *() const DASH_NOEXCEPT
176  {
177  return local();
178  }
179 
187  explicit operator value_type *()
188  {
189  return local();
190  }
191 
195  explicit constexpr operator dart_gptr_t() const DASH_NOEXCEPT
196  {
197  return m_dart_pointer;
198  }
199 
203  constexpr dart_gptr_t dart_gptr() const DASH_NOEXCEPT
204  {
205  return m_dart_pointer;
206  }
207 
211  constexpr index_type operator-(const self_t &rhs) const DASH_NOEXCEPT
212  {
213  return dash::distance(rhs, *this);
214  }
215 
219  self_t &operator++() DASH_NOEXCEPT
220  {
221  increment(1);
222  return *this;
223  }
224 
228  self_t operator++(int) DASH_NOEXCEPT
229  {
230  self_t res(*this);
231  increment(1);
232  return res;
233  }
234 
238  self_t operator+(index_type n) const DASH_NOEXCEPT
239  {
240  self_t res(*this);
241  res += n;
242  return res;
243  }
244 
248  self_t &operator+=(index_type n) DASH_NOEXCEPT
249  {
250  if (n >= 0) {
251  increment(n);
252  }
253  else {
254  decrement(-n);
255  }
256  return *this;
257  }
258 
262  self_t &operator--() DASH_NOEXCEPT
263  {
264  decrement(1);
265  return *this;
266  }
267 
271  self_t operator--(int) DASH_NOEXCEPT
272  {
273  self_t res(*this);
274  decrement(1);
275  return res;
276  }
277 
281  self_t operator-(index_type n) const DASH_NOEXCEPT
282  {
283  self_t res(*this);
284  res -= n;
285  return res;
286  }
287 
291  self_t &operator-=(index_type n) DASH_NOEXCEPT
292  {
293  if (n >= 0) {
294  decrement(n);
295  }
296  else {
297  increment(-n);
298  }
299  return *this;
300  }
301 
305  // template <class GlobPtrT>
306  constexpr bool operator==(const GlobPtr &other) const DASH_NOEXCEPT
307  {
308  return DART_GPTR_EQUAL(
309  static_cast<dart_gptr_t>(m_dart_pointer),
310  static_cast<dart_gptr_t>(other.m_dart_pointer));
311  }
312 
316  constexpr bool operator==(std::nullptr_t) const DASH_NOEXCEPT
317  {
318  return DART_GPTR_ISNULL(static_cast<dart_gptr_t>(m_dart_pointer));
319  }
320 
324  template <class GlobPtrT>
325  constexpr bool operator!=(const GlobPtrT &other) const DASH_NOEXCEPT
326  {
327  return !(*this == other);
328  }
329 
333  template <class GlobPtrT>
334  constexpr bool operator<(const GlobPtrT &other) const DASH_NOEXCEPT
335  {
336  return (other - *this) > 0;
337  }
338 
342  template <class GlobPtrT>
343  constexpr bool operator<=(const GlobPtrT &other) const DASH_NOEXCEPT
344  {
345  return !(*this > other);
346  }
347 
351  template <class GlobPtrT>
352  constexpr bool operator>(const GlobPtrT &other) const DASH_NOEXCEPT
353  {
354  return (*this - other) > 0;
355  }
356 
360  template <class GlobPtrT>
361  constexpr bool operator>=(const GlobPtrT &other) const DASH_NOEXCEPT
362  {
363  return (*this - other) >= 0;
364  }
365 
369  constexpr GlobRef<const value_type> operator[](gptrdiff_t n) const DASH_NOEXCEPT
370  {
371  return GlobRef<const value_type>(self_t((*this) + n));
372  }
373 
377  GlobRef<value_type> operator[](gptrdiff_t n) DASH_NOEXCEPT
378  {
379  return GlobRef<value_type>(self_t((*this) + n));
380  }
381 
386  {
387  return GlobRef<value_type>(*this);
388  }
389 
393  constexpr GlobRef<const value_type> operator*() const DASH_NOEXCEPT
394  {
395  return GlobRef<const value_type>(*this);
396  }
397 
405  value_type *local()
406  {
407  void *addr = nullptr;
408  if (dart_gptr_getaddr(m_dart_pointer, &addr) == DART_OK) {
409  return static_cast<value_type *>(addr);
410  }
411  return nullptr;
412  }
413 
421  const value_type * local() const {
422  void *addr = nullptr;
423  if (dart_gptr_getaddr(m_dart_pointer, &addr) == DART_OK) {
424  return static_cast<const value_type *>(addr);
425  }
426  return nullptr;
427  }
428 
432  void set_unit(team_unit_t unit_id) DASH_NOEXCEPT
433  {
434  m_dart_pointer.unitid = unit_id.id;
435  }
436 
437  constexpr auto get_unit() const DASH_NOEXCEPT {
438  return team_unit_t{m_dart_pointer.unitid};
439  }
440 
445  bool is_local() const
446  {
447  return dash::internal::is_local(m_dart_pointer);
448  }
449 
450  constexpr explicit operator bool() const DASH_NOEXCEPT
451  {
452  return !DART_GPTR_ISNULL(m_dart_pointer);
453  }
454 
455 private:
456  void increment(size_type offs) DASH_NOEXCEPT
457  {
458  if (offs == 0) {
459  return;
460  }
462 
463  if (DART_GPTR_ISNULL(m_dart_pointer)) {
464  DASH_LOG_ERROR(
465  "GlobPtr.increment(offs)",
466  "cannot increment a global null pointer");
467  }
468 
469  auto& reg = dash::internal::MemorySpaceRegistry::GetInstance();
470  auto const * mem_space = static_cast<const GlobMemT *>(reg.lookup(m_dart_pointer));
471  // get a new dart with the requested offset
472  auto const newPtr = dash::internal::increment<value_type>(
473  m_dart_pointer,
474  offs,
475  mem_space,
477 
478  DASH_ASSERT(!DART_GPTR_ISNULL(newPtr));
479 
480  m_dart_pointer = newPtr;
481  }
482 
483  void decrement(size_type offs) DASH_NOEXCEPT
484  {
485  if (offs == 0) {
486  return;
487  }
488 
490 
491  if (DART_GPTR_ISNULL(m_dart_pointer)) {
492  DASH_LOG_ERROR(
493  "GlobPtr.increment(offs)",
494  "cannot decrement a global null pointer");
495  }
496 
497  auto & reg = dash::internal::MemorySpaceRegistry::GetInstance();
498  auto const *mem_space =
499  static_cast<const GlobMemT *>(reg.lookup(m_dart_pointer));
500 
501  // get a new dart with the requested offset
502  auto const newPtr = dash::internal::decrement<value_type>(
503  m_dart_pointer,
504  offs,
505  mem_space,
507 
508  DASH_ASSERT(!DART_GPTR_ISNULL(newPtr));
509 
510  m_dart_pointer = newPtr;
511  }
512 };
513 
514 template <typename T, class MemSpaceT>
515 std::ostream &operator<<(std::ostream &os, const GlobPtr<T, MemSpaceT> &gptr)
516 {
517  std::ostringstream ss;
518  char buf[100];
519  sprintf(
520  buf,
521  "u%06X|f%02X|s%04X|t%04X|o%016lX",
522  gptr.m_dart_pointer.unitid,
523  gptr.m_dart_pointer.flags,
524  gptr.m_dart_pointer.segid,
525  gptr.m_dart_pointer.teamid,
526  gptr.m_dart_pointer.addr_or_offs.offset);
527  ss << "dash::GlobPtr<" << typeid(T).name() << ">(" << buf << ")";
528  return operator<<(os, ss.str());
529 }
530 
546 template <class T, class MemSpaceT>
548  // First global pointer in range
549  GlobPtr<T, MemSpaceT> gbegin,
550  // Final global pointer in range
552 {
554 
555  auto const begin = static_cast<dart_gptr_t>(gbegin);
556  auto const end = static_cast<dart_gptr_t>(gbegin);
557 
558  DASH_ASSERT_EQ(begin.teamid, end.teamid, "teamid must be equal");
559  DASH_ASSERT_EQ(begin.segid, end.segid, "segid must be equal");
560 
561  auto & reg = dash::internal::MemorySpaceRegistry::GetInstance();
562  auto const *mem_space =
563  static_cast<const MemSpaceT *>(reg.lookup(begin));
564 
565  return dash::internal::distance<T>(
566  static_cast<dart_gptr_t>(gbegin),
567  static_cast<dart_gptr_t>(gend),
568  mem_space,
570 }
571 
586 template <class T, class MemSpaceT>
587 DASH_CONSTEXPR inline typename std::enable_if<
588  std::is_same<
589  typename dash::memory_space_traits<
590  MemSpaceT>::memory_space_layout_tag,
591  memory_space_contiguous>::value,
592  T>::type *
594 {
595  // reset offset to 0
596  auto dart_gptr = static_cast<dart_gptr_t>(global_begin);
597  dart_gptr.unitid = unit.id;
598  dart_gptr.addr_or_offs.offset = 0;
600 }
601 
602 } // namespace dash
603 
604 #endif // DASH__GLOB_PTR_H_
internal::default_signed_index gptrdiff_t
Difference type for global pointers.
Definition: Types.h:74
DASH_CONSTEXPR std::enable_if< std::is_same< typename dash::memory_space_traits< MemSpaceT >::memory_space_layout_tag, memory_space_contiguous >::value, T >::type * local_begin(GlobPtr< T, MemSpaceT > global_begin, dash::team_unit_t unit)
Returns the begin of the local memory portion within a global memory segment.
Definition: GlobPtr.h:593
self_t & operator=(const self_t &rhs)=default
Assignment operator.
self_t operator--(int) DASH_NOEXCEPT
Postfix decrement operator.
Definition: GlobPtr.h:271
value_type * local()
Conversion to local pointer.
Definition: GlobPtr.h:405
constexpr auto end(RangeType &&range) -> decltype(std::forward< RangeType >(range).end())
Definition: Range.h:98
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
const value_type * local() const
Conversion to local const pointer.
Definition: GlobPtr.h:421
self_t & operator=(std::nullptr_t)
nullptr assignment operator.
Definition: GlobPtr.h:127
constexpr bool operator==(const GlobPtr &other) const DASH_NOEXCEPT
Equality comparison operator.
Definition: GlobPtr.h:306
Signals success.
Definition: dart_types.h:33
self_t & operator-=(index_type n) DASH_NOEXCEPT
Pointer decrement operator.
Definition: GlobPtr.h:291
union dart_gptr_t::@0 addr_or_offs
Absolute address or relative offset.
constexpr GlobPtr()=default
Default constructor, underlying global address is unspecified.
constexpr bool operator<=(const GlobPtrT &other) const DASH_NOEXCEPT
Less-equal comparison operator.
Definition: GlobPtr.h:343
constexpr auto begin(RangeType &&range) -> decltype(std::forward< RangeType >(range).begin())
Definition: Range.h:89
constexpr bool operator>=(const GlobPtrT &other) const DASH_NOEXCEPT
Greater-equal comparison operator.
Definition: GlobPtr.h:361
constexpr GlobPtr(dart_gptr_t gptr) DASH_NOEXCEPT
Constructor, specifies underlying global address.
Definition: GlobPtr.h:101
self_t operator-(index_type n) const DASH_NOEXCEPT
Pointer decrement operator.
Definition: GlobPtr.h:281
constexpr GlobRef< const value_type > operator[](gptrdiff_t n) const DASH_NOEXCEPT
Subscript operator.
Definition: GlobPtr.h:369
constexpr GlobRef< const value_type > operator*() const DASH_NOEXCEPT
Dereference operator.
Definition: GlobPtr.h:393
self_t & operator--() DASH_NOEXCEPT
Prefix decrement operator.
Definition: GlobPtr.h:262
dart_unit_t unitid
The unit holding the memory element.
Definition: dart_globmem.h:83
constexpr bool operator!=(const GlobPtrT &other) const DASH_NOEXCEPT
Inequality comparison operator.
Definition: GlobPtr.h:325
GlobRef< value_type > operator[](gptrdiff_t n) DASH_NOEXCEPT
Subscript assignment operator.
Definition: GlobPtr.h:377
constexpr GlobPtr(const GlobPtr< From, GlobMemT > &other) DASH_NOEXCEPT
Implicit Converting Constructor, only allowed if one of the following conditions is satisfied: ...
Definition: GlobPtr.h:155
void set_unit(team_unit_t unit_id) DASH_NOEXCEPT
Set the global pointer&#39;s associated unit.
Definition: GlobPtr.h:432
#define DART_GPTR_NULL
A NULL global pointer.
Definition: dart_globmem.h:105
friend dash::gptrdiff_t distance(GlobPtr< T, MemSpaceT > gbegin, GlobPtr< T, MemSpaceT > gend)
Returns the number of hops from gbegin to gend.
Definition: GlobPtr.h:547
DART Global pointer type.
Definition: dart_globmem.h:77
constexpr index_type operator-(const self_t &rhs) const DASH_NOEXCEPT
Pointer offset difference operator.
Definition: GlobPtr.h:211
#define DART_GPTR_ISNULL(gptr_)
Test for NULL global pointer.
Definition: dart_globmem.h:118
constexpr bool operator<(const GlobPtrT &other) const DASH_NOEXCEPT
Less comparison operator.
Definition: GlobPtr.h:334
GlobRef< value_type > operator*() DASH_NOEXCEPT
Dereference operator.
Definition: GlobPtr.h:385
#define DART_GPTR_EQUAL(gptr1_, gptr2_)
Compare two global pointers.
Definition: dart_globmem.h:128
self_t operator++(int) DASH_NOEXCEPT
Postfix increment operator.
Definition: GlobPtr.h:228
constexpr dart_gptr_t dart_gptr() const DASH_NOEXCEPT
The pointer&#39;s underlying global address.
Definition: GlobPtr.h:203
dart_ret_t dart_gptr_getaddr(const dart_gptr_t gptr, void **addr)
Get the local memory address for the specified global pointer gptr.
struct dash::unit_id< dash::local_unit, dart_team_unit_t > team_unit_t
Unit ID to use for team-local IDs.
Definition: Types.h:319
constexpr bool operator==(std::nullptr_t) const DASH_NOEXCEPT
Equality comparison operator.
Definition: GlobPtr.h:316
self_t operator+(index_type n) const DASH_NOEXCEPT
Pointer increment operator.
Definition: GlobPtr.h:238
Pointer in global memory space with random access arithmetics.
Definition: GlobPtr.h:33
constexpr bool operator>(const GlobPtrT &other) const DASH_NOEXCEPT
Greater comparison operator.
Definition: GlobPtr.h:352
constexpr GlobPtr(std::nullptr_t) DASH_NOEXCEPT
Constructor for conversion of std::nullptr_t.
Definition: GlobPtr.h:109
self_t & operator++() DASH_NOEXCEPT
Prefix increment operator.
Definition: GlobPtr.h:219
typename details::memspace_traits_layout_tag< MemSpace >::type memory_space_layout_tag
May be contiguous or noncontiguous.
self_t & operator+=(index_type n) DASH_NOEXCEPT
Pointer increment operator.
Definition: GlobPtr.h:248
bool is_local() const
Check whether the global pointer is in the local address space the pointer&#39;s associated unit...
Definition: GlobPtr.h:445
dash::gptrdiff_t distance(GlobPtr< T, MemSpaceT > gbegin, GlobPtr< T, MemSpaceT > gend)
Returns the number of hops from gbegin to gend.
Definition: GlobPtr.h:547