DPC++ Runtime
Runtime libraries for oneAPI DPC++
aligned_allocator.hpp
Go to the documentation of this file.
1 //==------------ aligned_allocator.hpp - SYCL standard header file ---------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
11 #include <sycl/detail/os_util.hpp> // for OSUtil
12 
13 #include <limits> // for numeric_limits
14 #include <memory> // for pointer_traits, allocator_traits
15 #include <new> // for bad_alloc, operator new
16 #include <stddef.h> // for size_t
17 #include <type_traits> // for false_type, is_empty, make_unsign...
18 
19 namespace sycl {
20 inline namespace _V1 {
21 namespace detail {
22 template <typename T> class aligned_allocator {
23 public:
24  using value_type = T;
25  using pointer = T *;
26  using const_pointer = const T *;
27  using reference = T &;
28  using const_reference = const T &;
29 
30 public:
31  template <typename U> struct rebind {
33  };
34 
35  aligned_allocator() = default;
36  ~aligned_allocator() = default;
37 
38  explicit aligned_allocator(size_t Alignment) : MAlignment(Alignment) {}
39 
40  // Construct an object
42  new (Ptr) value_type(Val);
43  }
44 
45  // Destroy an object
46  void destroy(pointer Ptr) { Ptr->~value_type(); }
47 
48  pointer address(reference Val) const { return &Val; }
49  const_pointer address(const_reference Val) { return &Val; }
50 
51  // Allocate memory aligned to Alignment
52  pointer allocate(size_t Size) {
53  size_t NumBytes = Size * sizeof(value_type);
54  NumBytes = ((NumBytes - 1) | (MAlignment - 1)) + 1;
55  if (Size > NumBytes)
56  throw std::bad_alloc();
57 
58  pointer Result = reinterpret_cast<pointer>(
59  detail::OSUtil::alignedAlloc(MAlignment, NumBytes));
60  if (!Result)
61  throw std::bad_alloc();
62  return Result;
63  }
64 
65  // Release allocated memory
66  void deallocate(pointer Ptr, size_t) {
67  if (Ptr)
69  }
70 
71  bool operator==(const aligned_allocator &) { return true; }
72  bool operator!=(const aligned_allocator &) { return false; }
73 
74  void setAlignment(size_t Alignment) { MAlignment = Alignment; }
75 
76 private:
77  // By default assume the "worst" case
78  size_t MAlignment = 128;
79 };
80 } // namespace detail
81 } // namespace _V1
82 } // namespace sycl
83 
84 namespace std {
85 template <typename T>
86 struct allocator_traits<sycl::detail::aligned_allocator<T>> {
87  using allocator_type = typename sycl::detail::aligned_allocator<T>;
88  using value_type = typename allocator_type::value_type;
89  using pointer = typename allocator_type::pointer;
90  using const_pointer = typename allocator_type::const_pointer;
91  using void_pointer =
92  typename std::pointer_traits<pointer>::template rebind<void>;
93  using const_void_pointer =
94  typename std::pointer_traits<pointer>::template rebind<const void>;
95  using difference_type =
97  using size_type = std::make_unsigned_t<difference_type>;
98  using propagate_on_container_copy_assignment = std::false_type;
99  using propagate_on_container_move_assignment = std::false_type;
100  using propagate_on_container_swap = std::false_type;
101  using is_always_equal = typename std::is_empty<allocator_type>::type;
102 
103  template <typename U>
104  using rebind_alloc =
105  typename sycl::detail::aligned_allocator<T>::template rebind<U>::other;
106  template <typename U> using rebind_traits = allocator_traits<rebind_alloc<U>>;
107 
108  static pointer allocate(allocator_type &Allocator, size_type NumElems) {
109  return Allocator.allocate(NumElems);
110  }
111 
112  static pointer allocate(allocator_type &Allocator, size_type NumElems,
114  // TODO: Utilize the locality hint argument.
115  return Allocator.allocate(NumElems);
116  }
117 
118  static void deallocate(allocator_type &Allocator, pointer Ptr,
119  size_type NumElems) {
120  Allocator.deallocate(Ptr, NumElems);
121  }
122 
123  template <class U, class... ArgsT>
124  static void construct(allocator_type &Allocator, U *Ptr, ArgsT &&...Args) {
125  return Allocator.construct(Ptr, Args...);
126  }
127 
128  template <class U> static void destroy(allocator_type &Allocator, U *Ptr) {
129  Allocator.destroy(Ptr);
130  }
131 
132  static size_type max_size(const allocator_type &) noexcept {
133  // max is a macro on Windows...
134  return (std::numeric_limits<size_type>::max)() / sizeof(value_type);
135  }
136 
137  static allocator_type
139  return Allocator;
140  }
141 };
142 } // namespace std
sycl::_V1::detail::aligned_allocator::construct
void construct(pointer Ptr, const_reference Val)
Definition: aligned_allocator.hpp:41
std::allocator_traits< sycl::detail::aligned_allocator< T > >::allocate
static pointer allocate(allocator_type &Allocator, size_type NumElems)
Definition: aligned_allocator.hpp:108
std::allocator_traits< sycl::detail::aligned_allocator< T > >::void_pointer
typename std::pointer_traits< pointer >::template rebind< void > void_pointer
Definition: aligned_allocator.hpp:92
std::allocator_traits< sycl::detail::aligned_allocator< T > >::construct
static void construct(allocator_type &Allocator, U *Ptr, ArgsT &&...Args)
Definition: aligned_allocator.hpp:124
std::allocator_traits< sycl::detail::aligned_allocator< T > >::value_type
typename allocator_type::value_type value_type
Definition: aligned_allocator.hpp:88
sycl::_V1::detail::aligned_allocator::~aligned_allocator
~aligned_allocator()=default
std::allocator_traits< sycl::detail::aligned_allocator< T > >::destroy
static void destroy(allocator_type &Allocator, U *Ptr)
Definition: aligned_allocator.hpp:128
os_util.hpp
detail
---— Error handling, matching OpenCL plugin semantics.
Definition: common.hpp:44
sycl
Definition: access.hpp:18
std::allocator_traits< sycl::detail::aligned_allocator< T > >::propagate_on_container_move_assignment
std::false_type propagate_on_container_move_assignment
Definition: aligned_allocator.hpp:99
max
simd< _Tp, _Abi > max(const simd< _Tp, _Abi > &, const simd< _Tp, _Abi > &) noexcept
std::allocator_traits< sycl::detail::aligned_allocator< T > >::size_type
std::make_unsigned_t< difference_type > size_type
Definition: aligned_allocator.hpp:97
std::allocator_traits< sycl::detail::aligned_allocator< T > >::const_void_pointer
typename std::pointer_traits< pointer >::template rebind< const void > const_void_pointer
Definition: aligned_allocator.hpp:94
std::allocator_traits< sycl::detail::aligned_allocator< T > >::rebind_alloc
typename sycl::detail::aligned_allocator< T >::template rebind< U >::other rebind_alloc
Definition: aligned_allocator.hpp:105
sycl::_V1::detail::aligned_allocator::setAlignment
void setAlignment(size_t Alignment)
Definition: aligned_allocator.hpp:74
std::allocator_traits< sycl::detail::aligned_allocator< T > >::max_size
static size_type max_size(const allocator_type &) noexcept
Definition: aligned_allocator.hpp:132
sycl::_V1::detail::aligned_allocator::operator!=
bool operator!=(const aligned_allocator &)
Definition: aligned_allocator.hpp:72
sycl::_V1::detail::aligned_allocator
Definition: aligned_allocator.hpp:22
sycl::_V1::detail::OSUtil::alignedAlloc
static void * alignedAlloc(size_t Alignment, size_t NumBytes)
Allocates NumBytes bytes of uninitialized storage whose alignment is specified by Alignment.
Definition: os_util.cpp:215
sycl::_V1::detail::aligned_allocator::deallocate
void deallocate(pointer Ptr, size_t)
Definition: aligned_allocator.hpp:66
sycl::_V1::detail::aligned_allocator::value_type
T value_type
Definition: aligned_allocator.hpp:24
std::allocator_traits< sycl::detail::aligned_allocator< T > >::deallocate
static void deallocate(allocator_type &Allocator, pointer Ptr, size_type NumElems)
Definition: aligned_allocator.hpp:118
sycl::_V1::detail::aligned_allocator::aligned_allocator
aligned_allocator(size_t Alignment)
Definition: aligned_allocator.hpp:38
sycl::_V1::detail::aligned_allocator::pointer
T * pointer
Definition: aligned_allocator.hpp:25
sycl::_V1::ext::oneapi::experimental::detail::Alignment
@ Alignment
Definition: property.hpp:193
sycl::_V1::detail::aligned_allocator::address
const_pointer address(const_reference Val)
Definition: aligned_allocator.hpp:49
std::allocator_traits< sycl::detail::aligned_allocator< T > >::difference_type
typename std::pointer_traits< pointer >::difference_type difference_type
Definition: aligned_allocator.hpp:96
sycl::_V1::detail::aligned_allocator::const_pointer
const T * const_pointer
Definition: aligned_allocator.hpp:26
sycl::_V1::detail::aligned_allocator::const_reference
const T & const_reference
Definition: aligned_allocator.hpp:28
std::allocator_traits< sycl::detail::aligned_allocator< T > >::const_pointer
typename allocator_type::const_pointer const_pointer
Definition: aligned_allocator.hpp:90
sycl::_V1::detail::aligned_allocator::allocate
pointer allocate(size_t Size)
Definition: aligned_allocator.hpp:52
sycl::_V1::detail::aligned_allocator::rebind
Definition: aligned_allocator.hpp:31
sycl::_V1::detail::aligned_allocator::rebind::other
aligned_allocator< U > other
Definition: aligned_allocator.hpp:32
sycl::_V1::difference_type
std::ptrdiff_t difference_type
Definition: multi_ptr.hpp:753
sycl::_V1::detail::aligned_allocator::reference
T & reference
Definition: aligned_allocator.hpp:27
std::allocator_traits< sycl::detail::aligned_allocator< T > >::pointer
typename allocator_type::pointer pointer
Definition: aligned_allocator.hpp:89
sycl::_V1::detail::aligned_allocator::aligned_allocator
aligned_allocator()=default
sycl::_V1::detail::aligned_allocator::destroy
void destroy(pointer Ptr)
Definition: aligned_allocator.hpp:46
std
Definition: accessor.hpp:4139
sycl::_V1::detail::aligned_allocator::address
pointer address(reference Val) const
Definition: aligned_allocator.hpp:48
std::allocator_traits< sycl::detail::aligned_allocator< T > >::select_on_container_copy_construction
static allocator_type select_on_container_copy_construction(const allocator_type &Allocator)
Definition: aligned_allocator.hpp:138
std::allocator_traits< sycl::detail::aligned_allocator< T > >::allocate
static pointer allocate(allocator_type &Allocator, size_type NumElems, const_void_pointer)
Definition: aligned_allocator.hpp:112
std::allocator_traits< sycl::detail::aligned_allocator< T > >::propagate_on_container_copy_assignment
std::false_type propagate_on_container_copy_assignment
Definition: aligned_allocator.hpp:98
std::allocator_traits< sycl::detail::aligned_allocator< T > >::allocator_type
typename sycl::detail::aligned_allocator< T > allocator_type
Definition: aligned_allocator.hpp:87
std::allocator_traits< sycl::detail::aligned_allocator< T > >::rebind_traits
allocator_traits< rebind_alloc< U > > rebind_traits
Definition: aligned_allocator.hpp:106
sycl::_V1::detail::aligned_allocator::operator==
bool operator==(const aligned_allocator &)
Definition: aligned_allocator.hpp:71
std::allocator_traits< sycl::detail::aligned_allocator< T > >::is_always_equal
typename std::is_empty< allocator_type >::type is_always_equal
Definition: aligned_allocator.hpp:101
sycl::_V1::detail::OSUtil::alignedFree
static void alignedFree(void *Ptr)
Deallocates the memory referenced by Ptr.
Definition: os_util.cpp:228
std::allocator_traits< sycl::detail::aligned_allocator< T > >::propagate_on_container_swap
std::false_type propagate_on_container_swap
Definition: aligned_allocator.hpp:100