Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +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 | |
| 25 | #include "arm_compute/runtime/GLES_COMPUTE/GCTensorAllocator.h" |
| 26 | |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/TensorInfo.h" |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 29 | #include "arm_compute/runtime/GLES_COMPUTE/GCMemoryRegion.h" |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h" |
| 31 | #include "support/ToolchainSupport.h" |
| 32 | |
| 33 | using namespace arm_compute; |
| 34 | |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 35 | GCTensorAllocator::GCTensorAllocator(GCTensor *owner) |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 36 | : _associated_memory_group(nullptr), _memory(), _mapping(nullptr), _owner(owner) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | |
| 40 | uint8_t *GCTensorAllocator::data() |
| 41 | { |
| 42 | return _mapping; |
| 43 | } |
| 44 | |
| 45 | void GCTensorAllocator::allocate() |
| 46 | { |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 47 | if(_associated_memory_group == nullptr) |
| 48 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 49 | _memory.set_owned_region(support::cpp14::make_unique<GCBufferMemoryRegion>(info().total_size())); |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 50 | } |
| 51 | else |
| 52 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 53 | _associated_memory_group->finalize_memory(_owner, _memory, info().total_size()); |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 54 | } |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 55 | info().set_is_resizable(false); |
| 56 | } |
| 57 | |
| 58 | void GCTensorAllocator::free() |
| 59 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 60 | _mapping = nullptr; |
| 61 | _memory.set_region(nullptr); |
| 62 | info().set_is_resizable(true); |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void GCTensorAllocator::set_associated_memory_group(GCMemoryGroup *associated_memory_group) |
| 66 | { |
| 67 | ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr); |
| 68 | ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr); |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 69 | ARM_COMPUTE_ERROR_ON(_memory.region() != nullptr && _memory.gc_region()->gc_ssbo_name() != 0); |
| 70 | |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 71 | _associated_memory_group = associated_memory_group; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | uint8_t *GCTensorAllocator::lock() |
| 75 | { |
| 76 | return map(true); |
| 77 | } |
| 78 | |
| 79 | void GCTensorAllocator::unlock() |
| 80 | { |
| 81 | unmap(); |
| 82 | } |
| 83 | |
| 84 | GLuint GCTensorAllocator::get_gl_ssbo_name() const |
| 85 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 86 | return (_memory.region() == nullptr) ? static_cast<GLuint>(0) : _memory.gc_region()->gc_ssbo_name(); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | uint8_t *GCTensorAllocator::map(bool blocking) |
| 90 | { |
| 91 | ARM_COMPUTE_ERROR_ON(_mapping != nullptr); |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 92 | ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 93 | |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 94 | _mapping = reinterpret_cast<uint8_t *>(_memory.gc_region()->map(blocking)); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 95 | return _mapping; |
| 96 | } |
| 97 | |
| 98 | void GCTensorAllocator::unmap() |
| 99 | { |
| 100 | ARM_COMPUTE_ERROR_ON(_mapping == nullptr); |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 101 | ARM_COMPUTE_ERROR_ON(_memory.region() == nullptr); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 102 | |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 103 | _memory.gc_region()->unmap(); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 104 | _mapping = nullptr; |
| 105 | } |