blob: 2ce64551aed9038dee01504949d4c5907ebe9833 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas4d0351c2019-04-03 15:11:16 +01002 * Copyright (c) 2016-2019 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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/CLTensorAllocator.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/TensorInfo.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010028#include "arm_compute/runtime/CL/CLMemoryGroup.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/runtime/CL/CLScheduler.h"
30
Georgios Pinitasdf310362018-11-14 13:16:56 +000031namespace arm_compute
32{
33const cl::Buffer CLTensorAllocator::_empty_buffer = cl::Buffer();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
Georgios Pinitas99d40952018-04-23 16:26:46 +010035namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036{
Georgios Pinitasdf310362018-11-14 13:16:56 +000037std::unique_ptr<ICLMemoryRegion> allocate_region(cl::Context context, size_t size, cl_uint alignment)
Georgios Pinitas99d40952018-04-23 16:26:46 +010038{
39 // Try fine-grain SVM
Georgios Pinitasdf310362018-11-14 13:16:56 +000040 std::unique_ptr<ICLMemoryRegion> region = support::cpp14::make_unique<CLFineSVMMemoryRegion>(context,
41 CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_BUFFER,
42 size,
43 alignment);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044
Georgios Pinitas99d40952018-04-23 16:26:46 +010045 // Try coarse-grain SVM in case of failure
46 if(region != nullptr && region->ptr() == nullptr)
47 {
Georgios Pinitasdf310362018-11-14 13:16:56 +000048 region = support::cpp14::make_unique<CLCoarseSVMMemoryRegion>(context, CL_MEM_READ_WRITE, size, alignment);
Georgios Pinitas99d40952018-04-23 16:26:46 +010049 }
50 // Try legacy buffer memory in case of failure
51 if(region != nullptr && region->ptr() == nullptr)
52 {
Georgios Pinitasdf310362018-11-14 13:16:56 +000053 region = support::cpp14::make_unique<CLBufferMemoryRegion>(context, CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, size);
Georgios Pinitas99d40952018-04-23 16:26:46 +010054 }
55 return region;
56}
57} // namespace
58
59CLTensorAllocator::CLTensorAllocator(CLTensor *owner)
Georgios Pinitasdf310362018-11-14 13:16:56 +000060 : _associated_memory_group(nullptr), _memory(), _mapping(nullptr), _owner(owner)
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010061{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010062}
63
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064uint8_t *CLTensorAllocator::data()
65{
Georgios Pinitasdf310362018-11-14 13:16:56 +000066 return _mapping;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067}
68
69const cl::Buffer &CLTensorAllocator::cl_data() const
70{
Georgios Pinitasdf310362018-11-14 13:16:56 +000071 return _memory.region() == nullptr ? _empty_buffer : _memory.cl_region()->cl_data();
Pablo Telloe86a09f2018-01-11 15:44:48 +000072}
73
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074void CLTensorAllocator::allocate()
75{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010076 if(_associated_memory_group == nullptr)
77 {
Georgios Pinitasdf310362018-11-14 13:16:56 +000078 if(_memory.region() != nullptr && _memory.cl_region()->cl_data().get() != nullptr)
Michele Di Giorgioaaab6022018-05-14 11:58:24 +010079 {
80 // Memory is already allocated. Reuse it if big enough, otherwise fire an assertion
Georgios Pinitasdf310362018-11-14 13:16:56 +000081 ARM_COMPUTE_ERROR_ON_MSG(info().total_size() > _memory.region()->size(),
82 "Reallocation of a bigger memory region is not allowed!");
Michele Di Giorgioaaab6022018-05-14 11:58:24 +010083 }
84 else
85 {
86 // Perform memory allocation
Georgios Pinitasdf310362018-11-14 13:16:56 +000087 _memory.set_owned_region(allocate_region(CLScheduler::get().context(), info().total_size(), 0));
Michele Di Giorgioaaab6022018-05-14 11:58:24 +010088 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010089 }
90 else
91 {
Georgios Pinitasdf310362018-11-14 13:16:56 +000092 _associated_memory_group->finalize_memory(_owner, _memory, info().total_size());
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010093 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 info().set_is_resizable(false);
95}
96
97void CLTensorAllocator::free()
98{
Georgios Pinitasdf310362018-11-14 13:16:56 +000099 _mapping = nullptr;
100 _memory.set_region(nullptr);
101 info().set_is_resizable(true);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100102}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100104Status CLTensorAllocator::import_memory(cl::Buffer buffer)
Georgios Pinitas99d40952018-04-23 16:26:46 +0100105{
Georgios Pinitasdf310362018-11-14 13:16:56 +0000106 ARM_COMPUTE_RETURN_ERROR_ON(buffer.get() == nullptr);
Georgios Pinitas4d0351c2019-04-03 15:11:16 +0100107 ARM_COMPUTE_RETURN_ERROR_ON(buffer.getInfo<CL_MEM_SIZE>() < info().total_size());
Georgios Pinitasdf310362018-11-14 13:16:56 +0000108 ARM_COMPUTE_RETURN_ERROR_ON(buffer.getInfo<CL_MEM_CONTEXT>().get() != CLScheduler::get().context().get());
Georgios Pinitas99d40952018-04-23 16:26:46 +0100109 ARM_COMPUTE_RETURN_ERROR_ON(_associated_memory_group != nullptr);
Georgios Pinitasdf310362018-11-14 13:16:56 +0000110
111 _memory.set_owned_region(support::cpp14::make_unique<CLBufferMemoryRegion>(buffer));
Georgios Pinitas99d40952018-04-23 16:26:46 +0100112 info().set_is_resizable(false);
113
114 return Status{};
115}
116
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100117void CLTensorAllocator::set_associated_memory_group(CLMemoryGroup *associated_memory_group)
118{
119 ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr);
120 ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr);
Georgios Pinitasdf310362018-11-14 13:16:56 +0000121 ARM_COMPUTE_ERROR_ON(_memory.region() != nullptr && _memory.cl_region()->cl_data().get() != nullptr);
122
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100123 _associated_memory_group = associated_memory_group;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124}
125
126uint8_t *CLTensorAllocator::lock()
127{
Georgios Pinitas99d40952018-04-23 16:26:46 +0100128 return map(CLScheduler::get().queue(), true);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129}
130
131void CLTensorAllocator::unlock()
132{
Georgios Pinitas99d40952018-04-23 16:26:46 +0100133 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
134 unmap(CLScheduler::get().queue(), reinterpret_cast<uint8_t *>(_memory.region()->buffer()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135}
136
137uint8_t *CLTensorAllocator::map(cl::CommandQueue &q, bool blocking)
138{
Georgios Pinitasdf310362018-11-14 13:16:56 +0000139 ARM_COMPUTE_ERROR_ON(_mapping != nullptr);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100140 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
141 ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() != nullptr);
Georgios Pinitasdf310362018-11-14 13:16:56 +0000142
143 _mapping = reinterpret_cast<uint8_t *>(_memory.cl_region()->map(q, blocking));
144 return _mapping;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145}
146
147void CLTensorAllocator::unmap(cl::CommandQueue &q, uint8_t *mapping)
148{
Georgios Pinitasdf310362018-11-14 13:16:56 +0000149 ARM_COMPUTE_ERROR_ON(_mapping == nullptr);
150 ARM_COMPUTE_ERROR_ON(_mapping != mapping);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100151 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
152 ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() == nullptr);
Georgios Pinitasdf310362018-11-14 13:16:56 +0000153 ARM_COMPUTE_UNUSED(mapping);
154
155 _memory.cl_region()->unmap(q);
156 _mapping = nullptr;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157}
Georgios Pinitasdf310362018-11-14 13:16:56 +0000158} // namespace arm_compute