blob: dd716f77ff265c3bccf955ed11e42562b33c44ad [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Pablo Telloe86a09f2018-01-11 15:44:48 +00002 * Copyright (c) 2016-2018 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
31using namespace arm_compute;
32
Georgios Pinitas99d40952018-04-23 16:26:46 +010033namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034{
Georgios Pinitas99d40952018-04-23 16:26:46 +010035std::shared_ptr<arm_compute::ICLMemoryRegion> allocate_region(cl::Context context, size_t size, cl_uint alignment)
36{
37 // Try fine-grain SVM
38 std::shared_ptr<ICLMemoryRegion> region = std::make_shared<CLFineSVMMemoryRegion>(context, CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_BUFFER, size, alignment);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
Georgios Pinitas99d40952018-04-23 16:26:46 +010040 // Try coarse-grain SVM in case of failure
41 if(region != nullptr && region->ptr() == nullptr)
42 {
43 region = std::make_shared<CLCoarseSVMMemoryRegion>(context, CL_MEM_READ_WRITE, size, alignment);
44 }
45 // Try legacy buffer memory in case of failure
46 if(region != nullptr && region->ptr() == nullptr)
47 {
48 region = std::make_shared<CLBufferMemoryRegion>(context, CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, size);
49 }
50 return region;
51}
52} // namespace
53
54CLTensorAllocator::CLTensorAllocator(CLTensor *owner)
55 : _associated_memory_group(nullptr), _memory(), _owner(owner)
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010056{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010057}
58
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059uint8_t *CLTensorAllocator::data()
60{
Georgios Pinitas99d40952018-04-23 16:26:46 +010061 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
62 return reinterpret_cast<uint8_t *>(_memory.region()->buffer());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063}
64
65const cl::Buffer &CLTensorAllocator::cl_data() const
66{
Georgios Pinitas99d40952018-04-23 16:26:46 +010067 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
68 return _memory.region()->cl_data();
Pablo Telloe86a09f2018-01-11 15:44:48 +000069}
70
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071void CLTensorAllocator::allocate()
72{
Georgios Pinitas99d40952018-04-23 16:26:46 +010073 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
74
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010075 if(_associated_memory_group == nullptr)
76 {
Michele Di Giorgioaaab6022018-05-14 11:58:24 +010077 if(_memory.region()->cl_data().get() != nullptr)
78 {
79 // Memory is already allocated. Reuse it if big enough, otherwise fire an assertion
80 ARM_COMPUTE_ERROR_ON_MSG(info().total_size() > _memory.region()->size(), "Reallocation of a bigger memory region is not allowed!");
81 }
82 else
83 {
84 // Perform memory allocation
85 _memory = CLMemory(allocate_region(CLScheduler::get().context(), info().total_size(), 0));
86 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010087 }
88 else
89 {
Georgios Pinitas99d40952018-04-23 16:26:46 +010090 _associated_memory_group->finalize_memory(_owner, _memory.region()->handle(), info().total_size());
91 _memory.region()->set_size(info().total_size());
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010092 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093 info().set_is_resizable(false);
94}
95
96void CLTensorAllocator::free()
97{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010098 if(_associated_memory_group == nullptr)
99 {
Georgios Pinitas99d40952018-04-23 16:26:46 +0100100 _memory = CLMemory();
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100101 info().set_is_resizable(true);
102 }
103}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104
Georgios Pinitas99d40952018-04-23 16:26:46 +0100105arm_compute::Status CLTensorAllocator::import_memory(CLMemory memory)
106{
107 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
108 ARM_COMPUTE_RETURN_ERROR_ON(memory.region()->cl_data().get() == nullptr);
109 ARM_COMPUTE_RETURN_ERROR_ON(_associated_memory_group != nullptr);
110 _memory = memory;
111 info().set_is_resizable(false);
112
113 return Status{};
114}
115
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100116void CLTensorAllocator::set_associated_memory_group(CLMemoryGroup *associated_memory_group)
117{
Georgios Pinitas99d40952018-04-23 16:26:46 +0100118 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100119 ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr);
120 ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr);
Georgios Pinitas99d40952018-04-23 16:26:46 +0100121 ARM_COMPUTE_ERROR_ON(_memory.region()->cl_data().get() != nullptr);
122 _memory = CLMemory(std::make_shared<CLBufferMemoryRegion>(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, 0));
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 Pinitas99d40952018-04-23 16:26:46 +0100139 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
140 ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() != nullptr);
141 _memory.region()->map(q, blocking);
142 return reinterpret_cast<uint8_t *>(_memory.region()->buffer());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143}
144
145void CLTensorAllocator::unmap(cl::CommandQueue &q, uint8_t *mapping)
146{
Georgios Pinitas99d40952018-04-23 16:26:46 +0100147 ARM_COMPUTE_UNUSED(mapping);
148 ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr);
149 ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() == nullptr);
150 _memory.region()->unmap(q);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151}