blob: 93ea3d2c72e8979101870dd7e1f79d3fbee64ce0 [file] [log] [blame]
Georgios Pinitasbaf174e2017-09-08 19:47:30 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Georgios Pinitasbaf174e2017-09-08 19:47:30 +01003 *
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 Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_MEMORYGROUP_H
25#define ARM_COMPUTE_MEMORYGROUP_H
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010026
Georgios Pinitas26014cf2019-09-09 19:00:57 +010027#include "arm_compute/core/Error.h"
28#include "arm_compute/core/utils/misc/Macros.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029#include "arm_compute/runtime/IMemoryGroup.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010030#include "arm_compute/runtime/IMemoryManager.h"
31#include "arm_compute/runtime/IMemoryPool.h"
32
33#include <cstddef>
34#include <memory>
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010035
36namespace arm_compute
37{
Georgios Pinitas26014cf2019-09-09 19:00:57 +010038// Forward declarations
39class IMemory;
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010040
Georgios Pinitas26014cf2019-09-09 19:00:57 +010041/** Memory group */
42class MemoryGroup final : public IMemoryGroup
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010043{
Georgios Pinitas26014cf2019-09-09 19:00:57 +010044public:
45 /** Default Constructor */
Georgios Pinitas0b192e82020-02-20 17:09:28 +000046 MemoryGroup(std::shared_ptr<IMemoryManager> = nullptr) noexcept;
Georgios Pinitas26014cf2019-09-09 19:00:57 +010047 /** Default destructor */
48 ~MemoryGroup() = default;
49 /** Prevent instances of this class from being copied (As this class contains pointers) */
50 MemoryGroup(const MemoryGroup &) = delete;
51 /** Prevent instances of this class from being copy assigned (As this class contains pointers) */
52 MemoryGroup &operator=(const MemoryGroup &) = delete;
53 /** Allow instances of this class to be moved */
54 MemoryGroup(MemoryGroup &&) = default;
55 /** Allow instances of this class to be moved */
56 MemoryGroup &operator=(MemoryGroup &&) = default;
57
58 // Inherited methods overridden:
59 void manage(IMemoryManageable *obj) override;
60 void finalize_memory(IMemoryManageable *obj, IMemory &obj_memory, size_t size, size_t alignment) override;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010061 void acquire() override;
62 void release() override;
Georgios Pinitas26014cf2019-09-09 19:00:57 +010063 MemoryMappings &mappings() override;
64
65private:
66 std::shared_ptr<IMemoryManager> _memory_manager; /**< Memory manager to be used by the group */
67 IMemoryPool *_pool; /**< Memory pool that the group is scheduled with */
68 MemoryMappings _mappings; /**< Memory mappings of the group */
69};
70
Georgios Pinitas0b192e82020-02-20 17:09:28 +000071inline MemoryGroup::MemoryGroup(std::shared_ptr<IMemoryManager> memory_manager) noexcept
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072 : _memory_manager(memory_manager), _pool(nullptr), _mappings()
Georgios Pinitas26014cf2019-09-09 19:00:57 +010073{
74}
75
76inline void MemoryGroup::manage(IMemoryManageable *obj)
77{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010078 if (_memory_manager && (obj != nullptr))
Georgios Pinitas26014cf2019-09-09 19:00:57 +010079 {
80 ARM_COMPUTE_ERROR_ON(!_memory_manager->lifetime_manager());
81
82 // Defer registration to the first managed object
83 _memory_manager->lifetime_manager()->register_group(this);
84
85 // Associate this memory group with the tensor
86 obj->associate_memory_group(this);
87
88 // Start object lifetime
89 _memory_manager->lifetime_manager()->start_lifetime(obj);
90 }
91}
92
93inline void MemoryGroup::finalize_memory(IMemoryManageable *obj, IMemory &obj_memory, size_t size, size_t alignment)
94{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010095 if (_memory_manager)
Georgios Pinitas26014cf2019-09-09 19:00:57 +010096 {
97 ARM_COMPUTE_ERROR_ON(!_memory_manager->lifetime_manager());
98 _memory_manager->lifetime_manager()->end_lifetime(obj, obj_memory, size, alignment);
99 }
100}
101
102inline void MemoryGroup::acquire()
103{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100104 if (!_mappings.empty())
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100105 {
106 ARM_COMPUTE_ERROR_ON(!_memory_manager->pool_manager());
107 _pool = _memory_manager->pool_manager()->lock_pool();
108 _pool->acquire(_mappings);
109 }
110}
111
112inline void MemoryGroup::release()
113{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114 if (_pool != nullptr)
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100115 {
116 ARM_COMPUTE_ERROR_ON(!_memory_manager->pool_manager());
117 ARM_COMPUTE_ERROR_ON(_mappings.empty());
118 _pool->release(_mappings);
119 _memory_manager->pool_manager()->unlock_pool(_pool);
120 _pool = nullptr;
121 }
122}
123
124inline MemoryMappings &MemoryGroup::mappings()
125{
126 return _mappings;
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100127}
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100128} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000129#endif /*ARM_COMPUTE_MEMORYGROUP_H */