DPC++ Runtime
Runtime libraries for oneAPI DPC++
memory_manager.hpp
Go to the documentation of this file.
1 //==-------------- memory_manager.hpp - 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 #pragma once
10 
12 #include <CL/sycl/detail/cl.h>
15 #include <CL/sycl/id.hpp>
17 #include <CL/sycl/range.hpp>
18 
19 #include <memory>
20 #include <vector>
21 
23 namespace sycl {
24 namespace detail {
25 
26 class queue_impl;
27 class event_impl;
28 class context_impl;
29 
30 using QueueImplPtr = std::shared_ptr<detail::queue_impl>;
31 using EventImplPtr = std::shared_ptr<detail::event_impl>;
32 using ContextImplPtr = std::shared_ptr<detail::context_impl>;
33 
34 // The class contains methods that work with memory. All operations with
35 // device memory should go through MemoryManager.
36 
37 class __SYCL_EXPORT MemoryManager {
38 public:
39  // The following method releases memory allocation of memory object.
40  // Depending on the context it releases memory on host or on device.
41  static void release(ContextImplPtr TargetContext, SYCLMemObjI *MemObj,
42  void *MemAllocation, std::vector<EventImplPtr> DepEvents,
43  RT::PiEvent &OutEvent);
44 
45  // The following method allocates memory allocation of memory object.
46  // Depending on the context it allocates memory on host or on device.
47  static void *allocate(ContextImplPtr TargetContext, SYCLMemObjI *MemObj,
48  bool InitFromUserData, void *HostPtr,
49  std::vector<EventImplPtr> DepEvents,
50  RT::PiEvent &OutEvent);
51 
52  // Allocates memory buffer wrapped into an image. MemObj must be a buffer,
53  // not an image.
54  // TODO not used - remove.
55  static void *wrapIntoImageBuffer(ContextImplPtr TargetContext, void *MemBuf,
56  SYCLMemObjI *MemObj);
57 
58  // Releases the image buffer created by wrapIntoImageBuffer.
59  // TODO not used - remove.
60  static void releaseImageBuffer(ContextImplPtr TargetContext, void *ImageBuf);
61 
62  // The following method creates OpenCL sub buffer for specified
63  // offset, range, and memory object.
64  static void *allocateMemSubBuffer(ContextImplPtr TargetContext,
65  void *ParentMemObj, size_t ElemSize,
66  size_t Offset, range<3> Range,
67  std::vector<EventImplPtr> DepEvents,
68  RT::PiEvent &OutEvent);
69 
70  // Allocates buffer in specified context taking into account situations such
71  // as host ptr or cl_mem provided by user. TargetContext should be device
72  // one(not host).
73  static void *allocateMemBuffer(ContextImplPtr TargetContext,
74  SYCLMemObjI *MemObj, void *UserPtr,
75  bool HostPtrReadOnly, size_t Size,
76  const EventImplPtr &InteropEvent,
77  const ContextImplPtr &InteropContext,
78  const sycl::property_list &PropsList,
79  RT::PiEvent &OutEventToWait);
80 
81  // Allocates images in specified context taking into account situations such
82  // as host ptr or cl_mem provided by user. TargetContext should be device
83  // one(not host).
84  static void *allocateMemImage(
85  ContextImplPtr TargetContext, SYCLMemObjI *MemObj, void *UserPtr,
86  bool HostPtrReadOnly, size_t Size, const RT::PiMemImageDesc &Desc,
87  const RT::PiMemImageFormat &Format, const EventImplPtr &InteropEvent,
88  const ContextImplPtr &InteropContext,
89  const sycl::property_list &PropsList, RT::PiEvent &OutEventToWait);
90 
91  // Releases memory object(buffer or image). TargetContext should be device
92  // one(not host).
93  static void releaseMemObj(ContextImplPtr TargetContext, SYCLMemObjI *MemObj,
94  void *MemAllocation, void *UserPtr);
95 
96  static void *allocateHostMemory(SYCLMemObjI *MemObj, void *UserPtr,
97  bool HostPtrReadOnly, size_t Size,
98  const sycl::property_list &PropsList);
99 
100  static void *allocateInteropMemObject(ContextImplPtr TargetContext,
101  void *UserPtr,
102  const EventImplPtr &InteropEvent,
103  const ContextImplPtr &InteropContext,
104  const sycl::property_list &PropsList,
105  RT::PiEvent &OutEventToWait);
106 
107  static void *allocateImageObject(ContextImplPtr TargetContext, void *UserPtr,
108  bool HostPtrReadOnly,
109  const RT::PiMemImageDesc &Desc,
110  const RT::PiMemImageFormat &Format,
111  const sycl::property_list &PropsList);
112 
113  static void *allocateBufferObject(ContextImplPtr TargetContext, void *UserPtr,
114  bool HostPtrReadOnly, const size_t Size,
115  const sycl::property_list &PropsList);
116 
117  // Copies memory between: host and device, host and host,
118  // device and device if memory objects bound to the one context.
119  static void copy(SYCLMemObjI *SYCLMemObj, void *SrcMem, QueueImplPtr SrcQueue,
120  unsigned int DimSrc, sycl::range<3> SrcSize,
121  sycl::range<3> SrcAccessRange, sycl::id<3> SrcOffset,
122  unsigned int SrcElemSize, void *DstMem,
123  QueueImplPtr TgtQueue, unsigned int DimDst,
124  sycl::range<3> DstSize, sycl::range<3> DstAccessRange,
125  sycl::id<3> DstOffset, unsigned int DstElemSize,
126  std::vector<RT::PiEvent> DepEvents, RT::PiEvent &OutEvent);
127 
128  static void fill(SYCLMemObjI *SYCLMemObj, void *Mem, QueueImplPtr Queue,
129  size_t PatternSize, const char *Pattern, unsigned int Dim,
130  sycl::range<3> Size, sycl::range<3> AccessRange,
131  sycl::id<3> AccessOffset, unsigned int ElementSize,
132  std::vector<RT::PiEvent> DepEvents, RT::PiEvent &OutEvent);
133 
134  static void *map(SYCLMemObjI *SYCLMemObj, void *Mem, QueueImplPtr Queue,
135  access::mode AccessMode, unsigned int Dim,
136  sycl::range<3> Size, sycl::range<3> AccessRange,
137  sycl::id<3> AccessOffset, unsigned int ElementSize,
138  std::vector<RT::PiEvent> DepEvents, RT::PiEvent &OutEvent);
139 
140  static void unmap(SYCLMemObjI *SYCLMemObj, void *Mem, QueueImplPtr Queue,
141  void *MappedPtr, std::vector<RT::PiEvent> DepEvents,
142  RT::PiEvent &OutEvent);
143 
144  static void copy_usm(const void *SrcMem, QueueImplPtr Queue, size_t Len,
145  void *DstMem, std::vector<RT::PiEvent> DepEvents,
146  RT::PiEvent *OutEvent);
147 
148  __SYCL_DEPRECATED("copy_usm() accepting PiEvent& is deprecated, use "
149  "copy_usm() accepting PiEvent* instead")
150  static void copy_usm(const void *SrcMem, QueueImplPtr Queue, size_t Len,
151  void *DstMem, std::vector<RT::PiEvent> DepEvents,
152  RT::PiEvent &OutEvent);
153 
154  static void fill_usm(void *DstMem, QueueImplPtr Queue, size_t Len,
155  int Pattern, std::vector<RT::PiEvent> DepEvents,
156  RT::PiEvent *OutEvent);
157 
158  __SYCL_DEPRECATED("fill_usm() accepting PiEvent& is deprecated, use "
159  "fill_usm() accepting PiEvent* instead")
160  static void fill_usm(void *DstMem, QueueImplPtr Queue, size_t Len,
161  int Pattern, std::vector<RT::PiEvent> DepEvents,
162  RT::PiEvent &OutEvent);
163 
164  static void prefetch_usm(void *Ptr, QueueImplPtr Queue, size_t Len,
165  std::vector<RT::PiEvent> DepEvents,
166  RT::PiEvent *OutEvent);
167 
168  __SYCL_DEPRECATED("prefetch_usm() accepting PiEvent& is deprecated, use "
169  "prefetch_usm() accepting PiEvent* instead")
170  static void prefetch_usm(void *Ptr, QueueImplPtr Queue, size_t Len,
171  std::vector<RT::PiEvent> DepEvents,
172  RT::PiEvent &OutEvent);
173 
174  static void advise_usm(const void *Ptr, QueueImplPtr Queue, size_t Len,
175  pi_mem_advice Advice,
176  std::vector<RT::PiEvent> DepEvents,
177  RT::PiEvent *OutEvent);
178 
179  __SYCL_DEPRECATED("advise_usm() accepting PiEvent& is deprecated, use "
180  "advise_usm() accepting PiEvent* instead")
181  static void advise_usm(const void *Ptr, QueueImplPtr Queue, size_t Len,
183  std::vector<RT::PiEvent> DepEvents,
184  RT::PiEvent &OutEvent);
185 };
186 } // namespace detail
187 } // namespace sycl
188 } // __SYCL_INLINE_NAMESPACE(cl)
property_list.hpp
cl::sycl::detail::ContextImplPtr
std::shared_ptr< detail::context_impl > ContextImplPtr
Definition: memory_manager.hpp:32
sycl_mem_obj_i.hpp
cl::sycl::id< 3 >
__SYCL_DEPRECATED
#define __SYCL_DEPRECATED(message)
Definition: defines_elementary.hpp:47
_pi_mem_advice
_pi_mem_advice
Definition: pi.h:459
sycl
Definition: invoke_simd.hpp:68
cl::sycl::property_list
Objects of the property_list class are containers for the SYCL properties.
Definition: property_list.hpp:26
access.hpp
id.hpp
cl::sycl::range< 3 >
export.hpp
cl.h
range.hpp
cl::sycl::detail::EventImplPtr
std::shared_ptr< detail::event_impl > EventImplPtr
Definition: memory_manager.hpp:31
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::detail::pi::PiEvent
::pi_event PiEvent
Definition: pi.hpp:113
cl::sycl::detail::MemoryManager
Definition: memory_manager.hpp:37
_pi_image_format
Definition: pi.h:931
cl::sycl::detail::QueueImplPtr
std::shared_ptr< detail::queue_impl > QueueImplPtr
Definition: memory_manager.hpp:30
_pi_image_desc
Definition: pi.h:936
std
Definition: accessor.hpp:2616
cl::sycl::detail::pi
Definition: pi.hpp:51
_pi_event
PI Event mapping to CUevent.
Definition: pi_cuda.hpp:458
cl::sycl::memory_order::release
@ release
cl::sycl::access::mode
mode
Definition: access.hpp:28
cl::sycl::instead
std::uint8_t instead
Definition: aliases.hpp:68
cl::sycl::detail::SYCLMemObjI
Definition: sycl_mem_obj_i.hpp:28
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12