DPC++ Runtime
Runtime libraries for oneAPI DPC++
sycl_mem_obj_allocator.hpp
Go to the documentation of this file.
1 //==------- sycl_mem_obj_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 
12 namespace sycl {
13 namespace detail {
14 
15 template <typename T>
16 class aligned_allocator;
17 
19 
20 protected:
21  virtual void *getAllocatorImpl() = 0;
22 
23 public:
24  virtual ~SYCLMemObjAllocator() = default;
25  virtual void *allocate(std::size_t) = 0;
26  virtual void deallocate(void *, std::size_t) = 0;
27  virtual std::size_t getValueSize() const = 0;
28  virtual void setAlignment(std::size_t RequiredAlign) = 0;
29  template <typename AllocatorT> AllocatorT getAllocator() {
30  return *reinterpret_cast<AllocatorT *>(getAllocatorImpl());
31  }
32 };
33 
34 template <typename AllocatorT>
37 
38  template <typename T>
39  using EnableIfDefaultAllocator =
41 
42  template <typename T>
43  using EnableIfNonDefaultAllocator =
45 
46 public:
47  SYCLMemObjAllocatorHolder(AllocatorT Allocator)
48  : MAllocator(Allocator),
49  MValueSize(sizeof(typename AllocatorT::value_type)) {}
50 
52  : MAllocator(AllocatorT()),
53  MValueSize(sizeof(typename AllocatorT::value_type)) {}
54 
55  ~SYCLMemObjAllocatorHolder() = default;
56 
57  virtual void *allocate(std::size_t Count) override {
58  return reinterpret_cast<void *>(MAllocator.allocate(Count));
59  }
60 
61  virtual void deallocate(void *Ptr, std::size_t Count) override {
62  MAllocator.deallocate(
63  reinterpret_cast<typename AllocatorT::value_type *>(Ptr), Count);
64  }
65 
66  void setAlignment(std::size_t RequiredAlign) override {
67  setAlignImpl(RequiredAlign);
68  }
69 
70  virtual std::size_t getValueSize() const override { return MValueSize; }
71 
72 protected:
73  virtual void *getAllocatorImpl() override { return &MAllocator; }
74 
75 private:
76  template <typename T = AllocatorT>
77  EnableIfNonDefaultAllocator<T> setAlignImpl(std::size_t) {
78  // Do nothing in case of user's allocator.
79  }
80 
81  template <typename T = AllocatorT>
82  EnableIfDefaultAllocator<T> setAlignImpl(std::size_t RequiredAlign) {
83  MAllocator.setAlignment(std::max<size_t>(RequiredAlign, 64));
84  }
85 
86  AllocatorT MAllocator;
87  std::size_t MValueSize;
88 };
89 } // namespace detail
90 } // namespace sycl
91 }
cl::sycl::detail::aligned_allocator
Definition: aligned_allocator.hpp:23
cl::sycl::detail::SYCLMemObjAllocatorHolder::allocate
virtual void * allocate(std::size_t Count) override
Definition: sycl_mem_obj_allocator.hpp:57
cl::sycl::detail::SYCLMemObjAllocatorHolder::deallocate
virtual void deallocate(void *Ptr, std::size_t Count) override
Definition: sycl_mem_obj_allocator.hpp:61
cl::sycl::detail::SYCLMemObjAllocatorHolder::getValueSize
virtual std::size_t getValueSize() const override
Definition: sycl_mem_obj_allocator.hpp:70
sycl
Definition: invoke_simd.hpp:68
cl::sycl::detail::SYCLMemObjAllocatorHolder::setAlignment
void setAlignment(std::size_t RequiredAlign) override
Definition: sycl_mem_obj_allocator.hpp:66
cl::sycl::detail::SYCLMemObjAllocatorHolder::getAllocatorImpl
virtual void * getAllocatorImpl() override
Definition: sycl_mem_obj_allocator.hpp:73
cl::sycl::detail::SYCLMemObjAllocatorHolder::SYCLMemObjAllocatorHolder
SYCLMemObjAllocatorHolder()
Definition: sycl_mem_obj_allocator.hpp:51
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::detail::SYCLMemObjAllocator::getAllocator
AllocatorT getAllocator()
Definition: sycl_mem_obj_allocator.hpp:29
cl::sycl::detail::SYCLMemObjAllocatorHolder::SYCLMemObjAllocatorHolder
SYCLMemObjAllocatorHolder(AllocatorT Allocator)
Definition: sycl_mem_obj_allocator.hpp:47
cl::sycl::detail::SYCLMemObjAllocatorHolder
Definition: sycl_mem_obj_allocator.hpp:35
cl::sycl::detail::enable_if_t
typename std::enable_if< B, T >::type enable_if_t
Definition: stl_type_traits.hpp:24
cl::sycl::detail::SYCLMemObjAllocator
Definition: sycl_mem_obj_allocator.hpp:18
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12