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 
13 #include <CL/sycl/range.hpp>
14 
15 #include <cstdlib>
16 #include <cstring>
17 #include <memory>
18 #include <vector>
19 
21 namespace sycl {
22 namespace detail {
23 template <typename T> class aligned_allocator {
24 public:
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 
31 public:
32  template <typename U> struct rebind { typedef aligned_allocator<U> other; };
33 
34  aligned_allocator() = default;
35  ~aligned_allocator() = default;
36 
37  explicit aligned_allocator(size_t Alignment) : MAlignment(Alignment) {}
38 
39  // Construct an object
41  new (Ptr) value_type(Val);
42  }
43 
44  // Destroy an object
45  void destroy(pointer Ptr) { Ptr->~value_type(); }
46 
47  pointer address(reference Val) const { return &Val; }
48  const_pointer address(const_reference Val) { return &Val; }
49 
50  // Allocate memory aligned to Alignment
51  pointer allocate(size_t Size) {
52  size_t NumBytes = Size * sizeof(value_type);
53  NumBytes = ((NumBytes - 1) | (MAlignment - 1)) + 1;
54  if (Size > NumBytes)
55  throw std::bad_alloc();
56 
57  pointer Result = reinterpret_cast<pointer>(
58  detail::OSUtil::alignedAlloc(MAlignment, NumBytes));
59  if (!Result)
60  throw std::bad_alloc();
61  return Result;
62  }
63 
64  // Release allocated memory
65  void deallocate(pointer Ptr, size_t) {
66  if (Ptr)
67  detail::OSUtil::alignedFree(Ptr);
68  }
69 
70  bool operator==(const aligned_allocator &) { return true; }
71  bool operator!=(const aligned_allocator &) { return false; }
72 
73  void setAlignment(size_t Alignment) { MAlignment = Alignment; }
74 
75 private:
76  // By default assume the "worst" case
77  size_t MAlignment = 128;
78 };
79 } // namespace detail
80 } // namespace sycl
81 } // __SYCL_INLINE_NAMESPACE(cl)
cl::sycl::detail::aligned_allocator::aligned_allocator
aligned_allocator(size_t Alignment)
Definition: aligned_allocator.hpp:37
cl::sycl::detail::aligned_allocator
Definition: aligned_allocator.hpp:23
cl::sycl::detail::aligned_allocator::const_reference
const T & const_reference
Definition: aligned_allocator.hpp:29
cl::sycl::detail::aligned_allocator::setAlignment
void setAlignment(size_t Alignment)
Definition: aligned_allocator.hpp:73
os_util.hpp
sycl
Definition: invoke_simd.hpp:68
cl::sycl::detail::aligned_allocator::pointer
T * pointer
Definition: aligned_allocator.hpp:26
cl::sycl::detail::aligned_allocator::reference
T & reference
Definition: aligned_allocator.hpp:28
cl::sycl::detail::aligned_allocator::address
pointer address(reference Val) const
Definition: aligned_allocator.hpp:47
cl::sycl::detail::aligned_allocator::value_type
T value_type
Definition: aligned_allocator.hpp:25
cl::sycl::detail::aligned_allocator::operator==
bool operator==(const aligned_allocator &)
Definition: aligned_allocator.hpp:70
cl::sycl::detail::aligned_allocator::destroy
void destroy(pointer Ptr)
Definition: aligned_allocator.hpp:45
range.hpp
cl::sycl::detail::aligned_allocator::allocate
pointer allocate(size_t Size)
Definition: aligned_allocator.hpp:51
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::detail::aligned_allocator::const_pointer
const T * const_pointer
Definition: aligned_allocator.hpp:27
cl::sycl::detail::aligned_allocator::address
const_pointer address(const_reference Val)
Definition: aligned_allocator.hpp:48
cl::sycl::detail::aligned_allocator::construct
void construct(pointer Ptr, const_reference Val)
Definition: aligned_allocator.hpp:40
cl::sycl::detail::aligned_allocator::deallocate
void deallocate(pointer Ptr, size_t)
Definition: aligned_allocator.hpp:65
common.hpp
cl::sycl::detail::aligned_allocator::rebind::other
aligned_allocator< U > other
Definition: aligned_allocator.hpp:32
cl::sycl::detail::aligned_allocator::rebind
Definition: aligned_allocator.hpp:32
cl::sycl::detail::aligned_allocator::operator!=
bool operator!=(const aligned_allocator &)
Definition: aligned_allocator.hpp:71
cl::sycl::detail::usm::alignedAlloc
void * alignedAlloc(size_t Alignment, size_t Bytes, const context &Ctxt, const device &Dev, cl::sycl::usm::alloc Kind, const code_location &CL)
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12