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>
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, 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
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:41
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:40
Objects of the property_list class are containers for the SYCL properties.
bool has_property() const noexcept
#define __SYCL_INLINE_VER_NAMESPACE(X)
::pi_event PiEvent
Definition: pi.hpp:121
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: event_impl.hpp:30
void constructorNotification(void *BufferObj, void *AccessorObj, access::target Target, access::mode Mode, const code_location &CodeLoc)
class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(local_accessor) local_accessor class __SYCL_EBO __SYCL_SPECIAL_CLASS Dimensions
Definition: accessor.hpp:2782
class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(local_accessor) local_accessor class __SYCL_EBO __SYCL_SPECIAL_CLASS IsPlaceholder
Definition: accessor.hpp:2783
class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(local_accessor) local_accessor class __SYCL_EBO __SYCL_SPECIAL_CLASS AccessMode
Definition: accessor.hpp:2782
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
uintptr_t pi_native_handle
Definition: pi.h:111
To cast(From value)
Definition: pi_opencl.cpp:42