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 {
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  RT::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  RT::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  RT::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  RT::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 *allocateMemImage(
74  ContextImplPtr TargetContext, SYCLMemObjI *MemObj, void *UserPtr,
75  bool HostPtrReadOnly, size_t Size, const RT::PiMemImageDesc &Desc,
76  const RT::PiMemImageFormat &Format, const EventImplPtr &InteropEvent,
77  const ContextImplPtr &InteropContext,
78  const sycl::property_list &PropsList, RT::PiEvent &OutEventToWait);
79 
80  // Releases memory object(buffer or image). TargetContext should be device
81  // one(not host).
82  static void releaseMemObj(ContextImplPtr TargetContext, SYCLMemObjI *MemObj,
83  void *MemAllocation, void *UserPtr);
84 
85  static void *allocateHostMemory(SYCLMemObjI *MemObj, void *UserPtr,
86  bool HostPtrReadOnly, size_t Size,
87  const sycl::property_list &PropsList);
88 
89  static void *allocateInteropMemObject(ContextImplPtr TargetContext,
90  void *UserPtr,
91  const EventImplPtr &InteropEvent,
92  const ContextImplPtr &InteropContext,
93  const sycl::property_list &PropsList,
94  RT::PiEvent &OutEventToWait);
95 
96  static void *allocateImageObject(ContextImplPtr TargetContext, void *UserPtr,
97  bool HostPtrReadOnly,
98  const RT::PiMemImageDesc &Desc,
99  const RT::PiMemImageFormat &Format,
100  const sycl::property_list &PropsList);
101 
102  static void *allocateBufferObject(ContextImplPtr TargetContext, void *UserPtr,
103  bool HostPtrReadOnly, const size_t Size,
104  const sycl::property_list &PropsList);
105 
106  // Copies memory between: host and device, host and host,
107  // device and device if memory objects bound to the one context.
108  static void copy(SYCLMemObjI *SYCLMemObj, void *SrcMem, QueueImplPtr SrcQueue,
109  unsigned int DimSrc, sycl::range<3> SrcSize,
110  sycl::range<3> SrcAccessRange, sycl::id<3> SrcOffset,
111  unsigned int SrcElemSize, void *DstMem,
112  QueueImplPtr TgtQueue, unsigned int DimDst,
113  sycl::range<3> DstSize, sycl::range<3> DstAccessRange,
114  sycl::id<3> DstOffset, unsigned int DstElemSize,
115  std::vector<RT::PiEvent> DepEvents, RT::PiEvent &OutEvent);
116 
117  static void fill(SYCLMemObjI *SYCLMemObj, void *Mem, QueueImplPtr Queue,
118  size_t PatternSize, const char *Pattern, unsigned int Dim,
119  sycl::range<3> Size, sycl::range<3> AccessRange,
120  sycl::id<3> AccessOffset, unsigned int ElementSize,
121  std::vector<RT::PiEvent> DepEvents, RT::PiEvent &OutEvent);
122 
123  static void *map(SYCLMemObjI *SYCLMemObj, void *Mem, QueueImplPtr Queue,
124  access::mode AccessMode, unsigned int Dim,
125  sycl::range<3> Size, sycl::range<3> AccessRange,
126  sycl::id<3> AccessOffset, unsigned int ElementSize,
127  std::vector<RT::PiEvent> DepEvents, RT::PiEvent &OutEvent);
128 
129  static void unmap(SYCLMemObjI *SYCLMemObj, void *Mem, QueueImplPtr Queue,
130  void *MappedPtr, std::vector<RT::PiEvent> DepEvents,
131  RT::PiEvent &OutEvent);
132 
133  static void copy_usm(const void *SrcMem, QueueImplPtr Queue, size_t Len,
134  void *DstMem, std::vector<RT::PiEvent> DepEvents,
135  RT::PiEvent *OutEvent);
136 
137  static void fill_usm(void *DstMem, QueueImplPtr Queue, size_t Len,
138  int Pattern, std::vector<RT::PiEvent> DepEvents,
139  RT::PiEvent *OutEvent);
140 
141  static void prefetch_usm(void *Ptr, QueueImplPtr Queue, size_t Len,
142  std::vector<RT::PiEvent> DepEvents,
143  RT::PiEvent *OutEvent);
144 
145  static void advise_usm(const void *Ptr, QueueImplPtr Queue, size_t Len,
146  pi_mem_advice Advice,
147  std::vector<RT::PiEvent> DepEvents,
148  RT::PiEvent *OutEvent);
149 
150  static void copy_2d_usm(const void *SrcMem, size_t SrcPitch,
151  QueueImplPtr Queue, void *DstMem, size_t DstPitch,
152  size_t Width, size_t Height,
153  std::vector<RT::PiEvent> DepEvents,
154  RT::PiEvent *OutEvent);
155 
156  static void fill_2d_usm(void *DstMem, QueueImplPtr Queue, size_t Pitch,
157  size_t Width, size_t Height,
158  const std::vector<char> &Pattern,
159  std::vector<RT::PiEvent> DepEvents,
160  RT::PiEvent *OutEvent);
161 
162  static void memset_2d_usm(void *DstMem, QueueImplPtr Queue, size_t Pitch,
163  size_t Width, size_t Height, char Value,
164  std::vector<RT::PiEvent> DepEvents,
165  RT::PiEvent *OutEvent);
166 
167  static void copy_to_device_global(const void *DeviceGlobalPtr,
168  bool IsDeviceImageScoped,
169  QueueImplPtr Queue, size_t NumBytes,
170  size_t Offset, const void *SrcMem,
171  const std::vector<RT::PiEvent> &DepEvents,
172  RT::PiEvent *OutEvent);
173 
174  static void copy_from_device_global(const void *DeviceGlobalPtr,
175  bool IsDeviceImageScoped,
176  QueueImplPtr Queue, size_t NumBytes,
177  size_t Offset, void *DstMem,
178  const std::vector<RT::PiEvent> &DepEvents,
179  RT::PiEvent *OutEvent);
180 };
181 } // namespace detail
182 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
183 } // namespace sycl
sycl::_V1::detail::pi::PiMemImageDesc
::pi_image_desc PiMemImageDesc
Definition: pi.hpp:145
property_list.hpp
sycl::_V1::access::mode
mode
Definition: access.hpp:30
sycl_mem_obj_i.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
_pi_mem_advice
_pi_mem_advice
Definition: pi.h:491
sycl::_V1::memory_order::release
@ release
sycl
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
access.hpp
sycl::_V1::detail::MemoryManager
Definition: memory_manager.hpp:36
id.hpp
sycl::_V1::range< 3 >
export.hpp
range.hpp
sycl::_V1::detail::EventImplPtr
std::shared_ptr< event_impl > EventImplPtr
Definition: cg.hpp:42
sycl::_V1::detail::pi::PiMemImageFormat
::pi_image_format PiMemImageFormat
Definition: pi.hpp:144
sycl::_V1::detail::QueueImplPtr
std::shared_ptr< sycl::detail::queue_impl > QueueImplPtr
Definition: event_impl.hpp:32
sycl::_V1::detail::SYCLMemObjI
Definition: sycl_mem_obj_i.hpp:28
sycl::_V1::AccessMode
class __SYCL_EBO __SYCL_SPECIAL_CLASS __SYCL_TYPE(local_accessor) local_accessor class __SYCL_EBO __SYCL_SPECIAL_CLASS AccessMode
Definition: accessor.hpp:3059
sycl::_V1::detail::pi::PiEvent
::pi_event PiEvent
Definition: pi.hpp:138