Tensorium
Loading...
Searching...
No Matches
Allocator.hpp
Go to the documentation of this file.
1#pragma once
2#include "SIMD.hpp"
3#include <iostream>
4#include <cmath>
5#include <vector>
6
7#if defined(USE_KNL)
8#include <hbwmalloc.h>
9#endif
23template <typename T, std::size_t Alignment>
25 using value_type = T;
26 using pointer = T*;
27 using const_pointer = const T*;
28 using reference = T&;
29 using const_reference = const T&;
30 using size_type = std::size_t;
31 using difference_type = std::ptrdiff_t;
32
41 template <typename U>
53 AlignedAllocator() noexcept = default;
54 template <typename U>
60 AlignedAllocator(const AlignedAllocator<U, Alignment>&) noexcept {}
72 [[nodiscard]] T* allocate(std::size_t n) {
73 void* ptr = nullptr;
74 const std::size_t alloc_size = n * sizeof(T) + Alignment;
75
76#if defined(USE_KNL)
77 if (hbw_posix_memalign(&ptr, alignment, alloc_size) != 0)
78 throw std::bad_alloc();
79#else
80 if (posix_memalign(&ptr, Alignment, alloc_size) != 0)
81 throw std::bad_alloc();
82#endif
83 return reinterpret_cast<T*>(ptr);
84 }
93 void deallocate(T* p, std::size_t) noexcept {
94#if defined(USE_KNL)
95 hbw_free(p);
96#else
97 free(p);
98#endif
99 }
100};
110template<typename K>
111using aligned_vector = std::vector<K, AlignedAllocator<K, ALIGN>>;
112
124template <typename T, std::size_t Alignment>
126 return true;
127}
138template <typename T, std::size_t Alignment>
140 return false;
141}
142
std::vector< K, AlignedAllocator< K, ALIGN > > aligned_vector
Type alias for a std::vector with aligned memory allocation.
Definition Allocator.hpp:111
bool operator!=(const AlignedAllocator< T, Alignment > &, const AlignedAllocator< T, Alignment > &) noexcept
Inequality operator for AlignedAllocator.
Definition Allocator.hpp:139
bool operator==(const AlignedAllocator< T, Alignment > &, const AlignedAllocator< T, Alignment > &) noexcept
Equality operator for AlignedAllocator.
Definition Allocator.hpp:125
Rebinding structure for allocator traits.
Definition Allocator.hpp:42
Aligned memory allocator for high-performance computing.
Definition Allocator.hpp:24
T value_type
Definition Allocator.hpp:25
T * pointer
Definition Allocator.hpp:26
std::size_t size_type
Definition Allocator.hpp:30
std::ptrdiff_t difference_type
Definition Allocator.hpp:31
T * allocate(std::size_t n)
Allocates aligned memory for n elements of type T.
Definition Allocator.hpp:72
T & reference
Definition Allocator.hpp:28
void deallocate(T *p, std::size_t) noexcept
Deallocates memory previously allocated with allocate().
Definition Allocator.hpp:93
AlignedAllocator() noexcept=default
Conversion constructor from another allocator of different type.
const T & const_reference
Definition Allocator.hpp:29
const T * const_pointer
Definition Allocator.hpp:27