1 #ifndef DASH__GLOB_VIEW_ITER_H__INCLUDED 2 #define DASH__GLOB_VIEW_ITER_H__INCLUDED 4 #include <dash/Pattern.h> 5 #include <dash/Allocator.h> 6 #include <dash/GlobRef.h> 7 #include <dash/GlobPtr.h> 9 #include <dash/iterator/GlobIter.h> 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,
44 typename PatternType::index_type,
72 typedef typename std::remove_const<ElementType>::type
75 typedef typename PatternType::viewspec_type
77 typedef typename PatternType::index_type
81 typedef ElementType value_type;
83 typedef ReferenceType reference;
84 typedef typename ReferenceType::const_type const_reference;
86 typedef PointerType pointer;
87 typedef typename PointerType::const_type const_pointer;
89 typedef typename pointer::local_type local_type;
91 typedef typename pointer::const_local_type const_local_type;
93 typedef PatternType pattern_type;
94 typedef typename PatternType::index_type index_type;
95 typedef typename PatternType::size_type size_type;
107 typedef std::integral_constant<bool, true> has_view;
117 friend std::ostream & operator<<(
139 static const MemArrange Arrangement = PatternType::memory_order();
143 GlobMemType * _globmem;
145 const PatternType * _pattern;
148 const ViewSpecType * _viewspec;
152 IndexType _view_idx_offset = 0;
154 IndexType _max_idx = 0;
167 , _view_idx_offset(0)
176 template <
class GlobStaticMemT_>
178 GlobStaticMemT_ * gmem,
179 const PatternType & pat,
181 IndexType position = 0,
182 IndexType view_index_offset = 0)
183 : _globmem(reinterpret_cast<GlobMemType *>(gmem))
185 , _viewspec(&viewspec)
187 , _view_idx_offset(view_index_offset)
188 , _max_idx(viewspec.
size() - 1)
189 , _myid(pat.team().
myid())
196 template <
class GlobStaticMemT_>
198 GlobStaticMemT_ * gmem,
199 const PatternType & pat,
200 IndexType position = 0,
201 IndexType view_index_offset = 0)
202 : _globmem(reinterpret_cast<GlobMemType *>(gmem))
206 , _view_idx_offset(view_index_offset)
207 , _max_idx(pat.
size() - 1)
222 IndexType view_offs = 0)
223 : _globmem(other._globmem)
224 , _pattern(other._pattern)
225 , _viewspec(&viewspec)
227 , _view_idx_offset(view_offs)
228 , _max_idx(other._max_idx)
244 : _globmem (other._globmem)
245 , _pattern (other._pattern)
246 , _viewspec (other._viewspec)
248 , _view_idx_offset(other._view_idx_offset)
249 , _max_idx (other._max_idx)
250 , _myid (other._myid)
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))
286 _globmem = other._globmem;
287 _pattern = other._pattern;
288 _viewspec = other._viewspec;
290 _view_idx_offset = other._view_idx_offset;
291 _max_idx = other._max_idx;
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);
325 return NumDimensions;
333 explicit operator const_pointer()
const 335 DASH_LOG_TRACE_VAR(
"GlobViewIter.const_pointer()", _idx);
336 typedef typename pattern_type::local_index_t
338 IndexType idx = _idx;
339 IndexType offset = 0;
340 DASH_LOG_TRACE_VAR(
"GlobViewIter.const_pointer", _max_idx);
342 if (_idx > _max_idx) {
346 offset += _idx - _max_idx;
348 DASH_LOG_TRACE_VAR(
"GlobViewIter.const_pointer", idx);
349 DASH_LOG_TRACE_VAR(
"GlobViewIter.const_pointer", offset);
351 local_pos_t local_pos;
352 if (_viewspec ==
nullptr) {
354 local_pos = _pattern->local(idx);
357 auto glob_coords = coords(idx);
358 local_pos = _pattern->local_index(glob_coords);
360 DASH_LOG_TRACE_VAR(
"GlobViewIter.const_pointer >",
362 DASH_LOG_TRACE_VAR(
"GlobViewIter.const_pointer >",
363 local_pos.index + offset);
365 auto const dart_pointer = _get_pointer_at(local_pos);
366 const_pointer gptr(dart_pointer);
376 explicit operator pointer()
378 DASH_LOG_TRACE_VAR(
"GlobViewIter.pointer()", _idx);
379 typedef typename pattern_type::local_index_t
381 IndexType idx = _idx;
382 IndexType offset = 0;
383 DASH_LOG_TRACE_VAR(
"GlobViewIter.pointer", _max_idx);
385 if (_idx > _max_idx) {
389 offset += _idx - _max_idx;
391 DASH_LOG_TRACE_VAR(
"GlobViewIter.pointer", idx);
392 DASH_LOG_TRACE_VAR(
"GlobViewIter.pointer", offset);
394 local_pos_t local_pos;
395 if (_viewspec ==
nullptr) {
397 local_pos = _pattern->local(idx);
400 auto glob_coords = coords(idx);
401 local_pos = _pattern->local_index(glob_coords);
403 DASH_LOG_TRACE_VAR(
"GlobViewIter.pointer >", local_pos.unit);
404 DASH_LOG_TRACE_VAR(
"GlobViewIter.pointer >", local_pos.index + offset);
407 auto const dart_pointer = _get_pointer_at(local_pos);
408 pointer gptr(dart_pointer);
421 DASH_LOG_TRACE_VAR(
"GlobViewIter.dart_gptr()", _idx);
422 typedef typename pattern_type::local_index_t
424 IndexType idx = _idx;
425 IndexType offset = 0;
427 if (_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);
437 local_pos_t local_pos;
438 if (_viewspec ==
nullptr) {
440 local_pos = _pattern->local(idx);
443 DASH_LOG_TRACE_VAR(
"GlobViewIter.dart_gptr", _viewspec);
444 auto glob_coords = coords(idx);
445 local_pos = _pattern->local_index(glob_coords);
447 DASH_LOG_TRACE(
"GlobViewIter.dart_gptr",
448 "unit:", local_pos.unit,
449 "local index:", local_pos.index);
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();
485 typedef typename pattern_type::local_index_t
488 local_pos_t local_pos;
489 if (_viewspec ==
nullptr) {
491 local_pos = _pattern->local(g_index);
494 auto glob_coords = coords(g_index);
495 local_pos = _pattern->local_index(glob_coords);
497 DASH_LOG_TRACE(
"GlobViewIter.[]",
498 "(index:", g_index,
" voffset:", _view_idx_offset,
") ->",
499 "(unit:", local_pos.unit,
" index:", local_pos.index,
")");
502 auto const dart_pointer = _get_pointer_at(local_pos);
503 return reference(dart_pointer);
512 index_type g_index)
const 514 typedef typename pattern_type::local_index_t
517 local_pos_t local_pos;
518 if (_viewspec ==
nullptr) {
520 local_pos = _pattern->local(g_index);
523 auto glob_coords = coords(g_index);
524 local_pos = _pattern->local_index(glob_coords);
526 DASH_LOG_TRACE(
"GlobViewIter.[]",
527 "(index:", g_index,
" voffset:", _view_idx_offset,
") ->",
528 "(unit:", local_pos.unit,
" index:", local_pos.index,
")");
531 auto const dart_pointer = _get_pointer_at(local_pos);
532 return const_reference(dart_pointer);
541 return (_myid ==
lpos().unit);
549 DASH_LOG_TRACE_VAR(
"GlobViewIter.local=()", _idx);
550 typedef typename pattern_type::local_index_t
552 IndexType idx = _idx;
553 IndexType offset = 0;
554 DASH_LOG_TRACE_VAR(
"GlobViewIter.local=", _max_idx);
556 if (_idx > _max_idx) {
560 offset += _idx - _max_idx;
562 DASH_LOG_TRACE_VAR(
"GlobViewIter.local=", idx);
563 DASH_LOG_TRACE_VAR(
"GlobViewIter.local=", offset);
566 local_pos_t local_pos;
567 if (_viewspec ==
nullptr) {
569 local_pos = _pattern->local(idx);
571 DASH_LOG_TRACE_VAR(
"GlobViewIter.local=", *_viewspec);
573 auto glob_coords = coords(idx);
574 local_pos = _pattern->local_index(glob_coords);
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) {
584 static_cast<pointer>(_globmem->begin()), _pattern->team().myid());
588 return std::next(lbegin, local_pos.index + offset);
597 return const_global_type(
620 constexpr index_type
pos() const noexcept
622 return _idx + _view_idx_offset;
629 inline index_type
rpos() const noexcept
640 DASH_LOG_TRACE_VAR(
"GlobViewIter.gpos()", _idx);
641 if (_viewspec ==
nullptr) {
643 DASH_LOG_TRACE_VAR(
"GlobViewIter.gpos >", _idx);
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);
651 if (_idx > _max_idx) {
655 offset = _idx - _max_idx;
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);
664 DASH_LOG_TRACE_VAR(
"GlobViewIter.gpos >", g_idx);
673 inline typename pattern_type::local_index_t
lpos()
const 675 DASH_LOG_TRACE_VAR(
"GlobViewIter.lpos()", _idx);
676 typedef typename pattern_type::local_index_t
678 IndexType idx = _idx;
679 IndexType offset = 0;
681 if (_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);
691 local_pos_t local_pos;
692 if (_viewspec ==
nullptr) {
694 local_pos = _pattern->local(idx);
697 DASH_LOG_TRACE_VAR(
"GlobViewIter.lpos", *_viewspec);
698 auto glob_coords = coords(idx);
699 local_pos = _pattern->local_index(glob_coords);
701 local_pos.index += offset;
702 DASH_LOG_TRACE(
"GlobViewIter.lpos >",
703 "unit:", local_pos.unit,
704 "local index:", local_pos.index);
713 return _viewspec !=
nullptr;
721 if (_viewspec !=
nullptr) {
724 return ViewSpecType(_pattern->extents());
731 constexpr
const GlobMemType &
globmem() const noexcept
759 self_t result = *
this;
778 self_t result = *
this;
783 self_t & operator+=(index_type n) noexcept
789 self_t & operator-=(index_type n) noexcept
795 self_t operator+(index_type n)
const noexcept
797 if (_viewspec ==
nullptr) {
801 _idx + static_cast<IndexType>(n),
809 _idx + static_cast<IndexType>(n),
814 self_t operator-(index_type n)
const noexcept
816 if (_viewspec ==
nullptr) {
820 _idx - static_cast<IndexType>(n),
828 _idx - static_cast<IndexType>(n),
833 index_type operator+(
834 const self_t & other)
const noexcept
836 return _idx + other._idx;
839 index_type operator-(
840 const self_t & other)
const 842 return _idx - other._idx;
845 inline bool operator<(
const self_t & other)
const noexcept
850 return compare(other,
851 std::less<index_type>(),
852 std::less<pointer>());
855 inline bool operator<=(
const self_t & other)
const noexcept
860 return compare(other,
861 std::less_equal<index_type>(),
862 std::less_equal<pointer>());
865 inline bool operator>(
const self_t & other)
const noexcept
870 return compare(other,
871 std::greater<index_type>(),
872 std::greater<pointer>());
875 inline bool operator>=(
const self_t & other)
const noexcept
880 return compare(other,
881 std::greater_equal<index_type>(),
882 std::greater_equal<pointer>());
885 inline bool operator==(
const self_t & other)
const noexcept
888 if (_viewspec == other._viewspec) {
890 return _idx == other._idx;
892 if ((_viewspec !=
nullptr && other._viewspec !=
nullptr) &&
893 (*_viewspec) == *(other._viewspec)) {
895 return _idx == other._idx;
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);
903 inline bool operator!=(
const self_t & other)
const noexcept
906 if (_viewspec == other._viewspec) {
908 return _idx != other._idx;
910 if ((_viewspec !=
nullptr && other._viewspec !=
nullptr) &&
911 (*_viewspec) == *(other._viewspec)) {
913 return _idx != other._idx;
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);
921 constexpr
const PatternType & pattern()
const noexcept
928 return _pattern->team();
938 class GlobIndexCmpFun,
939 class GlobPtrCmpFun >
941 const self_t & other,
942 const GlobIndexCmpFun & gidx_cmp,
943 const GlobPtrCmpFun & gptr_cmp)
const noexcept
948 if (
this == &other) {
955 if (_viewspec == other._viewspec) {
957 return gidx_cmp(_idx, other._idx);
959 if ((_viewspec !=
nullptr && other._viewspec !=
nullptr) &&
960 (*_viewspec) == *(other._viewspec)) {
962 return gidx_cmp(_idx, other._idx);
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);
990 std::array<IndexType, NumDimensions> coords(
991 IndexType glob_index)
const 993 DASH_LOG_TRACE_VAR(
"GlobViewIter.coords()", glob_index);
995 std::array<IndexType, NumDimensions> glob_coords{};
996 if (_viewspec !=
nullptr) {
997 DASH_LOG_TRACE_VAR(
"GlobViewIter.coords", *_viewspec);
1000 _viewspec->extents());
1003 glob_coords = index_space.
coords(glob_index);
1005 for (
dim_t d = 0; d < NumDimensions; ++d) {
1006 auto dim_offset = _viewspec->offset(d);
1007 glob_coords[d] += dim_offset;
1010 glob_coords = _pattern->
coords(glob_index);
1012 DASH_LOG_TRACE_VAR(
"GlobViewIter.coords >", glob_coords);
1016 dart_gptr_t _get_pointer_at(
typename pattern_type::local_index_t
pos)
const {
1018 auto dart_pointer =
static_cast<dart_gptr_t>(_globmem->begin());
1020 DASH_ASSERT(pos.index >= 0);
1022 dart_pointer.unitid = pos.unit;
1024 dart_pointer.addr_or_offs.offset += pos.index *
sizeof(value_type);
1026 return dart_pointer;
1057 typename ElementType,
1068 ->
typename Pattern::index_type
1070 return last -
first;
1074 typename ElementType,
1076 class GlobStaticMem,
1079 std::ostream & operator<<(
1082 ElementType, Pattern, GlobStaticMem, Pointer, Reference> & it)
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());
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.
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.
constexpr GlobViewIter()
Default constructor.
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.
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 ...
self_t operator--(int) noexcept
Postfix decrement operator.
const_reference operator*() const
Dereference operator.
Global iterator on an index set specified by a view modifier.
dash::BlockPattern< NumDimensions, Arrangement, IndexType > Pattern
Template alias for dash::Pattern with the same default template arguments.
int dim_t
Scalar type for a dimension value, with 0 indicating the first dimension.
global_type global()
Map iterator to global index domain by projecting the iterator's view.
Global memory with address space of static size.
bool is_relative() const noexcept
Whether the iterator's position is relative to a view.
ViewSpecType viewspec() const
The view that specifies this iterator's index range.
self_t operator++(int) noexcept
Postfix increment operator.
GlobMemType & globmem() noexcept
The instance of GlobStaticMem used by this iterator to resolve addresses in global memory...
pattern_type::local_index_t lpos() const
Unit and local offset at the iterator's position.
constexpr const GlobMemType & globmem() const noexcept
The instance of GlobStaticMem used by this iterator to resolve addresses in global memory...
index_type gpos() const
Position of the iterator in global index range.
Iterator on Partitioned Global Address Space.
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 ...
A Team instance specifies a subset of all available units.
self_t & operator=(GlobViewIter< T_, P_, GM_, Ptr_, Ref_ > &&other)
Move-assignment operator.
static dim_t ndim()
The number of dimensions of the iterator's underlying pattern.
std::array< IndexType, NumDimensions > coords(IndexType index) const
Convert given linear offset (index) to cartesian coordinates.
constexpr GlobViewIter(const GlobViewIter< T_, P_, GM_, Ptr_, Ref_ > &other)
Copy constructor.
dart_gptr_t dart_gptr() const
Explicit conversion to dart_gptr_t.
Defines a cartesian, totally-ordered index space by mapping linear indices to cartesian coordinates d...
self_t & operator++() noexcept
Prefix increment operator.
constexpr dim_t ndim(const DimensionalType &d)
DART Global pointer type.
constexpr GlobViewIter(GlobViewIter< T_, P_, GM_, Ptr_, Ref_ > &&other)
Move constructor.
const_global_type global() const
Map iterator to global index domain by projecting the iterator's view.
struct dash::unit_id< dash::local_unit, dart_team_unit_t > team_unit_t
Unit ID to use for team-local IDs.
constexpr bool is_local() const
Checks whether the element referenced by this global iterator is in the calling unit's local memory...
self_t & operator=(const GlobViewIter< T_, P_, GM_, Ptr_, Ref_ > &other)
Assignment operator.
constexpr index_type pos() const noexcept
Position of the iterator in its view's iteration space and the view's offset in global index space...
reference operator*()
Dereference operator.
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.
local_type local() const
Convert global iterator to native pointer.
see https://en.cppreference.com/w/cpp/feature_test for recommended feature tests
const_reference operator[](index_type g_index) const
Subscript operator, returns global reference to element at given global index.
self_t & operator--() noexcept
Prefix decrement operator.
dash::gptrdiff_t distance(GlobPtr< T, MemSpaceT > gbegin, GlobPtr< T, MemSpaceT > gend)
Returns the number of hops from gbegin to gend.
index_type rpos() const noexcept
Position of the iterator in its view's iteration space, disregarding the view's offset in global inde...