DASH  0.3.0
GlobViewIter.h
1 #ifndef DASH__GLOB_VIEW_ITER_H__INCLUDED
2 #define DASH__GLOB_VIEW_ITER_H__INCLUDED
3 
4 #include <dash/Pattern.h>
5 #include <dash/Allocator.h>
6 #include <dash/GlobRef.h>
7 #include <dash/GlobPtr.h>
8 
9 #include <dash/iterator/GlobIter.h>
10 
11 #include <cstddef>
12 #include <functional>
13 #include <sstream>
14 #include <iostream>
15 
16 namespace dash {
17 
18 #ifndef DOXYGEN
19 // Forward-declaration
20 template<
21  typename ElementType,
22  class PatternType,
23  class GlobMemType,
24  class PointerType,
25  class ReferenceType >
26 class GlobIter;
27 #endif
28 
34 template<
35  typename ElementType,
36  class PatternType,
37  class GlobMemType,
38  class PointerType = typename GlobMemType::void_pointer::template rebind<ElementType>,
39  class ReferenceType = GlobRef<ElementType> >
41 : public std::iterator<
42  std::random_access_iterator_tag,
43  ElementType,
44  typename PatternType::index_type,
45  PointerType,
46  ReferenceType > {
47 private:
48  typedef GlobViewIter<
49  ElementType,
50  PatternType,
51  GlobMemType,
52  PointerType,
53  ReferenceType>
54  self_t;
55 
56  typedef GlobIter<
57  ElementType,
58  PatternType,
59  GlobMemType,
60  PointerType,
61  ReferenceType>
63 
64  typedef GlobIter<
65  const ElementType,
66  PatternType,
67  GlobMemType,
68  PointerType,
69  ReferenceType>
71 
72  typedef typename std::remove_const<ElementType>::type
73  nonconst_value_type;
74 
75  typedef typename PatternType::viewspec_type
76  ViewSpecType;
77  typedef typename PatternType::index_type
78  IndexType;
79 
80 public:
81  typedef ElementType value_type;
82 
83  typedef ReferenceType reference;
84  typedef typename ReferenceType::const_type const_reference;
85 
86  typedef PointerType pointer;
87  typedef typename PointerType::const_type const_pointer;
88 
89  typedef typename pointer::local_type local_type;
90 
91  typedef typename pointer::const_local_type const_local_type;
92 
93  typedef PatternType pattern_type;
94  typedef typename PatternType::index_type index_type;
95  typedef typename PatternType::size_type size_type;
96 
97 private:
98  typedef GlobViewIter<
99  const ElementType,
100  PatternType,
101  GlobMemType,
102  const_pointer,
103  const_reference >
104  self_const_t;
105 
106 public:
107  typedef std::integral_constant<bool, true> has_view;
108 
109 public:
110  // For ostream output
111  template <
112  typename T_,
113  class P_,
114  class GM_,
115  class Ptr_,
116  class Ref_ >
117  friend std::ostream & operator<<(
118  std::ostream & os,
120 
121  template<
122  typename T_,
123  class P_,
124  class GM_,
125  class Ptr_,
126  class Ref_ >
127  friend class GlobViewIter;
128 
129  template<
130  typename T_,
131  class P_,
132  class GM_,
133  class Ptr_,
134  class Ref_ >
135  friend class GlobIter;
136 
137 private:
138  static const dim_t NumDimensions = PatternType::ndim();
139  static const MemArrange Arrangement = PatternType::memory_order();
140 
141 protected:
143  GlobMemType * _globmem;
145  const PatternType * _pattern;
148  const ViewSpecType * _viewspec;
150  IndexType _idx = 0;
152  IndexType _view_idx_offset = 0;
154  IndexType _max_idx = 0;
156  team_unit_t _myid;
157 
158 public:
162  constexpr GlobViewIter()
163  : _globmem(nullptr)
164  , _pattern(nullptr)
165  , _viewspec(nullptr)
166  , _idx(0)
167  , _view_idx_offset(0)
168  , _max_idx(0)
169  , _myid(dash::Team::All().myid())
170  { }
171 
176  template <class GlobStaticMemT_>
177  constexpr GlobViewIter(
178  GlobStaticMemT_ * gmem,
179  const PatternType & pat,
180  const ViewSpecType & viewspec,
181  IndexType position = 0,
182  IndexType view_index_offset = 0)
183  : _globmem(reinterpret_cast<GlobMemType *>(gmem))
184  , _pattern(&pat)
185  , _viewspec(&viewspec)
186  , _idx(position)
187  , _view_idx_offset(view_index_offset)
188  , _max_idx(viewspec.size() - 1)
189  , _myid(pat.team().myid())
190  { }
191 
196  template <class GlobStaticMemT_>
197  constexpr GlobViewIter(
198  GlobStaticMemT_ * gmem,
199  const PatternType & pat,
200  IndexType position = 0,
201  IndexType view_index_offset = 0)
202  : _globmem(reinterpret_cast<GlobMemType *>(gmem))
203  , _pattern(&pat)
204  , _viewspec(nullptr)
205  , _idx(position)
206  , _view_idx_offset(view_index_offset)
207  , _max_idx(pat.size() - 1)
208  , _myid(dash::Team::GlobalUnitID())
209  { }
210 
214  template <
215  class P_,
216  class GM_,
217  class Ptr_,
218  class Ref_ >
219  constexpr GlobViewIter(
221  const ViewSpecType & viewspec,
222  IndexType view_offs = 0)
223  : _globmem(other._globmem)
224  , _pattern(other._pattern)
225  , _viewspec(&viewspec)
226  , _idx(other._idx)
227  , _view_idx_offset(view_offs)
228  , _max_idx(other._max_idx)
229  , _myid(other._myid)
230  { }
231 
235  template <
236  typename T_,
237  class P_,
238  class GM_,
239  class Ptr_,
240  class Ref_ >
241  constexpr GlobViewIter(
242 // const GlobViewIter<nonconst_value_type, P_, GM_, Ptr_, Ref_> & other)
244  : _globmem (other._globmem)
245  , _pattern (other._pattern)
246  , _viewspec (other._viewspec)
247  , _idx (other._idx)
248  , _view_idx_offset(other._view_idx_offset)
249  , _max_idx (other._max_idx)
250  , _myid (other._myid)
251  { }
252 
256  template <
257  typename T_,
258  class P_,
259  class GM_,
260  class Ptr_,
261  class Ref_ >
262  constexpr GlobViewIter(
263 // GlobViewIter<nonconst_value_type, P_, GM_, Ptr_, Ref_> && other)
265  : _globmem (std::move(other._globmem))
266  , _pattern (std::move(other._pattern))
267  , _viewspec (std::move(other._viewspec))
268  , _idx (std::move(other._idx))
269  , _view_idx_offset(std::move(other._view_idx_offset))
270  , _max_idx (std::move(other._max_idx))
271  , _myid (std::move(other._myid))
272  { }
273 
277  template <
278  typename T_,
279  class P_,
280  class GM_,
281  class Ptr_,
282  class Ref_ >
283  self_t & operator=(
285  {
286  _globmem = other._globmem;
287  _pattern = other._pattern;
288  _viewspec = other._viewspec;
289  _idx = other._idx;
290  _view_idx_offset = other._view_idx_offset;
291  _max_idx = other._max_idx;
292  _myid = other._myid;
293 
294  return *this;
295  }
296 
300  template <
301  typename T_,
302  class P_,
303  class GM_,
304  class Ptr_,
305  class Ref_ >
306  self_t & operator=(
308  {
309  _globmem = std::move(other._globmem);
310  _pattern = std::move(other._pattern);
311  _viewspec = std::move(other._viewspec);
312  _idx = std::move(other._idx);
313  _view_idx_offset = std::move(other._view_idx_offset);
314  _max_idx = std::move(other._max_idx);
315  _myid = std::move(other._myid);
316  // no ownership to transfer
317  return *this;
318  }
319 
323  static dim_t ndim()
324  {
325  return NumDimensions;
326  }
327 
333  explicit operator const_pointer() const
334  {
335  DASH_LOG_TRACE_VAR("GlobViewIter.const_pointer()", _idx);
336  typedef typename pattern_type::local_index_t
337  local_pos_t;
338  IndexType idx = _idx;
339  IndexType offset = 0;
340  DASH_LOG_TRACE_VAR("GlobViewIter.const_pointer", _max_idx);
341  // Convert iterator position (_idx) to local index and unit.
342  if (_idx > _max_idx) {
343  // Global iterator pointing past the range indexed by the pattern
344  // which is the case for .end() iterators.
345  idx = _max_idx;
346  offset += _idx - _max_idx;
347  }
348  DASH_LOG_TRACE_VAR("GlobViewIter.const_pointer", idx);
349  DASH_LOG_TRACE_VAR("GlobViewIter.const_pointer", offset);
350  // Global index to local index and unit:
351  local_pos_t local_pos;
352  if (_viewspec == nullptr) {
353  // No viewspec mapping required:
354  local_pos = _pattern->local(idx);
355  } else {
356  // Viewspec projection required:
357  auto glob_coords = coords(idx);
358  local_pos = _pattern->local_index(glob_coords);
359  }
360  DASH_LOG_TRACE_VAR("GlobViewIter.const_pointer >",
361  local_pos.unit);
362  DASH_LOG_TRACE_VAR("GlobViewIter.const_pointer >",
363  local_pos.index + offset);
364  // Create global pointer from unit and local offset:
365  auto const dart_pointer = _get_pointer_at(local_pos);
366  const_pointer gptr(dart_pointer);
367  gptr += offset;
368  return gptr;
369  }
370 
376  explicit operator pointer()
377  {
378  DASH_LOG_TRACE_VAR("GlobViewIter.pointer()", _idx);
379  typedef typename pattern_type::local_index_t
380  local_pos_t;
381  IndexType idx = _idx;
382  IndexType offset = 0;
383  DASH_LOG_TRACE_VAR("GlobViewIter.pointer", _max_idx);
384  // Convert iterator position (_idx) to local index and unit.
385  if (_idx > _max_idx) {
386  // Global iterator pointing past the range indexed by the pattern
387  // which is the case for .end() iterators.
388  idx = _max_idx;
389  offset += _idx - _max_idx;
390  }
391  DASH_LOG_TRACE_VAR("GlobViewIter.pointer", idx);
392  DASH_LOG_TRACE_VAR("GlobViewIter.pointer", offset);
393  // Global index to local index and unit:
394  local_pos_t local_pos;
395  if (_viewspec == nullptr) {
396  // No viewspec mapping required:
397  local_pos = _pattern->local(idx);
398  } else {
399  // Viewspec projection required:
400  auto glob_coords = coords(idx);
401  local_pos = _pattern->local_index(glob_coords);
402  }
403  DASH_LOG_TRACE_VAR("GlobViewIter.pointer >", local_pos.unit);
404  DASH_LOG_TRACE_VAR("GlobViewIter.pointer >", local_pos.index + offset);
405  // Create global pointer from unit and local offset:
406 
407  auto const dart_pointer = _get_pointer_at(local_pos);
408  pointer gptr(dart_pointer);
409  gptr += offset;
410  return gptr;
411  }
412 
420  {
421  DASH_LOG_TRACE_VAR("GlobViewIter.dart_gptr()", _idx);
422  typedef typename pattern_type::local_index_t
423  local_pos_t;
424  IndexType idx = _idx;
425  IndexType offset = 0;
426  // Convert iterator position (_idx) to local index and unit.
427  if (_idx > _max_idx) {
428  // Global iterator pointing past the range indexed by the pattern
429  // which is the case for .end() iterators.
430  idx = _max_idx;
431  offset += _idx - _max_idx;
432  DASH_LOG_TRACE_VAR("GlobViewIter.dart_gptr", _max_idx);
433  DASH_LOG_TRACE_VAR("GlobViewIter.dart_gptr", idx);
434  DASH_LOG_TRACE_VAR("GlobViewIter.dart_gptr", offset);
435  }
436  // Global index to local index and unit:
437  local_pos_t local_pos;
438  if (_viewspec == nullptr) {
439  // No viewspec mapping required:
440  local_pos = _pattern->local(idx);
441  } else {
442  // Viewspec projection required:
443  DASH_LOG_TRACE_VAR("GlobViewIter.dart_gptr", _viewspec);
444  auto glob_coords = coords(idx);
445  local_pos = _pattern->local_index(glob_coords);
446  }
447  DASH_LOG_TRACE("GlobViewIter.dart_gptr",
448  "unit:", local_pos.unit,
449  "local index:", local_pos.index);
450  // Global pointer to element at given position:
451  auto const dart_pointer = _get_pointer_at(local_pos);
452  const_pointer gptr(dart_pointer);
453  DASH_LOG_TRACE_VAR("GlobIter.dart_gptr >", gptr);
454  return (gptr + offset).dart_gptr();
455  }
456 
462  inline reference operator*()
463  {
464  return this->operator[](_idx);
465  }
466 
472  inline const_reference operator*() const
473  {
474  return this->operator[](_idx);
475  }
476 
481  reference operator[](
483  index_type g_index)
484  {
485  typedef typename pattern_type::local_index_t
486  local_pos_t;
487  // Global index to local index and unit:
488  local_pos_t local_pos;
489  if (_viewspec == nullptr) {
490  // No viewspec mapping required:
491  local_pos = _pattern->local(g_index);
492  } else {
493  // Viewspec projection required:
494  auto glob_coords = coords(g_index);
495  local_pos = _pattern->local_index(glob_coords);
496  }
497  DASH_LOG_TRACE("GlobViewIter.[]",
498  "(index:", g_index, " voffset:", _view_idx_offset, ") ->",
499  "(unit:", local_pos.unit, " index:", local_pos.index, ")");
500  // Global reference to element at given position:
501 
502  auto const dart_pointer = _get_pointer_at(local_pos);
503  return reference(dart_pointer);
504  }
505 
510  const_reference operator[](
512  index_type g_index) const
513  {
514  typedef typename pattern_type::local_index_t
515  local_pos_t;
516  // Global index to local index and unit:
517  local_pos_t local_pos;
518  if (_viewspec == nullptr) {
519  // No viewspec mapping required:
520  local_pos = _pattern->local(g_index);
521  } else {
522  // Viewspec projection required:
523  auto glob_coords = coords(g_index);
524  local_pos = _pattern->local_index(glob_coords);
525  }
526  DASH_LOG_TRACE("GlobViewIter.[]",
527  "(index:", g_index, " voffset:", _view_idx_offset, ") ->",
528  "(unit:", local_pos.unit, " index:", local_pos.index, ")");
529  // Global reference to element at given position:
530 
531  auto const dart_pointer = _get_pointer_at(local_pos);
532  return const_reference(dart_pointer);
533  }
534 
539  constexpr bool is_local() const
540  {
541  return (_myid == lpos().unit);
542  }
543 
547  local_type local() const
548  {
549  DASH_LOG_TRACE_VAR("GlobViewIter.local=()", _idx);
550  typedef typename pattern_type::local_index_t
551  local_pos_t;
552  IndexType idx = _idx;
553  IndexType offset = 0;
554  DASH_LOG_TRACE_VAR("GlobViewIter.local=", _max_idx);
555  // Convert iterator position (_idx) to local index and unit.
556  if (_idx > _max_idx) {
557  // Global iterator pointing past the range indexed by the pattern
558  // which is the case for .end() iterators.
559  idx = _max_idx;
560  offset += _idx - _max_idx;
561  }
562  DASH_LOG_TRACE_VAR("GlobViewIter.local=", idx);
563  DASH_LOG_TRACE_VAR("GlobViewIter.local=", offset);
564 
565  // Global index to local index and unit:
566  local_pos_t local_pos;
567  if (_viewspec == nullptr) {
568  // No viewspec mapping required:
569  local_pos = _pattern->local(idx);
570  } else {
571  DASH_LOG_TRACE_VAR("GlobViewIter.local=", *_viewspec);
572  // Viewspec projection required:
573  auto glob_coords = coords(idx);
574  local_pos = _pattern->local_index(glob_coords);
575  }
576  DASH_LOG_TRACE_VAR("GlobViewIter.local= >", local_pos.unit);
577  DASH_LOG_TRACE_VAR("GlobViewIter.local= >", local_pos.index);
578  if (_myid != local_pos.unit) {
579  // Iterator position does not point to local element
580  return nullptr;
581  }
582 
583  auto* lbegin = dash::local_begin(
584  static_cast<pointer>(_globmem->begin()), _pattern->team().myid());
585 
586  DASH_ASSERT(lbegin);
587 
588  return std::next(lbegin, local_pos.index + offset);
589  }
590 
594  inline const_global_type global() const
595  {
596  auto g_idx = gpos();
597  return const_global_type(
598  _globmem,
599  *_pattern,
600  g_idx
601  );
602  }
603 
607  inline global_type global()
608  {
609  return global_type(
610  _globmem,
611  *_pattern,
612  gpos()
613  );
614  }
615 
620  constexpr index_type pos() const noexcept
621  {
622  return _idx + _view_idx_offset;
623  }
624 
629  inline index_type rpos() const noexcept
630  {
631  return _idx;
632  }
633 
638  inline index_type gpos() const
639  {
640  DASH_LOG_TRACE_VAR("GlobViewIter.gpos()", _idx);
641  if (_viewspec == nullptr) {
642  // No viewspec mapping required:
643  DASH_LOG_TRACE_VAR("GlobViewIter.gpos >", _idx);
644  return _idx;
645  } else {
646  IndexType idx = _idx;
647  IndexType offset = 0;
648  DASH_LOG_TRACE_VAR("GlobViewIter.gpos", *_viewspec);
649  DASH_LOG_TRACE_VAR("GlobViewIter.gpos", _max_idx);
650  // Convert iterator position (_idx) to local index and unit.
651  if (_idx > _max_idx) {
652  // Global iterator pointing past the range indexed by the pattern
653  // which is the case for .end() iterators.
654  idx = _max_idx;
655  offset = _idx - _max_idx;
656  }
657  // Viewspec projection required:
658  auto g_coords = coords(idx);
659  DASH_LOG_TRACE_VAR("GlobViewIter.gpos", _idx);
660  DASH_LOG_TRACE_VAR("GlobViewIter.gpos", g_coords);
661  auto g_idx = _pattern->global_at(g_coords);
662  DASH_LOG_TRACE_VAR("GlobViewIter.gpos", g_idx);
663  g_idx += offset;
664  DASH_LOG_TRACE_VAR("GlobViewIter.gpos >", g_idx);
665  return g_idx;
666  }
667  }
668 
673  inline typename pattern_type::local_index_t lpos() const
674  {
675  DASH_LOG_TRACE_VAR("GlobViewIter.lpos()", _idx);
676  typedef typename pattern_type::local_index_t
677  local_pos_t;
678  IndexType idx = _idx;
679  IndexType offset = 0;
680  // Convert iterator position (_idx) to local index and unit.
681  if (_idx > _max_idx) {
682  // Global iterator pointing past the range indexed by the pattern
683  // which is the case for .end() iterators.
684  idx = _max_idx;
685  offset = _idx - _max_idx;
686  DASH_LOG_TRACE_VAR("GlobViewIter.lpos", _max_idx);
687  DASH_LOG_TRACE_VAR("GlobViewIter.lpos", idx);
688  DASH_LOG_TRACE_VAR("GlobViewIter.lpos", offset);
689  }
690  // Global index to local index and unit:
691  local_pos_t local_pos;
692  if (_viewspec == nullptr) {
693  // No viewspec mapping required:
694  local_pos = _pattern->local(idx);
695  } else {
696  // Viewspec projection required:
697  DASH_LOG_TRACE_VAR("GlobViewIter.lpos", *_viewspec);
698  auto glob_coords = coords(idx);
699  local_pos = _pattern->local_index(glob_coords);
700  }
701  local_pos.index += offset;
702  DASH_LOG_TRACE("GlobViewIter.lpos >",
703  "unit:", local_pos.unit,
704  "local index:", local_pos.index);
705  return local_pos;
706  }
707 
711  inline bool is_relative() const noexcept
712  {
713  return _viewspec != nullptr;
714  }
715 
719  inline ViewSpecType viewspec() const
720  {
721  if (_viewspec != nullptr) {
722  return *_viewspec;
723  }
724  return ViewSpecType(_pattern->extents());
725  }
726 
731  constexpr const GlobMemType & globmem() const noexcept
732  {
733  return *_globmem;
734  }
735 
740  inline GlobMemType & globmem() noexcept
741  {
742  return *_globmem;
743  }
744 
748  self_t & operator++() noexcept
749  {
750  ++_idx;
751  return *this;
752  }
753 
757  self_t operator++(int) noexcept
758  {
759  self_t result = *this;
760  ++_idx;
761  return result;
762  }
763 
767  self_t & operator--() noexcept
768  {
769  --_idx;
770  return *this;
771  }
772 
776  self_t operator--(int) noexcept
777  {
778  self_t result = *this;
779  --_idx;
780  return result;
781  }
782 
783  self_t & operator+=(index_type n) noexcept
784  {
785  _idx += n;
786  return *this;
787  }
788 
789  self_t & operator-=(index_type n) noexcept
790  {
791  _idx -= n;
792  return *this;
793  }
794 
795  self_t operator+(index_type n) const noexcept
796  {
797  if (_viewspec == nullptr) {
798  self_t res(
799  _globmem,
800  *_pattern,
801  _idx + static_cast<IndexType>(n),
802  _view_idx_offset);
803  return res;
804  }
805  self_t res(
806  _globmem,
807  *_pattern,
808  *_viewspec,
809  _idx + static_cast<IndexType>(n),
810  _view_idx_offset);
811  return res;
812  }
813 
814  self_t operator-(index_type n) const noexcept
815  {
816  if (_viewspec == nullptr) {
817  self_t res(
818  _globmem,
819  *_pattern,
820  _idx - static_cast<IndexType>(n),
821  _view_idx_offset);
822  return res;
823  }
824  self_t res(
825  _globmem,
826  *_pattern,
827  *_viewspec,
828  _idx - static_cast<IndexType>(n),
829  _view_idx_offset);
830  return res;
831  }
832 
833  index_type operator+(
834  const self_t & other) const noexcept
835  {
836  return _idx + other._idx;
837  }
838 
839  index_type operator-(
840  const self_t & other) const
841  {
842  return _idx - other._idx;
843  }
844 
845  inline bool operator<(const self_t & other) const noexcept
846  {
847  // NOTE:
848  // This function call is significantly slower than the explicit
849  // implementation in operator== and operator!=.
850  return compare(other,
851  std::less<index_type>(),
852  std::less<pointer>());
853  }
854 
855  inline bool operator<=(const self_t & other) const noexcept
856  {
857  // NOTE:
858  // This function call is significantly slower than the explicit
859  // implementation in operator== and operator!=.
860  return compare(other,
861  std::less_equal<index_type>(),
862  std::less_equal<pointer>());
863  }
864 
865  inline bool operator>(const self_t & other) const noexcept
866  {
867  // NOTE:
868  // This function call is significantly slower than the explicit
869  // implementation in operator== and operator!=.
870  return compare(other,
871  std::greater<index_type>(),
872  std::greater<pointer>());
873  }
874 
875  inline bool operator>=(const self_t & other) const noexcept
876  {
877  // NOTE:
878  // This function call is significantly slower than the explicit
879  // implementation in operator== and operator!=.
880  return compare(other,
881  std::greater_equal<index_type>(),
882  std::greater_equal<pointer>());
883  }
884 
885  inline bool operator==(const self_t & other) const noexcept
886  {
887  // NOTE: See comments in method compare().
888  if (_viewspec == other._viewspec) {
889  // Same viewspec pointer
890  return _idx == other._idx;
891  }
892  if ((_viewspec != nullptr && other._viewspec != nullptr) &&
893  (*_viewspec) == *(other._viewspec)) {
894  // Viewspec instances are equal
895  return _idx == other._idx;
896  }
897  auto lhs_local = lpos();
898  auto rhs_local = other.lpos();
899  return (lhs_local.unit == rhs_local.unit &&
900  lhs_local.index == rhs_local.index);
901  }
902 
903  inline bool operator!=(const self_t & other) const noexcept
904  {
905  // NOTE: See comments in method compare().
906  if (_viewspec == other._viewspec) {
907  // Same viewspec pointer
908  return _idx != other._idx;
909  }
910  if ((_viewspec != nullptr && other._viewspec != nullptr) &&
911  (*_viewspec) == *(other._viewspec)) {
912  // Viewspec instances are equal
913  return _idx != other._idx;
914  }
915  auto lhs_local = lpos();
916  auto rhs_local = other.lpos();
917  return (lhs_local.unit != rhs_local.unit ||
918  lhs_local.index != rhs_local.index);
919  }
920 
921  constexpr const PatternType & pattern() const noexcept
922  {
923  return *_pattern;
924  }
925 
926  constexpr dash::Team & team() const noexcept
927  {
928  return _pattern->team();
929  }
930 
931 
932 private:
937  template<
938  class GlobIndexCmpFun,
939  class GlobPtrCmpFun >
940  bool compare(
941  const self_t & other,
942  const GlobIndexCmpFun & gidx_cmp,
943  const GlobPtrCmpFun & gptr_cmp) const noexcept
944  {
945 #if __REMARK__
946  // Usually this is a best practice check, but it's an infrequent case
947  // so we rather avoid this comparison:
948  if (this == &other) {
949  return true;
950  }
951 #endif
952  // NOTE:
953  // Do not check _idx first, as it would never match for comparison with
954  // an end iterator.
955  if (_viewspec == other._viewspec) {
956  // Same viewspec pointer
957  return gidx_cmp(_idx, other._idx);
958  }
959  if ((_viewspec != nullptr && other._viewspec != nullptr) &&
960  (*_viewspec) == *(other._viewspec)) {
961  // Viewspec instances are equal
962  return gidx_cmp(_idx, other._idx);
963  }
964  // View projection at lhs and/or rhs set.
965  // Convert both to GlobPtr (i.e. apply view projection) and compare.
966  //
967  // NOTE:
968  // This conversion is quite expensive but will never be necessary
969  // if both iterators have been created from the same range.
970  // Example:
971  // a.block(1).begin() == a.block(1).end().
972  // does not require viewspace projection while
973  // a.block(1).begin() == a.end()
974  // does. The latter case should be avoided for this reason.
975  const pointer lhs_dart_gptr(dart_gptr());
976  const pointer rhs_dart_gptr(other.dart_gptr());
977  return gptr_cmp(lhs_dart_gptr, rhs_dart_gptr);
978  }
979 
990  std::array<IndexType, NumDimensions> coords(
991  IndexType glob_index) const
992  {
993  DASH_LOG_TRACE_VAR("GlobViewIter.coords()", glob_index);
994  // Global cartesian coords of current iterator position:
995  std::array<IndexType, NumDimensions> glob_coords{};
996  if (_viewspec != nullptr) {
997  DASH_LOG_TRACE_VAR("GlobViewIter.coords", *_viewspec);
998  // Create cartesian index space from extents of view projection:
1000  _viewspec->extents());
1001  // Initialize global coords with view coords (coords of iterator
1002  // position in view index space):
1003  glob_coords = index_space.coords(glob_index);
1004  // Apply offset of view projection to view coords:
1005  for (dim_t d = 0; d < NumDimensions; ++d) {
1006  auto dim_offset = _viewspec->offset(d);
1007  glob_coords[d] += dim_offset;
1008  }
1009  } else {
1010  glob_coords = _pattern->coords(glob_index);
1011  }
1012  DASH_LOG_TRACE_VAR("GlobViewIter.coords >", glob_coords);
1013  return glob_coords;
1014  }
1015 
1016  dart_gptr_t _get_pointer_at(typename pattern_type::local_index_t pos) const {
1017  //dart pointer to global begin
1018  auto dart_pointer = static_cast<dart_gptr_t>(_globmem->begin());
1019 
1020  DASH_ASSERT(pos.index >= 0);
1021 
1022  dart_pointer.unitid = pos.unit;
1023 
1024  dart_pointer.addr_or_offs.offset += pos.index * sizeof(value_type);
1025 
1026  return dart_pointer;
1027  }
1028 
1029 
1030 }; // class GlobViewIter
1031 
1056 template<
1057  typename ElementType,
1058  class Pattern,
1059  class GlobStaticMem,
1060  class Pointer,
1061  class Reference >
1064  first,
1067  last)
1068 -> typename Pattern::index_type
1069 {
1070  return last - first;
1071 }
1072 
1073 template <
1074  typename ElementType,
1075  class Pattern,
1076  class GlobStaticMem,
1077  class Pointer,
1078  class Reference >
1079 std::ostream & operator<<(
1080  std::ostream & os,
1081  const dash::GlobViewIter<
1082  ElementType, Pattern, GlobStaticMem, Pointer, Reference> & it)
1083 {
1084  std::ostringstream ss;
1085  Pointer ptr(it.dart_gptr());
1086  ss << "dash::GlobViewIter<" << typeid(ElementType).name() << ">("
1087  << "idx:" << it._idx << ", "
1088  << "gptr:" << ptr << ")";
1089  return operator<<(os, ss.str());
1090 }
1091 
1092 } // namespace dash
1093 
1094 #endif // DASH__GLOB_VIEW_ITER_H__INCLUDED
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
global_unit_t myid()
Shortcut to query the global unit ID of the calling unit.
reference operator[](index_type g_index)
Subscript operator, returns global reference to element at given global index.
Definition: GlobViewIter.h:481
constexpr GlobViewIter()
Default constructor.
Definition: GlobViewIter.h:162
size_t size()
Return the number of units in the global team.
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
constexpr GlobViewIter(GlobStaticMemT_ *gmem, const PatternType &pat, const ViewSpecType &viewspec, IndexType position=0, IndexType view_index_offset=0)
Constructor, creates a global iterator on global memory following the element order specified by the ...
Definition: GlobViewIter.h:177
self_t operator--(int) noexcept
Postfix decrement operator.
Definition: GlobViewIter.h:776
const_reference operator*() const
Dereference operator.
Definition: GlobViewIter.h:472
Global iterator on an index set specified by a view modifier.
Definition: GlobViewIter.h:40
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
global_type global()
Map iterator to global index domain by projecting the iterator&#39;s view.
Definition: GlobViewIter.h:607
Global memory with address space of static size.
Definition: GlobStaticMem.h:88
Returns second operand.
Definition: Operation.h:201
bool is_relative() const noexcept
Whether the iterator&#39;s position is relative to a view.
Definition: GlobViewIter.h:711
ViewSpecType viewspec() const
The view that specifies this iterator&#39;s index range.
Definition: GlobViewIter.h:719
self_t operator++(int) noexcept
Postfix increment operator.
Definition: GlobViewIter.h:757
GlobMemType & globmem() noexcept
The instance of GlobStaticMem used by this iterator to resolve addresses in global memory...
Definition: GlobViewIter.h:740
pattern_type::local_index_t lpos() const
Unit and local offset at the iterator&#39;s position.
Definition: GlobViewIter.h:673
constexpr const GlobMemType & globmem() const noexcept
The instance of GlobStaticMem used by this iterator to resolve addresses in global memory...
Definition: GlobViewIter.h:731
index_type gpos() const
Position of the iterator in global index range.
Definition: GlobViewIter.h:638
Iterator on Partitioned Global Address Space.
Definition: GlobIter.h:45
constexpr GlobViewIter(GlobStaticMemT_ *gmem, const PatternType &pat, IndexType position=0, IndexType view_index_offset=0)
Constructor, creates a global iterator on global memory following the element order specified by the ...
Definition: GlobViewIter.h:197
A Team instance specifies a subset of all available units.
Definition: Team.h:41
self_t & operator=(GlobViewIter< T_, P_, GM_, Ptr_, Ref_ > &&other)
Move-assignment operator.
Definition: GlobViewIter.h:306
static dim_t ndim()
The number of dimensions of the iterator&#39;s underlying pattern.
Definition: GlobViewIter.h:323
std::array< IndexType, NumDimensions > coords(IndexType index) const
Convert given linear offset (index) to cartesian coordinates.
Definition: Cartesian.h:497
constexpr GlobViewIter(const GlobViewIter< T_, P_, GM_, Ptr_, Ref_ > &other)
Copy constructor.
Definition: GlobViewIter.h:241
dart_gptr_t dart_gptr() const
Explicit conversion to dart_gptr_t.
Definition: GlobViewIter.h:419
Defines a cartesian, totally-ordered index space by mapping linear indices to cartesian coordinates d...
Definition: Cartesian.h:239
self_t & operator++() noexcept
Prefix increment operator.
Definition: GlobViewIter.h:748
constexpr dim_t ndim(const DimensionalType &d)
Definition: Dimensional.h:56
DART Global pointer type.
Definition: dart_globmem.h:77
constexpr GlobViewIter(GlobViewIter< T_, P_, GM_, Ptr_, Ref_ > &&other)
Move constructor.
Definition: GlobViewIter.h:262
const_global_type global() const
Map iterator to global index domain by projecting the iterator&#39;s view.
Definition: GlobViewIter.h:594
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 is_local() const
Checks whether the element referenced by this global iterator is in the calling unit&#39;s local memory...
Definition: GlobViewIter.h:539
self_t & operator=(const GlobViewIter< T_, P_, GM_, Ptr_, Ref_ > &other)
Assignment operator.
Definition: GlobViewIter.h:283
constexpr index_type pos() const noexcept
Position of the iterator in its view&#39;s iteration space and the view&#39;s offset in global index space...
Definition: GlobViewIter.h:620
reference operator*()
Dereference operator.
Definition: GlobViewIter.h:462
constexpr GlobViewIter(const GlobIter< nonconst_value_type, P_, GM_, Ptr_, Ref_ > &other, const ViewSpecType &viewspec, IndexType view_offs=0)
Constructor, creates a global view iterator from a global iterator.
Definition: GlobViewIter.h:219
local_type local() const
Convert global iterator to native pointer.
Definition: GlobViewIter.h:547
see https://en.cppreference.com/w/cpp/feature_test for recommended feature tests
Definition: cstddef.h:8
const_reference operator[](index_type g_index) const
Subscript operator, returns global reference to element at given global index.
Definition: GlobViewIter.h:510
self_t & operator--() noexcept
Prefix decrement operator.
Definition: GlobViewIter.h:767
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
index_type rpos() const noexcept
Position of the iterator in its view&#39;s iteration space, disregarding the view&#39;s offset in global inde...
Definition: GlobViewIter.h:629