DPC++ Runtime
Runtime libraries for oneAPI Data Parallel C++
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>
16 #include <CL/sycl/range.hpp>
17 
18 #include <memory>
19 #include <vector>
20 
22 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  // Allocates memory buffer wrapped into an image. MemObj must be a buffer,
52  // not an image.
53  // TODO not used - remove.
54  static void *wrapIntoImageBuffer(ContextImplPtr TargetContext, void *MemBuf,
55  SYCLMemObjI *MemObj);
56 
57  // Releases the image buffer created by wrapIntoImageBuffer.
58  // TODO not used - remove.
59  static void releaseImageBuffer(ContextImplPtr TargetContext, void *ImageBuf);
60 
61  // The following method creates OpenCL sub buffer for specified
62  // offset, range, and memory object.
63  static void *allocateMemSubBuffer(ContextImplPtr TargetContext,
64  void *ParentMemObj, size_t ElemSize,
65  size_t Offset, range<3> Range,
66  std::vector<EventImplPtr> DepEvents,
67  RT::PiEvent &OutEvent);
68 
69  // Allocates buffer in specified context taking into account situations such
70  // as host ptr or cl_mem provided by user. TargetContext should be device
71  // one(not host).
72  static void *allocateMemBuffer(ContextImplPtr TargetContext,
73  SYCLMemObjI *MemObj, void *UserPtr,
74  bool HostPtrReadOnly, size_t Size,
75  const EventImplPtr &InteropEvent,
76  const ContextImplPtr &InteropContext,
77  const sycl::property_list &PropsList,
78  RT::PiEvent &OutEventToWait);
79 
80  // Allocates images in specified context taking into account situations such
81  // as host ptr or cl_mem provided by user. TargetContext should be device
82  // one(not host).
83  static void *allocateMemImage(
84  ContextImplPtr TargetContext, SYCLMemObjI *MemObj, void *UserPtr,
85  bool HostPtrReadOnly, size_t Size, const RT::PiMemImageDesc &Desc,
86  const RT::PiMemImageFormat &Format, const EventImplPtr &InteropEvent,
87  const ContextImplPtr &InteropContext,
88  const sycl::property_list &PropsList, RT::PiEvent &OutEventToWait);
89 
90  // Releases memory object(buffer or image). TargetContext should be device
91  // one(not host).
92  static void releaseMemObj(ContextImplPtr TargetContext, SYCLMemObjI *MemObj,
93  void *MemAllocation, void *UserPtr);
94 
95  static void *allocateHostMemory(SYCLMemObjI *MemObj, void *UserPtr,
96  bool HostPtrReadOnly, size_t Size,
97  const sycl::property_list &PropsList);
98 
99  static void *allocateInteropMemObject(ContextImplPtr TargetContext,
100  void *UserPtr,
101  const EventImplPtr &InteropEvent,
102  const ContextImplPtr &InteropContext,
103  const sycl::property_list &PropsList,
104  RT::PiEvent &OutEventToWait);
105 
106  static void *allocateImageObject(ContextImplPtr TargetContext, void *UserPtr,
107  bool HostPtrReadOnly,
108  const RT::PiMemImageDesc &Desc,
109  const RT::PiMemImageFormat &Format,
110  const sycl::property_list &PropsList);
111 
112  static void *allocateBufferObject(ContextImplPtr TargetContext, void *UserPtr,
113  bool HostPtrReadOnly, const size_t Size,
114  const sycl::property_list &PropsList);
115 
116  // Copies memory between: host and device, host and host,
117  // device and device if memory objects bound to the one context.
118  static void copy(SYCLMemObjI *SYCLMemObj, void *SrcMem, QueueImplPtr SrcQueue,
119  unsigned int DimSrc, sycl::range<3> SrcSize,
120  sycl::range<3> SrcAccessRange, sycl::id<3> SrcOffset,
121  unsigned int SrcElemSize, void *DstMem,
122  QueueImplPtr TgtQueue, unsigned int DimDst,
123  sycl::range<3> DstSize, sycl::range<3> DstAccessRange,
124  sycl::id<3> DstOffset, unsigned int DstElemSize,
125  std::vector<RT::PiEvent> DepEvents, RT::PiEvent &OutEvent);
126 
127  static void fill(SYCLMemObjI *SYCLMemObj, void *Mem, QueueImplPtr Queue,
128  size_t PatternSize, const char *Pattern, unsigned int Dim,
129  sycl::range<3> Size, sycl::range<3> AccessRange,
130  sycl::id<3> AccessOffset, unsigned int ElementSize,
131  std::vector<RT::PiEvent> DepEvents, RT::PiEvent &OutEvent);
132 
133  static void *map(SYCLMemObjI *SYCLMemObj, void *Mem, QueueImplPtr Queue,
134  access::mode AccessMode, unsigned int Dim,
135  sycl::range<3> Size, sycl::range<3> AccessRange,
136  sycl::id<3> AccessOffset, unsigned int ElementSize,
137  std::vector<RT::PiEvent> DepEvents, RT::PiEvent &OutEvent);
138 
139  static void unmap(SYCLMemObjI *SYCLMemObj, void *Mem, QueueImplPtr Queue,
140  void *MappedPtr, std::vector<RT::PiEvent> DepEvents,
141  RT::PiEvent &OutEvent);
142 
143  static void copy_usm(const void *SrcMem, QueueImplPtr Queue, size_t Len,
144  void *DstMem, std::vector<RT::PiEvent> DepEvents,
145  RT::PiEvent &OutEvent);
146 
147  static void fill_usm(void *DstMem, QueueImplPtr Queue, size_t Len,
148  int Pattern, std::vector<RT::PiEvent> DepEvents,
149  RT::PiEvent &OutEvent);
150 
151  static void prefetch_usm(void *Ptr, QueueImplPtr Queue, size_t Len,
152  std::vector<RT::PiEvent> DepEvents,
153  RT::PiEvent &OutEvent);
154 
155  static void advise_usm(const void *Ptr, QueueImplPtr Queue, size_t Len,
156  pi_mem_advice Advice,
157  std::vector<RT::PiEvent> DepEvents,
158  RT::PiEvent &OutEvent);
159 };
160 } // namespace detail
161 } // namespace sycl
162 } // __SYCL_INLINE_NAMESPACE(cl)
property_list.hpp
cl::sycl::detail::ContextImplPtr
std::shared_ptr< detail::context_impl > ContextImplPtr
Definition: memory_manager.hpp:31
sycl_mem_obj_i.hpp
cl::sycl::id< 3 >
_pi_mem_advice
_pi_mem_advice
Definition: pi.h:441
cl::sycl::property_list
Objects of the property_list class are containers for the SYCL properties.
Definition: property_list.hpp:26
access.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:30
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::detail::MemoryManager
Definition: memory_manager.hpp:36
_pi_image_format
Definition: pi.h:876
cl::sycl::detail::QueueImplPtr
std::shared_ptr< detail::queue_impl > QueueImplPtr
Definition: memory_manager.hpp:29
_pi_image_desc
Definition: pi.h:881
_pi_event
PI Event mapping to CUevent.
Definition: pi_cuda.hpp:419
cl::sycl::memory_order::release
@ release
cl::sycl::access::mode
mode
Definition: access.hpp:28
cl::sycl::detail::SYCLMemObjI
Definition: sycl_mem_obj_i.hpp:28
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12