DASH  0.3.0
dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc > Class Template Reference

Encapsulates a memory allocation and deallocation strategy of global memory regions distributed across local memory of units in a specified team. More...

#include <EpochSynchronizedAllocator.h>

Public Types

using local_allocator_type = LocalAlloc< ElementType >
 
using value_type = ElementType
 Allocator Traits. More...
 
using size_type = dash::default_size_t
 
using difference_type = dash::default_index_t
 
using pointer = dart_gptr_t
 
using const_pointer = dart_gptr_t const
 
using void_pointer = dart_gptr_t
 
using const_void_pointer = dart_gptr_t const
 
using local_pointer = typename allocator_traits::pointer
 
using const_local_pointer = typename allocator_traits::const_pointer
 
using local_void_pointer = typename allocator_traits::void_pointer
 
using const_local_void_pointer = typename allocator_traits::const_void_pointer
 
using allocation_policy = std::integral_constant< global_allocation_policy, AllocationPolicy >
 
template<class U >
using rebind = dash::EpochSynchronizedAllocator< U, LMemSpace, AllocationPolicy, LocalAlloc >
 Convert EpochSynchronizedAllocator<T> to EpochSynchronizedAllocator<U>. More...
 

Public Member Functions

 EpochSynchronizedAllocator (Team const &team, LMemSpace *r=nullptr) noexcept
 Constructor. More...
 
 EpochSynchronizedAllocator (const self_t &other) noexcept
 Copy constructor. More...
 
self_toperator= (const self_t &other) noexcept
 Copy Assignment operator. More...
 
 EpochSynchronizedAllocator (self_t &&other) noexcept
 Move-constructor. More...
 
self_toperator= (self_t &&other) noexcept
 Move-assignment operator. More...
 
void swap (self_t &other) noexcept
 Swap two collective Allocators. More...
 
 ~EpochSynchronizedAllocator () noexcept
 Destructor. More...
 
size_type max_size () const noexcept
 Estimate the largest supported size. More...
 
pointer attach (local_pointer lptr, size_type num_local_elem)
 Register pre-allocated local memory segment of num_local_elem elements in global memory space. More...
 
void detach (pointer gptr, size_type num_local_elem)
 Unregister local memory segment from global memory space. More...
 
local_pointer allocate_local (size_type num_local_elem)
 Allocates num_local_elem local elements in the active unit's local memory. More...
 
void deallocate_local (local_pointer lptr, size_type num_local_elem)
 Deallocates memory segment in the active unit's local memory. More...
 
pointer allocate (size_type num_local_elem)
 Allocates a global memory segment with at least num_local_elem bytes. More...
 
void deallocate (pointer gptr, size_type num_local_elem)
 Deallocates a memory segment from global memory space. More...
 
local_allocator_type get_local_allocator () const noexcept
 Returns a copy of the local allocator object associated with the vector. More...
 

Friends

template<class T , class U >
bool operator== (const EpochSynchronizedAllocator< T, LMemSpace, AllocationPolicy, LocalAlloc > &lhs, const EpochSynchronizedAllocator< U, LMemSpace, AllocationPolicy, LocalAlloc > &rhs)
 
template<class T , class U >
bool operator!= (const EpochSynchronizedAllocator< T, LMemSpace, AllocationPolicy, LocalAlloc > &lhs, const EpochSynchronizedAllocator< U, LMemSpace, AllocationPolicy, LocalAlloc > &rhs)
 

Detailed Description

template<typename ElementType, typename LMemSpace = dash::HostSpace, global_allocation_policy AllocationPolicy = global_allocation_policy::epoch_synchronized, template< class T > class LocalAlloc = allocator::DefaultAllocator>
class dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >

Encapsulates a memory allocation and deallocation strategy of global memory regions distributed across local memory of units in a specified team.

Satisfied STL concepts:

  • Allocator
  • CopyAssignable

    Implemented concept:
    DashAllocatorConcept

Definition at line 45 of file EpochSynchronizedAllocator.h.

Member Typedef Documentation

◆ rebind

template<typename ElementType , typename LMemSpace = dash::HostSpace, global_allocation_policy AllocationPolicy = global_allocation_policy::epoch_synchronized, template< class T > class LocalAlloc = allocator::DefaultAllocator>
template<class U >
using dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::rebind = dash:: EpochSynchronizedAllocator<U, LMemSpace, AllocationPolicy, LocalAlloc>

Convert EpochSynchronizedAllocator<T> to EpochSynchronizedAllocator<U>.

Definition at line 121 of file EpochSynchronizedAllocator.h.

◆ value_type

template<typename ElementType , typename LMemSpace = dash::HostSpace, global_allocation_policy AllocationPolicy = global_allocation_policy::epoch_synchronized, template< class T > class LocalAlloc = allocator::DefaultAllocator>
using dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::value_type = ElementType

Allocator Traits.

Definition at line 100 of file EpochSynchronizedAllocator.h.

Constructor & Destructor Documentation

◆ EpochSynchronizedAllocator() [1/3]

template<typename ElementType , typename LMemSpace , global_allocation_policy AllocationPolicy, template< class > class LocalAlloc>
dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::EpochSynchronizedAllocator ( Team const &  team,
LMemSpace *  r = nullptr 
)
explicitnoexcept

Constructor.

Creates a new instance of dash::EpochSynchronizedAllocator for a given team.

Definition at line 347 of file EpochSynchronizedAllocator.h.

Referenced by dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::get_local_allocator().

348  : _team(&team)
349  , _alloc(
350  r ? r
351  : static_cast<LMemSpace *>(
353  memory_domain_local,
354  typename memory_traits::memory_space_type_category>()))
355  , _policy{}
356 {
357  DASH_LOG_DEBUG(
358  "EpochSynchronizedAllocator.SymmetricAllocator(team, alloc) >");
359  _segments.reserve(1);
360 }
MemorySpace< MSpaceDomainCategory, MSpaceTypeCategory > * get_default_memory_space()
Forward declarations.
Definition: MemorySpace.h:29

◆ EpochSynchronizedAllocator() [2/3]

template<typename ElementType , typename LMemSpace , global_allocation_policy AllocationPolicy, template< class > class LocalAlloc>
dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::EpochSynchronizedAllocator ( const self_t other)
noexcept

Copy constructor.

See also
DashAllocatorConcept

Definition at line 371 of file EpochSynchronizedAllocator.h.

References dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::operator=().

372 {
373  operator=(other);
374 }
self_t & operator=(const self_t &other) noexcept
Copy Assignment operator.

◆ EpochSynchronizedAllocator() [3/3]

template<typename ElementType , typename LMemSpace , global_allocation_policy AllocationPolicy, template< class > class LocalAlloc>
dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::EpochSynchronizedAllocator ( self_t &&  other)
noexcept

Move-constructor.

Takes ownership of the moved instance's allocation.

Definition at line 385 of file EpochSynchronizedAllocator.h.

386  : _team(other._team)
387  , _alloc(std::move(other._alloc))
388  , _segments(std::move(other._segments))
389  , _policy(other._policy)
390 {
391  other.clear();
392 }

◆ ~EpochSynchronizedAllocator()

template<typename ElementType , typename LMemSpace , global_allocation_policy AllocationPolicy, template< class > class LocalAlloc>
dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::~EpochSynchronizedAllocator ( )
noexcept

Destructor.

Frees all global memory regions allocated by this allocator instance.

Definition at line 465 of file EpochSynchronizedAllocator.h.

466 {
467  clear();
468 }

Member Function Documentation

◆ allocate()

template<typename ElementType , typename LMemSpace , global_allocation_policy AllocationPolicy, template< class > class LocalAlloc>
EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::pointer dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::allocate ( size_type  num_local_elem)

Allocates a global memory segment with at least num_local_elem bytes.

It is semantically identical to call allocate_local (obtaining a local pointer) and subsequently attaching it to obtain a global pointer. it. Collective operation.

See also
DashAllocatorConcept

Definition at line 693 of file EpochSynchronizedAllocator.h.

References DART_GPTR_ISNULL.

Referenced by dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::max_size().

694 {
695  DASH_LOG_DEBUG(
696  "EpochSynchronizedAllocator.allocate",
697  "num_local_elem",
698  num_local_elem);
699 
700  auto it = do_local_allocate(num_local_elem);
701 
702  DASH_ASSERT_MSG(it->lptr() != nullptr, "local pointer must not be NULL");
703 
704  auto gptr = _policy.do_global_attach(
705  _team->dart_id(), it->lptr(), num_local_elem * sizeof(ElementType));
706 
707  DASH_ASSERT_MSG(!DART_GPTR_ISNULL(gptr), "gptr must not be null");
708 
709  it->gptr() = gptr;
710 
711  return gptr;
712 }
#define DART_GPTR_ISNULL(gptr_)
Test for NULL global pointer.
Definition: dart_globmem.h:118
dart_team_t dart_id() const
Index of this team relative to global team dash::Team::All().
Definition: Team.h:522

◆ allocate_local()

template<typename ElementType , typename LMemSpace , global_allocation_policy AllocationPolicy, template< class > class LocalAlloc>
EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::local_pointer dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::allocate_local ( size_type  num_local_elem)

Allocates num_local_elem local elements in the active unit's local memory.

Local operation.

See also
DashEpochSynchronizedAllocatorConcept

Definition at line 501 of file EpochSynchronizedAllocator.h.

References dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::deallocate_local().

Referenced by dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::max_size().

502 {
503  DASH_LOG_DEBUG(
504  "SymmetricAllocator.allocate_local(n)",
505  "number of local values:",
506  num_local_elem);
507 
508  auto it = do_local_allocate(num_local_elem);
509 
510  DASH_ASSERT_MSG(it->lptr() != nullptr, "invalid pointer");
511 
512  return it->lptr();
513 }

◆ attach()

template<typename ElementType , typename LMemSpace , global_allocation_policy AllocationPolicy, template< class > class LocalAlloc>
EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::pointer dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::attach ( local_pointer  lptr,
size_type  num_local_elem 
)

Register pre-allocated local memory segment of num_local_elem elements in global memory space.

Collective operation. The number of allocated elements may differ between units.

See also
DashEpochSynchronizedAllocatorConcept

Definition at line 573 of file EpochSynchronizedAllocator.h.

References DART_GPTR_ISNULL, DART_GPTR_NULL, and dash::end().

Referenced by dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::max_size().

574 {
575  DASH_LOG_DEBUG(
576  "EpochSynchronizedAllocator.attach",
577  "local_pointer:",
578  lptr,
579  "length:",
580  num_local_elem);
581 
582  auto pos = lookup_segment_by_lptr(lptr);
583 
584  if (pos == std::end(_segments)) {
585  DASH_LOG_ERROR(
586  "EpochSynchronizedAllocator.attach", "Invalid local pointer", lptr);
587  return DART_GPTR_NULL;
588  }
589 
590  DASH_ASSERT_EQ(num_local_elem, pos->length(), "invalid block length");
591 
592  auto gptr = _policy.do_global_attach(
593  _team->dart_id(), pos->lptr(), pos->length() * sizeof(ElementType));
594 
595  DASH_ASSERT_MSG(!DART_GPTR_ISNULL(gptr), "gptr must not be null");
596 
597  pos->gptr() = gptr;
598 
599  return gptr;
600 }
constexpr auto end(RangeType &&range) -> decltype(std::forward< RangeType >(range).end())
Definition: Range.h:98
#define DART_GPTR_NULL
A NULL global pointer.
Definition: dart_globmem.h:105
#define DART_GPTR_ISNULL(gptr_)
Test for NULL global pointer.
Definition: dart_globmem.h:118
dart_team_t dart_id() const
Index of this team relative to global team dash::Team::All().
Definition: Team.h:522

◆ deallocate()

template<typename ElementType , typename LMemSpace , global_allocation_policy AllocationPolicy, template< class > class LocalAlloc>
void dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::deallocate ( pointer  gptr,
size_type  num_local_elem 
)

Deallocates a memory segment from global memory space.

It is semantically identical to first call detach on the specified global pointer and subsequently locally deallocate it.

Collective operation.

See also
DashAllocatorConcept

Definition at line 648 of file EpochSynchronizedAllocator.h.

References dash::begin(), DART_GPTR_EQUAL, DART_GPTR_ISNULL, dash::end(), and dash::find_if().

Referenced by dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::get_local_allocator(), and dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::max_size().

649 {
650  DASH_LOG_DEBUG(
651  "EpochSynchronizedAllocator.deallocate", "deallocate local memory");
652 
653  if (DART_GPTR_ISNULL(gptr)) {
654  return;
655  }
656 
657  auto pos = std::find_if(
658  std::begin(_segments),
659  std::end(_segments),
660  [gptr](const allocation_rec_t &a) {
661  return DART_GPTR_EQUAL(a.gptr(), gptr);
662  });
663 
664  if (pos == std::end(_segments)) {
665  DASH_LOG_ERROR(
666  "EpochSynchronizedAllocator.detach", "Invalid global pointer", gptr);
667  return;
668  }
669 
670  DASH_ASSERT_EQ(pos->length(), num_local_elem, "invalid block length");
671 
672  _policy.do_global_detach(pos->gptr());
673 
674  allocator_traits::deallocate(_alloc, pos->lptr(), pos->length());
675 
676  _segments.erase(pos);
677 }
constexpr auto end(RangeType &&range) -> decltype(std::forward< RangeType >(range).end())
Definition: Range.h:98
constexpr auto begin(RangeType &&range) -> decltype(std::forward< RangeType >(range).begin())
Definition: Range.h:89
GlobIter find_if(GlobIter first, GlobIter last, UnaryPredicate predicate)
Returns an iterator to the first element in the range [first,last) that satisfies the predicate p...
Definition: Find.h:104
#define DART_GPTR_ISNULL(gptr_)
Test for NULL global pointer.
Definition: dart_globmem.h:118
#define DART_GPTR_EQUAL(gptr1_, gptr2_)
Compare two global pointers.
Definition: dart_globmem.h:128

◆ deallocate_local()

template<typename ElementType , typename LMemSpace , global_allocation_policy AllocationPolicy, template< class > class LocalAlloc>
void dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::deallocate_local ( local_pointer  lptr,
size_type  num_local_elem 
)

Deallocates memory segment in the active unit's local memory.

Local operation.

See also
DashEpochSynchronizedAllocatorConcept

Definition at line 525 of file EpochSynchronizedAllocator.h.

References DART_GPTR_ISNULL, and dash::end().

Referenced by dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::allocate_local(), and dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::max_size().

526 {
527  DASH_LOG_DEBUG(
528  "EpochSynchronizedAllocator.deallocate_local(n)",
529  "local pointer",
530  lptr,
531  "length:",
532  num_local_elem);
533 
534  auto pos = lookup_segment_by_lptr(lptr);
535 
536  if (pos == std::end(_segments)) {
537  DASH_LOG_ERROR(
538  "EpochSynchronizedAllocator.deallocate_local",
539  "invalid local pointer",
540  lptr);
541  return;
542  }
543 
544  DASH_ASSERT_EQ(pos->length(), num_local_elem, "Invalid block length");
545 
546  if (!DART_GPTR_ISNULL(pos->gptr())) {
547  DASH_LOG_ERROR(
548  "EpochSynchronizedAllocator.deallocate_local",
549  "local pointer must not be attached to global memory",
550  lptr);
551  return;
552  }
553 
554  allocator_traits::deallocate(_alloc, pos->lptr(), pos->length());
555 
556  _segments.erase(pos);
557 }
constexpr auto end(RangeType &&range) -> decltype(std::forward< RangeType >(range).end())
Definition: Range.h:98
#define DART_GPTR_ISNULL(gptr_)
Test for NULL global pointer.
Definition: dart_globmem.h:118

◆ detach()

template<typename ElementType , typename LMemSpace , global_allocation_policy AllocationPolicy, template< class > class LocalAlloc>
void dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::detach ( pointer  gptr,
size_type  num_local_elem 
)

Unregister local memory segment from global memory space.

Does not deallocate local memory.

Collective operation.

See also
DashEpochSynchronizedAllocatorConcept

Definition at line 611 of file EpochSynchronizedAllocator.h.

References DART_GPTR_ISNULL, DART_GPTR_NULL, and dash::end().

Referenced by dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::max_size().

612 {
613  DASH_LOG_DEBUG(
614  "EpochSynchronizedAllocator.detach",
615  "global pointer:",
616  gptr,
617  "length:",
618  num_local_elem);
619 
620  if (DART_GPTR_ISNULL(gptr)) {
621  return;
622  }
623 
624  auto pos = lookup_segment_by_gptr(gptr);
625 
626  if (pos == std::end(_segments)) {
627  DASH_LOG_ERROR(
628  "EpochSynchronizedAllocator.detach", "Invalid global pointer", gptr);
629  return;
630  }
631 
632  DASH_ASSERT_EQ(num_local_elem, pos->length(), "invalid block length");
633 
634  _policy.do_global_detach(pos->gptr());
635 
636  pos->gptr() = DART_GPTR_NULL;
637 }
constexpr auto end(RangeType &&range) -> decltype(std::forward< RangeType >(range).end())
Definition: Range.h:98
#define DART_GPTR_NULL
A NULL global pointer.
Definition: dart_globmem.h:105
#define DART_GPTR_ISNULL(gptr_)
Test for NULL global pointer.
Definition: dart_globmem.h:118

◆ get_local_allocator()

template<typename ElementType , typename LMemSpace = dash::HostSpace, global_allocation_policy AllocationPolicy = global_allocation_policy::epoch_synchronized, template< class T > class LocalAlloc = allocator::DefaultAllocator>
local_allocator_type dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::get_local_allocator ( ) const
inlinenoexcept

◆ max_size()

template<typename ElementType , typename LMemSpace = dash::HostSpace, global_allocation_policy AllocationPolicy = global_allocation_policy::epoch_synchronized, template< class T > class LocalAlloc = allocator::DefaultAllocator>
size_type dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::max_size ( ) const
inlinenoexcept

◆ operator=() [1/2]

template<typename ElementType , typename LMemSpace , global_allocation_policy AllocationPolicy, template< class > class LocalAlloc>
EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::self_t & dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::operator= ( const self_t other)
noexcept

Copy Assignment operator.

See also
DashAllocatorConcept

Definition at line 408 of file EpochSynchronizedAllocator.h.

Referenced by dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::EpochSynchronizedAllocator(), and dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::operator=().

409 {
410  if (&other == this) {
411  return *this;
412  }
413 
414  _alloc = other._alloc;
415 
416  _team = other._team;
417 
418  _policy = other._policy;
419 
420  return *this;
421 }

◆ operator=() [2/2]

template<typename ElementType , typename LMemSpace , global_allocation_policy AllocationPolicy, template< class > class LocalAlloc>
EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::self_t & dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::operator= ( self_t &&  other)
noexcept

Move-assignment operator.

Definition at line 437 of file EpochSynchronizedAllocator.h.

References dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::operator=(), and dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::swap().

438 {
439  if (&other == this) {
440  return *this;
441  }
442 
443  if (_alloc == other._alloc) {
444  // If the local allocators equal each other we can move everything
445  clear();
446  swap(other);
447  }
448  else {
449  // otherwise we do not touch any data and copy assign it
450  operator=(other);
451  }
452 
453  return *this;
454 }
void swap(self_t &other) noexcept
Swap two collective Allocators.
self_t & operator=(const self_t &other) noexcept
Copy Assignment operator.

◆ swap()

template<typename ElementType , typename LMemSpace , global_allocation_policy AllocationPolicy, template< class > class LocalAlloc>
void dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::swap ( self_t other)
noexcept

Swap two collective Allocators.

Definition at line 479 of file EpochSynchronizedAllocator.h.

Referenced by dash::EpochSynchronizedAllocator< ElementType, LMemSpace, AllocationPolicy, LocalAlloc >::operator=().

480 {
481  std::swap(_team, other._team);
482  std::swap(_alloc, other._alloc);
483  std::swap(_segments, other._segments);
484  std::swap(_policy, other._policy);
485 }

The documentation for this class was generated from the following file: