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 <sycl/access/access.hpp>
13 #include <sycl/detail/export.hpp>
14 #include <sycl/id.hpp>
15 #include <sycl/property_list.hpp>
16 #include <sycl/range.hpp>
17 
18 #include <memory>
19 #include <vector>
20 
21 namespace sycl {
22 inline namespace _V1 {
23 namespace detail {
24 
25 class queue_impl;
26 class event_impl;
27 class context_impl;
28 
29 using QueueImplPtr = std::shared_ptr<detail::queue_impl>;
30 using EventImplPtr = std::shared_ptr<detail::event_impl>;
31 using ContextImplPtr = std::shared_ptr<detail::context_impl>;
32 
33 // The class contains methods that work with memory. All operations with
34 // device memory should go through MemoryManager.
35 
36 class __SYCL_EXPORT MemoryManager {
37 public:
38  // The following method releases memory allocation of memory object.
39  // Depending on the context it releases memory on host or on device.
40  static void release(ContextImplPtr TargetContext, SYCLMemObjI *MemObj,
41  void *MemAllocation, std::vector<EventImplPtr> DepEvents,
42  sycl::detail::pi::PiEvent &OutEvent);
43 
44  // The following method allocates memory allocation of memory object.
45  // Depending on the context it allocates memory on host or on device.
46  static void *allocate(ContextImplPtr TargetContext, SYCLMemObjI *MemObj,
47  bool InitFromUserData, void *HostPtr,
48  std::vector<EventImplPtr> DepEvents,
49  sycl::detail::pi::PiEvent &OutEvent);
50 
51  // The following method creates OpenCL sub buffer for specified
52  // offset, range, and memory object.
53  static void *allocateMemSubBuffer(ContextImplPtr TargetContext,
54  void *ParentMemObj, size_t ElemSize,
55  size_t Offset, range<3> Range,
56  std::vector<EventImplPtr> DepEvents,
57  sycl::detail::pi::PiEvent &OutEvent);
58 
59  // Allocates buffer in specified context taking into account situations such
60  // as host ptr or cl_mem provided by user. TargetContext should be device
61  // one(not host).
62  static void *allocateMemBuffer(ContextImplPtr TargetContext,
63  SYCLMemObjI *MemObj, void *UserPtr,
64  bool HostPtrReadOnly, size_t Size,
65  const EventImplPtr &InteropEvent,
66  const ContextImplPtr &InteropContext,
67  const sycl::property_list &PropsList,
68  sycl::detail::pi::PiEvent &OutEventToWait);
69 
70  // Allocates images 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 *
74  allocateMemImage(ContextImplPtr TargetContext, SYCLMemObjI *MemObj,
75  void *UserPtr, bool HostPtrReadOnly, size_t Size,
78  const EventImplPtr &InteropEvent,
79  const ContextImplPtr &InteropContext,
80  const sycl::property_list &PropsList,
81  sycl::detail::pi::PiEvent &OutEventToWait);
82 
83  // Releases memory object(buffer or image). TargetContext should be device
84  // one(not host).
85  static void releaseMemObj(ContextImplPtr TargetContext, SYCLMemObjI *MemObj,
86  void *MemAllocation, void *UserPtr);
87 
88  static void *allocateHostMemory(SYCLMemObjI *MemObj, void *UserPtr,
89  bool HostPtrReadOnly, size_t Size,
90  const sycl::property_list &PropsList);
91 
92  static void *
93  allocateInteropMemObject(ContextImplPtr TargetContext, void *UserPtr,
94  const EventImplPtr &InteropEvent,
95  const ContextImplPtr &InteropContext,
96  const sycl::property_list &PropsList,
97  sycl::detail::pi::PiEvent &OutEventToWait);
98 
99  static void *
100  allocateImageObject(ContextImplPtr TargetContext, void *UserPtr,
101  bool HostPtrReadOnly,
104  const sycl::property_list &PropsList);
105 
106  static void *allocateBufferObject(ContextImplPtr TargetContext, void *UserPtr,
107  bool HostPtrReadOnly, const size_t Size,
108  const sycl::property_list &PropsList);
109 
110  // Copies memory between: host and device, host and host,
111  // device and device if memory objects bound to the one context.
112  static void copy(SYCLMemObjI *SYCLMemObj, void *SrcMem, QueueImplPtr SrcQueue,
113  unsigned int DimSrc, sycl::range<3> SrcSize,
114  sycl::range<3> SrcAccessRange, sycl::id<3> SrcOffset,
115  unsigned int SrcElemSize, void *DstMem,
116  QueueImplPtr TgtQueue, unsigned int DimDst,
117  sycl::range<3> DstSize, sycl::range<3> DstAccessRange,
118  sycl::id<3> DstOffset, unsigned int DstElemSize,
119  std::vector<sycl::detail::pi::PiEvent> DepEvents,
120  sycl::detail::pi::PiEvent &OutEvent,
121  const detail::EventImplPtr &OutEventImpl);
122 
123  // TODO: This function will remain until ABI-breaking change
124  static void copy(SYCLMemObjI *SYCLMemObj, void *SrcMem, QueueImplPtr SrcQueue,
125  unsigned int DimSrc, sycl::range<3> SrcSize,
126  sycl::range<3> SrcAccessRange, sycl::id<3> SrcOffset,
127  unsigned int SrcElemSize, void *DstMem,
128  QueueImplPtr TgtQueue, unsigned int DimDst,
129  sycl::range<3> DstSize, sycl::range<3> DstAccessRange,
130  sycl::id<3> DstOffset, unsigned int DstElemSize,
131  std::vector<sycl::detail::pi::PiEvent> DepEvents,
132  sycl::detail::pi::PiEvent &OutEvent);
133 
134  static void fill(SYCLMemObjI *SYCLMemObj, void *Mem, QueueImplPtr Queue,
135  size_t PatternSize, const char *Pattern, unsigned int Dim,
136  sycl::range<3> Size, sycl::range<3> AccessRange,
137  sycl::id<3> AccessOffset, unsigned int ElementSize,
138  std::vector<sycl::detail::pi::PiEvent> DepEvents,
139  sycl::detail::pi::PiEvent &OutEvent,
140  const detail::EventImplPtr &OutEventImpl);
141 
142  // TODO: This function will remain until ABI-breaking change
143  static void fill(SYCLMemObjI *SYCLMemObj, void *Mem, QueueImplPtr Queue,
144  size_t PatternSize, const char *Pattern, unsigned int Dim,
145  sycl::range<3> Size, sycl::range<3> AccessRange,
146  sycl::id<3> AccessOffset, unsigned int ElementSize,
147  std::vector<sycl::detail::pi::PiEvent> DepEvents,
148  sycl::detail::pi::PiEvent &OutEvent);
149 
150  static void *map(SYCLMemObjI *SYCLMemObj, void *Mem, QueueImplPtr Queue,
151  access::mode AccessMode, unsigned int Dim,
152  sycl::range<3> Size, sycl::range<3> AccessRange,
153  sycl::id<3> AccessOffset, unsigned int ElementSize,
154  std::vector<sycl::detail::pi::PiEvent> DepEvents,
155  sycl::detail::pi::PiEvent &OutEvent);
156 
157  static void unmap(SYCLMemObjI *SYCLMemObj, void *Mem, QueueImplPtr Queue,
158  void *MappedPtr,
159  std::vector<sycl::detail::pi::PiEvent> DepEvents,
160  sycl::detail::pi::PiEvent &OutEvent);
161 
162  static void copy_usm(const void *SrcMem, QueueImplPtr Queue, size_t Len,
163  void *DstMem,
164  std::vector<sycl::detail::pi::PiEvent> DepEvents,
165  sycl::detail::pi::PiEvent *OutEvent,
166  const detail::EventImplPtr &OutEventImpl);
167 
168  // TODO: This function will remain until ABI-breaking change
169  static void copy_usm(const void *SrcMem, QueueImplPtr Queue, size_t Len,
170  void *DstMem,
171  std::vector<sycl::detail::pi::PiEvent> DepEvents,
172  sycl::detail::pi::PiEvent *OutEvent);
173 
174  static void fill_usm(void *DstMem, QueueImplPtr Queue, size_t Len,
175  int Pattern,
176  std::vector<sycl::detail::pi::PiEvent> DepEvents,
177  sycl::detail::pi::PiEvent *OutEvent,
178  const detail::EventImplPtr &OutEventImpl);
179 
180  // TODO: This function will remain until ABI-breaking change
181  static void fill_usm(void *DstMem, QueueImplPtr Queue, size_t Len,
182  int Pattern,
183  std::vector<sycl::detail::pi::PiEvent> DepEvents,
184  sycl::detail::pi::PiEvent *OutEvent);
185 
186  static void prefetch_usm(void *Ptr, QueueImplPtr Queue, size_t Len,
187  std::vector<sycl::detail::pi::PiEvent> DepEvents,
188  sycl::detail::pi::PiEvent *OutEvent,
189  const detail::EventImplPtr &OutEventImpl);
190 
191  // TODO: This function will remain until ABI-breaking change
192  static void prefetch_usm(void *Ptr, QueueImplPtr Queue, size_t Len,
193  std::vector<sycl::detail::pi::PiEvent> DepEvents,
194  sycl::detail::pi::PiEvent *OutEvent);
195 
196  static void advise_usm(const void *Ptr, QueueImplPtr Queue, size_t Len,
197  pi_mem_advice Advice,
198  std::vector<sycl::detail::pi::PiEvent> DepEvents,
199  sycl::detail::pi::PiEvent *OutEvent,
200  const detail::EventImplPtr &OutEventImpl);
201 
202  // TODO: This function will remain until ABI-breaking change
203  static void advise_usm(const void *Ptr, QueueImplPtr Queue, size_t Len,
204  pi_mem_advice Advice,
205  std::vector<sycl::detail::pi::PiEvent> DepEvents,
206  sycl::detail::pi::PiEvent *OutEvent);
207 
208  static void copy_2d_usm(const void *SrcMem, size_t SrcPitch,
209  QueueImplPtr Queue, void *DstMem, size_t DstPitch,
210  size_t Width, size_t Height,
211  std::vector<sycl::detail::pi::PiEvent> DepEvents,
212  sycl::detail::pi::PiEvent *OutEvent,
213  const detail::EventImplPtr &OutEventImpl);
214 
215  // TODO: This function will remain until ABI-breaking change
216  static void copy_2d_usm(const void *SrcMem, size_t SrcPitch,
217  QueueImplPtr Queue, void *DstMem, size_t DstPitch,
218  size_t Width, size_t Height,
219  std::vector<sycl::detail::pi::PiEvent> DepEvents,
220  sycl::detail::pi::PiEvent *OutEvent);
221 
222  static void fill_2d_usm(void *DstMem, QueueImplPtr Queue, size_t Pitch,
223  size_t Width, size_t Height,
224  const std::vector<char> &Pattern,
225  std::vector<sycl::detail::pi::PiEvent> DepEvents,
226  sycl::detail::pi::PiEvent *OutEvent,
227  const detail::EventImplPtr &OutEventImpl);
228 
229  // TODO: This function will remain until ABI-breaking change
230  static void fill_2d_usm(void *DstMem, QueueImplPtr Queue, size_t Pitch,
231  size_t Width, size_t Height,
232  const std::vector<char> &Pattern,
233  std::vector<sycl::detail::pi::PiEvent> DepEvents,
234  sycl::detail::pi::PiEvent *OutEvent);
235 
236  static void memset_2d_usm(void *DstMem, QueueImplPtr Queue, size_t Pitch,
237  size_t Width, size_t Height, char Value,
238  std::vector<sycl::detail::pi::PiEvent> DepEvents,
239  sycl::detail::pi::PiEvent *OutEvent,
240  const detail::EventImplPtr &OutEventImpl);
241 
242  // TODO: This function will remain until ABI-breaking change
243  static void memset_2d_usm(void *DstMem, QueueImplPtr Queue, size_t Pitch,
244  size_t Width, size_t Height, char Value,
245  std::vector<sycl::detail::pi::PiEvent> DepEvents,
246  sycl::detail::pi::PiEvent *OutEvent);
247 
248  static void
249  copy_to_device_global(const void *DeviceGlobalPtr, bool IsDeviceImageScoped,
250  QueueImplPtr Queue, size_t NumBytes, size_t Offset,
251  const void *SrcMem,
252  const std::vector<sycl::detail::pi::PiEvent> &DepEvents,
253  sycl::detail::pi::PiEvent *OutEvent,
254  const detail::EventImplPtr &OutEventImpl);
255 
256  // TODO: This function will remain until ABI-breaking change
257  static void
258  copy_to_device_global(const void *DeviceGlobalPtr, bool IsDeviceImageScoped,
259  QueueImplPtr Queue, size_t NumBytes, size_t Offset,
260  const void *SrcMem,
261  const std::vector<sycl::detail::pi::PiEvent> &DepEvents,
262  sycl::detail::pi::PiEvent *OutEvent);
263 
264  static void copy_from_device_global(
265  const void *DeviceGlobalPtr, bool IsDeviceImageScoped, QueueImplPtr Queue,
266  size_t NumBytes, size_t Offset, void *DstMem,
267  const std::vector<sycl::detail::pi::PiEvent> &DepEvents,
268  sycl::detail::pi::PiEvent *OutEvent,
269  const detail::EventImplPtr &OutEventImpl);
270 
271  // TODO: This function will remain until ABI-breaking change
272  static void copy_from_device_global(
273  const void *DeviceGlobalPtr, bool IsDeviceImageScoped, QueueImplPtr Queue,
274  size_t NumBytes, size_t Offset, void *DstMem,
275  const std::vector<sycl::detail::pi::PiEvent> &DepEvents,
276  sycl::detail::pi::PiEvent *OutEvent);
277 
278  // Command buffer extension methods
279  static void ext_oneapi_copyD2D_cmd_buffer(
282  SYCLMemObjI *SYCLMemObj, void *SrcMem, unsigned int DimSrc,
283  sycl::range<3> SrcSize, sycl::range<3> SrcAccessRange,
284  sycl::id<3> SrcOffset, unsigned int SrcElemSize, void *DstMem,
285  unsigned int DimDst, sycl::range<3> DstSize,
286  sycl::range<3> DstAccessRange, sycl::id<3> DstOffset,
287  unsigned int DstElemSize,
288  std::vector<sycl::detail::pi::PiExtSyncPoint> Deps,
289  sycl::detail::pi::PiExtSyncPoint *OutSyncPoint);
290 
291  static void ext_oneapi_copyD2H_cmd_buffer(
294  SYCLMemObjI *SYCLMemObj, void *SrcMem, unsigned int DimSrc,
295  sycl::range<3> SrcSize, sycl::range<3> SrcAccessRange,
296  sycl::id<3> SrcOffset, unsigned int SrcElemSize, char *DstMem,
297  unsigned int DimDst, sycl::range<3> DstSize, sycl::id<3> DstOffset,
298  unsigned int DstElemSize,
299  std::vector<sycl::detail::pi::PiExtSyncPoint> Deps,
300  sycl::detail::pi::PiExtSyncPoint *OutSyncPoint);
301 
302  static void ext_oneapi_copyH2D_cmd_buffer(
305  SYCLMemObjI *SYCLMemObj, char *SrcMem, unsigned int DimSrc,
306  sycl::range<3> SrcSize, sycl::id<3> SrcOffset, unsigned int SrcElemSize,
307  void *DstMem, unsigned int DimDst, sycl::range<3> DstSize,
308  sycl::range<3> DstAccessRange, sycl::id<3> DstOffset,
309  unsigned int DstElemSize,
310  std::vector<sycl::detail::pi::PiExtSyncPoint> Deps,
311  sycl::detail::pi::PiExtSyncPoint *OutSyncPoint);
312 
313  static void ext_oneapi_copy_usm_cmd_buffer(
314  ContextImplPtr Context, const void *SrcMem,
315  sycl::detail::pi::PiExtCommandBuffer CommandBuffer, size_t Len,
316  void *DstMem, std::vector<sycl::detail::pi::PiExtSyncPoint> Deps,
317  sycl::detail::pi::PiExtSyncPoint *OutSyncPoint);
318 
319  static void ext_oneapi_fill_usm_cmd_buffer(
321  sycl::detail::pi::PiExtCommandBuffer CommandBuffer, void *DstMem,
322  size_t Len, int Pattern,
323  std::vector<sycl::detail::pi::PiExtSyncPoint> Deps,
324  sycl::detail::pi::PiExtSyncPoint *OutSyncPoint);
325 
326  static void
327  ext_oneapi_fill_cmd_buffer(sycl::detail::ContextImplPtr Context,
329  SYCLMemObjI *SYCLMemObj, void *Mem,
330  size_t PatternSize, const char *Pattern,
331  unsigned int Dim, sycl::range<3> Size,
332  sycl::range<3> AccessRange,
333  sycl::id<3> AccessOffset, unsigned int ElementSize,
334  std::vector<sycl::detail::pi::PiExtSyncPoint> Deps,
335  sycl::detail::pi::PiExtSyncPoint *OutSyncPoint);
336 
337  static void ext_oneapi_prefetch_usm_cmd_buffer(
339  sycl::detail::pi::PiExtCommandBuffer CommandBuffer, void *Mem,
340  size_t Length, std::vector<sycl::detail::pi::PiExtSyncPoint> Deps,
341  sycl::detail::pi::PiExtSyncPoint *OutSyncPoint);
342 
343  static void ext_oneapi_advise_usm_cmd_buffer(
345  sycl::detail::pi::PiExtCommandBuffer CommandBuffer, const void *Mem,
346  size_t Length, pi_mem_advice Advice,
347  std::vector<sycl::detail::pi::PiExtSyncPoint> Deps,
348  sycl::detail::pi::PiExtSyncPoint *OutSyncPoint);
349 
350  static void
351  copy_image_bindless(void *Src, QueueImplPtr Queue, void *Dst,
359  const std::vector<sycl::detail::pi::PiEvent> &DepEvents,
360  sycl::detail::pi::PiEvent *OutEvent);
361 };
362 } // namespace detail
363 } // namespace _V1
364 } // namespace sycl
A unique identifier of an item in an index space.
Definition: id.hpp:36
Objects of the property_list class are containers for the SYCL properties.
::pi_ext_sync_point PiExtSyncPoint
Definition: pi.hpp:156
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: event_impl.hpp:32
std::shared_ptr< event_impl > EventImplPtr
Definition: cg.hpp:43
std::shared_ptr< sycl::detail::queue_impl > QueueImplPtr
Definition: event_impl.hpp:34
void copy(handler &CGH, const T *Src, T *Dest, size_t Count)
void fill(sycl::handler &CGH, T *Ptr, const T &Pattern, size_t Count)
class __SYCL_EBO __SYCL_SPECIAL_CLASS AccessMode
Definition: access.hpp:18
_pi_mem_advice
Definition: pi.h:621
_pi_image_copy_flags
Definition: pi.h:698