Tensorium
Loading...
Searching...
No Matches
AlignedAllocator< T, Alignment > Struct Template Reference

Aligned memory allocator for high-performance computing. More...

#include <Allocator.hpp>

Collaboration diagram for AlignedAllocator< T, Alignment >:

Classes

struct  rebind
 Rebinding structure for allocator traits. More...

Public Types

using value_type = T
using pointer = T *
using const_pointer = const T *
using reference = T &
using const_reference = const T &
using size_type = std::size_t
using difference_type = std::ptrdiff_t

Public Member Functions

 AlignedAllocator () noexcept=default
 Conversion constructor from another allocator of different type.
template<typename U>
 AlignedAllocator (const AlignedAllocator< U, Alignment > &) noexcept
 Default constructor.
T * allocate (std::size_t n, int node_id=-1)
 Allocates aligned memory for n elements of type T.
void deallocate (T *p, std::size_t n) noexcept
 Deallocates memory previously allocated with allocate().

Detailed Description

template<typename T, std::size_t Alignment>
struct AlignedAllocator< T, Alignment >

Aligned memory allocator for high-performance computing.

This allocator provides memory aligned to a specified boundary, ensuring compatibility with SIMD instructions and optimal cache usage. On Intel KNL (Knights Landing) architectures, this allocator automatically uses the high-bandwidth MCDRAM via hbw_posix_memalign if the macro USE_KNL is defined. Otherwise, standard posix_memalign is used.

This is particularly useful for vectorized numerical libraries, where memory alignment is essential for instruction-level parallelism (e.g., AVX, SSE, AVX-512).

Template Parameters
TType of the objects being allocated.
AlignmentMemory alignment in bytes. Must be a power of two and compatible with the ISA in use (e.g., 32 for AVX256).

Member Typedef Documentation

◆ const_pointer

template<typename T, std::size_t Alignment>
using AlignedAllocator< T, Alignment >::const_pointer = const T *

◆ const_reference

template<typename T, std::size_t Alignment>
using AlignedAllocator< T, Alignment >::const_reference = const T &

◆ difference_type

template<typename T, std::size_t Alignment>
using AlignedAllocator< T, Alignment >::difference_type = std::ptrdiff_t

◆ pointer

template<typename T, std::size_t Alignment>
using AlignedAllocator< T, Alignment >::pointer = T *

◆ reference

template<typename T, std::size_t Alignment>
using AlignedAllocator< T, Alignment >::reference = T &

◆ size_type

template<typename T, std::size_t Alignment>
using AlignedAllocator< T, Alignment >::size_type = std::size_t

◆ value_type

template<typename T, std::size_t Alignment>
using AlignedAllocator< T, Alignment >::value_type = T

Constructor & Destructor Documentation

◆ AlignedAllocator() [1/2]

template<typename T, std::size_t Alignment>
AlignedAllocator< T, Alignment >::AlignedAllocator ( )
defaultnoexcept

Conversion constructor from another allocator of different type.

Required by the STL allocator model. Does nothing as this allocator is stateless.

Template Parameters
UOther type.
Parameters
otherThe other allocator.

◆ AlignedAllocator() [2/2]

template<typename T, std::size_t Alignment>
template<typename U>
AlignedAllocator< T, Alignment >::AlignedAllocator ( const AlignedAllocator< U, Alignment > & )
inlinenoexcept

Default constructor.

Stateless and noexcept.

Member Function Documentation

◆ allocate()

template<typename T, std::size_t Alignment>
T * AlignedAllocator< T, Alignment >::allocate ( std::size_t n,
int node_id = -1 )
inlinenodiscard

Allocates aligned memory for n elements of type T.

The alignment is guaranteed to be at least Alignment bytes. On KNL platforms, high-bandwidth memory (HBM) will be used via libhbw.

On NUMA systems, memory is allocated on the node local to the calling thread.

Parameters
nNumber of elements to allocate.
node_idNUMA node to allocate on (-1 = auto/local or no NUMA).
Returns
T* Pointer to aligned memory block.
Exceptions
std::bad_allocIf memory allocation fails.

◆ deallocate()

template<typename T, std::size_t Alignment>
void AlignedAllocator< T, Alignment >::deallocate ( T * p,
std::size_t n )
inlinenoexcept

Deallocates memory previously allocated with allocate().

On KNL platforms, this calls hbw_free. On NUMA systems, it uses numa_free(). Otherwise, standard free() is used.

Parameters
pPointer to memory to deallocate.
nNumber of elements.

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