Compute Library
 23.08
CLMemoryRegion.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021, 2023 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef ARM_COMPUTE_RUNTIME_CL_CL_MEMORY_REGION_H
25 #define ARM_COMPUTE_RUNTIME_CL_CL_MEMORY_REGION_H
26 
29 
30 #include <cstddef>
31 
32 namespace arm_compute
33 {
34 /** OpenCL memory region interface */
36 {
37 public:
38  /** Constructor
39  *
40  * @param[in] size Region size
41  */
42  ICLMemoryRegion(size_t size);
43  /** Default Destructor */
44  virtual ~ICLMemoryRegion() = default;
45  /** Prevent instances of this class from being copied (As this class contains pointers) */
46  ICLMemoryRegion(const ICLMemoryRegion &) = delete;
47  /** Default move constructor */
48  ICLMemoryRegion(ICLMemoryRegion &&) = default;
49  /** Prevent instances of this class from being copied (As this class contains pointers) */
50  ICLMemoryRegion &operator=(const ICLMemoryRegion &) = delete;
51  /** Default move assignment operator */
53  /** Returns the underlying CL buffer
54  *
55  * @return CL memory buffer object
56  */
57  const cl::Buffer &cl_data() const;
58  /** Host/SVM pointer accessor
59  *
60  * @return Host/SVM pointer base
61  */
62  virtual void *ptr() = 0;
63  /** Enqueue a map operation of the allocated buffer on the given queue.
64  *
65  * @param[in,out] q The CL command queue to use for the mapping operation.
66  * @param[in] blocking If true, then the mapping will be ready to use by the time
67  * this method returns, else it is the caller's responsibility
68  * to flush the queue and wait for the mapping operation to have completed before using the returned mapping pointer.
69  *
70  * @return The mapping address.
71  */
72  virtual void *map(cl::CommandQueue &q, bool blocking) = 0;
73  /** Enqueue an unmap operation of the allocated buffer on the given queue.
74  *
75  * @note This method simply enqueue the unmap operation, it is the caller's responsibility to flush the queue and make sure the unmap is finished before
76  * the memory is accessed by the device.
77  *
78  * @param[in,out] q The CL command queue to use for the mapping operation.
79  */
80  virtual void unmap(cl::CommandQueue &q) = 0;
81 
82  // Inherited methods overridden :
83  void *buffer() override;
84  const void *buffer() const override;
85  std::unique_ptr<IMemoryRegion> extract_subregion(size_t offset, size_t size) override;
86 
87 protected:
88  cl::Context _ctx;
89  void *_mapping;
90  cl::Buffer _mem;
91 };
92 
93 /** OpenCL buffer memory region implementation */
95 {
96 public:
97  /** Constructor
98  *
99  * @param[in] flags Memory flags
100  * @param[in] size Region size
101  */
102  CLBufferMemoryRegion(cl_mem_flags flags, size_t size);
103  /** Constructor
104  *
105  * @param[in] buffer Buffer to be used as a memory region
106  */
107  CLBufferMemoryRegion(const cl::Buffer &buffer);
108  virtual ~CLBufferMemoryRegion() override;
109 
110  // Inherited methods overridden :
111  void *ptr() final;
112  void *map(cl::CommandQueue &q, bool blocking) final;
113  void unmap(cl::CommandQueue &q) final;
114 };
115 
116 /** OpenCL SVM memory region interface */
118 {
119 protected:
120  /** Constructor
121  *
122  * @param[in] flags Memory flags
123  * @param[in] size Region size
124  * @param[in] alignment Alignment
125  */
126  ICLSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment);
127  /** Destructor */
128  virtual ~ICLSVMMemoryRegion();
129  /** Prevent instances of this class from being copied (As this class contains pointers) */
130  ICLSVMMemoryRegion(const ICLSVMMemoryRegion &) = delete;
131  /** Default move constructor */
133  /** Prevent instances of this class from being copied (As this class contains pointers) */
134  ICLSVMMemoryRegion &operator=(const ICLSVMMemoryRegion &) = delete;
135  /** Default move assignment operator */
136  ICLSVMMemoryRegion &operator=(ICLSVMMemoryRegion &&) = default;
137 
138  // Inherited methods overridden :
139  void *ptr() override;
140 
141 protected:
142  void *_ptr;
143 };
144 
145 /** OpenCL coarse-grain SVM memory region implementation */
147 {
148 public:
149  /** Constructor
150  *
151  * @param[in] flags Memory flags
152  * @param[in] size Region size
153  * @param[in] alignment Alignment
154  */
155  CLCoarseSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment);
156 
157  // Inherited methods overridden :
158  void *map(cl::CommandQueue &q, bool blocking) final;
159  void unmap(cl::CommandQueue &q) final;
160 };
161 
162 /** OpenCL fine-grain SVM memory region implementation */
164 {
165 public:
166  /** Constructor
167  *
168  * @param[in] flags Memory flags
169  * @param[in] size Region size
170  * @param[in] alignment Alignment
171  */
172  CLFineSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment);
173 
174  // Inherited methods overridden :
175  void *map(cl::CommandQueue &q, bool blocking) final;
176  void unmap(cl::CommandQueue &q) final;
177 };
178 } // namespace arm_compute
179 #endif /* ARM_COMPUTE_RUNTIME_CL_CL_MEMORY_REGION_H */
arm_compute::IMemoryRegion::size
size_t size() const
Memory region size accessor.
Definition: IMemoryRegion.h:73
arm_compute::IMemoryRegion
Memory region interface.
Definition: IMemoryRegion.h:33
arm_compute::CLCoarseSVMMemoryRegion::unmap
void unmap(cl::CommandQueue &q) final
Enqueue an unmap operation of the allocated buffer on the given queue.
Definition: CLMemoryRegion.cpp:152
arm_compute::CLFineSVMMemoryRegion::unmap
void unmap(cl::CommandQueue &q) final
Enqueue an unmap operation of the allocated buffer on the given queue.
Definition: CLMemoryRegion.cpp:174
arm_compute::ICLMemoryRegion::cl_data
const cl::Buffer & cl_data() const
Returns the underlying CL buffer.
Definition: CLMemoryRegion.cpp:39
arm_compute::ICLMemoryRegion::ptr
virtual void * ptr()=0
Host/SVM pointer accessor.
arm_compute::CLBufferMemoryRegion::CLBufferMemoryRegion
CLBufferMemoryRegion(cl_mem_flags flags, size_t size)
Constructor.
Definition: CLMemoryRegion.cpp:60
arm_compute::ICLMemoryRegion::unmap
virtual void unmap(cl::CommandQueue &q)=0
Enqueue an unmap operation of the allocated buffer on the given queue.
arm_compute::CLCoarseSVMMemoryRegion
OpenCL coarse-grain SVM memory region implementation.
Definition: CLMemoryRegion.h:146
arm_compute::CLFineSVMMemoryRegion::map
void * map(cl::CommandQueue &q, bool blocking) final
Enqueue a map operation of the allocated buffer on the given queue.
Definition: CLMemoryRegion.cpp:164
arm_compute::CLBufferMemoryRegion::unmap
void unmap(cl::CommandQueue &q) final
Enqueue an unmap operation of the allocated buffer on the given queue.
Definition: CLMemoryRegion.cpp:95
arm_compute::CLCoarseSVMMemoryRegion::map
void * map(cl::CommandQueue &q, bool blocking) final
Enqueue a map operation of the allocated buffer on the given queue.
Definition: CLMemoryRegion.cpp:144
arm_compute::ICLMemoryRegion
OpenCL memory region interface.
Definition: CLMemoryRegion.h:35
arm_compute::CLFineSVMMemoryRegion
OpenCL fine-grain SVM memory region implementation.
Definition: CLMemoryRegion.h:163
arm_compute::CLBufferMemoryRegion::~CLBufferMemoryRegion
virtual ~CLBufferMemoryRegion() override
Definition: CLMemoryRegion.cpp:75
arm_compute::ICLMemoryRegion::ICLMemoryRegion
ICLMemoryRegion(size_t size)
Constructor.
Definition: CLMemoryRegion.cpp:31
arm_compute::ICLMemoryRegion::~ICLMemoryRegion
virtual ~ICLMemoryRegion()=default
Default Destructor.
offset
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:1112
OpenCL.h
Wrapper to configure the Khronos OpenCL C++ header.
arm_compute::CLCoarseSVMMemoryRegion::CLCoarseSVMMemoryRegion
CLCoarseSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment)
Constructor.
Definition: CLMemoryRegion.cpp:139
arm_compute::ICLSVMMemoryRegion
OpenCL SVM memory region interface.
Definition: CLMemoryRegion.h:117
arm_compute::ICLMemoryRegion::operator=
ICLMemoryRegion & operator=(const ICLMemoryRegion &)=delete
Prevent instances of this class from being copied (As this class contains pointers)
arm_compute::CLBufferMemoryRegion::map
void * map(cl::CommandQueue &q, bool blocking) final
Enqueue a map operation of the allocated buffer on the given queue.
Definition: CLMemoryRegion.cpp:88
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::ICLMemoryRegion::extract_subregion
std::unique_ptr< IMemoryRegion > extract_subregion(size_t offset, size_t size) override
Extract a sub-region from the memory.
Definition: CLMemoryRegion.cpp:54
arm_compute::CLFineSVMMemoryRegion::CLFineSVMMemoryRegion
CLFineSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment)
Constructor.
Definition: CLMemoryRegion.cpp:159
arm_compute::ICLMemoryRegion::buffer
void * buffer() override
Returns the pointer to the allocated data.
Definition: CLMemoryRegion.cpp:44
arm_compute::ICLMemoryRegion::map
virtual void * map(cl::CommandQueue &q, bool blocking)=0
Enqueue a map operation of the allocated buffer on the given queue.
arm_compute::CLBufferMemoryRegion
OpenCL buffer memory region implementation.
Definition: CLMemoryRegion.h:94
arm_compute::CLBufferMemoryRegion::ptr
void * ptr() final
Host/SVM pointer accessor.
Definition: CLMemoryRegion.cpp:83
IMemoryRegion.h