blob: 0ceaa900c5fcdb13a00798ebb482f962052e8d73 [file] [log] [blame]
Georgios Pinitasbaf174e2017-09-08 19:47:30 +01001/*
Alex Gildayc357c472018-03-21 13:54:09 +00002 * Copyright (c) 2017-2018 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 */
24#ifndef __ARM_COMPUTE_MEMORYGROUPBASE_H__
25#define __ARM_COMPUTE_MEMORYGROUPBASE_H__
26
27#include "arm_compute/runtime/IMemoryGroup.h"
28
Georgios Pinitas84b51ad2017-11-07 13:24:57 +000029#include "arm_compute/core/Error.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010030#include "arm_compute/runtime/IMemoryManager.h"
31#include "arm_compute/runtime/IMemoryPool.h"
32
33#include <cstddef>
34#include <memory>
35
36namespace arm_compute
37{
Georgios Pinitasdf310362018-11-14 13:16:56 +000038// Forward declarations
39class IMemory;
40
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010041/** Memory group */
42template <typename TensorType>
43class MemoryGroupBase : public IMemoryGroup
44{
45public:
46 /** Default Constructor */
47 MemoryGroupBase(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
48 /** Default destructor */
49 ~MemoryGroupBase() = default;
Alex Gildayc357c472018-03-21 13:54:09 +000050 /** Prevent instances of this class from being copied (As this class contains pointers) */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010051 MemoryGroupBase(const MemoryGroupBase &) = delete;
Alex Gildayc357c472018-03-21 13:54:09 +000052 /** Prevent instances of this class from being copy assigned (As this class contains pointers) */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010053 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 Pinitasdf310362018-11-14 13:16:56 +000069 * @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 Pinitasbaf174e2017-09-08 19:47:30 +010073 */
Georgios Pinitasdf310362018-11-14 13:16:56 +000074 void finalize_memory(TensorType *obj, IMemory &obj_memory, size_t size);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010075
76 // Inherited methods overridden:
77 void acquire() override;
78 void release() override;
79 MemoryMappings &mappings() override;
80
81private:
82 void associate_memory_group(TensorType *obj);
83
84private:
85 std::shared_ptr<IMemoryManager> _memory_manager; /**< Memory manager to be used by the group */
86 IMemoryPool *_pool; /**< Memory pool that the group is scheduled with */
87 MemoryMappings _mappings; /**< Memory mappings of the group */
88};
89
90template <typename TensorType>
91inline MemoryGroupBase<TensorType>::MemoryGroupBase(std::shared_ptr<IMemoryManager> memory_manager)
92 : _memory_manager(std::move(memory_manager)), _pool(nullptr), _mappings()
93{
94 if(_memory_manager)
95 {
96 ARM_COMPUTE_ERROR_ON(!_memory_manager->lifetime_manager());
97 }
98}
99
100template <typename TensorType>
101inline void MemoryGroupBase<TensorType>::manage(TensorType *obj)
102{
Georgios Pinitasceff0f92018-03-19 19:57:01 +0000103 if(_memory_manager && _mappings.empty())
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100104 {
105 ARM_COMPUTE_ERROR_ON(!_memory_manager->lifetime_manager());
106
107 // Defer registration to the first managed object
108 _memory_manager->lifetime_manager()->register_group(this);
109
110 // Associate this memory group with the tensor
111 associate_memory_group(obj);
112
113 // Start object lifetime
114 _memory_manager->lifetime_manager()->start_lifetime(obj);
115 }
116}
117
118template <typename TensorType>
Georgios Pinitasdf310362018-11-14 13:16:56 +0000119inline void MemoryGroupBase<TensorType>::finalize_memory(TensorType *obj, IMemory &obj_memory, size_t size)
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100120{
Georgios Pinitasceff0f92018-03-19 19:57:01 +0000121 // TODO (geopin01) : Check size (track size in MemoryMappings)
122 // Check if existing mapping is valid
Georgios Pinitasdf310362018-11-14 13:16:56 +0000123 ARM_COMPUTE_ERROR_ON(!_mappings.empty() && (_mappings.find(&obj_memory) == std::end(_mappings)));
Georgios Pinitasceff0f92018-03-19 19:57:01 +0000124
125 if(_memory_manager && _mappings.empty())
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100126 {
127 ARM_COMPUTE_ERROR_ON(!_memory_manager->lifetime_manager());
Georgios Pinitasdf310362018-11-14 13:16:56 +0000128 _memory_manager->lifetime_manager()->end_lifetime(obj, obj_memory, size);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100129 }
130}
131
132template <typename TensorType>
133inline void MemoryGroupBase<TensorType>::acquire()
134{
135 if(!_mappings.empty())
136 {
137 ARM_COMPUTE_ERROR_ON(!_memory_manager->pool_manager());
138 _pool = _memory_manager->pool_manager()->lock_pool();
139 _pool->acquire(_mappings);
140 }
141}
142
143template <typename TensorType>
144inline void MemoryGroupBase<TensorType>::release()
145{
146 if(_pool != nullptr)
147 {
148 ARM_COMPUTE_ERROR_ON(!_memory_manager->pool_manager());
149 ARM_COMPUTE_ERROR_ON(_mappings.empty());
150 _pool->release(_mappings);
151 _memory_manager->pool_manager()->unlock_pool(_pool);
152 _pool = nullptr;
153 }
154}
155
156template <typename TensorType>
157inline MemoryMappings &MemoryGroupBase<TensorType>::mappings()
158{
159 return _mappings;
160}
161
162template <typename TensorType>
163inline void MemoryGroupBase<TensorType>::associate_memory_group(TensorType *)
164{
165 ARM_COMPUTE_ERROR("Must be implemented by child class");
166}
167} // arm_compute
168#endif /*__ARM_COMPUTE_MEMORYGROUPBASE_H__ */