blob: 2976903c9304152474f72463ef4677c55f188ea9 [file] [log] [blame]
Georgios Pinitas99d40952018-04-23 16:26:46 +01001/*
Georgios Pinitas5a1320b2019-06-27 17:50:43 +01002 * Copyright (c) 2018-2019 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{
31ICLMemoryRegion::ICLMemoryRegion(cl::Context ctx, size_t size)
32 : IMemoryRegion(size), _ctx(std::move(ctx)), _mapping(nullptr), _mem()
33{
34}
35
36const cl::Buffer &ICLMemoryRegion::cl_data() const
37{
38 return _mem;
39}
40
41void *ICLMemoryRegion::buffer()
42{
43 return _mapping;
44}
45
46void *ICLMemoryRegion::buffer() const
47{
48 return _mapping;
49}
50
Georgios Pinitasdf310362018-11-14 13:16:56 +000051std::unique_ptr<IMemoryRegion> ICLMemoryRegion::extract_subregion(size_t offset, size_t size)
Georgios Pinitas99d40952018-04-23 16:26:46 +010052{
Georgios Pinitasdf310362018-11-14 13:16:56 +000053 ARM_COMPUTE_UNUSED(offset, size);
54 return nullptr;
Georgios Pinitas99d40952018-04-23 16:26:46 +010055}
56
57CLBufferMemoryRegion::CLBufferMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size)
58 : ICLMemoryRegion(std::move(ctx), size)
59{
60 if(_size != 0)
61 {
62 _mem = cl::Buffer(_ctx, flags, _size);
63 }
64}
65
Georgios Pinitasdf310362018-11-14 13:16:56 +000066CLBufferMemoryRegion::CLBufferMemoryRegion(const cl::Buffer &buffer)
67 : ICLMemoryRegion(buffer.getInfo<CL_MEM_CONTEXT>(), buffer.getInfo<CL_MEM_SIZE>())
68{
69 _mem = buffer;
70}
71
Georgios Pinitas99d40952018-04-23 16:26:46 +010072void *CLBufferMemoryRegion::ptr()
73{
74 return nullptr;
75}
76
77void *CLBufferMemoryRegion::map(cl::CommandQueue &q, bool blocking)
78{
79 ARM_COMPUTE_ERROR_ON(_mem.get() == nullptr);
80 _mapping = q.enqueueMapBuffer(_mem, blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, 0, _size);
81 return _mapping;
82}
83
84void CLBufferMemoryRegion::unmap(cl::CommandQueue &q)
85{
86 ARM_COMPUTE_ERROR_ON(_mem.get() == nullptr);
87 q.enqueueUnmapMemObject(_mem, _mapping);
88 _mapping = nullptr;
89}
90
91ICLSVMMemoryRegion::ICLSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment)
92 : ICLMemoryRegion(std::move(ctx), size), _ptr(nullptr)
93{
94 if(size != 0)
95 {
96 _ptr = clSVMAlloc(_ctx.get(), flags, size, alignment);
97 if(_ptr != nullptr)
98 {
99 _mem = cl::Buffer(_ctx, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, _size, _ptr);
100 }
101 }
102}
103
104ICLSVMMemoryRegion::~ICLSVMMemoryRegion()
105{
106 if(_ptr != nullptr)
107 {
Georgios Pinitas5a1320b2019-06-27 17:50:43 +0100108 try
109 {
110 clFinish(CLScheduler::get().queue().get());
111 _mem = cl::Buffer();
112 clSVMFree(_ctx.get(), _ptr);
113 }
114 catch(...)
115 {
116 }
Georgios Pinitas99d40952018-04-23 16:26:46 +0100117 }
118}
119
120void *ICLSVMMemoryRegion::ptr()
121{
122 return _ptr;
123}
124
125CLCoarseSVMMemoryRegion::CLCoarseSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment)
126 : ICLSVMMemoryRegion(std::move(ctx), flags, size, alignment)
127{
128}
129
130void *CLCoarseSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking)
131{
132 ARM_COMPUTE_ERROR_ON(_ptr == nullptr);
133 clEnqueueSVMMap(q.get(), blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, _ptr, _size, 0, nullptr, nullptr);
134 _mapping = _ptr;
135 return _mapping;
136}
137
138void CLCoarseSVMMemoryRegion::unmap(cl::CommandQueue &q)
139{
140 ARM_COMPUTE_ERROR_ON(_ptr == nullptr);
141 clEnqueueSVMUnmap(q.get(), _ptr, 0, nullptr, nullptr);
142 _mapping = nullptr;
143}
144
145CLFineSVMMemoryRegion::CLFineSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment)
146 : ICLSVMMemoryRegion(std::move(ctx), flags, size, alignment)
147{
148}
149
150void *CLFineSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking)
151{
152 if(blocking)
153 {
154 clFinish(q.get());
155 }
156 _mapping = _ptr;
157 return _mapping;
158}
159
160void CLFineSVMMemoryRegion::unmap(cl::CommandQueue &q)
161{
162 ARM_COMPUTE_UNUSED(q);
163 _mapping = nullptr;
164}
165} // namespace arm_compute