blob: 380e4062eefc13507c7bcf37b85784611213aa03 [file] [log] [blame]
Georgios Pinitas99d40952018-04-23 16:26:46 +01001/*
SiCong Lia359ee92023-04-18 13:49:56 +01002 * Copyright (c) 2018-2021, 2023 Arm Limited.
Georgios Pinitas99d40952018-04-23 16:26:46 +01003 *
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#include "arm_compute/runtime/CL/CLMemoryRegion.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/runtime/CL/CLScheduler.h"
28
29namespace arm_compute
30{
Georgios Pinitas2cd5b312021-05-04 21:39:57 +010031ICLMemoryRegion::ICLMemoryRegion(size_t size)
Pablo Tellodb8485a2019-09-24 11:03:47 +010032 : IMemoryRegion(size),
Georgios Pinitas2cd5b312021-05-04 21:39:57 +010033 _ctx(CLScheduler::get().context()),
Pablo Tellodb8485a2019-09-24 11:03:47 +010034 _mapping(nullptr),
35 _mem()
Georgios Pinitas99d40952018-04-23 16:26:46 +010036{
37}
38
39const cl::Buffer &ICLMemoryRegion::cl_data() const
40{
41 return _mem;
42}
43
44void *ICLMemoryRegion::buffer()
45{
46 return _mapping;
47}
48
Georgios Pinitas74ef1db2020-01-20 17:01:15 +000049const void *ICLMemoryRegion::buffer() const
Georgios Pinitas99d40952018-04-23 16:26:46 +010050{
51 return _mapping;
52}
53
Georgios Pinitasdf310362018-11-14 13:16:56 +000054std::unique_ptr<IMemoryRegion> ICLMemoryRegion::extract_subregion(size_t offset, size_t size)
Georgios Pinitas99d40952018-04-23 16:26:46 +010055{
Georgios Pinitasdf310362018-11-14 13:16:56 +000056 ARM_COMPUTE_UNUSED(offset, size);
57 return nullptr;
Georgios Pinitas99d40952018-04-23 16:26:46 +010058}
59
Georgios Pinitas2cd5b312021-05-04 21:39:57 +010060CLBufferMemoryRegion::CLBufferMemoryRegion(cl_mem_flags flags, size_t size)
61 : ICLMemoryRegion(size)
Georgios Pinitas99d40952018-04-23 16:26:46 +010062{
63 if(_size != 0)
64 {
Georgios Pinitas2cd5b312021-05-04 21:39:57 +010065 _mem = cl::Buffer(CLScheduler::get().context(), flags, _size);
Georgios Pinitas99d40952018-04-23 16:26:46 +010066 }
67}
68
Georgios Pinitas2cd5b312021-05-04 21:39:57 +010069CLBufferMemoryRegion::CLBufferMemoryRegion(const cl::Buffer &buffer)
70 : ICLMemoryRegion(buffer.getInfo<CL_MEM_SIZE>())
Georgios Pinitasdf310362018-11-14 13:16:56 +000071{
72 _mem = buffer;
73}
74
Georgios Pinitas99d40952018-04-23 16:26:46 +010075void *CLBufferMemoryRegion::ptr()
76{
77 return nullptr;
78}
79
80void *CLBufferMemoryRegion::map(cl::CommandQueue &q, bool blocking)
81{
82 ARM_COMPUTE_ERROR_ON(_mem.get() == nullptr);
83 _mapping = q.enqueueMapBuffer(_mem, blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, 0, _size);
84 return _mapping;
85}
86
87void CLBufferMemoryRegion::unmap(cl::CommandQueue &q)
88{
89 ARM_COMPUTE_ERROR_ON(_mem.get() == nullptr);
90 q.enqueueUnmapMemObject(_mem, _mapping);
91 _mapping = nullptr;
92}
93
Georgios Pinitas2cd5b312021-05-04 21:39:57 +010094ICLSVMMemoryRegion::ICLSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment)
95 : ICLMemoryRegion(size), _ptr(nullptr)
Georgios Pinitas99d40952018-04-23 16:26:46 +010096{
97 if(size != 0)
98 {
Georgios Pinitas2cd5b312021-05-04 21:39:57 +010099 _ptr = clSVMAlloc(CLScheduler::get().context().get(), flags, size, alignment);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100100 if(_ptr != nullptr)
101 {
Georgios Pinitas2cd5b312021-05-04 21:39:57 +0100102 _mem = cl::Buffer(CLScheduler::get().context(), CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, _size, _ptr);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100103 }
104 }
105}
106
107ICLSVMMemoryRegion::~ICLSVMMemoryRegion()
108{
109 if(_ptr != nullptr)
110 {
Georgios Pinitas5a1320b2019-06-27 17:50:43 +0100111 try
112 {
SiCong Lia359ee92023-04-18 13:49:56 +0100113 clFinish(CLScheduler::get().queue().get());
Georgios Pinitas5a1320b2019-06-27 17:50:43 +0100114 _mem = cl::Buffer();
115 clSVMFree(_ctx.get(), _ptr);
116 }
117 catch(...)
118 {
119 }
Georgios Pinitas99d40952018-04-23 16:26:46 +0100120 }
121}
122
123void *ICLSVMMemoryRegion::ptr()
124{
125 return _ptr;
126}
127
Georgios Pinitas2cd5b312021-05-04 21:39:57 +0100128CLCoarseSVMMemoryRegion::CLCoarseSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment)
129 : ICLSVMMemoryRegion(flags, size, alignment)
Georgios Pinitas99d40952018-04-23 16:26:46 +0100130{
131}
132
133void *CLCoarseSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking)
134{
135 ARM_COMPUTE_ERROR_ON(_ptr == nullptr);
136 clEnqueueSVMMap(q.get(), blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, _ptr, _size, 0, nullptr, nullptr);
137 _mapping = _ptr;
138 return _mapping;
139}
140
141void CLCoarseSVMMemoryRegion::unmap(cl::CommandQueue &q)
142{
143 ARM_COMPUTE_ERROR_ON(_ptr == nullptr);
144 clEnqueueSVMUnmap(q.get(), _ptr, 0, nullptr, nullptr);
145 _mapping = nullptr;
146}
147
Georgios Pinitas2cd5b312021-05-04 21:39:57 +0100148CLFineSVMMemoryRegion::CLFineSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment)
149 : ICLSVMMemoryRegion(flags, size, alignment)
Georgios Pinitas99d40952018-04-23 16:26:46 +0100150{
151}
152
153void *CLFineSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking)
154{
155 if(blocking)
156 {
157 clFinish(q.get());
158 }
159 _mapping = _ptr;
160 return _mapping;
161}
162
163void CLFineSVMMemoryRegion::unmap(cl::CommandQueue &q)
164{
165 ARM_COMPUTE_UNUSED(q);
166 _mapping = nullptr;
167}
Pablo Tellodb8485a2019-09-24 11:03:47 +0100168} // namespace arm_compute