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/detail/stl_type_traits.hpp> // for iterator_to_const_type_t
19 #include <sycl/property_list.hpp>
20 #include <sycl/types.hpp>
21 
22 #include <cstdint>
23 #include <functional>
24 #include <memory>
25 #include <type_traits>
26 
27 namespace sycl {
28 inline namespace _V1 {
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 
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<
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<
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<
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<
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>
117  std::enable_if_t<!iterator_to_const_type_t<T>::value, T>;
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,
150  sycl::detail::pi::PiEvent &OutEventToWait) override;
152  void *UserObj, const void *HostObj,
153  const void *Type, uint32_t Dim,
154  uint32_t ElemType, size_t Range[3]);
155  void destructorNotification(void *UserObj);
156 
157  MemObjType getType() const override { return MemObjType::Buffer; }
158 
160  try {
161  BaseT::updateHostMemory();
162  } catch (...) {
163  }
164  destructorNotification(this);
165  }
166 
167  void resize(size_t size) { BaseT::MSizeInBytes = size; }
168 
169  void addInteropObject(std::vector<pi_native_handle> &Handles) const;
170 
171  std::vector<pi_native_handle> getNativeVector(backend BackendName) const;
172 };
173 
174 } // namespace detail
175 } // namespace _V1
176 } // namespace sycl
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:51
buffer_impl(void *HostData, size_t SizeInBytes, size_t RequiredAlign, const property_list &Props, std::unique_ptr< SYCLMemObjAllocator > Allocator)
Definition: buffer_impl.hpp:56
MemObjType getType() const override
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)
buffer_impl(pi_native_handle MemObject, const context &SyclContext, std::unique_ptr< SYCLMemObjAllocator > Allocator, bool OwnNativeHandle, event AvailableEvent)
buffer_impl(cl_mem MemObject, const context &SyclContext, std::unique_ptr< SYCLMemObjAllocator > Allocator, event AvailableEvent)
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
buffer_impl(cl_mem MemObject, const context &SyclContext, const size_t SizeInBytes, std::unique_ptr< SYCLMemObjAllocator > Allocator, event AvailableEvent)
buffer_impl(size_t SizeInBytes, size_t, const property_list &Props, std::unique_ptr< SYCLMemObjAllocator > Allocator)
Definition: buffer_impl.hpp:46
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
std::enable_if_t<!iterator_to_const_type_t< T >::value, T > EnableIfNotConstIterator
buffer_impl(pi_native_handle MemObject, const context &SyclContext, const size_t SizeInBytes, std::unique_ptr< SYCLMemObjAllocator > Allocator, event AvailableEvent)
An event object can be used to synchronize memory transfers, enqueues of kernels and signaling barrie...
Definition: event.hpp:44
Objects of the property_list class are containers for the SYCL properties.
bool has_property() const noexcept
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.
PiProgram cast(cl_program)=delete
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: event_impl.hpp:32
void constructorNotification(void *BufferObj, void *AccessorObj, access::target Target, access::mode Mode, const code_location &CodeLoc)
host_accessor(buffer< DataT, Dimensions, AllocatorT >) -> host_accessor< DataT, Dimensions, access::mode::read_write >
class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(local_accessor) local_accessor class __SYCL_EBO __SYCL_SPECIAL_CLASS Dimensions
Definition: accessor.hpp:3233
class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(local_accessor) local_accessor class __SYCL_EBO __SYCL_SPECIAL_CLASS IsPlaceholder
Definition: accessor.hpp:3234
class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(local_accessor) local_accessor class __SYCL_EBO __SYCL_SPECIAL_CLASS AccessMode
Definition: accessor.hpp:3233
Definition: access.hpp:18
uintptr_t pi_native_handle
Definition: pi.h:217