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 "sycl/detail/pi.h"
13 #include <sycl/access/access.hpp>
14 #include <sycl/context.hpp>
15 #include <sycl/detail/common.hpp>
16 #include <sycl/detail/export.hpp>
17 #include <sycl/detail/helpers.hpp>
18 #include <sycl/property_list.hpp>
19 #include <sycl/stl.hpp>
20 #include <sycl/types.hpp>
21 
22 #include <cstdint>
23 #include <functional>
24 #include <memory>
25 #include <type_traits>
26 
27 namespace sycl {
29 // Forward declarations
30 template <typename DataT, int Dimensions, access::mode AccessMode,
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 
39 namespace detail {
40 
41 class __SYCL_EXPORT buffer_impl final : public SYCLMemObjT {
42  using BaseT = SYCLMemObjT;
43  using typename BaseT::MemObjType;
44 
45 public:
46  buffer_impl(size_t SizeInBytes, size_t, const property_list &Props,
47  std::unique_ptr<SYCLMemObjAllocator> Allocator)
48  : BaseT(SizeInBytes, Props, std::move(Allocator)) {
49 
50  if (Props.has_property<sycl::property::buffer::use_host_ptr>())
51  throw sycl::invalid_object_error(
52  "The use_host_ptr property requires host pointer to be provided",
53  PI_ERROR_INVALID_OPERATION);
54  }
55 
56  buffer_impl(void *HostData, size_t SizeInBytes, size_t RequiredAlign,
57  const property_list &Props,
58  std::unique_ptr<SYCLMemObjAllocator> Allocator)
59  : BaseT(SizeInBytes, Props, std::move(Allocator)) {
60 
61  if (Props.has_property<
62  sycl::ext::oneapi::property::buffer::use_pinned_host_memory>())
63  throw sycl::invalid_object_error(
64  "The use_pinned_host_memory cannot be used with host pointer",
65  PI_ERROR_INVALID_OPERATION);
66 
67  BaseT::handleHostData(HostData, RequiredAlign);
68  }
69 
70  buffer_impl(const void *HostData, size_t SizeInBytes, size_t RequiredAlign,
71  const property_list &Props,
72  std::unique_ptr<SYCLMemObjAllocator> Allocator)
73  : BaseT(SizeInBytes, Props, std::move(Allocator)) {
74 
75  if (Props.has_property<
76  sycl::ext::oneapi::property::buffer::use_pinned_host_memory>())
77  throw sycl::invalid_object_error(
78  "The use_pinned_host_memory cannot be used with host pointer",
79  PI_ERROR_INVALID_OPERATION);
80 
81  BaseT::handleHostData(HostData, RequiredAlign);
82  }
83 
84  buffer_impl(const std::shared_ptr<const void> &HostData,
85  const size_t SizeInBytes, size_t RequiredAlign,
86  const property_list &Props,
87  std::unique_ptr<SYCLMemObjAllocator> Allocator, bool IsConstPtr)
88  : BaseT(SizeInBytes, Props, std::move(Allocator)) {
89 
90  if (Props.has_property<
91  sycl::ext::oneapi::property::buffer::use_pinned_host_memory>())
92  throw sycl::invalid_object_error(
93  "The use_pinned_host_memory cannot be used with host pointer",
94  PI_ERROR_INVALID_OPERATION);
95 
96  BaseT::handleHostData(std::const_pointer_cast<void>(HostData),
97  RequiredAlign, IsConstPtr);
98  }
99 
100  buffer_impl(const std::function<void(void *)> &CopyFromInput,
101  const size_t SizeInBytes, size_t RequiredAlign,
102  const property_list &Props,
103  std::unique_ptr<detail::SYCLMemObjAllocator> Allocator,
104  bool IsConstPtr)
105  : BaseT(SizeInBytes, Props, std::move(Allocator)) {
106  if (Props.has_property<
107  sycl::ext::oneapi::property::buffer::use_pinned_host_memory>())
108  throw sycl::invalid_object_error(
109  "The use_pinned_host_memory cannot be used with host pointer",
110  PI_ERROR_INVALID_OPERATION);
111 
112  BaseT::handleHostData(CopyFromInput, RequiredAlign, IsConstPtr);
113  }
114 
115  template <typename T>
116  using EnableIfNotConstIterator =
118 
119  buffer_impl(cl_mem MemObject, const context &SyclContext,
120  std::unique_ptr<SYCLMemObjAllocator> Allocator,
121  event AvailableEvent)
122  : buffer_impl(pi::cast<pi_native_handle>(MemObject), SyclContext,
123  std::move(Allocator), /*OwnNativeHandle*/ true,
124  std::move(AvailableEvent)) {}
125 
126  buffer_impl(pi_native_handle MemObject, const context &SyclContext,
127  std::unique_ptr<SYCLMemObjAllocator> Allocator,
128  bool OwnNativeHandle, event AvailableEvent)
129  : BaseT(MemObject, SyclContext, OwnNativeHandle,
130  std::move(AvailableEvent), std::move(Allocator)) {}
131 
132  // TODO: remove the following 2 constructors when it is allowed to break ABI.
133  buffer_impl(cl_mem MemObject, const context &SyclContext,
134  const size_t SizeInBytes,
135  std::unique_ptr<SYCLMemObjAllocator> Allocator,
136  event AvailableEvent)
137  : buffer_impl(pi::cast<pi_native_handle>(MemObject), SyclContext,
138  SizeInBytes, std::move(Allocator),
139  std::move(AvailableEvent)) {}
140 
141  buffer_impl(pi_native_handle MemObject, const context &SyclContext,
142  const size_t SizeInBytes,
143  std::unique_ptr<SYCLMemObjAllocator> Allocator,
144  event AvailableEvent)
145  : BaseT(MemObject, SyclContext, SizeInBytes, std::move(AvailableEvent),
146  std::move(Allocator)) {}
147 
148  void *allocateMem(ContextImplPtr Context, bool InitFromUserData,
149  void *HostPtr, RT::PiEvent &OutEventToWait) override;
151  void *UserObj, const void *HostObj,
152  const void *Type, uint32_t Dim,
153  uint32_t ElemType, size_t Range[3]);
154  void destructorNotification(void *UserObj);
155 
156  MemObjType getType() const override { return MemObjType::Buffer; }
157 
159  try {
160  BaseT::updateHostMemory();
161  } catch (...) {
162  }
163  destructorNotification(this);
164  }
165 
166  void resize(size_t size) { BaseT::MSizeInBytes = size; }
167 
168  void addInteropObject(std::vector<pi_native_handle> &Handles) const;
169 
170  std::vector<pi_native_handle> getNativeVector(backend BackendName) const;
171 };
172 
173 } // namespace detail
174 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
175 } // namespace sycl
sycl::_V1::IsPlaceholder
class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(local_accessor) local_accessor class __SYCL_EBO __SYCL_SPECIAL_CLASS IsPlaceholder
Definition: accessor.hpp:2855
sycl::_V1::property_list
Objects of the property_list class are containers for the SYCL properties.
Definition: property_list.hpp:24
sycl::_V1::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:141
property_list.hpp
sycl::_V1::host_accessor
host_accessor(buffer< DataT, Dimensions, AllocatorT >) -> host_accessor< DataT, Dimensions, access::mode::read_write >
sycl::_V1::access::mode
mode
Definition: access.hpp:30
pi.h
sycl::_V1::backend
backend
Definition: backend_types.hpp:21
sycl::_V1::detail::ContextImplPtr
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: event_impl.hpp:30
sycl::_V1::detail::buffer_impl::buffer_impl
buffer_impl(const std::shared_ptr< const void > &HostData, const size_t SizeInBytes, size_t RequiredAlign, const property_list &Props, std::unique_ptr< SYCLMemObjAllocator > Allocator, bool IsConstPtr)
Definition: buffer_impl.hpp:84
stl.hpp
__SYCL_INLINE_VER_NAMESPACE
#define __SYCL_INLINE_VER_NAMESPACE(X)
Definition: defines_elementary.hpp:11
types.hpp
context.hpp
sycl::_V1::detail::buffer_impl::buffer_impl
buffer_impl(const std::function< void(void *)> &CopyFromInput, const size_t SizeInBytes, size_t RequiredAlign, const property_list &Props, std::unique_ptr< detail::SYCLMemObjAllocator > Allocator, bool IsConstPtr)
Definition: buffer_impl.hpp:100
sycl::_V1::detail::constructorNotification
void constructorNotification(void *BufferObj, void *AccessorObj, access::target Target, access::mode Mode, const code_location &CodeLoc)
sycl::_V1::Dimensions
class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(local_accessor) local_accessor class __SYCL_EBO __SYCL_SPECIAL_CLASS Dimensions
Definition: accessor.hpp:2854
helpers.hpp
sycl
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
sycl::_V1::accessor
class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(accessor) accessor accessor(buffer< DataT, Dimensions, AllocatorT >) -> accessor< DataT, Dimensions, access::mode::read_write, target::device, access::placeholder::true_t >
Buffer accessor.
sycl::_V1::detail::buffer_impl::buffer_impl
buffer_impl(cl_mem MemObject, const context &SyclContext, std::unique_ptr< SYCLMemObjAllocator > Allocator, event AvailableEvent)
Definition: buffer_impl.hpp:119
sycl::_V1::event
An event object can be used to synchronize memory transfers, enqueues of kernels and signaling barrie...
Definition: event.hpp:40
access.hpp
cast
To cast(From value)
Definition: pi_opencl.cpp:42
sycl::_V1::detail::enable_if_t
typename std::enable_if< B, T >::type enable_if_t
Definition: stl_type_traits.hpp:24
sycl::_V1::detail::buffer_impl::~buffer_impl
~buffer_impl()
Definition: buffer_impl.hpp:158
export.hpp
sycl::_V1::access::placeholder
placeholder
Definition: access.hpp:45
sycl::_V1::detail::buffer_impl::resize
void resize(size_t size)
Definition: buffer_impl.hpp:166
sycl::_V1::detail::buffer_impl::getType
MemObjType getType() const override
Definition: buffer_impl.hpp:156
sycl::_V1::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:70
common.hpp
sycl::_V1::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:126
sycl::_V1::access::target
target
Definition: access.hpp:18
pi_native_handle
uintptr_t pi_native_handle
Definition: pi.h:133
sycl::_V1::property_list::has_property
bool has_property() const noexcept
Definition: property_list.hpp:48
sycl::_V1::detail::code_location
Definition: common.hpp:66
sycl::_V1::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:133
sycl::_V1::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:56
std
Definition: accessor.hpp:3230
sycl::_V1::detail::SYCLMemObjT
Definition: sycl_mem_obj_t.hpp:39
sycl::_V1::detail::SYCLMemObjI::MemObjType
MemObjType
Definition: sycl_mem_obj_i.hpp:32
sycl::_V1::detail::buffer_impl
Definition: buffer_impl.hpp:41
sycl::_V1::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:46
sycl::_V1::AccessMode
class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(local_accessor) local_accessor class __SYCL_EBO __SYCL_SPECIAL_CLASS AccessMode
Definition: accessor.hpp:2854
sycl::_V1::detail::pi::PiEvent
::pi_event PiEvent
Definition: pi.hpp:136
sycl_mem_obj_t.hpp
sycl::_V1::context
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:41