DPC++ Runtime
Runtime libraries for oneAPI DPC++
buffer_impl.cpp
Go to the documentation of this file.
1 //==----------------- buffer_impl.cpp - 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 #include <detail/buffer_impl.hpp>
10 #include <detail/context_impl.hpp>
14 #include <detail/xpti_registry.hpp>
15 
16 namespace sycl {
17 inline namespace _V1 {
18 namespace detail {
19 #ifdef XPTI_ENABLE_INSTRUMENTATION
20 uint8_t GBufferStreamID;
21 #endif
22 void *buffer_impl::allocateMem(ContextImplPtr Context, bool InitFromUserData,
23  void *HostPtr,
24  sycl::detail::pi::PiEvent &OutEventToWait) {
25  bool HostPtrReadOnly = false;
26  BaseT::determineHostPtr(Context, InitFromUserData, HostPtr, HostPtrReadOnly);
27 
28  assert(!(nullptr == HostPtr && BaseT::useHostPtr() && Context->is_host()) &&
29  "Internal error. Allocating memory on the host "
30  "while having use_host_ptr property");
32  std::move(Context), this, HostPtr, HostPtrReadOnly,
34  MProps, OutEventToWait);
35 }
37  void *UserObj, const void *HostObj,
38  const void *Type, uint32_t Dim,
39  uint32_t ElemSize, size_t Range[3]) {
40  XPTIRegistry::bufferConstructorNotification(UserObj, CodeLoc, HostObj, Type,
41  Dim, ElemSize, Range);
42 }
43 
46 }
47 
49  std::vector<pi_native_handle> &Handles) const {
50  if (MOpenCLInterop) {
51  if (std::find(Handles.begin(), Handles.end(),
52  pi::cast<pi_native_handle>(MInteropMemObject)) ==
53  Handles.end()) {
54  const PluginPtr &Plugin = getPlugin();
55  Plugin->call<PiApiKind::piMemRetain>(
56  pi::cast<sycl::detail::pi::PiMem>(MInteropMemObject));
57  Handles.push_back(pi::cast<pi_native_handle>(MInteropMemObject));
58  }
59  }
60 }
61 
62 std::vector<pi_native_handle>
64  std::vector<pi_native_handle> Handles{};
65  if (!MRecord) {
66  addInteropObject(Handles);
67  return Handles;
68  }
69 
70  for (auto &Cmd : MRecord->MAllocaCommands) {
71  sycl::detail::pi::PiMem NativeMem =
72  pi::cast<sycl::detail::pi::PiMem>(Cmd->getMemAllocation());
73  auto Ctx = Cmd->getWorkerContext();
74  auto Platform = Ctx->getPlatformImpl();
75  // If Host Shared Memory is not supported then there is alloca for host that
76  // doesn't have platform
77  if (!Platform || (Platform->getBackend() != BackendName))
78  continue;
79 
80  auto Plugin = Platform->getPlugin();
81 
82  if (Platform->getBackend() == backend::opencl) {
83  Plugin->call<PiApiKind::piMemRetain>(NativeMem);
84  }
85 
86  pi_native_handle Handle;
87  // When doing buffer interop we don't know what device the memory should be
88  // resident on, so pass nullptr for Device param. Buffer interop may not be
89  // supported by all backends.
90  Plugin->call<PiApiKind::piextMemGetNativeHandle>(NativeMem, /*Dev*/ nullptr,
91  &Handle);
92  Handles.push_back(Handle);
93  }
94 
95  addInteropObject(Handles);
96  return Handles;
97 }
98 } // namespace detail
99 } // namespace _V1
100 } // namespace sycl
static void * allocateMemBuffer(ContextImplPtr TargetContext, SYCLMemObjI *MemObj, void *UserPtr, bool HostPtrReadOnly, size_t Size, const EventImplPtr &InteropEvent, const ContextImplPtr &InteropContext, const sycl::property_list &PropsList, sycl::detail::pi::PiEvent &OutEventToWait)
std::shared_ptr< MemObjRecord > MRecord
const PluginPtr & getPlugin() const
size_t getSizeInBytes() const noexcept override
void determineHostPtr(const ContextImplPtr &Context, bool InitFromUserData, void *&HostPtr, bool &HostPtrReadOnly)
sycl::detail::pi::PiMem MInteropMemObject
static void bufferConstructorNotification(const void *, const detail::code_location &, const void *, const void *, uint32_t, uint32_t, size_t[3])
static void bufferDestructorNotification(const void *)
std::vector< pi_native_handle > getNativeVector(backend BackendName) const
Definition: buffer_impl.cpp:63
void * allocateMem(ContextImplPtr Context, bool InitFromUserData, void *HostPtr, sycl::detail::pi::PiEvent &OutEventToWait) override
Definition: buffer_impl.cpp:22
void destructorNotification(void *UserObj)
Definition: buffer_impl.cpp:44
void constructorNotification(const detail::code_location &CodeLoc, void *UserObj, const void *HostObj, const void *Type, uint32_t Dim, uint32_t ElemType, size_t Range[3])
Definition: buffer_impl.cpp:36
void addInteropObject(std::vector< pi_native_handle > &Handles) const
Definition: buffer_impl.cpp:48
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: event_impl.hpp:32
std::shared_ptr< plugin > PluginPtr
Definition: pi.hpp:48
Definition: access.hpp:18
pi_result piextMemGetNativeHandle(pi_mem mem, pi_device dev, pi_native_handle *nativeHandle)
Gets the native handle of a PI mem object.
Definition: pi_cuda.cpp:236
uintptr_t pi_native_handle
Definition: pi.h:217
pi_result piMemRetain(pi_mem mem)
Definition: pi_cuda.cpp:223