Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Pablo Tello | e86a09f | 2018-01-11 15:44:48 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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/CLTensorAllocator.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/TensorInfo.h" |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/CL/CLMemoryGroup.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 30 | |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 31 | namespace arm_compute |
| 32 | { |
| 33 | const cl::Buffer CLTensorAllocator::_empty_buffer = cl::Buffer(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 35 | namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 37 | std::unique_ptr<ICLMemoryRegion> allocate_region(cl::Context context, size_t size, cl_uint alignment) |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 38 | { |
| 39 | // Try fine-grain SVM |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 40 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 45 | // Try coarse-grain SVM in case of failure |
| 46 | if(region != nullptr && region->ptr() == nullptr) |
| 47 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 48 | region = support::cpp14::make_unique<CLCoarseSVMMemoryRegion>(context, CL_MEM_READ_WRITE, size, alignment); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 49 | } |
| 50 | // Try legacy buffer memory in case of failure |
| 51 | if(region != nullptr && region->ptr() == nullptr) |
| 52 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 53 | region = support::cpp14::make_unique<CLBufferMemoryRegion>(context, CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, size); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 54 | } |
| 55 | return region; |
| 56 | } |
| 57 | } // namespace |
| 58 | |
| 59 | CLTensorAllocator::CLTensorAllocator(CLTensor *owner) |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 60 | : _associated_memory_group(nullptr), _memory(), _mapping(nullptr), _owner(owner) |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 61 | { |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | uint8_t *CLTensorAllocator::data() |
| 65 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 66 | return _mapping; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | const cl::Buffer &CLTensorAllocator::cl_data() const |
| 70 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 71 | return _memory.region() == nullptr ? _empty_buffer : _memory.cl_region()->cl_data(); |
Pablo Tello | e86a09f | 2018-01-11 15:44:48 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | void CLTensorAllocator::allocate() |
| 75 | { |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 76 | if(_associated_memory_group == nullptr) |
| 77 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 78 | if(_memory.region() != nullptr && _memory.cl_region()->cl_data().get() != nullptr) |
Michele Di Giorgio | aaab602 | 2018-05-14 11:58:24 +0100 | [diff] [blame] | 79 | { |
| 80 | // Memory is already allocated. Reuse it if big enough, otherwise fire an assertion |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 81 | ARM_COMPUTE_ERROR_ON_MSG(info().total_size() > _memory.region()->size(), |
| 82 | "Reallocation of a bigger memory region is not allowed!"); |
Michele Di Giorgio | aaab602 | 2018-05-14 11:58:24 +0100 | [diff] [blame] | 83 | } |
| 84 | else |
| 85 | { |
| 86 | // Perform memory allocation |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 87 | _memory.set_owned_region(allocate_region(CLScheduler::get().context(), info().total_size(), 0)); |
Michele Di Giorgio | aaab602 | 2018-05-14 11:58:24 +0100 | [diff] [blame] | 88 | } |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 89 | } |
| 90 | else |
| 91 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 92 | _associated_memory_group->finalize_memory(_owner, _memory, info().total_size()); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 93 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 94 | info().set_is_resizable(false); |
| 95 | } |
| 96 | |
| 97 | void CLTensorAllocator::free() |
| 98 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 99 | _mapping = nullptr; |
| 100 | _memory.set_region(nullptr); |
| 101 | info().set_is_resizable(true); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 102 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 104 | arm_compute::Status CLTensorAllocator::import_memory(cl::Buffer buffer) |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 105 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 106 | ARM_COMPUTE_RETURN_ERROR_ON(buffer.get() == nullptr); |
| 107 | ARM_COMPUTE_RETURN_ERROR_ON(buffer.getInfo<CL_MEM_SIZE>() == 0); |
| 108 | ARM_COMPUTE_RETURN_ERROR_ON(buffer.getInfo<CL_MEM_CONTEXT>().get() != CLScheduler::get().context().get()); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 109 | ARM_COMPUTE_RETURN_ERROR_ON(_associated_memory_group != nullptr); |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 110 | |
| 111 | _memory.set_owned_region(support::cpp14::make_unique<CLBufferMemoryRegion>(buffer)); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 112 | info().set_is_resizable(false); |
| 113 | |
| 114 | return Status{}; |
| 115 | } |
| 116 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 117 | void 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 Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 121 | ARM_COMPUTE_ERROR_ON(_memory.region() != nullptr && _memory.cl_region()->cl_data().get() != nullptr); |
| 122 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 123 | _associated_memory_group = associated_memory_group; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | uint8_t *CLTensorAllocator::lock() |
| 127 | { |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 128 | return map(CLScheduler::get().queue(), true); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void CLTensorAllocator::unlock() |
| 132 | { |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 133 | ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr); |
| 134 | unmap(CLScheduler::get().queue(), reinterpret_cast<uint8_t *>(_memory.region()->buffer())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | uint8_t *CLTensorAllocator::map(cl::CommandQueue &q, bool blocking) |
| 138 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 139 | ARM_COMPUTE_ERROR_ON(_mapping != nullptr); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 140 | ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr); |
| 141 | ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() != nullptr); |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 142 | |
| 143 | _mapping = reinterpret_cast<uint8_t *>(_memory.cl_region()->map(q, blocking)); |
| 144 | return _mapping; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void CLTensorAllocator::unmap(cl::CommandQueue &q, uint8_t *mapping) |
| 148 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 149 | ARM_COMPUTE_ERROR_ON(_mapping == nullptr); |
| 150 | ARM_COMPUTE_ERROR_ON(_mapping != mapping); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 151 | ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr); |
| 152 | ARM_COMPUTE_ERROR_ON(_memory.region()->buffer() == nullptr); |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 153 | ARM_COMPUTE_UNUSED(mapping); |
| 154 | |
| 155 | _memory.cl_region()->unmap(q); |
| 156 | _mapping = nullptr; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 157 | } |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 158 | } // namespace arm_compute |