QMCPACK
SYCLAllocator< T, ALIGN > Class Template Reference

allocator for SYCL device memory T data type ALIGN alignment in bytes More...

+ Inheritance diagram for SYCLAllocator< T, ALIGN >:
+ Collaboration diagram for SYCLAllocator< T, ALIGN >:

Classes

struct  rebind
 

Public Types

typedef T value_type
 
typedef size_t size_type
 
typedef T * pointer
 
typedef const T * const_pointer
 

Public Member Functions

 SYCLAllocator ()=default
 
template<class U >
 SYCLAllocator (const SYCLAllocator< U > &)
 
T * allocate (std::size_t n)
 
void deallocate (T *p, std::size_t n)
 
void copyToDevice (T *device_ptr, T *host_ptr, size_t n)
 
void copyFromDevice (T *host_ptr, T *device_ptr, size_t n)
 
void copyDeviceToDevice (T *to_ptr, size_t n, T *from_ptr)
 

Static Public Member Functions

template<class U , class... Args>
static void construct (U *p, Args &&... args)
 Provide a construct for std::allocator_traits::contruct to call. More...
 
template<class U >
static void destroy (U *p)
 Give std::allocator_traits something to call. More...
 

Static Public Attributes

static constexpr size_t alignment = ALIGN
 

Detailed Description

template<typename T, size_t ALIGN = 64>
class qmcplusplus::SYCLAllocator< T, ALIGN >

allocator for SYCL device memory T data type ALIGN alignment in bytes

using this with something other than Ohmms containers? – use caution, write unit tests! – It's not tested beyond use in some unit tests using std::vector with constant size. SYCLAllocator appears to meet all the nonoptional requirements of a c++ Allocator.

Some of the default implementations in std::allocator_traits of optional Allocator requirements may cause runtime or compilation failures. They assume there is only one memory space and that the host has access to it.

Definition at line 97 of file SYCLallocator.hpp.


Class Documentation

◆ qmcplusplus::SYCLAllocator::rebind

struct qmcplusplus::SYCLAllocator::rebind

template<typename T, size_t ALIGN = 64>
template<class U>
struct qmcplusplus::SYCLAllocator< T, ALIGN >::rebind< U >

Definition at line 113 of file SYCLallocator.hpp.

+ Collaboration diagram for SYCLAllocator< T, ALIGN >::rebind< U >:
Class Members
typedef SYCLAllocator< U > other

Member Typedef Documentation

◆ const_pointer

typedef const T* const_pointer

Definition at line 103 of file SYCLallocator.hpp.

◆ pointer

typedef T* pointer

Definition at line 102 of file SYCLallocator.hpp.

◆ size_type

typedef size_t size_type

Definition at line 101 of file SYCLallocator.hpp.

◆ value_type

typedef T value_type

Definition at line 100 of file SYCLallocator.hpp.

Constructor & Destructor Documentation

◆ SYCLAllocator() [1/2]

SYCLAllocator ( )
default

◆ SYCLAllocator() [2/2]

SYCLAllocator ( const SYCLAllocator< U > &  )
inline

Definition at line 109 of file SYCLallocator.hpp.

110  {}

Member Function Documentation

◆ allocate()

T* allocate ( std::size_t  n)
inline

Definition at line 118 of file SYCLallocator.hpp.

119  {
120  T* pt = sycl::aligned_alloc_device<T>(ALIGN, n, getSYCLDefaultDeviceDefaultQueue());
121  SYCLallocator_device_mem_allocated += n * sizeof(T);
122  return pt;
123  }
sycl::queue & getSYCLDefaultDeviceDefaultQueue()
return a reference to the per-device default queue
Definition: SYCLruntime.cpp:18
std::atomic< size_t > SYCLallocator_device_mem_allocated

◆ construct()

static void construct ( U *  p,
Args &&...  args 
)
inlinestatic

Provide a construct for std::allocator_traits::contruct to call.

Don't do anything on construct, pointer p is on the device!

For example std::vector calls this to default initialize each element. You'll segfault if std::allocator_traits::construct tries doing that at p.

The standard is a bit confusing on this point. Implementing this is an optional requirement of Allocator from C++11 on, its not slated to be removed.

Its deprecated for the std::allocator in c++17 and will be removed in c++20. But we are not implementing std::allocator.

STL containers only use Allocators through allocator_traits and std::allocator_traits handles the case where no construct method is present in the Allocator. But std::allocator_traits will call the Allocators construct method if present.

Definition at line 148 of file SYCLallocator.hpp.

149  {}

◆ copyDeviceToDevice()

void copyDeviceToDevice ( T *  to_ptr,
size_t  n,
T *  from_ptr 
)
inline

Definition at line 169 of file SYCLallocator.hpp.

170  {
171  getSYCLDefaultDeviceDefaultQueue().memcpy(to_ptr, from_ptr, n * sizeof(T)).wait();
172  }
sycl::queue & getSYCLDefaultDeviceDefaultQueue()
return a reference to the per-device default queue
Definition: SYCLruntime.cpp:18

◆ copyFromDevice()

void copyFromDevice ( T *  host_ptr,
T *  device_ptr,
size_t  n 
)
inline

Definition at line 164 of file SYCLallocator.hpp.

Referenced by qmc_allocator_traits< qmcplusplus::SYCLAllocator< T > >::updateFrom().

165  {
166  getSYCLDefaultDeviceDefaultQueue().memcpy(host_ptr, device_ptr, n * sizeof(T)).wait();
167  }
sycl::queue & getSYCLDefaultDeviceDefaultQueue()
return a reference to the per-device default queue
Definition: SYCLruntime.cpp:18

◆ copyToDevice()

void copyToDevice ( T *  device_ptr,
T *  host_ptr,
size_t  n 
)
inline

Definition at line 159 of file SYCLallocator.hpp.

Referenced by qmc_allocator_traits< qmcplusplus::SYCLAllocator< T > >::updateTo().

160  {
161  getSYCLDefaultDeviceDefaultQueue().memcpy(device_ptr, host_ptr, n * sizeof(T)).wait();
162  }
sycl::queue & getSYCLDefaultDeviceDefaultQueue()
return a reference to the per-device default queue
Definition: SYCLruntime.cpp:18

◆ deallocate()

void deallocate ( T *  p,
std::size_t  n 
)
inline

Definition at line 125 of file SYCLallocator.hpp.

126  {
127  sycl::free(p, getSYCLDefaultDeviceDefaultQueue());
128  SYCLallocator_device_mem_allocated -= n * sizeof(T);
129  }
sycl::queue & getSYCLDefaultDeviceDefaultQueue()
return a reference to the per-device default queue
Definition: SYCLruntime.cpp:18
std::atomic< size_t > SYCLallocator_device_mem_allocated

◆ destroy()

static void destroy ( U *  p)
inlinestatic

Give std::allocator_traits something to call.

The default if this isn't present is to call p->~T() which we can't do on device memory.

Definition at line 156 of file SYCLallocator.hpp.

157  {}

Member Data Documentation

◆ alignment

constexpr size_t alignment = ALIGN
static

Definition at line 105 of file SYCLallocator.hpp.


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