DPC++ Runtime
Runtime libraries for oneAPI DPC++
usm_allocator.hpp
Go to the documentation of this file.
1 //==------ usm_allocator.hpp - SYCL USM Allocator ------*- C++ -*-----------==//
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 #pragma once
9 
10 #include <CL/sycl/context.hpp>
13 #include <CL/sycl/device.hpp>
14 #include <CL/sycl/exception.hpp>
15 #include <CL/sycl/queue.hpp>
17 
18 #include <cstdlib>
19 #include <memory>
20 
22 namespace sycl {
23 
24 // Forward declarations.
25 __SYCL_EXPORT void *aligned_alloc(size_t alignment, size_t size,
26  const device &dev, const context &ctxt,
27  usm::alloc kind,
28  const property_list &propList,
29  const detail::code_location CodeLoc);
30 __SYCL_EXPORT void free(void *ptr, const context &ctxt,
31  const detail::code_location CodeLoc);
32 
33 template <typename T, usm::alloc AllocKind, size_t Alignment = alignof(T)>
35 public:
36  using value_type = T;
39  using propagate_on_container_swap = std::true_type;
40 
41 public:
42  template <typename U> struct rebind {
44  };
45 
46  static_assert(
47  AllocKind != usm::alloc::device,
48  "usm_allocator does not support AllocKind == usm::alloc::device");
49 
50  usm_allocator() noexcept = delete;
51  usm_allocator(const context &Ctxt, const device &Dev,
52  const property_list &PropList = {}) noexcept
53  : MContext(Ctxt), MDevice(Dev), MPropList(PropList) {}
54  usm_allocator(const queue &Q, const property_list &PropList = {}) noexcept
55  : MContext(Q.get_context()), MDevice(Q.get_device()),
56  MPropList(PropList) {}
57  usm_allocator(const usm_allocator &) noexcept = default;
58  usm_allocator(usm_allocator &&) noexcept = default;
59  usm_allocator &operator=(const usm_allocator &Other) {
60  MContext = Other.MContext;
61  MDevice = Other.MDevice;
62  MPropList = Other.MPropList;
63  return *this;
64  }
66  MContext = std::move(Other.MContext);
67  MDevice = std::move(Other.MDevice);
68  MPropList = std::move(Other.MPropList);
69  return *this;
70  }
71 
72  template <class U>
74  : MContext(Other.MContext), MDevice(Other.MDevice),
75  MPropList(Other.MPropList) {}
76 
80  T *allocate(size_t NumberOfElements, const detail::code_location CodeLoc =
81  detail::code_location::current()) {
82 
83  auto Result = reinterpret_cast<T *>(
84  aligned_alloc(getAlignment(), NumberOfElements * sizeof(value_type),
85  MDevice, MContext, AllocKind, MPropList, CodeLoc));
86  if (!Result) {
87  throw memory_allocation_error();
88  }
89  return Result;
90  }
91 
96  void deallocate(
97  T *Ptr, size_t,
98  const detail::code_location CodeLoc = detail::code_location::current()) {
99  if (Ptr) {
100  free(Ptr, MContext, CodeLoc);
101  }
102  }
103 
104  template <class U, usm::alloc AllocKindU, size_t AlignmentU>
107  return ((AllocKind == AllocKindU) && (One.MContext == Two.MContext) &&
108  (One.MDevice == Two.MDevice));
109  }
110 
111  template <class U, usm::alloc AllocKindU, size_t AlignmentU>
114  return !((AllocKind == AllocKindU) && (One.MContext == Two.MContext) &&
115  (One.MDevice == Two.MDevice));
116  }
117 
118 private:
119  constexpr size_t getAlignment() const { return Alignment; }
120 
121  template <class U, usm::alloc AllocKindU, size_t AlignmentU>
122  friend class usm_allocator;
123 
124  context MContext;
125  device MDevice;
126  property_list MPropList;
127 };
128 
129 } // namespace sycl
130 } // __SYCL_INLINE_NAMESPACE(cl)
cl::sycl::info::device
device
Definition: info_desc.hpp:53
cl::sycl::usm_allocator::operator==
friend bool operator==(const usm_allocator< T, AllocKind, Alignment > &One, const usm_allocator< U, AllocKindU, AlignmentU > &Two)
Definition: usm_allocator.hpp:105
cl::sycl::usm_allocator::rebind::other
usm_allocator< U, AllocKind, Alignment > other
Definition: usm_allocator.hpp:43
cl::sycl::usm_allocator::rebind
Definition: usm_allocator.hpp:42
device.hpp
cl::sycl::usm_allocator::propagate_on_container_copy_assignment
std::true_type propagate_on_container_copy_assignment
Definition: usm_allocator.hpp:37
context.hpp
cl::sycl::usm_allocator::propagate_on_container_swap
std::true_type propagate_on_container_swap
Definition: usm_allocator.hpp:39
cl::sycl::usm_allocator::operator!=
friend bool operator!=(const usm_allocator< T, AllocKind, Alignment > &One, const usm_allocator< U, AllocKindU, AlignmentU > &Two)
Definition: usm_allocator.hpp:112
sycl
Definition: invoke_simd.hpp:68
cl::sycl::property_list
Objects of the property_list class are containers for the SYCL properties.
Definition: property_list.hpp:26
cl::sycl::detail::code_location
Definition: common.hpp:54
cl::sycl::queue
Encapsulates a single SYCL queue which schedules kernels on a SYCL device.
Definition: queue.hpp:103
cl::sycl::usm_allocator::operator=
usm_allocator & operator=(usm_allocator &&Other)
Definition: usm_allocator.hpp:65
export.hpp
cl::sycl::usm_allocator::usm_allocator
usm_allocator(const usm_allocator< U, AllocKind, Alignment > &Other) noexcept
Definition: usm_allocator.hpp:73
cl::sycl::device
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:35
cl::sycl::usm_allocator::propagate_on_container_move_assignment
std::true_type propagate_on_container_move_assignment
Definition: usm_allocator.hpp:38
cl::sycl::usm_allocator::value_type
T value_type
Definition: usm_allocator.hpp:36
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::usm_allocator::allocate
T * allocate(size_t NumberOfElements, const detail::code_location CodeLoc=detail::code_location::current())
Allocates memory.
Definition: usm_allocator.hpp:80
queue.hpp
cl::sycl::usm_allocator
Definition: usm_allocator.hpp:34
cl::sycl::queue::get_device
device get_device() const
Definition: queue.cpp:75
cl::sycl::queue::get_context
context get_context() const
Definition: queue.cpp:73
cl::sycl::usm_allocator::deallocate
void deallocate(T *Ptr, size_t, const detail::code_location CodeLoc=detail::code_location::current())
Deallocates memory.
Definition: usm_allocator.hpp:96
exception.hpp
cl::sycl::context
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:35
cl::sycl::aligned_alloc
void * aligned_alloc(size_t alignment, size_t size, const device &dev, const context &ctxt, usm::alloc kind, const property_list &propList, const detail::code_location CodeLoc)
Definition: usm.hpp:277
common.hpp
usm_enums.hpp
cl::sycl::usm::alloc
alloc
Definition: usm_enums.hpp:14
cl::sycl::usm_allocator::usm_allocator
usm_allocator(const queue &Q, const property_list &PropList={}) noexcept
Definition: usm_allocator.hpp:54
cl::sycl::free
void free(void *ptr, const context &ctxt, const detail::code_location CodeLoc)
Definition: usm_impl.cpp:265
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12