1 #ifndef DASH__MEMORY__GLOB_HEAP_LOCAL_PTR_H__INCLUDED 2 #define DASH__MEMORY__GLOB_HEAP_LOCAL_PTR_H__INCLUDED 5 #include <dash/Types.h> 7 #include <dash/Exception.h> 8 #include <dash/internal/Logging.h> 10 #include <dash/Exception.h> 11 #include <dash/memory/internal/GlobHeapMemTypes.h> 13 #include <type_traits> 30 typename PointerType = ElementType *,
31 typename ReferenceType = ElementType & >
33 :
public std::iterator<
34 std::random_access_iterator_tag,
40 template<
typename E_,
typename I_,
typename P_,
typename R_>
43 template<
typename E_,
typename I_,
typename P_,
typename R_>
44 friend std::ostream & dash::operator<<(
53 typedef IndexType index_type;
54 typedef typename std::make_unsigned<index_type>::type size_type;
59 typedef IndexType difference_type;
60 typedef ElementType value_type;
61 typedef ElementType * pointer;
62 typedef ElementType & reference;
64 typedef internal::glob_dynamic_mem_bucket_type<size_type, value_type>
68 typedef typename std::list<bucket_type>
71 typedef typename bucket_list::iterator
74 typedef typename bucket_list::const_iterator
75 const_bucket_iterator;
78 template<
typename BucketIter>
80 const BucketIter & bucket_first,
81 const BucketIter & bucket_last,
83 const BucketIter & bucket_it,
84 index_type bucket_phase)
85 : _bucket_first(bucket_first),
86 _bucket_last(bucket_last),
88 _bucket_it(bucket_it),
89 _bucket_phase(bucket_phase)
92 template<
typename BucketIter>
94 const BucketIter & bucket_first,
95 const BucketIter & bucket_last,
97 : _bucket_first(bucket_first),
98 _bucket_last(bucket_last),
100 _bucket_it(bucket_first),
103 DASH_LOG_TRACE_VAR(
"GlobHeapLocalPtr(idx)", position);
104 #ifdef DASH_ENABLE_TRACE_LOGGING 105 index_type bucket_idx = 0;
107 for (_bucket_it = _bucket_first;
108 _bucket_it != _bucket_last; ++_bucket_it) {
109 if (position >= _bucket_it->size) {
110 position -= _bucket_it->size;
112 _bucket_phase = position;
115 #ifdef DASH_ENABLE_TRACE_LOGGING 119 DASH_LOG_TRACE(
"GlobHeapLocalPtr(idx) >",
120 "bucket:", bucket_idx,
121 "phase:", _bucket_phase);
127 : _bucket_first(other._bucket_first),
128 _bucket_last(other._bucket_last),
130 _bucket_it(other._bucket_it),
131 _bucket_phase(other._bucket_phase),
132 _is_nullptr(other._is_nullptr)
135 self_t & operator=(
const self_t & rhs)
137 if (
this != std::addressof(rhs)) {
138 _bucket_first = rhs._bucket_first;
139 _bucket_last = rhs._bucket_last;
141 _bucket_it = rhs._bucket_it;
142 _bucket_phase = rhs._bucket_phase;
143 _is_nullptr = rhs._is_nullptr;
151 template<
typename I_,
typename P_,
typename R_>
169 self_t & operator=(std::nullptr_t)
175 inline bool operator==(std::nullptr_t)
const 180 inline bool operator!=(std::nullptr_t)
const 190 DASH_ASSERT(!_is_nullptr);
191 if (_bucket_phase > _bucket_it->size) {
193 "dereferenced position " << _idx <<
" is out of range: " <<
194 "bucket phase: " << _bucket_phase <<
", " <<
195 "bucket size: " << _bucket_it->size);
197 return _bucket_it->lptr[_bucket_phase];
205 DASH_ASSERT(!_is_nullptr);
206 if (_bucket_phase + offset < _bucket_it->
size) {
208 return _bucket_it->lptr[_bucket_phase + offset];
211 for (
auto b_it = _bucket_it; b_it != _bucket_last; ++b_it) {
212 if (offset >= b_it->size) {
213 offset -= b_it->size;
214 }
else if (offset < b_it->size) {
215 return b_it->lptr[offset];
220 "dereferenced position " << _idx + offset <<
" " <<
221 "is out of range: pointer position: " << _idx <<
", " <<
222 "offset: " << offset);
232 explicit operator pointer()
const 234 DASH_LOG_TRACE(
"GlobHeapLocalPtr.pointer()");
235 pointer lptr =
nullptr;
237 DASH_LOG_TRACE(
"GlobHeapLocalPtr.pointer",
"is nullptr");
239 auto bucket_size = _bucket_it->size;
251 if (_bucket_it == _bucket_last) {
252 DASH_LOG_TRACE(
"GlobHeapLocalPtr.pointer",
"position at lend");
253 }
else if (_bucket_phase >= bucket_size) {
254 DASH_LOG_TRACE(
"GlobHeapLocalPtr.pointer",
255 "bucket size:", bucket_size,
",",
256 "bucket phase:", _bucket_phase);
257 DASH_LOG_TRACE(
"GlobHeapLocalPtr.pointer",
258 "note: iterator position out of bounds (lend?)");
260 lptr = _bucket_it->lptr + _bucket_phase;
262 DASH_LOG_TRACE_VAR(
"GlobHeapLocalPtr.pointer >", lptr);
266 self_t & operator++()
272 self_t & operator--()
278 self_t operator++(
int)
285 self_t operator--(
int)
292 self_t & operator+=(
int offset)
298 self_t & operator-=(
int offset)
304 self_t operator+(
int offset)
const 311 self_t operator-(
int offset)
const 318 inline index_type operator+(
319 const self_t & other)
const 321 return _idx + other._idx;
324 inline index_type operator-(
325 const self_t & other)
const 327 return _idx - other._idx;
330 template<
typename E_,
typename I_,
typename P_,
typename R_>
331 inline bool operator<(const GlobHeapLocalPtr<E_,I_,P_,R_> & other)
const 333 return (_idx < other._idx);
336 template<
typename E_,
typename I_,
typename P_,
typename R_>
337 inline bool operator<=(const GlobHeapLocalPtr<E_,I_,P_,R_> & other)
const 339 return (_idx <= other._idx);
342 template<
typename E_,
typename I_,
typename P_,
typename R_>
345 return (_idx > other._idx);
348 template<
typename E_,
typename I_,
typename P_,
typename R_>
351 return (_idx >= other._idx);
354 template<
typename E_,
typename I_,
typename P_,
typename R_>
357 return (
this == std::addressof(other) || _idx == other._idx);
360 template<
typename E_,
typename I_,
typename P_,
typename R_>
363 return !(*
this == other);
388 void increment(
int offset)
390 DASH_ASSERT(!_is_nullptr);
392 if (_bucket_phase + offset < _bucket_it->
size) {
394 _bucket_phase += offset;
397 for (; _bucket_it != _bucket_last; ++_bucket_it) {
398 if (offset >= _bucket_it->size) {
399 offset -= _bucket_it->size;
400 }
else if (offset < _bucket_it->size) {
401 _bucket_phase = offset;
407 if (_bucket_it == _bucket_last) {
408 _bucket_phase = offset;
415 void decrement(
int offset)
417 DASH_ASSERT(!_is_nullptr);
420 "offset " << offset <<
" is out of range");
423 if (offset <= _bucket_phase) {
425 _bucket_phase -= offset;
427 offset -= _bucket_phase;
429 for (; _bucket_it != _bucket_first; --_bucket_it) {
430 if (offset >= _bucket_it->size) {
431 offset -= _bucket_it->size;
432 }
else if (offset < _bucket_it->
size) {
433 _bucket_phase = _bucket_it->size - offset;
438 if (_bucket_it == _bucket_first) {
439 _bucket_phase = _bucket_it->size - offset;
443 "offset " << offset <<
" is out of range");
448 bucket_iterator _bucket_first;
449 bucket_iterator _bucket_last;
451 bucket_iterator _bucket_it;
452 index_type _bucket_phase = 0;
453 bool _is_nullptr =
false;
465 typename ElementType,
472 ElementType, IndexType, Pointer, Reference> &
first,
475 ElementType, IndexType, Pointer, Reference> & last)
482 typename ElementType,
486 std::ostream & operator<<(
489 ElementType, IndexType, Pointer, Reference> & it)
491 std::ostringstream ss;
492 auto * lptr =
static_cast<ElementType *
>(it);
493 ss <<
"dash::GlobHeapLocalPtr<" 494 <<
typeid(ElementType).name() <<
">" 496 <<
"idx:" << it._idx <<
", " 497 <<
"bp:" << it._bucket_phase <<
", " 500 return operator<<(os, ss.str());
505 #endif // DASH__MEMORY__GLOB_HEAP_LOCAL_PTR_H__INCLUDED 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 bool is_local() const
Whether the pointer references an element in local memory space.
index_type pos() const
Position of the pointer relative to its referenced memory space.
Iterator on local buckets.
std::random_access_iterator_tag iterator_category
Type definitions required for std::iterator_traits:
reference operator*()
Dereference operator.
reference operator[](index_type offset)
Random access operator.
dash::gptrdiff_t distance(GlobPtr< T, MemSpaceT > gbegin, GlobPtr< T, MemSpaceT > gend)
Returns the number of hops from gbegin to gend.