blob: 780a563d63e211665b9d26a670f8c2da5670a3c0 [file] [log] [blame]
Georgios Pinitas99d40952018-04-23 16:26:46 +01001/*
Georgios Pinitas2cd5b312021-05-04 21:39:57 +01002 * Copyright (c) 2018-2021 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 _queue(CLScheduler::get().queue()),
34 _ctx(CLScheduler::get().context()),
Pablo Tellodb8485a2019-09-24 11:03:47 +010035 _mapping(nullptr),
36 _mem()
Georgios Pinitas99d40952018-04-23 16:26:46 +010037{
38}
39
40const cl::Buffer &ICLMemoryRegion::cl_data() const
41{
42 return _mem;
43}
44
45void *ICLMemoryRegion::buffer()
46{
47 return _mapping;
48}
49
Georgios Pinitas74ef1db2020-01-20 17:01:15 +000050const void *ICLMemoryRegion::buffer() const
Georgios Pinitas99d40952018-04-23 16:26:46 +010051{
52 return _mapping;
53}
54
Georgios Pinitasdf310362018-11-14 13:16:56 +000055std::unique_ptr<IMemoryRegion> ICLMemoryRegion::extract_subregion(size_t offset, size_t size)
Georgios Pinitas99d40952018-04-23 16:26:46 +010056{
Georgios Pinitasdf310362018-11-14 13:16:56 +000057 ARM_COMPUTE_UNUSED(offset, size);
58 return nullptr;
Georgios Pinitas99d40952018-04-23 16:26:46 +010059}
60
Georgios Pinitas2cd5b312021-05-04 21:39:57 +010061CLBufferMemoryRegion::CLBufferMemoryRegion(cl_mem_flags flags, size_t size)
62 : ICLMemoryRegion(size)
Georgios Pinitas99d40952018-04-23 16:26:46 +010063{
64 if(_size != 0)
65 {
Georgios Pinitas2cd5b312021-05-04 21:39:57 +010066 _mem = cl::Buffer(CLScheduler::get().context(), flags, _size);
Georgios Pinitas99d40952018-04-23 16:26:46 +010067 }
68}
69
Georgios Pinitas2cd5b312021-05-04 21:39:57 +010070CLBufferMemoryRegion::CLBufferMemoryRegion(const cl::Buffer &buffer)
71 : ICLMemoryRegion(buffer.getInfo<CL_MEM_SIZE>())
Georgios Pinitasdf310362018-11-14 13:16:56 +000072{
73 _mem = buffer;
74}
75
Georgios Pinitas99d40952018-04-23 16:26:46 +010076void *CLBufferMemoryRegion::ptr()
77{
78 return nullptr;
79}
80
81void *CLBufferMemoryRegion::map(cl::CommandQueue &q, bool blocking)
82{
83 ARM_COMPUTE_ERROR_ON(_mem.get() == nullptr);
84 _mapping = q.enqueueMapBuffer(_mem, blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, 0, _size);
85 return _mapping;
86}
87
88void CLBufferMemoryRegion::unmap(cl::CommandQueue &q)
89{
90 ARM_COMPUTE_ERROR_ON(_mem.get() == nullptr);
91 q.enqueueUnmapMemObject(_mem, _mapping);
92 _mapping = nullptr;
93}
94
Georgios Pinitas2cd5b312021-05-04 21:39:57 +010095ICLSVMMemoryRegion::ICLSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment)
96 : ICLMemoryRegion(size), _ptr(nullptr)
Georgios Pinitas99d40952018-04-23 16:26:46 +010097{
98 if(size != 0)
99 {
Georgios Pinitas2cd5b312021-05-04 21:39:57 +0100100 _ptr = clSVMAlloc(CLScheduler::get().context().get(), flags, size, alignment);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100101 if(_ptr != nullptr)
102 {
Georgios Pinitas2cd5b312021-05-04 21:39:57 +0100103 _mem = cl::Buffer(CLScheduler::get().context(), CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, _size, _ptr);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100104 }
105 }
106}
107
108ICLSVMMemoryRegion::~ICLSVMMemoryRegion()
109{
110 if(_ptr != nullptr)
111 {
Georgios Pinitas5a1320b2019-06-27 17:50:43 +0100112 try
113 {
Pablo Tellodb8485a2019-09-24 11:03:47 +0100114 clFinish(_queue.get());
Georgios Pinitas5a1320b2019-06-27 17:50:43 +0100115 _mem = cl::Buffer();
116 clSVMFree(_ctx.get(), _ptr);
117 }
118 catch(...)
119 {
120 }
Georgios Pinitas99d40952018-04-23 16:26:46 +0100121 }
122}
123
124void *ICLSVMMemoryRegion::ptr()
125{
126 return _ptr;
127}
128
Georgios Pinitas2cd5b312021-05-04 21:39:57 +0100129CLCoarseSVMMemoryRegion::CLCoarseSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment)
130 : ICLSVMMemoryRegion(flags, size, alignment)
Georgios Pinitas99d40952018-04-23 16:26:46 +0100131{
132}
133
134void *CLCoarseSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking)
135{
136 ARM_COMPUTE_ERROR_ON(_ptr == nullptr);
137 clEnqueueSVMMap(q.get(), blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, _ptr, _size, 0, nullptr, nullptr);
138 _mapping = _ptr;
139 return _mapping;
140}
141
142void CLCoarseSVMMemoryRegion::unmap(cl::CommandQueue &q)
143{
144 ARM_COMPUTE_ERROR_ON(_ptr == nullptr);
145 clEnqueueSVMUnmap(q.get(), _ptr, 0, nullptr, nullptr);
146 _mapping = nullptr;
147}
148
Georgios Pinitas2cd5b312021-05-04 21:39:57 +0100149CLFineSVMMemoryRegion::CLFineSVMMemoryRegion(cl_mem_flags flags, size_t size, size_t alignment)
150 : ICLSVMMemoryRegion(flags, size, alignment)
Georgios Pinitas99d40952018-04-23 16:26:46 +0100151{
152}
153
154void *CLFineSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking)
155{
156 if(blocking)
157 {
158 clFinish(q.get());
159 }
160 _mapping = _ptr;
161 return _mapping;
162}
163
164void CLFineSVMMemoryRegion::unmap(cl::CommandQueue &q)
165{
166 ARM_COMPUTE_UNUSED(q);
167 _mapping = nullptr;
168}
Pablo Tellodb8485a2019-09-24 11:03:47 +0100169} // namespace arm_compute