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 | |
| 31 | using namespace arm_compute; |
| 32 | |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 33 | namespace |
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 | std::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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 40 | // 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 | |
| 54 | CLTensorAllocator::CLTensorAllocator(CLTensor *owner) |
| 55 | : _associated_memory_group(nullptr), _memory(), _owner(owner) |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 56 | { |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | uint8_t *CLTensorAllocator::data() |
| 60 | { |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 61 | ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr); |
| 62 | return reinterpret_cast<uint8_t *>(_memory.region()->buffer()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | const cl::Buffer &CLTensorAllocator::cl_data() const |
| 66 | { |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 67 | ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr); |
| 68 | return _memory.region()->cl_data(); |
Pablo Tello | e86a09f | 2018-01-11 15:44:48 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 71 | void CLTensorAllocator::allocate() |
| 72 | { |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 73 | ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr); |
| 74 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 75 | if(_associated_memory_group == nullptr) |
| 76 | { |
Michele Di Giorgio | aaab602 | 2018-05-14 11:58:24 +0100 | [diff] [blame] | 77 | 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 Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 87 | } |
| 88 | else |
| 89 | { |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 90 | _associated_memory_group->finalize_memory(_owner, _memory.region()->handle(), info().total_size()); |
| 91 | _memory.region()->set_size(info().total_size()); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 92 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | info().set_is_resizable(false); |
| 94 | } |
| 95 | |
| 96 | void CLTensorAllocator::free() |
| 97 | { |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 98 | if(_associated_memory_group == nullptr) |
| 99 | { |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 100 | _memory = CLMemory(); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 101 | info().set_is_resizable(true); |
| 102 | } |
| 103 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 105 | arm_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 Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 116 | void CLTensorAllocator::set_associated_memory_group(CLMemoryGroup *associated_memory_group) |
| 117 | { |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 118 | ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 119 | ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr); |
| 120 | ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr); |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 121 | 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 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 | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 139 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void CLTensorAllocator::unmap(cl::CommandQueue &q, uint8_t *mapping) |
| 146 | { |
Georgios Pinitas | 99d4095 | 2018-04-23 16:26:46 +0100 | [diff] [blame] | 147 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 151 | } |