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 #include <sycl/detail/ur.hpp>
16 
17 namespace sycl {
18 inline namespace _V1 {
19 namespace detail {
20 #ifdef XPTI_ENABLE_INSTRUMENTATION
21 uint8_t GBufferStreamID;
22 #endif
23 void *buffer_impl::allocateMem(ContextImplPtr Context, bool InitFromUserData,
24  void *HostPtr,
25  ur_event_handle_t &OutEventToWait) {
26  bool HostPtrReadOnly = false;
27  BaseT::determineHostPtr(Context, InitFromUserData, HostPtr, HostPtrReadOnly);
28  assert(!(nullptr == HostPtr && BaseT::useHostPtr() && !Context) &&
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<ur_native_handle_t> &Handles) const {
50  if (MOpenCLInterop) {
51  if (std::find(Handles.begin(), Handles.end(),
52  ur::cast<ur_native_handle_t>(MInteropMemObject)) ==
53  Handles.end()) {
54  const PluginPtr &Plugin = getPlugin();
55  Plugin->call<UrApiKind::urMemRetain>(
56  ur::cast<ur_mem_handle_t>(MInteropMemObject));
57  ur_native_handle_t NativeHandle = 0;
58  Plugin->call<UrApiKind::urMemGetNativeHandle>(MInteropMemObject, nullptr,
59  &NativeHandle);
60  Handles.push_back(NativeHandle);
61  }
62  }
63 }
64 
65 std::vector<ur_native_handle_t>
67  std::vector<ur_native_handle_t> Handles{};
68  if (!MRecord) {
69  addInteropObject(Handles);
70  return Handles;
71  }
72 
73  for (auto &Cmd : MRecord->MAllocaCommands) {
74  ur_mem_handle_t NativeMem =
75  ur::cast<ur_mem_handle_t>(Cmd->getMemAllocation());
76  auto Ctx = Cmd->getWorkerContext();
77  // If Host Shared Memory is not supported then there is alloca for host that
78  // doesn't have context and platform
79  if (!Ctx)
80  continue;
81  PlatformImplPtr Platform = Ctx->getPlatformImpl();
82  assert(Platform && "Platform must be present for device context");
83  if (Platform->getBackend() != BackendName)
84  continue;
85 
86  auto Plugin = Platform->getPlugin();
87 
88  if (Platform->getBackend() == backend::opencl) {
89  Plugin->call<UrApiKind::urMemRetain>(NativeMem);
90  }
91 
92  ur_native_handle_t Handle = 0;
93  // When doing buffer interop we don't know what device the memory should be
94  // resident on, so pass nullptr for Device param. Buffer interop may not be
95  // supported by all backends.
96  Plugin->call<UrApiKind::urMemGetNativeHandle>(NativeMem, /*Dev*/ nullptr,
97  &Handle);
98  Handles.push_back(Handle);
99  }
100 
101  addInteropObject(Handles);
102  return Handles;
103 }
104 } // namespace detail
105 } // namespace _V1
106 } // 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, ur_event_handle_t &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)
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 *)
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
std::vector< ur_native_handle_t > getNativeVector(backend BackendName) const
Definition: buffer_impl.cpp:66
void addInteropObject(std::vector< ur_native_handle_t > &Handles) const
Definition: buffer_impl.cpp:48
void * allocateMem(ContextImplPtr Context, bool InitFromUserData, void *HostPtr, ur_event_handle_t &OutEventToWait) override
Definition: buffer_impl.cpp:23
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: event_impl.hpp:32
std::shared_ptr< plugin > PluginPtr
Definition: ur.hpp:107
std::shared_ptr< detail::platform_impl > PlatformImplPtr
Definition: access.hpp:18
C++ utilities for Unified Runtime integration.