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(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(urMemRetain, ur::cast<ur_mem_handle_t>(MInteropMemObject));
56  ur_native_handle_t NativeHandle = 0;
57  Plugin->call(urMemGetNativeHandle, MInteropMemObject, nullptr,
58  &NativeHandle);
59  Handles.push_back(NativeHandle);
60  }
61  }
62 }
63 
64 std::vector<ur_native_handle_t>
66  std::vector<ur_native_handle_t> Handles{};
67  if (!MRecord) {
68  addInteropObject(Handles);
69  return Handles;
70  }
71 
72  for (auto &Cmd : MRecord->MAllocaCommands) {
73  ur_mem_handle_t NativeMem =
74  ur::cast<ur_mem_handle_t>(Cmd->getMemAllocation());
75  auto Ctx = Cmd->getWorkerContext();
76  // If Host Shared Memory is not supported then there is alloca for host that
77  // doesn't have context and platform
78  if (!Ctx)
79  continue;
80  PlatformImplPtr Platform = Ctx->getPlatformImpl();
81  assert(Platform && "Platform must be present for device context");
82  if (Platform->getBackend() != BackendName)
83  continue;
84 
85  auto Plugin = Platform->getPlugin();
86 
87  if (Platform->getBackend() == backend::opencl) {
88  Plugin->call(urMemRetain, NativeMem);
89  }
90 
91  ur_native_handle_t Handle = 0;
92  // When doing buffer interop we don't know what device the memory should be
93  // resident on, so pass nullptr for Device param. Buffer interop may not be
94  // supported by all backends.
95  Plugin->call(urMemGetNativeHandle, NativeMem, /*Dev*/ nullptr, &Handle);
96  Handles.push_back(Handle);
97  }
98 
99  addInteropObject(Handles);
100  return Handles;
101 }
102 } // namespace detail
103 } // namespace _V1
104 } // 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
void determineHostPtr(bool InitFromUserData, void *&HostPtr, bool &HostPtrReadOnly)
size_t getSizeInBytes() const noexcept override
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:65
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:60
std::shared_ptr< detail::platform_impl > PlatformImplPtr
Definition: access.hpp:18
C++ utilities for Unified Runtime integration.