Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_MEMORYGROUP_H |
| 25 | #define ARM_COMPUTE_MEMORYGROUP_H |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 26 | |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 27 | #include "arm_compute/runtime/IMemoryGroup.h" |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 28 | |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Error.h" |
| 30 | #include "arm_compute/core/utils/misc/Macros.h" |
| 31 | #include "arm_compute/runtime/IMemoryManager.h" |
| 32 | #include "arm_compute/runtime/IMemoryPool.h" |
| 33 | |
| 34 | #include <cstddef> |
| 35 | #include <memory> |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 36 | |
| 37 | namespace arm_compute |
| 38 | { |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 39 | // Forward declarations |
| 40 | class IMemory; |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 41 | |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 42 | /** Memory group */ |
| 43 | class MemoryGroup final : public IMemoryGroup |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 44 | { |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 45 | public: |
| 46 | /** Default Constructor */ |
Georgios Pinitas | 0b192e8 | 2020-02-20 17:09:28 +0000 | [diff] [blame] | 47 | MemoryGroup(std::shared_ptr<IMemoryManager> = nullptr) noexcept; |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 48 | /** Default destructor */ |
| 49 | ~MemoryGroup() = default; |
| 50 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 51 | MemoryGroup(const MemoryGroup &) = delete; |
| 52 | /** Prevent instances of this class from being copy assigned (As this class contains pointers) */ |
| 53 | MemoryGroup &operator=(const MemoryGroup &) = delete; |
| 54 | /** Allow instances of this class to be moved */ |
| 55 | MemoryGroup(MemoryGroup &&) = default; |
| 56 | /** Allow instances of this class to be moved */ |
| 57 | MemoryGroup &operator=(MemoryGroup &&) = default; |
| 58 | |
| 59 | // Inherited methods overridden: |
| 60 | void manage(IMemoryManageable *obj) override; |
| 61 | void finalize_memory(IMemoryManageable *obj, IMemory &obj_memory, size_t size, size_t alignment) override; |
| 62 | void acquire() override; |
| 63 | void release() override; |
| 64 | MemoryMappings &mappings() override; |
| 65 | |
| 66 | private: |
| 67 | std::shared_ptr<IMemoryManager> _memory_manager; /**< Memory manager to be used by the group */ |
| 68 | IMemoryPool *_pool; /**< Memory pool that the group is scheduled with */ |
| 69 | MemoryMappings _mappings; /**< Memory mappings of the group */ |
| 70 | }; |
| 71 | |
Georgios Pinitas | 0b192e8 | 2020-02-20 17:09:28 +0000 | [diff] [blame] | 72 | inline MemoryGroup::MemoryGroup(std::shared_ptr<IMemoryManager> memory_manager) noexcept |
| 73 | : _memory_manager(memory_manager), |
| 74 | _pool(nullptr), |
| 75 | _mappings() |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 76 | { |
| 77 | } |
| 78 | |
| 79 | inline void MemoryGroup::manage(IMemoryManageable *obj) |
| 80 | { |
| 81 | if(_memory_manager && (obj != nullptr)) |
| 82 | { |
| 83 | ARM_COMPUTE_ERROR_ON(!_memory_manager->lifetime_manager()); |
| 84 | |
| 85 | // Defer registration to the first managed object |
| 86 | _memory_manager->lifetime_manager()->register_group(this); |
| 87 | |
| 88 | // Associate this memory group with the tensor |
| 89 | obj->associate_memory_group(this); |
| 90 | |
| 91 | // Start object lifetime |
| 92 | _memory_manager->lifetime_manager()->start_lifetime(obj); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | inline void MemoryGroup::finalize_memory(IMemoryManageable *obj, IMemory &obj_memory, size_t size, size_t alignment) |
| 97 | { |
| 98 | if(_memory_manager) |
| 99 | { |
| 100 | ARM_COMPUTE_ERROR_ON(!_memory_manager->lifetime_manager()); |
| 101 | _memory_manager->lifetime_manager()->end_lifetime(obj, obj_memory, size, alignment); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | inline void MemoryGroup::acquire() |
| 106 | { |
| 107 | if(!_mappings.empty()) |
| 108 | { |
| 109 | ARM_COMPUTE_ERROR_ON(!_memory_manager->pool_manager()); |
| 110 | _pool = _memory_manager->pool_manager()->lock_pool(); |
| 111 | _pool->acquire(_mappings); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | inline void MemoryGroup::release() |
| 116 | { |
| 117 | if(_pool != nullptr) |
| 118 | { |
| 119 | ARM_COMPUTE_ERROR_ON(!_memory_manager->pool_manager()); |
| 120 | ARM_COMPUTE_ERROR_ON(_mappings.empty()); |
| 121 | _pool->release(_mappings); |
| 122 | _memory_manager->pool_manager()->unlock_pool(_pool); |
| 123 | _pool = nullptr; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | inline MemoryMappings &MemoryGroup::mappings() |
| 128 | { |
| 129 | return _mappings; |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 130 | } |
| 131 | } // arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 132 | #endif /*ARM_COMPUTE_MEMORYGROUP_H */ |