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 | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 33 | CLTensorAllocator::CLTensorAllocator(CLTensor *owner) |
Pablo Tello | e86a09f | 2018-01-11 15:44:48 +0000 | [diff] [blame] | 34 | : _associated_memory_group(nullptr), _buffer(), _mapping(nullptr), _owner(owner), _svm_memory() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | { |
| 36 | } |
| 37 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 38 | CLTensorAllocator::~CLTensorAllocator() |
| 39 | { |
| 40 | _buffer = cl::Buffer(); |
| 41 | } |
| 42 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | uint8_t *CLTensorAllocator::data() |
| 44 | { |
| 45 | return _mapping; |
| 46 | } |
| 47 | |
| 48 | const cl::Buffer &CLTensorAllocator::cl_data() const |
| 49 | { |
| 50 | return _buffer; |
| 51 | } |
| 52 | |
Pablo Tello | e86a09f | 2018-01-11 15:44:48 +0000 | [diff] [blame] | 53 | void *SVMMemory::allocate(cl_context context, size_t size, cl_svm_mem_flags flags, cl_uint alignment) |
| 54 | { |
| 55 | ARM_COMPUTE_ERROR_ON_NULLPTR(context); |
| 56 | ARM_COMPUTE_ERROR_ON(size == 0); |
| 57 | ARM_COMPUTE_ERROR_ON(_ptr != nullptr); |
| 58 | ARM_COMPUTE_ERROR_ON(size > CL_DEVICE_MAX_MEM_ALLOC_SIZE); |
| 59 | _ptr = clSVMAlloc(context, flags, size, alignment); |
Georgios Pinitas | ceff0f9 | 2018-03-19 19:57:01 +0000 | [diff] [blame] | 60 | if(_ptr != nullptr) |
Pablo Tello | e86a09f | 2018-01-11 15:44:48 +0000 | [diff] [blame] | 61 | { |
| 62 | _size = size; |
| 63 | _fine_grain = static_cast<bool>(flags & CL_MEM_SVM_FINE_GRAIN_BUFFER); |
| 64 | } |
| 65 | return _ptr; |
| 66 | } |
| 67 | void *CLTensorAllocator::svm_ptr() |
| 68 | { |
| 69 | return _svm_memory.ptr(); |
| 70 | } |
| 71 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | void CLTensorAllocator::allocate() |
| 73 | { |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 74 | if(_associated_memory_group == nullptr) |
| 75 | { |
Georgios Pinitas | ceff0f9 | 2018-03-19 19:57:01 +0000 | [diff] [blame] | 76 | ARM_COMPUTE_ERROR_ON(_buffer.get() != nullptr); |
Pablo Tello | e86a09f | 2018-01-11 15:44:48 +0000 | [diff] [blame] | 77 | if(_svm_memory.allocate(CLScheduler::get().context()(), CL_MEM_READ_WRITE | CL_MEM_SVM_FINE_GRAIN_BUFFER, info().total_size(), 0) == nullptr) |
| 78 | { |
| 79 | // try at coarse grain svm memory |
| 80 | _svm_memory.allocate(CLScheduler::get().context()(), CL_MEM_READ_WRITE, info().total_size(), 0); |
| 81 | } |
| 82 | if(_svm_memory.ptr() != nullptr) |
| 83 | { |
| 84 | _buffer = cl::Buffer(CLScheduler::get().context(), CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, info().total_size(), _svm_memory.ptr()); |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | _buffer = cl::Buffer(CLScheduler::get().context(), CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE, info().total_size()); |
| 89 | } |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 90 | } |
| 91 | else |
| 92 | { |
| 93 | _associated_memory_group->finalize_memory(_owner, reinterpret_cast<void **>(&_buffer()), info().total_size()); |
| 94 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 95 | info().set_is_resizable(false); |
| 96 | } |
| 97 | |
| 98 | void CLTensorAllocator::free() |
| 99 | { |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 100 | if(_associated_memory_group == nullptr) |
| 101 | { |
| 102 | _buffer = cl::Buffer(); |
Pablo Tello | e86a09f | 2018-01-11 15:44:48 +0000 | [diff] [blame] | 103 | if(_svm_memory.ptr() != nullptr) |
| 104 | { |
| 105 | clSVMFree(CLScheduler::get().context()(), _svm_memory.ptr()); |
| 106 | } |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 107 | info().set_is_resizable(true); |
| 108 | } |
| 109 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 110 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 111 | void CLTensorAllocator::set_associated_memory_group(CLMemoryGroup *associated_memory_group) |
| 112 | { |
| 113 | ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr); |
| 114 | ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr); |
| 115 | ARM_COMPUTE_ERROR_ON(_buffer.get() != nullptr); |
| 116 | _associated_memory_group = associated_memory_group; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | uint8_t *CLTensorAllocator::lock() |
| 120 | { |
| 121 | ARM_COMPUTE_ERROR_ON(_mapping != nullptr); |
| 122 | _mapping = map(CLScheduler::get().queue(), true); |
| 123 | return _mapping; |
| 124 | } |
| 125 | |
| 126 | void CLTensorAllocator::unlock() |
| 127 | { |
| 128 | ARM_COMPUTE_ERROR_ON(_mapping == nullptr); |
| 129 | unmap(CLScheduler::get().queue(), _mapping); |
| 130 | _mapping = nullptr; |
| 131 | } |
| 132 | |
| 133 | uint8_t *CLTensorAllocator::map(cl::CommandQueue &q, bool blocking) |
| 134 | { |
Pablo Tello | e86a09f | 2018-01-11 15:44:48 +0000 | [diff] [blame] | 135 | const bool svm_mem = _svm_memory.ptr() != nullptr; |
| 136 | const bool fine_grain_svm = _svm_memory.fine_grain(); |
| 137 | if(!svm_mem) |
| 138 | { |
| 139 | ARM_COMPUTE_ERROR_ON(_buffer.get() == nullptr); |
| 140 | return static_cast<uint8_t *>(q.enqueueMapBuffer(_buffer, blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, 0, info().total_size())); |
| 141 | } |
| 142 | else if(!fine_grain_svm) |
| 143 | { |
| 144 | const cl_int ret = clEnqueueSVMMap(q(), blocking ? CL_TRUE : CL_FALSE, CL_MAP_READ | CL_MAP_WRITE, _svm_memory.ptr(), _svm_memory.size(), 0, nullptr, nullptr); |
| 145 | ARM_COMPUTE_ERROR_ON(ret != CL_SUCCESS); |
| 146 | if(ret == CL_SUCCESS) |
| 147 | { |
| 148 | return reinterpret_cast<uint8_t *>(_svm_memory.ptr()); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | return nullptr; |
| 153 | } |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | if(blocking) |
| 158 | { |
| 159 | clFinish(q()); |
| 160 | } |
| 161 | return reinterpret_cast<uint8_t *>(_svm_memory.ptr()); |
| 162 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void CLTensorAllocator::unmap(cl::CommandQueue &q, uint8_t *mapping) |
| 166 | { |
Pablo Tello | e86a09f | 2018-01-11 15:44:48 +0000 | [diff] [blame] | 167 | const bool svm_mem = _svm_memory.ptr() != nullptr; |
| 168 | const bool fine_grain_svm = _svm_memory.fine_grain(); |
| 169 | if(!svm_mem) |
| 170 | { |
| 171 | ARM_COMPUTE_ERROR_ON(_buffer.get() == nullptr); |
| 172 | q.enqueueUnmapMemObject(_buffer, mapping); |
| 173 | } |
| 174 | else if(!fine_grain_svm) |
| 175 | { |
| 176 | clEnqueueSVMUnmap(q(), _svm_memory.ptr(), 0, nullptr, nullptr); |
| 177 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 178 | } |