Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 1 | /* |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +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 | #ifndef __ARM_COMPUTE_MEMORYGROUPBASE_H__ |
| 25 | #define __ARM_COMPUTE_MEMORYGROUPBASE_H__ |
| 26 | |
| 27 | #include "arm_compute/runtime/IMemoryGroup.h" |
| 28 | |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/Error.h" |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/IMemoryManager.h" |
| 31 | #include "arm_compute/runtime/IMemoryPool.h" |
| 32 | |
| 33 | #include <cstddef> |
| 34 | #include <memory> |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 38 | // Forward declarations |
| 39 | class IMemory; |
| 40 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 41 | /** Memory group */ |
| 42 | template <typename TensorType> |
| 43 | class MemoryGroupBase : public IMemoryGroup |
| 44 | { |
| 45 | public: |
| 46 | /** Default Constructor */ |
| 47 | MemoryGroupBase(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
| 48 | /** Default destructor */ |
| 49 | ~MemoryGroupBase() = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 50 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 51 | MemoryGroupBase(const MemoryGroupBase &) = delete; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 52 | /** Prevent instances of this class from being copy assigned (As this class contains pointers) */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 53 | MemoryGroupBase &operator=(const MemoryGroupBase &) = delete; |
| 54 | /** Allow instances of this class to be moved */ |
| 55 | MemoryGroupBase(MemoryGroupBase &&) = default; |
| 56 | /** Allow instances of this class to be moved */ |
| 57 | MemoryGroupBase &operator=(MemoryGroupBase &&) = default; |
| 58 | /** Sets a object to be managed by the given memory group |
| 59 | * |
| 60 | * @note Manager must not be finalized |
| 61 | * |
| 62 | * @param[in] obj Object to be managed |
| 63 | */ |
| 64 | void manage(TensorType *obj); |
| 65 | /** Finalizes memory for a given object |
| 66 | * |
| 67 | * @note Manager must not be finalized |
| 68 | * |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 69 | * @param[in, out] obj Object to request memory for |
| 70 | * @param[in, out] obj_memory Object's memory handling interface which can be used to alter the underlying memory |
| 71 | * that is used by the object. |
| 72 | * @param[in] size Size of memory to allocate |
Georgios Pinitas | 555f1c2 | 2018-12-14 17:11:20 +0000 | [diff] [blame] | 73 | * @param[in] alignment (Optional) Alignment to use |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 74 | */ |
Georgios Pinitas | 555f1c2 | 2018-12-14 17:11:20 +0000 | [diff] [blame] | 75 | void finalize_memory(TensorType *obj, IMemory &obj_memory, size_t size, size_t alignment = 0); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 76 | |
| 77 | // Inherited methods overridden: |
| 78 | void acquire() override; |
| 79 | void release() override; |
| 80 | MemoryMappings &mappings() override; |
| 81 | |
| 82 | private: |
| 83 | void associate_memory_group(TensorType *obj); |
| 84 | |
| 85 | private: |
| 86 | std::shared_ptr<IMemoryManager> _memory_manager; /**< Memory manager to be used by the group */ |
| 87 | IMemoryPool *_pool; /**< Memory pool that the group is scheduled with */ |
| 88 | MemoryMappings _mappings; /**< Memory mappings of the group */ |
| 89 | }; |
| 90 | |
| 91 | template <typename TensorType> |
| 92 | inline MemoryGroupBase<TensorType>::MemoryGroupBase(std::shared_ptr<IMemoryManager> memory_manager) |
| 93 | : _memory_manager(std::move(memory_manager)), _pool(nullptr), _mappings() |
| 94 | { |
| 95 | if(_memory_manager) |
| 96 | { |
| 97 | ARM_COMPUTE_ERROR_ON(!_memory_manager->lifetime_manager()); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | template <typename TensorType> |
| 102 | inline void MemoryGroupBase<TensorType>::manage(TensorType *obj) |
| 103 | { |
Georgios Pinitas | ceff0f9 | 2018-03-19 19:57:01 +0000 | [diff] [blame] | 104 | if(_memory_manager && _mappings.empty()) |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 105 | { |
| 106 | ARM_COMPUTE_ERROR_ON(!_memory_manager->lifetime_manager()); |
| 107 | |
| 108 | // Defer registration to the first managed object |
| 109 | _memory_manager->lifetime_manager()->register_group(this); |
| 110 | |
| 111 | // Associate this memory group with the tensor |
| 112 | associate_memory_group(obj); |
| 113 | |
| 114 | // Start object lifetime |
| 115 | _memory_manager->lifetime_manager()->start_lifetime(obj); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | template <typename TensorType> |
Georgios Pinitas | 555f1c2 | 2018-12-14 17:11:20 +0000 | [diff] [blame] | 120 | inline void MemoryGroupBase<TensorType>::finalize_memory(TensorType *obj, IMemory &obj_memory, size_t size, size_t alignment) |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 121 | { |
Georgios Pinitas | ceff0f9 | 2018-03-19 19:57:01 +0000 | [diff] [blame] | 122 | // TODO (geopin01) : Check size (track size in MemoryMappings) |
| 123 | // Check if existing mapping is valid |
Georgios Pinitas | df31036 | 2018-11-14 13:16:56 +0000 | [diff] [blame] | 124 | ARM_COMPUTE_ERROR_ON(!_mappings.empty() && (_mappings.find(&obj_memory) == std::end(_mappings))); |
Georgios Pinitas | ceff0f9 | 2018-03-19 19:57:01 +0000 | [diff] [blame] | 125 | |
| 126 | if(_memory_manager && _mappings.empty()) |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 127 | { |
| 128 | ARM_COMPUTE_ERROR_ON(!_memory_manager->lifetime_manager()); |
Georgios Pinitas | 555f1c2 | 2018-12-14 17:11:20 +0000 | [diff] [blame] | 129 | _memory_manager->lifetime_manager()->end_lifetime(obj, obj_memory, size, alignment); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | template <typename TensorType> |
| 134 | inline void MemoryGroupBase<TensorType>::acquire() |
| 135 | { |
| 136 | if(!_mappings.empty()) |
| 137 | { |
| 138 | ARM_COMPUTE_ERROR_ON(!_memory_manager->pool_manager()); |
| 139 | _pool = _memory_manager->pool_manager()->lock_pool(); |
| 140 | _pool->acquire(_mappings); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | template <typename TensorType> |
| 145 | inline void MemoryGroupBase<TensorType>::release() |
| 146 | { |
| 147 | if(_pool != nullptr) |
| 148 | { |
| 149 | ARM_COMPUTE_ERROR_ON(!_memory_manager->pool_manager()); |
| 150 | ARM_COMPUTE_ERROR_ON(_mappings.empty()); |
| 151 | _pool->release(_mappings); |
| 152 | _memory_manager->pool_manager()->unlock_pool(_pool); |
| 153 | _pool = nullptr; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | template <typename TensorType> |
| 158 | inline MemoryMappings &MemoryGroupBase<TensorType>::mappings() |
| 159 | { |
| 160 | return _mappings; |
| 161 | } |
| 162 | |
| 163 | template <typename TensorType> |
| 164 | inline void MemoryGroupBase<TensorType>::associate_memory_group(TensorType *) |
| 165 | { |
| 166 | ARM_COMPUTE_ERROR("Must be implemented by child class"); |
| 167 | } |
| 168 | } // arm_compute |
| 169 | #endif /*__ARM_COMPUTE_MEMORYGROUPBASE_H__ */ |