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 } // namespace detail
151 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
152 } // namespace sycl
#define __SYCL_INLINE_VER_NAMESPACE(X)
::pi_event PiEvent
Definition: pi.hpp:121
::pi_image_desc PiMemImageDesc
Definition: pi.hpp:128
::pi_image_format PiMemImageFormat
Definition: pi.hpp:127
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: event_impl.hpp:30
std::shared_ptr< event_impl > EventImplPtr
Definition: cg.hpp:42
std::shared_ptr< sycl::detail::queue_impl > QueueImplPtr
Definition: event_impl.hpp:32
class __SYCL_SPECIAL_CLASS __SYCL_TYPE(local_accessor) local_accessor class __SYCL_SPECIAL_CLASS AccessMode
Definition: accessor.hpp:2747
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
_pi_mem_advice
Definition: pi.h:422