QMCPACK
DualAllocator< T, DeviceAllocator, HostAllocator > Struct Template Reference

Generalizes the DualMemorySpace allocator This provides a limited alternative to OMPallocator for testing/benchmarking without dependence of OMPTarget/ offload. More...

+ Inheritance diagram for DualAllocator< T, DeviceAllocator, HostAllocator >:
+ Collaboration diagram for DualAllocator< T, DeviceAllocator, HostAllocator >:

Classes

struct  rebind
 

Public Types

using Value = typename HostAllocator::value_type
 
using Size = typename HostAllocator::size_type
 
using Pointer = typename HostAllocator::pointer
 
using ConstPointer = typename HostAllocator::const_pointer
 

Public Member Functions

 DualAllocator ()
 
 DualAllocator (const DualAllocator &)
 
DualAllocatoroperator= (const DualAllocator &)
 
template<class U , class V >
 DualAllocator (const DualAllocator< U, V > &)
 
Valueallocate (std::size_t n)
 
void deallocate (Value *pt, std::size_t n)
 
void attachReference (const DualAllocator &from, std::ptrdiff_t ptr_offset)
 
T * get_device_ptr ()
 
const T * get_device_ptr () const
 
DeviceAllocatorget_device_allocator ()
 
const DeviceAllocatorget_device_allocator () const
 

Private Attributes

HostAllocator allocator_
 
DeviceAllocator device_allocator_
 
T * device_ptr_
 

Detailed Description

template<typename T, class DeviceAllocator, class HostAllocator = std::allocator<T>>
struct qmcplusplus::DualAllocator< T, DeviceAllocator, HostAllocator >

Generalizes the DualMemorySpace allocator This provides a limited alternative to OMPallocator for testing/benchmarking without dependence of OMPTarget/ offload.

It does not provide an alternative to OMPtarget transfer semantics so many production objects will not be functional if it is used as the allocator for the data objects they depend on. If you use DualAllocator at this time you need to handle data transfer yourself.

Todo:
the OMPTarget allocation can be a "device" allocator comparable to CUDAAllocator Then OMPallocator can be replaced by a DualAllocator<T, OffloadAllocator<T>, PinnedAllocator<T>>

Definition at line 48 of file DualAllocator.hpp.


Class Documentation

◆ qmcplusplus::DualAllocator::rebind

struct qmcplusplus::DualAllocator::rebind

template<typename T, class DeviceAllocator, class HostAllocator = std::allocator<T>>
template<class U, class V>
struct qmcplusplus::DualAllocator< T, DeviceAllocator, HostAllocator >::rebind< U, V >

Definition at line 66 of file DualAllocator.hpp.

+ Collaboration diagram for DualAllocator< T, DeviceAllocator, HostAllocator >::rebind< U, V >:
Class Members
typedef DualAllocator< U, V > other

Member Typedef Documentation

◆ ConstPointer

using ConstPointer = typename HostAllocator::const_pointer

Definition at line 53 of file DualAllocator.hpp.

◆ Pointer

using Pointer = typename HostAllocator::pointer

Definition at line 52 of file DualAllocator.hpp.

◆ Size

using Size = typename HostAllocator::size_type

Definition at line 51 of file DualAllocator.hpp.

◆ Value

using Value = typename HostAllocator::value_type

Definition at line 50 of file DualAllocator.hpp.

Constructor & Destructor Documentation

◆ DualAllocator() [1/3]

DualAllocator ( )
inline

Definition at line 55 of file DualAllocator.hpp.

◆ DualAllocator() [2/3]

DualAllocator ( const DualAllocator< T, DeviceAllocator, HostAllocator > &  )
inline

Definition at line 56 of file DualAllocator.hpp.

◆ DualAllocator() [3/3]

DualAllocator ( const DualAllocator< U, V > &  )
inline

Definition at line 62 of file DualAllocator.hpp.

62  : device_ptr_(nullptr)
63  {}

Member Function Documentation

◆ allocate()

Value* allocate ( std::size_t  n)
inline

Definition at line 71 of file DualAllocator.hpp.

References DualAllocator< T, DeviceAllocator, HostAllocator >::allocator_, DualAllocator< T, DeviceAllocator, HostAllocator >::device_allocator_, DualAllocator< T, DeviceAllocator, HostAllocator >::device_ptr_, qmcplusplus::dual_device_mem_allocated, and qmcplusplus::n.

72  {
73  static_assert(std::is_same<T, Value>::value, "DualAllocator and HostAllocator data types must agree!");
74  if (device_ptr_ != nullptr)
75  throw std::runtime_error("DualAllocator does not support device reallocation");
76  Value* host_ptr = std::allocator_traits<HostAllocator>::allocate(allocator_, n);
77  device_ptr_ = std::allocator_traits<DeviceAllocator>::allocate(device_allocator_, n);
78  dual_device_mem_allocated += n * sizeof(T);
79  return host_ptr;
80  }
DeviceAllocator device_allocator_
std::atomic< size_t > dual_device_mem_allocated
typename HostAllocator::value_type Value

◆ attachReference()

void attachReference ( const DualAllocator< T, DeviceAllocator, HostAllocator > &  from,
std::ptrdiff_t  ptr_offset 
)
inline

◆ deallocate()

void deallocate ( Value pt,
std::size_t  n 
)
inline

Definition at line 82 of file DualAllocator.hpp.

References DualAllocator< T, DeviceAllocator, HostAllocator >::allocator_, DualAllocator< T, DeviceAllocator, HostAllocator >::device_allocator_, DualAllocator< T, DeviceAllocator, HostAllocator >::device_ptr_, qmcplusplus::dual_device_mem_allocated, and qmcplusplus::n.

83  {
84  dual_device_mem_allocated -= n * sizeof(T);
85  std::allocator_traits<DeviceAllocator>::deallocate(device_allocator_, device_ptr_, n);
86  std::allocator_traits<HostAllocator>::deallocate(allocator_, pt, n);
87  device_ptr_ = nullptr;
88  }
DeviceAllocator device_allocator_
std::atomic< size_t > dual_device_mem_allocated

◆ get_device_allocator() [1/2]

◆ get_device_allocator() [2/2]

const DeviceAllocator& get_device_allocator ( ) const
inline

◆ get_device_ptr() [1/2]

◆ get_device_ptr() [2/2]

const T* get_device_ptr ( ) const
inline

◆ operator=()

DualAllocator& operator= ( const DualAllocator< T, DeviceAllocator, HostAllocator > &  )
inline

Definition at line 57 of file DualAllocator.hpp.

References DualAllocator< T, DeviceAllocator, HostAllocator >::device_ptr_.

58  {
59  device_ptr_ = nullptr;
60  }

Member Data Documentation

◆ allocator_

◆ device_allocator_

◆ device_ptr_


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