Compute Library
 23.08
CLMemoryRegion.cpp
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  */
25 
26 #include "arm_compute/core/Error.h"
28 
29 namespace arm_compute
30 {
32  : IMemoryRegion(size),
33  _ctx(CLScheduler::get().context()),
34  _mapping(nullptr),
35  _mem()
36 {
37 }
38 
39 const cl::Buffer &ICLMemoryRegion::cl_data() const
40 {
41  return _mem;
42 }
43 
45 {
46  return _mapping;
47 }
48 
49 const void *ICLMemoryRegion::buffer() const
50 {
51  return _mapping;
52 }
53 
54 std::unique_ptr<IMemoryRegion> ICLMemoryRegion::extract_subregion(size_t offset, size_t size)
55 {
57  return nullptr;
58 }
59 
60 CLBufferMemoryRegion::CLBufferMemoryRegion(cl_mem_flags flags, size_t size)
61  : ICLMemoryRegion(size)
62 {
63  if(_size != 0)
64  {
65  _mem = cl::Buffer(CLScheduler::get().context(), flags, _size);
66  }
67 }
68 
70  : ICLMemoryRegion(buffer.getInfo<CL_MEM_SIZE>())
71 {
72  _mem = buffer;
73 }
74 
76 {
77  // Flush the command queue to ensure all commands that may use this memory buffer are scheduled to be finished before
78  // this buffer is freed
79  // Do not call finish as it is a blocking call which affects the performance
80  CLScheduler::get().queue().flush();
81 }
82 
84 {
85  return nullptr;
86 }
87 
88 void *CLBufferMemoryRegion::map(cl::CommandQueue &q, bool blocking)
89 {
90  ARM_COMPUTE_ERROR_ON(_mem.get() == nullptr);
91  _mapping = q.enqueueMapBuffer(_mem, blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, 0, _size);
92  return _mapping;
93 }
94 
95 void CLBufferMemoryRegion::unmap(cl::CommandQueue &q)
96 {
97  ARM_COMPUTE_ERROR_ON(_mem.get() == nullptr);
98  q.enqueueUnmapMemObject(_mem, _mapping);
99  _mapping = nullptr;
100 }
101 
102 ICLSVMMemoryRegion::ICLSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment)
103  : ICLMemoryRegion(size), _ptr(nullptr)
104 {
105  if(size != 0)
106  {
107  _ptr = clSVMAlloc(CLScheduler::get().context().get(), flags, size, alignment);
108  if(_ptr != nullptr)
109  {
110  _mem = cl::Buffer(CLScheduler::get().context(), CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, _size, _ptr);
111  }
112  }
113 }
114 
115 ICLSVMMemoryRegion::~ICLSVMMemoryRegion()
116 {
117  if(_ptr != nullptr)
118  {
119  try
120  {
121  // Can only use the blocking finish instead of the non-blocking flush here, because clSVMFree requires all
122  // commands that may use the svm pointer to finish beforehand
123  // https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clSVMFree.html
124  clFinish(CLScheduler::get().queue().get());
125  _mem = cl::Buffer();
126  clSVMFree(_ctx.get(), _ptr);
127  }
128  catch(...)
129  {
130  }
131  }
132 }
133 
134 void *ICLSVMMemoryRegion::ptr()
135 {
136  return _ptr;
137 }
138 
139 CLCoarseSVMMemoryRegion::CLCoarseSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment)
140  : ICLSVMMemoryRegion(flags, size, alignment)
141 {
142 }
143 
144 void *CLCoarseSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking)
145 {
146  ARM_COMPUTE_ERROR_ON(_ptr == nullptr);
147  clEnqueueSVMMap(q.get(), blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, _ptr, _size, 0, nullptr, nullptr);
148  _mapping = _ptr;
149  return _mapping;
150 }
151 
152 void CLCoarseSVMMemoryRegion::unmap(cl::CommandQueue &q)
153 {
154  ARM_COMPUTE_ERROR_ON(_ptr == nullptr);
155  clEnqueueSVMUnmap(q.get(), _ptr, 0, nullptr, nullptr);
156  _mapping = nullptr;
157 }
158 
159 CLFineSVMMemoryRegion::CLFineSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment)
160  : ICLSVMMemoryRegion(flags, size, alignment)
161 {
162 }
163 
164 void *CLFineSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking)
165 {
166  if(blocking)
167  {
168  clFinish(q.get());
169  }
170  _mapping = _ptr;
171  return _mapping;
172 }
173 
174 void CLFineSVMMemoryRegion::unmap(cl::CommandQueue &q)
175 {
177  _mapping = nullptr;
178 }
179 } // namespace arm_compute
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::CLScheduler
Provides global access to a CL context and command queue.
Definition: CLScheduler.h:43
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
clEnqueueSVMMap
cl_int clEnqueueSVMMap(cl_command_queue command_queue, cl_bool blocking_map, cl_map_flags flags, void *svm_ptr, size_t size, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event)
Definition: OpenCL.cpp:257
clSVMFree
void clSVMFree(cl_context context, void *svm_pointer)
Definition: OpenCL.cpp:301
CLMemoryRegion.h
arm_compute::CLBufferMemoryRegion::CLBufferMemoryRegion
CLBufferMemoryRegion(cl_mem_flags flags, size_t size)
Constructor.
Definition: CLMemoryRegion.cpp:60
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
Error.h
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
clFinish
cl_int clFinish(cl_command_queue command_queue)
Definition: OpenCL.cpp:740
arm_compute::ICLMemoryRegion
OpenCL memory region interface.
Definition: CLMemoryRegion.h:35
clEnqueueSVMUnmap
cl_int clEnqueueSVMUnmap(cl_command_queue command_queue, void *svm_ptr, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event)
Definition: OpenCL.cpp:272
ARM_COMPUTE_ERROR_ON
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:467
clSVMAlloc
void * clSVMAlloc(cl_context context, cl_svm_mem_flags_arm flags, size_t size, cl_uint alignment)
Definition: OpenCL.cpp:287
arm_compute::CLBufferMemoryRegion::~CLBufferMemoryRegion
virtual ~CLBufferMemoryRegion() override
Definition: CLMemoryRegion.cpp:75
arm_compute::ICLMemoryRegion::ICLMemoryRegion
ICLMemoryRegion(size_t size)
Constructor.
Definition: CLMemoryRegion.cpp:31
CLScheduler.h
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
offset
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:1112
arm_compute::CLCoarseSVMMemoryRegion::CLCoarseSVMMemoryRegion
CLCoarseSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment)
Constructor.
Definition: CLMemoryRegion.cpp:139
ARM_COMPUTE_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
arm_compute::test::validation::context
auto context
Definition: DirectConv2d.cpp:160
arm_compute::CLScheduler::get
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:103
arm_compute::ICLSVMMemoryRegion
OpenCL SVM memory region interface.
Definition: CLMemoryRegion.h:117
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::CLScheduler::queue
cl::CommandQueue & queue()
Accessor for the associated CL command queue.
Definition: CLScheduler.cpp:39
arm_compute::CLBufferMemoryRegion::ptr
void * ptr() final
Host/SVM pointer accessor.
Definition: CLMemoryRegion.cpp:83