DPC++ Runtime
Runtime libraries for oneAPI DPC++
buffer_impl.hpp
Go to the documentation of this file.
1 //==----------------- buffer_impl.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 "CL/sycl/detail/pi.h"
13 #include <CL/sycl/context.hpp>
19 #include <CL/sycl/stl.hpp>
20 #include <CL/sycl/types.hpp>
21 
22 #include <cstdint>
23 #include <functional>
24 #include <memory>
25 #include <type_traits>
26 
28 namespace sycl {
29 // Forward declarations
30 template <typename DataT, int Dimensions, access::mode AccessMode,
31  access::target AccessTarget, access::placeholder IsPlaceholder,
32  typename PropertyListT>
33 class accessor;
34 template <typename T, int Dimensions, typename AllocatorT, typename Enable>
35 class buffer;
36 template <typename DataT, int Dimensions, access::mode AccessMode>
37 class host_accessor;
38 
40 
41 namespace detail {
42 
43 class __SYCL_EXPORT buffer_impl final : public SYCLMemObjT {
44  using BaseT = SYCLMemObjT;
45  using typename BaseT::MemObjType;
46 
47 public:
48  buffer_impl(size_t SizeInBytes, size_t, const property_list &Props,
49  std::unique_ptr<SYCLMemObjAllocator> Allocator)
50  : BaseT(SizeInBytes, Props, std::move(Allocator)) {
51 
53  throw sycl::invalid_object_error(
54  "The use_host_ptr property requires host pointer to be provided",
56  }
57 
58  buffer_impl(void *HostData, size_t SizeInBytes, size_t RequiredAlign,
59  const property_list &Props,
60  std::unique_ptr<SYCLMemObjAllocator> Allocator)
61  : BaseT(SizeInBytes, Props, std::move(Allocator)) {
62 
63  if (Props.has_property<
65  throw sycl::invalid_object_error(
66  "The use_pinned_host_memory cannot be used with host pointer",
68 
69  BaseT::handleHostData(HostData, RequiredAlign);
70  }
71 
72  buffer_impl(const void *HostData, size_t SizeInBytes, size_t RequiredAlign,
73  const property_list &Props,
74  std::unique_ptr<SYCLMemObjAllocator> Allocator)
75  : BaseT(SizeInBytes, Props, std::move(Allocator)) {
76 
77  if (Props.has_property<
79  throw sycl::invalid_object_error(
80  "The use_pinned_host_memory cannot be used with host pointer",
82 
83  BaseT::handleHostData(HostData, RequiredAlign);
84  }
85 
86  template <typename T>
87  buffer_impl(const std::shared_ptr<T> &HostData, const size_t SizeInBytes,
88  size_t RequiredAlign, const property_list &Props,
89  std::unique_ptr<SYCLMemObjAllocator> Allocator)
90  : BaseT(SizeInBytes, Props, std::move(Allocator)) {
91 
92  if (Props.has_property<
94  throw sycl::invalid_object_error(
95  "The use_pinned_host_memory cannot be used with host pointer",
97 
98  BaseT::handleHostData(HostData, RequiredAlign);
99  }
100 
101  template <typename T>
102  using EnableIfNotConstIterator =
104 
105  template <class InputIterator>
107  const size_t SizeInBytes, size_t RequiredAlign,
108  const property_list &Props,
109  std::unique_ptr<SYCLMemObjAllocator> Allocator)
110  : BaseT(SizeInBytes, Props, std::move(Allocator)) {
111 
113  throw sycl::invalid_object_error(
114  "Buffer constructor from a pair of iterator values cannot have the "
115  "use_host_ptr property.",
117 
118  BaseT::handleHostData(First, Last, RequiredAlign);
119  }
120 
121  template <typename T>
122  using EnableIfConstIterator =
124 
125  template <class InputIterator>
127  const size_t SizeInBytes, size_t RequiredAlign,
128  const property_list &Props,
129  std::unique_ptr<SYCLMemObjAllocator> Allocator)
130  : BaseT(SizeInBytes, Props, std::move(Allocator)) {
131 
133  throw sycl::invalid_object_error(
134  "Buffer constructor from a pair of iterator values cannot have the "
135  "use_host_ptr property.",
137 
138  BaseT::handleHostData(First, Last, RequiredAlign);
139  }
140 
141  buffer_impl(cl_mem MemObject, const context &SyclContext,
142  std::unique_ptr<SYCLMemObjAllocator> Allocator,
143  event AvailableEvent)
144  : buffer_impl(pi::cast<pi_native_handle>(MemObject), SyclContext,
145  std::move(Allocator), /*OwnNativeHandle*/ true,
146  std::move(AvailableEvent)) {}
147 
148  buffer_impl(pi_native_handle MemObject, const context &SyclContext,
149  std::unique_ptr<SYCLMemObjAllocator> Allocator,
150  bool OwnNativeHandle, event AvailableEvent)
151  : BaseT(MemObject, SyclContext, OwnNativeHandle,
152  std::move(AvailableEvent), std::move(Allocator)) {}
153 
154  // TODO: remove the following 2 constructors when it is allowed to break ABI.
155  buffer_impl(cl_mem MemObject, const context &SyclContext,
156  const size_t SizeInBytes,
157  std::unique_ptr<SYCLMemObjAllocator> Allocator,
158  event AvailableEvent)
159  : buffer_impl(pi::cast<pi_native_handle>(MemObject), SyclContext,
160  SizeInBytes, std::move(Allocator),
161  std::move(AvailableEvent)) {}
162 
163  buffer_impl(pi_native_handle MemObject, const context &SyclContext,
164  const size_t SizeInBytes,
165  std::unique_ptr<SYCLMemObjAllocator> Allocator,
166  event AvailableEvent)
167  : BaseT(MemObject, SyclContext, SizeInBytes, std::move(AvailableEvent),
168  std::move(Allocator)) {}
169 
170  void *allocateMem(ContextImplPtr Context, bool InitFromUserData,
171  void *HostPtr, RT::PiEvent &OutEventToWait) override;
173  void *UserObj, const void *HostObj,
174  const void *Type, uint32_t Dim,
175  uint32_t ElemType, size_t Range[3]);
176  // TODO: remove once ABI break is allowed
178  void *UserObj);
179  void destructorNotification(void *UserObj);
180 
181  MemObjType getType() const override { return MemObjType::Buffer; }
182 
184  try {
185  BaseT::updateHostMemory();
186  } catch (...) {
187  }
188  destructorNotification(this);
189  }
190 
191  void resize(size_t size) { BaseT::MSizeInBytes = size; }
192 
193  void addInteropObject(std::vector<pi_native_handle> &Handles) const;
194 
195  std::vector<pi_native_handle> getNativeVector(backend BackendName) const;
196 };
197 
198 } // namespace detail
199 } // namespace sycl
200 } // __SYCL_INLINE_NAMESPACE(cl)
cl::sycl::detail::buffer_impl::getType
MemObjType getType() const override
Definition: buffer_impl.hpp:181
cl::sycl::backend
backend
Definition: backend_types.hpp:21
property_list.hpp
cl::sycl::property::buffer::use_host_ptr
Definition: buffer_properties.hpp:20
cl::sycl::detail::aligned_allocator
Definition: aligned_allocator.hpp:23
pi.h
cl::sycl::detail::ContextImplPtr
std::shared_ptr< detail::context_impl > ContextImplPtr
Definition: memory_manager.hpp:32
cl::sycl::detail::buffer_impl::EnableIfNotConstIterator
enable_if_t<!iterator_to_const_type_t< T >::value, T > EnableIfNotConstIterator
Definition: buffer_impl.hpp:103
cl::sycl::event
An event object can be used to synchronize memory transfers, enqueues of kernels and signaling barrie...
Definition: event.hpp:31
cl::sycl::detail::buffer_impl::buffer_impl
buffer_impl(pi_native_handle MemObject, const context &SyclContext, std::unique_ptr< SYCLMemObjAllocator > Allocator, bool OwnNativeHandle, event AvailableEvent)
Definition: buffer_impl.hpp:148
PI_INVALID_OPERATION
@ PI_INVALID_OPERATION
Definition: pi.h:88
cl::sycl::access::placeholder
placeholder
Definition: access.hpp:43
stl.hpp
context.hpp
cl::sycl::detail::buffer_impl::buffer_impl
buffer_impl(EnableIfConstIterator< InputIterator > First, InputIterator Last, const size_t SizeInBytes, size_t RequiredAlign, const property_list &Props, std::unique_ptr< SYCLMemObjAllocator > Allocator)
Definition: buffer_impl.hpp:126
cl::sycl::property_list::has_property
bool has_property() const
Definition: property_list.hpp:50
cl::sycl::detail::buffer_impl::buffer_impl
buffer_impl(const std::shared_ptr< T > &HostData, const size_t SizeInBytes, size_t RequiredAlign, const property_list &Props, std::unique_ptr< SYCLMemObjAllocator > Allocator)
Definition: buffer_impl.hpp:87
cl::sycl::detail::buffer_impl::~buffer_impl
~buffer_impl()
Definition: buffer_impl.hpp:183
helpers.hpp
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::buffer_impl::resize
void resize(size_t size)
Definition: buffer_impl.hpp:191
access.hpp
cl::sycl::detail::code_location
Definition: common.hpp:54
cast
To cast(From value)
Definition: pi_opencl.cpp:42
cl::sycl::detail::buffer_impl::buffer_impl
buffer_impl(cl_mem MemObject, const context &SyclContext, const size_t SizeInBytes, std::unique_ptr< SYCLMemObjAllocator > Allocator, event AvailableEvent)
Definition: buffer_impl.hpp:155
cl::sycl::detail::buffer_impl
Definition: buffer_impl.hpp:43
cl::sycl::detail::SYCLMemObjI::MemObjType
MemObjType
Definition: sycl_mem_obj_i.hpp:32
cl::sycl::detail::buffer_impl::EnableIfConstIterator
enable_if_t< iterator_to_const_type_t< T >::value, T > EnableIfConstIterator
Definition: buffer_impl.hpp:123
export.hpp
cl::sycl::access::target
target
Definition: access.hpp:17
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::detail::buffer_impl::buffer_impl
buffer_impl(size_t SizeInBytes, size_t, const property_list &Props, std::unique_ptr< SYCLMemObjAllocator > Allocator)
Definition: buffer_impl.hpp:48
pi_native_handle
uintptr_t pi_native_handle
Definition: pi.h:76
cl::sycl::detail::buffer_impl::buffer_impl
buffer_impl(EnableIfNotConstIterator< InputIterator > First, InputIterator Last, const size_t SizeInBytes, size_t RequiredAlign, const property_list &Props, std::unique_ptr< SYCLMemObjAllocator > Allocator)
Definition: buffer_impl.hpp:106
cl::sycl::detail::buffer_impl::buffer_impl
buffer_impl(cl_mem MemObject, const context &SyclContext, std::unique_ptr< SYCLMemObjAllocator > Allocator, event AvailableEvent)
Definition: buffer_impl.hpp:141
cl::sycl::detail::sycl_memory_object_allocator
aligned_allocator< char > sycl_memory_object_allocator
Definition: sycl_mem_obj_t.hpp:40
std
Definition: accessor.hpp:2616
_pi_event
PI Event mapping to CUevent.
Definition: pi_cuda.hpp:458
cl::sycl::context
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:35
common.hpp
cl::sycl::access::mode
mode
Definition: access.hpp:28
cl::sycl::detail::buffer_impl::buffer_impl
buffer_impl(const void *HostData, size_t SizeInBytes, size_t RequiredAlign, const property_list &Props, std::unique_ptr< SYCLMemObjAllocator > Allocator)
Definition: buffer_impl.hpp:72
types.hpp
cl::sycl::ext::oneapi::property::buffer::use_pinned_host_memory
Definition: buffer_properties.hpp:74
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::SYCLMemObjT
Definition: sycl_mem_obj_t.hpp:43
cl::sycl::Dimensions
Dimensions
Definition: backend.hpp:138
sycl_mem_obj_t.hpp
cl::sycl::detail::buffer_impl::buffer_impl
buffer_impl(pi_native_handle MemObject, const context &SyclContext, const size_t SizeInBytes, std::unique_ptr< SYCLMemObjAllocator > Allocator, event AvailableEvent)
Definition: buffer_impl.hpp:163
cl::sycl::detail::constructorNotification
void constructorNotification(void *BufferObj, void *AccessorObj, access::target Target, access::mode Mode, const code_location &CodeLoc)
Definition: accessor_impl.cpp:42
cl::sycl::errc::accessor
@ accessor
cl::sycl::detail::buffer_impl::buffer_impl
buffer_impl(void *HostData, size_t SizeInBytes, size_t RequiredAlign, const property_list &Props, std::unique_ptr< SYCLMemObjAllocator > Allocator)
Definition: buffer_impl.hpp:58
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12