blob: 9578d73934498f34f6b7d792e1076e09ce1b1211 [file] [log] [blame]
Georgios Pinitas99d40952018-04-23 16:26:46 +01001/*
2 * Copyright (c) 2018 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#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 {
108 clFinish(CLScheduler::get().queue().get());
109 _mem = cl::Buffer();
110 clSVMFree(_ctx.get(), _ptr);
111 }
112}
113
114void *ICLSVMMemoryRegion::ptr()
115{
116 return _ptr;
117}
118
119CLCoarseSVMMemoryRegion::CLCoarseSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment)
120 : ICLSVMMemoryRegion(std::move(ctx), flags, size, alignment)
121{
122}
123
124void *CLCoarseSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking)
125{
126 ARM_COMPUTE_ERROR_ON(_ptr == nullptr);
127 clEnqueueSVMMap(q.get(), blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, _ptr, _size, 0, nullptr, nullptr);
128 _mapping = _ptr;
129 return _mapping;
130}
131
132void CLCoarseSVMMemoryRegion::unmap(cl::CommandQueue &q)
133{
134 ARM_COMPUTE_ERROR_ON(_ptr == nullptr);
135 clEnqueueSVMUnmap(q.get(), _ptr, 0, nullptr, nullptr);
136 _mapping = nullptr;
137}
138
139CLFineSVMMemoryRegion::CLFineSVMMemoryRegion(cl::Context ctx, cl_mem_flags flags, size_t size, size_t alignment)
140 : ICLSVMMemoryRegion(std::move(ctx), flags, size, alignment)
141{
142}
143
144void *CLFineSVMMemoryRegion::map(cl::CommandQueue &q, bool blocking)
145{
146 if(blocking)
147 {
148 clFinish(q.get());
149 }
150 _mapping = _ptr;
151 return _mapping;
152}
153
154void CLFineSVMMemoryRegion::unmap(cl::CommandQueue &q)
155{
156 ARM_COMPUTE_UNUSED(q);
157 _mapping = nullptr;
158}
159} // namespace arm_compute