DPC++ Runtime
Runtime libraries for oneAPI DPC++
sycl_mem_obj_t.cpp
Go to the documentation of this file.
1 //==------------ sycl_mem_obj_t.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 
11 #include <detail/context_impl.hpp>
12 #include <detail/event_impl.hpp>
13 #include <detail/plugin.hpp>
15 
17 namespace sycl {
18 namespace detail {
19 SYCLMemObjT::SYCLMemObjT(cl_mem MemObject, const context &SyclContext,
20  const size_t SizeInBytes, event AvailableEvent,
21  std::unique_ptr<SYCLMemObjAllocator> Allocator)
22  : SYCLMemObjT(pi::cast<pi_native_handle>(MemObject), SyclContext,
23  SizeInBytes, AvailableEvent, std::move(Allocator)) {}
24 
25 SYCLMemObjT::SYCLMemObjT(pi_native_handle MemObject, const context &SyclContext,
26  const size_t, event AvailableEvent,
27  std::unique_ptr<SYCLMemObjAllocator> Allocator)
28  : SYCLMemObjT(MemObject, SyclContext, true, AvailableEvent,
29  std::move(Allocator)) {}
30 
31 SYCLMemObjT::SYCLMemObjT(pi_native_handle MemObject, const context &SyclContext,
32  bool OwnNativeHandle, event AvailableEvent,
33  std::unique_ptr<SYCLMemObjAllocator> Allocator)
34  : MAllocator(std::move(Allocator)), MProps(),
35  MInteropEvent(detail::getSyclObjImpl(std::move(AvailableEvent))),
36  MInteropContext(detail::getSyclObjImpl(SyclContext)),
37  MOpenCLInterop(true), MHostPtrReadOnly(false), MNeedWriteBack(true),
38  MUserPtr(nullptr), MShadowCopy(nullptr), MUploadDataFunctor(nullptr),
39  MSharedPtrStorage(nullptr) {
40  if (MInteropContext->is_host())
41  throw cl::sycl::invalid_parameter_error(
42  "Creation of interoperability memory object using host context is "
43  "not allowed",
45 
46  RT::PiContext Context = nullptr;
47  const plugin &Plugin = getPlugin();
48 
50  MemObject, MInteropContext->getHandleRef(), OwnNativeHandle,
52 
53  // Get the size of the buffer in bytes
55  MInteropMemObject, PI_MEM_SIZE, sizeof(size_t), &MSizeInBytes, nullptr);
56 
58  sizeof(Context), &Context, nullptr);
59 
60  if (MInteropContext->getHandleRef() != Context)
61  throw cl::sycl::invalid_parameter_error(
62  "Input context must be the same as the context of cl_mem",
64 
65  if (Plugin.getBackend() == backend::opencl)
67 }
68 
69 void SYCLMemObjT::releaseMem(ContextImplPtr Context, void *MemAllocation) {
70  void *Ptr = getUserPtr();
71  return MemoryManager::releaseMemObj(Context, this, MemAllocation, Ptr);
72 }
73 
74 void SYCLMemObjT::updateHostMemory(void *const Ptr) {
75  const id<3> Offset{0, 0, 0};
76  const range<3> AccessRange{MSizeInBytes, 1, 1};
77  const range<3> MemoryRange{MSizeInBytes, 1, 1};
78  const access::mode AccessMode = access::mode::read;
79  SYCLMemObjI *SYCLMemObject = this;
80  const int Dims = 1;
81  const int ElemSize = 1;
82 
83  Requirement Req(Offset, AccessRange, MemoryRange, AccessMode, SYCLMemObject,
84  Dims, ElemSize);
85  Req.MData = Ptr;
86 
88  if (Event)
89  Event->wait(Event);
90 }
91 
93  if ((MUploadDataFunctor != nullptr) && MNeedWriteBack)
95 
96  // If we're attached to a memory record, process the deletion of the memory
97  // record. We may get detached before we do this.
98  if (MRecord)
101 
102  if (MOpenCLInterop) {
103  const plugin &Plugin = getPlugin();
105  pi::cast<RT::PiMem>(MInteropMemObject));
106  }
107 }
109  assert((MInteropContext != nullptr) &&
110  "Trying to get Plugin from SYCLMemObjT with nullptr ContextImpl.");
111  return (MInteropContext->getPlugin());
112 }
113 
115  cl_mem MemObject) {
116  return getBufSizeForContext(Context, pi::cast<pi_native_handle>(MemObject));
117 }
119  pi_native_handle MemObject) {
120  size_t BufSize = 0;
121  const detail::plugin &Plugin = Context->getPlugin();
122  // TODO is there something required to support non-OpenCL backends?
124  detail::pi::cast<detail::RT::PiMem>(MemObject), PI_MEM_SIZE,
125  sizeof(size_t), &BufSize, nullptr);
126  return BufSize;
127 }
128 
129 bool SYCLMemObjT::isInterop() const { return MOpenCLInterop; }
130 
132  bool InitFromUserData, void *&HostPtr,
133  bool &HostPtrReadOnly) {
134  // The data for the allocation can be provided via either the user pointer
135  // (InitFromUserData, can be read-only) or a runtime-allocated read-write
136  // HostPtr. We can have one of these scenarios:
137  // 1. The allocation is the first one and on host. InitFromUserData == true.
138  // 2. The allocation is the first one and isn't on host. InitFromUserData
139  // varies based on unified host memory support and whether or not the data can
140  // be discarded.
141  // 3. The allocation is not the first one and is on host. InitFromUserData ==
142  // false, HostPtr == nullptr. This can only happen if the allocation command
143  // is not linked since it would be a no-op otherwise. Attempt to reuse the
144  // user pointer if it's read-write, but do not copy its contents if it's not.
145  // 4. The allocation is not the first one and not on host. InitFromUserData ==
146  // false, HostPtr is provided if the command is linked. The host pointer is
147  // guaranteed to be reused in this case.
148  if (Context->is_host() && !MOpenCLInterop && !MHostPtrReadOnly)
149  InitFromUserData = true;
150 
151  if (InitFromUserData) {
152  assert(!HostPtr && "Cannot init from user data and reuse host ptr provided "
153  "simultaneously");
154  HostPtr = getUserPtr();
155  HostPtrReadOnly = MHostPtrReadOnly;
156  } else
157  HostPtrReadOnly = false;
158 }
159 } // namespace detail
160 } // namespace sycl
161 } // __SYCL_INLINE_NAMESPACE(cl)
piMemGetInfo
pi_result piMemGetInfo(pi_mem mem, pi_mem_info param_name, size_t param_value_size, void *param_value, size_t *param_value_size_ret)
Definition: pi_esimd_emulator.cpp:1081
event_impl.hpp
cl::sycl::detail::SYCLMemObjT::MSizeInBytes
size_t MSizeInBytes
Definition: sycl_mem_obj_t.hpp:354
cl::sycl::detail::SYCLMemObjT::MInteropContext
ContextImplPtr MInteropContext
Definition: sycl_mem_obj_t.hpp:342
cl::sycl::detail::ContextImplPtr
std::shared_ptr< detail::context_impl > ContextImplPtr
Definition: memory_manager.hpp:32
context_impl.hpp
cl::sycl::detail::MemoryManager::releaseMemObj
static void releaseMemObj(ContextImplPtr TargetContext, SYCLMemObjI *MemObj, void *MemAllocation, void *UserPtr)
Definition: memory_manager.cpp:254
cl::sycl::detail::SYCLMemObjT::isInterop
bool isInterop() const
Definition: sycl_mem_obj_t.cpp:129
cl::sycl::event
An event object can be used to synchronize memory transfers, enqueues of kernels and signaling barrie...
Definition: event.hpp:31
cl::sycl::detail::SYCLMemObjT::releaseMem
void releaseMem(ContextImplPtr Context, void *MemAllocation) override
Definition: sycl_mem_obj_t.cpp:69
cl::sycl::detail::SYCLMemObjT::determineHostPtr
void determineHostPtr(const ContextImplPtr &Context, bool InitFromUserData, void *&HostPtr, bool &HostPtrReadOnly)
Definition: sycl_mem_obj_t.cpp:131
cl::sycl::backend::opencl
@ opencl
cl::sycl::access::mode::read
@ read
cl::sycl::id< 3 >
cl::sycl::detail::AccessorImplHost::MData
void * MData
Definition: accessor_impl.hpp:119
cl::sycl::detail::Scheduler::addCopyBack
EventImplPtr addCopyBack(Requirement *Req)
Registers a command group, that copies most recent memory to the memory pointed by the requirement.
Definition: scheduler.cpp:173
cl::sycl::detail::Scheduler::removeMemoryObject
void removeMemoryObject(detail::SYCLMemObjI *MemObj)
Removes buffer from the graph.
Definition: scheduler.cpp:261
PI_MEM_SIZE
@ PI_MEM_SIZE
Definition: pi.h:954
sycl
Definition: invoke_simd.hpp:68
plugin.hpp
cl::sycl::detail::SYCLMemObjT::MInteropMemObject
RT::PiMem MInteropMemObject
Definition: sycl_mem_obj_t.hpp:345
cast
To cast(From value)
Definition: pi_opencl.cpp:42
scheduler.hpp
cl::sycl::range< 3 >
cl::sycl::detail::plugin::getBackend
backend getBackend(void) const
Definition: plugin.hpp:229
cl::sycl::detail::AccessorImplHost
Definition: accessor_impl.hpp:74
piMemRelease
pi_result piMemRelease(pi_mem mem)
Definition: pi_esimd_emulator.cpp:1093
cl::sycl::detail::Scheduler::getInstance
static Scheduler & getInstance()
Definition: scheduler.cpp:209
cl::sycl::detail::SYCLMemObjT::getBufSizeForContext
static size_t getBufSizeForContext(const ContextImplPtr &Context, cl_mem MemObject)
Definition: sycl_mem_obj_t.cpp:114
cl::sycl::detail::SYCLMemObjI::MRecord
std::shared_ptr< MemObjRecord > MRecord
Definition: sycl_mem_obj_i.hpp:74
cl::sycl::detail::SYCLMemObjT::MOpenCLInterop
bool MOpenCLInterop
Definition: sycl_mem_obj_t.hpp:348
cl::sycl::detail::EventImplPtr
std::shared_ptr< detail::event_impl > EventImplPtr
Definition: memory_manager.hpp:31
cl::sycl::detail::plugin::call
void call(ArgsT... Args) const
Calls the API, traces the call, checks the result.
Definition: plugin.hpp:217
cl::sycl::detail::SYCLMemObjT::releaseHostMem
__SYCL_DLL_LOCAL void releaseHostMem(void *Ptr) override
Definition: sycl_mem_obj_t.hpp:136
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::detail::plugin
The plugin class provides a unified interface to the underlying low-level runtimes for the device-agn...
Definition: plugin.hpp:90
cl::sycl::detail::SYCLMemObjT::getPlugin
const plugin & getPlugin() const
Definition: sycl_mem_obj_t.cpp:108
pi_native_handle
uintptr_t pi_native_handle
Definition: pi.h:76
cl::sycl::detail::SYCLMemObjT::MUploadDataFunctor
std::function< void(void)> MUploadDataFunctor
Definition: sycl_mem_obj_t.hpp:360
PI_INVALID_CONTEXT
@ PI_INVALID_CONTEXT
Definition: pi.h:92
PI_MEM_CONTEXT
@ PI_MEM_CONTEXT
Definition: pi.h:953
piMemRetain
pi_result piMemRetain(pi_mem mem)
Definition: pi_esimd_emulator.cpp:1085
cl::sycl::detail::getSyclObjImpl
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: common.hpp:198
std
Definition: accessor.hpp:2616
cl::sycl::detail::SYCLMemObjT::getUserPtr
__SYCL_DLL_LOCAL void * getUserPtr() const
Definition: sycl_mem_obj_t.hpp:143
cl::sycl::context
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:35
cl::sycl::detail::SYCLMemObjT::updateHostMemory
void updateHostMemory()
Definition: sycl_mem_obj_t.cpp:92
cl::sycl::detail::SYCLMemObjT::SYCLMemObjT
SYCLMemObjT(const size_t SizeInBytes, const property_list &Props, std::unique_ptr< SYCLMemObjAllocator > Allocator)
Definition: sycl_mem_obj_t.hpp:66
piextMemCreateWithNativeHandle
pi_result piextMemCreateWithNativeHandle(pi_native_handle nativeHandle, pi_context context, bool ownNativeHandle, pi_mem *mem)
Creates PI mem object from a native handle.
Definition: pi_esimd_emulator.cpp:1281
cl::sycl::access::mode
mode
Definition: access.hpp:28
cl::sycl::detail::SYCLMemObjT
Definition: sycl_mem_obj_t.hpp:43
cl::sycl::detail::SYCLMemObjT::MHostPtrReadOnly
bool MHostPtrReadOnly
Definition: sycl_mem_obj_t.hpp:350
cl::sycl::detail::SYCLMemObjI
Definition: sycl_mem_obj_i.hpp:28
cl::sycl::detail::SYCLMemObjT::MNeedWriteBack
bool MNeedWriteBack
Definition: sycl_mem_obj_t.hpp:352
sycl_mem_obj_t.hpp
cl::sycl::detail::SYCLMemObjT::MShadowCopy
void * MShadowCopy
Definition: sycl_mem_obj_t.hpp:358
_pi_context
PI context mapping to a CUDA context object.
Definition: pi_cuda.hpp:150
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12
memory_manager.hpp