DASH  0.3.0
GlobIter.h
1 #ifndef DASH__GLOB_ITER_H__INCLUDED
2 #define DASH__GLOB_ITER_H__INCLUDED
3 
4 #include <dash/GlobPtr.h>
5 #include <dash/GlobRef.h>
6 #include <dash/Pattern.h>
7 
8 #include <functional>
9 #include <sstream>
10 
11 namespace dash {
12 
37 template <
38  typename ElementType,
39  class PatternType,
40  class GlobMemType,
41  // TODO rko: use pointer traits here
42  class PointerType =
43  typename GlobMemType::void_pointer::template rebind<ElementType>,
44  class ReferenceType = GlobRef<ElementType> >
45 class GlobIter {
46  public:
48  using iterator_category = std::random_access_iterator_tag;
49  using value_type = ElementType;
50  using difference_type = typename PatternType::index_type;
51  using size_type = typename PatternType::size_type;
52  using pointer = PointerType;
53  using reference = ReferenceType;
54 
55  typedef typename ReferenceType::const_type const_reference;
56 
57  typedef typename PointerType::const_type const_pointer;
58 
59  typedef typename pointer::local_type local_type;
60 
61  typedef typename pointer::const_local_type const_local_type;
62 
63  typedef PatternType pattern_type;
64  typedef typename std::make_signed<typename PatternType::index_type>::type index_type;
65 
66  private:
67  typedef GlobIter<
68  ElementType,
69  PatternType,
70  GlobMemType,
71  PointerType,
72  ReferenceType>
73  self_t;
74 
75  typedef GlobIter<
76  const ElementType,
77  PatternType,
78  GlobMemType,
79  const_pointer,
80  const_reference>
82 
83  typedef typename std::remove_const<ElementType>::type nonconst_value_type;
84 
85  public:
86  typedef std::integral_constant<bool, false> has_view;
87 
88  public:
89  // For ostream output
90  template <typename T_, class P_, class GM_, class Ptr_, class Ref_>
91  friend std::ostream& operator<<(
92  std::ostream& os, const GlobIter<T_, P_, GM_, Ptr_, Ref_>& it);
93 
94  // For comparison operators
95  template <typename T_, class P_, class GM_, class Ptr_, class Ref_>
96  friend class GlobIter;
97 
98  private:
99  static const dim_t NumDimensions = PatternType::ndim();
100  static const MemArrange Arrangement = PatternType::memory_order();
101 
102  protected:
104  GlobMemType* _globmem = nullptr;
106  PatternType const* _pattern = nullptr;
108  index_type _idx = 0;
110  index_type _max_idx = 0;
111 
112  public:
113  DASH_CONSTEXPR GlobIter() = default;
114 
119  DASH_CONSTEXPR GlobIter(
120  GlobMemType* gmem,
121  PatternType const& pat,
122  index_type position = 0) DASH_NOEXCEPT
123  : _globmem(gmem),
124  _pattern(&pat),
125  _idx(position),
126  _max_idx(index_type(pat.size()) - 1)
127  {
128  }
129 
130  template <
131  class ElementType_,
132  class PointerType_,
133  class ReferenceType_,
134  typename = typename std::enable_if<
135  // Converstion works only if ElementType_ & is assignable to
136  // value_type &
137  std::is_assignable<
138  typename dash::remove_atomic<ElementType_>::type,
139  typename dash::remove_atomic<value_type>::type>::value>
140 
141  ::type>
142  DASH_CONSTEXPR GlobIter(const GlobIter<
143  ElementType_,
144  PatternType,
145  GlobMemType,
146  PointerType_,
147  ReferenceType_>& other) DASH_NOEXCEPT
148  : _globmem(other._globmem),
149  _pattern(other._pattern),
150  _idx(other._idx),
151  _max_idx(other._max_idx)
152  {
153  }
154 
158  static constexpr dim_t ndim()
159  {
160  return NumDimensions;
161  }
162 
170  DASH_CONSTEXPR explicit operator const_pointer() const noexcept
171  {
172  return const_pointer(this->dart_gptr());
173  }
174 
182  DASH_CONSTEXPR explicit operator pointer() DASH_NOEXCEPT
183  {
184  return pointer(this->dart_gptr());
185  }
186 
193  DASH_CONSTEXPR dart_gptr_t dart_gptr() const DASH_NOEXCEPT
194  {
195  if (_globmem == nullptr) {
196  return DART_GPTR_NULL;
197  }
198  else if (_idx > _max_idx) {
199  // Global iterator pointing past the range indexed by the pattern
200  // which is the case for .end() iterators.
201  return static_cast<dart_gptr_t>(_globmem->end());
202  }
203 
204  DASH_LOG_TRACE_VAR("GlobIter.dart_gptr()", _idx);
205  typedef typename pattern_type::local_index_t local_pos_t;
206 
207  // Global index to local index and unit:
208  local_pos_t local_pos = _pattern->local(_idx);
209  DASH_LOG_TRACE(
210  "GlobIter.dart_gptr",
211  "unit:",
212  local_pos.unit,
213  "local index:",
214  local_pos.index);
215  auto const dart_pointer = _get_pointer_at(local_pos);
216  DASH_ASSERT_MSG(
217  !DART_GPTR_ISNULL(dart_pointer), "dart pointer must not be null");
218  return dart_pointer;
219  }
220 
226  DASH_CONSTEXPR reference operator*() noexcept
227  {
228  return reference{this->dart_gptr()};
229  }
230 
236  DASH_CONSTEXPR const_reference operator*() const noexcept
237  {
238  return const_reference{this->dart_gptr()};
239  }
240 
245  DASH_CONSTEXPR reference operator[](
247  index_type g_index) noexcept
248  {
249  auto p = *this;
250  p += g_index;
251  return reference(p.dart_gptr());
252  }
253 
258  DASH_CONSTEXPR const_reference operator[](
260  index_type g_index) const noexcept
261  {
262  auto p = *this;
263  p += g_index;
264  return const_reference(p.dart_gptr());
265  }
266 
271  DASH_CONSTEXPR bool is_local() const noexcept
272  {
273  return (_globmem->team().myid() == lpos().unit);
274  }
275 
279  DASH_CONSTEXPR local_type local() const
280  {
281  auto local_pos = lpos();
282 
283  if (local_pos.unit != _pattern->team().myid()) {
284  return nullptr;
285  }
286 
287  auto* lbegin = dash::local_begin(
288  static_cast<pointer>(_globmem->begin()), _pattern->team().myid());
289  DASH_ASSERT(lbegin);
290 
291  return std::next(lbegin, local_pos.index);
292  }
293 
297  DASH_CONSTEXPR auto lpos() const
298  {
299  DASH_LOG_TRACE_VAR("GlobIter.lpos()", _idx);
300 
301  index_type idx = _idx;
302  index_type offset = 0;
303 
304  // Convert iterator position (_idx) to local index and unit.
305  if (idx > _max_idx) {
306  idx = _max_idx;
307  offset = _idx - _max_idx;
308  DASH_ASSERT_EQ(offset, 1, "invalid index");
309  }
310  // Global index to local index and unit:
311  auto local_pos = _pattern->local(idx);
312  // Add the offset
313  local_pos.index += offset;
314 
315  DASH_LOG_TRACE(
316  "GlobIter.lpos >",
317  "unit:",
318  local_pos.unit,
319  "local index:",
320  local_pos.index);
321 
322  return local_pos;
323  }
324 
328  DASH_CONSTEXPR const self_t& global() const DASH_NOEXCEPT
329  {
330  return *this;
331  }
332 
336  DASH_CONSTEXPR index_type pos() const DASH_NOEXCEPT
337  {
338  return _idx;
339  }
340 
344  DASH_CONSTEXPR index_type gpos() const DASH_NOEXCEPT
345  {
346  return _idx;
347  }
348 
353  DASH_CONSTEXPR const GlobMemType& globmem() const DASH_NOEXCEPT
354  {
355  return *_globmem;
356  }
357 
362  inline GlobMemType& globmem()
363  {
364  return *_globmem;
365  }
366 
370  DASH_CONSTEXPR self_t& operator++() DASH_NOEXCEPT
371  {
372  ++_idx;
373  return *this;
374  }
375 
379  DASH_CONSTEXPR self_t operator++(int) DASH_NOEXCEPT
380  {
381  self_t result = *this;
382  ++_idx;
383  return result;
384  }
385 
389  DASH_CONSTEXPR self_t& operator--() DASH_NOEXCEPT
390  {
391  --_idx;
392  return *this;
393  }
394 
398  DASH_CONSTEXPR self_t operator--(int) DASH_NOEXCEPT
399  {
400  self_t result = *this;
401  --_idx;
402  return result;
403  }
404 
405  DASH_CONSTEXPR self_t& operator+=(index_type n) DASH_NOEXCEPT
406  {
407  _idx += n;
408  return *this;
409  }
410 
411  DASH_CONSTEXPR self_t& operator-=(index_type n) DASH_NOEXCEPT
412  {
413  _idx -= n;
414  return *this;
415  }
416 
417  DASH_CONSTEXPR self_t operator+(index_type n) const DASH_NOEXCEPT
418  {
419  return self_t(_globmem, *_pattern, _idx + static_cast<index_type>(n));
420  }
421 
422  DASH_CONSTEXPR self_t operator-(index_type n) const DASH_NOEXCEPT
423  {
424  return self_t(_globmem, *_pattern, _idx - static_cast<index_type>(n));
425  }
426 
427  template <class GlobIterT>
428  DASH_CONSTEXPR bool operator<(const GlobIterT& other) const DASH_NOEXCEPT
429  {
430  return (_idx < other._idx);
431  }
432 
433  template <class GlobIterT>
434  DASH_CONSTEXPR bool operator<=(const GlobIterT& other) const DASH_NOEXCEPT
435  {
436  return (_idx <= other._idx);
437  }
438 
439  template <class GlobIterT>
440  DASH_CONSTEXPR bool operator>(const GlobIterT& other) const DASH_NOEXCEPT
441  {
442  return (_idx > other._idx);
443  }
444 
445  template <class GlobIterT>
446  DASH_CONSTEXPR bool operator>=(const GlobIterT& other) const DASH_NOEXCEPT
447  {
448  return (_idx >= other._idx);
449  }
450 
451  template <class GlobIterT>
452  DASH_CONSTEXPR bool operator==(const GlobIterT& other) const DASH_NOEXCEPT
453  {
454  return _idx == other._idx;
455  }
456 
457  template <class GlobIterT>
458  DASH_CONSTEXPR bool operator!=(const GlobIterT& other) const DASH_NOEXCEPT
459  {
460  return _idx != other._idx;
461  }
462 
463  DASH_CONSTEXPR const PatternType& pattern() const DASH_NOEXCEPT
464  {
465  return *_pattern;
466  }
467 
468  DASH_CONSTEXPR dash::Team& team() const DASH_NOEXCEPT
469  {
470  return _pattern->team();
471  }
472 
473  private:
474  DASH_CONSTEXPR dart_gptr_t
475  _get_pointer_at(typename pattern_type::local_index_t pos) const
476  {
477  auto dart_pointer = static_cast<dart_gptr_t>(_globmem->begin());
478 
479  DASH_ASSERT(pos.index >= 0);
480 
481  dart_pointer.unitid = pos.unit;
482 
483  dart_pointer.addr_or_offs.offset += pos.index * sizeof(value_type);
484 
485  return dart_pointer;
486  }
487 
488 }; // class GlobIter
489 
490 template <
491  typename ElementType,
492  class Pattern,
493  class GlobStaticMem,
494  class Pointer,
495  class Reference>
496 std::ostream& operator<<(
497  std::ostream& os,
498  const dash::
500 {
501  std::ostringstream ss;
503  ss << "dash::GlobIter<" << typeid(ElementType).name() << ">("
504  << "idx:" << it._idx << ", "
505  << "gptr:" << ptr << ")";
506  return operator<<(os, ss.str());
507 }
508 } // namespace dash
509 
510 #endif // DASH__GLOB_ITER_H__INCLUDED
DASH_CONSTEXPR self_t & operator++() DASH_NOEXCEPT
Prefix increment operator.
Definition: GlobIter.h:370
DASH_CONSTEXPR dart_gptr_t dart_gptr() const DASH_NOEXCEPT
Explicit conversion to dart_gptr_t.
Definition: GlobIter.h:193
static constexpr dim_t ndim()
The number of dimensions of the iterator&#39;s underlying pattern.
Definition: GlobIter.h:158
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
DASH_CONSTEXPR const_reference operator[](index_type g_index) const noexcept
Subscript operator, returns global reference to element at given global index.
Definition: GlobIter.h:258
Defines how a list of global indices is mapped to single units within a Team.
Definition: BlockPattern.h:42
size_t size()
Return the number of units in the global team.
DASH_CONSTEXPR reference operator[](index_type g_index) noexcept
Subscript operator, returns global reference to element at given global index.
Definition: GlobIter.h:245
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
std::random_access_iterator_tag iterator_category
Iterator Traits.
Definition: GlobIter.h:48
dash::BlockPattern< NumDimensions, Arrangement, IndexType > Pattern
Template alias for dash::Pattern with the same default template arguments.
Definition: Pattern.h:644
int dim_t
Scalar type for a dimension value, with 0 indicating the first dimension.
Definition: Types.h:39
DASH_CONSTEXPR local_type local() const
Convert global iterator to native pointer.
Definition: GlobIter.h:279
DASH_CONSTEXPR const_reference operator*() const noexcept
Dereference operator.
Definition: GlobIter.h:236
Global memory with address space of static size.
Definition: GlobStaticMem.h:88
DASH_CONSTEXPR index_type gpos() const DASH_NOEXCEPT
Position of the iterator in global index range.
Definition: GlobIter.h:344
GlobMemType & globmem()
The instance of GlobStaticMem used by this iterator to resolve addresses in global memory...
Definition: GlobIter.h:362
Iterator on Partitioned Global Address Space.
Definition: GlobIter.h:45
#define DART_GPTR_NULL
A NULL global pointer.
Definition: dart_globmem.h:105
A Team instance specifies a subset of all available units.
Definition: Team.h:41
DASH_CONSTEXPR index_type pos() const DASH_NOEXCEPT
Position of the iterator in global index space.
Definition: GlobIter.h:336
constexpr dim_t ndim(const DimensionalType &d)
Definition: Dimensional.h:56
DASH_CONSTEXPR const GlobMemType & globmem() const DASH_NOEXCEPT
The instance of GlobStaticMem used by this iterator to resolve addresses in global memory...
Definition: GlobIter.h:353
DART Global pointer type.
Definition: dart_globmem.h:77
#define DART_GPTR_ISNULL(gptr_)
Test for NULL global pointer.
Definition: dart_globmem.h:118
DASH_CONSTEXPR const self_t & global() const DASH_NOEXCEPT
Map iterator to global index domain.
Definition: GlobIter.h:328
DASH_CONSTEXPR auto lpos() const
Unit and local offset at the iterator&#39;s position.
Definition: GlobIter.h:297
Pointer in global memory space with random access arithmetics.
Definition: GlobPtr.h:33
DASH_CONSTEXPR self_t & operator--() DASH_NOEXCEPT
Prefix decrement operator.
Definition: GlobIter.h:389
DASH_CONSTEXPR bool is_local() const noexcept
Checks whether the element referenced by this global iterator is in the calling unit&#39;s local memory...
Definition: GlobIter.h:271
DASH_CONSTEXPR GlobIter(GlobMemType *gmem, PatternType const &pat, index_type position=0) DASH_NOEXCEPT
Constructor, creates a global iterator on global memory following the element order specified by the ...
Definition: GlobIter.h:119
DASH_CONSTEXPR self_t operator--(int) DASH_NOEXCEPT
Postfix decrement operator.
Definition: GlobIter.h:398
DASH_CONSTEXPR self_t operator++(int) DASH_NOEXCEPT
Postfix increment operator.
Definition: GlobIter.h:379
DASH_CONSTEXPR reference operator*() noexcept
Dereference operator.
Definition: GlobIter.h:226