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 {
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, RT::PiEvent &OutEventToWait) {
24  bool HostPtrReadOnly = false;
25  BaseT::determineHostPtr(Context, InitFromUserData, HostPtr, HostPtrReadOnly);
26 
27  assert(!(nullptr == HostPtr && BaseT::useHostPtr() && Context->is_host()) &&
28  "Internal error. Allocating memory on the host "
29  "while having use_host_ptr property");
30  return MemoryManager::allocateMemBuffer(
31  std::move(Context), this, HostPtr, HostPtrReadOnly,
32  BaseT::getSizeInBytes(), BaseT::MInteropEvent, BaseT::MInteropContext,
33  MProps, OutEventToWait);
34 }
36  void *UserObj, const void *HostObj,
37  const void *Type, uint32_t Dim,
38  uint32_t ElemSize, size_t Range[3]) {
39  XPTIRegistry::bufferConstructorNotification(UserObj, CodeLoc, HostObj, Type,
40  Dim, ElemSize, Range);
41 }
42 
43 void buffer_impl::destructorNotification(void *UserObj) {
44  XPTIRegistry::bufferDestructorNotification(UserObj);
45 }
46 
47 void buffer_impl::addInteropObject(
48  std::vector<pi_native_handle> &Handles) const {
49  if (MOpenCLInterop) {
50  if (std::find(Handles.begin(), Handles.end(),
51  pi::cast<pi_native_handle>(MInteropMemObject)) ==
52  Handles.end()) {
53  const plugin &Plugin = getPlugin();
55  pi::cast<RT::PiMem>(MInteropMemObject));
56  Handles.push_back(pi::cast<pi_native_handle>(MInteropMemObject));
57  }
58  }
59 }
60 
61 std::vector<pi_native_handle>
62 buffer_impl::getNativeVector(backend BackendName) const {
63  std::vector<pi_native_handle> Handles{};
64  if (!MRecord) {
65  addInteropObject(Handles);
66  return Handles;
67  }
68 
69  for (auto &Cmd : MRecord->MAllocaCommands) {
70  RT::PiMem NativeMem = pi::cast<RT::PiMem>(Cmd->getMemAllocation());
71  auto Ctx = Cmd->getWorkerContext();
72  auto Platform = Ctx->getPlatformImpl();
73  // If Host Shared Memory is not supported then there is alloca for host that
74  // doesn't have platform
75  if (!Platform)
76  continue;
77  auto Plugin = Platform->getPlugin();
78 
79  if (Plugin.getBackend() != BackendName)
80  continue;
81  if (Plugin.getBackend() == backend::opencl) {
82  Plugin.call<PiApiKind::piMemRetain>(NativeMem);
83  }
84 
85  pi_native_handle Handle;
86  Plugin.call<PiApiKind::piextMemGetNativeHandle>(NativeMem, &Handle);
87  Handles.push_back(Handle);
88  }
89 
90  addInteropObject(Handles);
91  return Handles;
92 }
93 } // namespace detail
94 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
95 } // namespace sycl
sycl::_V1::backend
backend
Definition: backend_types.hpp:21
context_impl.hpp
sycl::_V1::detail::ContextImplPtr
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: event_impl.hpp:30
__SYCL_INLINE_VER_NAMESPACE
#define __SYCL_INLINE_VER_NAMESPACE(X)
Definition: defines_elementary.hpp:11
xpti_registry.hpp
sycl::_V1::detail::constructorNotification
void constructorNotification(void *BufferObj, void *AccessorObj, access::target Target, access::mode Mode, const code_location &CodeLoc)
sycl
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
scheduler.hpp
sycl::_V1::detail::pi::getPlugin
const plugin & getPlugin()
Definition: pi.cpp:515
sycl::_V1::detail::plugin
The plugin class provides a unified interface to the underlying low-level runtimes for the device-agn...
Definition: plugin.hpp:90
global_handler.hpp
sycl::_V1::detail::pi::PiMem
::pi_mem PiMem
Definition: pi.hpp:134
pi_native_handle
uintptr_t pi_native_handle
Definition: pi.h:128
piextMemGetNativeHandle
pi_result piextMemGetNativeHandle(pi_mem mem, pi_native_handle *nativeHandle)
Gets the native handle of a PI mem object.
Definition: pi_esimd_emulator.cpp:1291
sycl::_V1::detail::code_location
Definition: common.hpp:66
piMemRetain
pi_result piMemRetain(pi_mem mem)
Definition: pi_esimd_emulator.cpp:1097
sycl::_V1::detail::plugin::call
void call(ArgsT... Args) const
Calls the API, traces the call, checks the result.
Definition: plugin.hpp:217
sycl::_V1::detail::pi::PiEvent
::pi_event PiEvent
Definition: pi.hpp:136
buffer_impl.hpp
memory_manager.hpp