blob: ab8acb34940d77f151c97d03e6d450ee153835d8 [file] [log] [blame]
Georgios Pinitasbaf174e2017-09-08 19:47:30 +01001/*
2 * Copyright (c) 2017 ARM Limited.
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
29#include "arm_compute/runtime/IMemoryManager.h"
30#include "arm_compute/runtime/IMemoryPool.h"
31
32#include <cstddef>
33#include <memory>
34
35namespace arm_compute
36{
37/** Memory group */
38template <typename TensorType>
39class MemoryGroupBase : public IMemoryGroup
40{
41public:
42 /** Default Constructor */
43 MemoryGroupBase(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
44 /** Default destructor */
45 ~MemoryGroupBase() = default;
46 /** Prevent instances of this class from being copied (As this class contains pointers). */
47 MemoryGroupBase(const MemoryGroupBase &) = delete;
48 /** Prevent instances of this class from being copy assigned (As this class contains pointers). */
49 MemoryGroupBase &operator=(const MemoryGroupBase &) = delete;
50 /** Allow instances of this class to be moved */
51 MemoryGroupBase(MemoryGroupBase &&) = default;
52 /** Allow instances of this class to be moved */
53 MemoryGroupBase &operator=(MemoryGroupBase &&) = default;
54 /** Sets a object to be managed by the given memory group
55 *
56 * @note Manager must not be finalized
57 *
58 * @param[in] obj Object to be managed
59 */
60 void manage(TensorType *obj);
61 /** Finalizes memory for a given object
62 *
63 * @note Manager must not be finalized
64 *
65 * @param[in] obj Object to request memory for
66 * @param[in] handle Handle to store the memory
67 * @param[in] size Size of memory to allocate
68 */
69 void finalize_memory(TensorType *obj, void **handle, size_t size);
70
71 // Inherited methods overridden:
72 void acquire() override;
73 void release() override;
74 MemoryMappings &mappings() override;
75
76private:
77 void associate_memory_group(TensorType *obj);
78
79private:
80 std::shared_ptr<IMemoryManager> _memory_manager; /**< Memory manager to be used by the group */
81 IMemoryPool *_pool; /**< Memory pool that the group is scheduled with */
82 MemoryMappings _mappings; /**< Memory mappings of the group */
83};
84
85template <typename TensorType>
86inline MemoryGroupBase<TensorType>::MemoryGroupBase(std::shared_ptr<IMemoryManager> memory_manager)
87 : _memory_manager(std::move(memory_manager)), _pool(nullptr), _mappings()
88{
89 if(_memory_manager)
90 {
91 ARM_COMPUTE_ERROR_ON(!_memory_manager->lifetime_manager());
92 }
93}
94
95template <typename TensorType>
96inline void MemoryGroupBase<TensorType>::manage(TensorType *obj)
97{
98 if(_memory_manager)
99 {
100 ARM_COMPUTE_ERROR_ON(!_memory_manager->lifetime_manager());
101
102 // Defer registration to the first managed object
103 _memory_manager->lifetime_manager()->register_group(this);
104
105 // Associate this memory group with the tensor
106 associate_memory_group(obj);
107
108 // Start object lifetime
109 _memory_manager->lifetime_manager()->start_lifetime(obj);
110 }
111}
112
113template <typename TensorType>
114inline void MemoryGroupBase<TensorType>::finalize_memory(TensorType *obj, void **handle, size_t size)
115{
116 if(_memory_manager)
117 {
118 ARM_COMPUTE_ERROR_ON(!_memory_manager->lifetime_manager());
119 _memory_manager->lifetime_manager()->end_lifetime(obj, handle, size);
120 }
121}
122
123template <typename TensorType>
124inline void MemoryGroupBase<TensorType>::acquire()
125{
126 if(!_mappings.empty())
127 {
128 ARM_COMPUTE_ERROR_ON(!_memory_manager->pool_manager());
129 _pool = _memory_manager->pool_manager()->lock_pool();
130 _pool->acquire(_mappings);
131 }
132}
133
134template <typename TensorType>
135inline void MemoryGroupBase<TensorType>::release()
136{
137 if(_pool != nullptr)
138 {
139 ARM_COMPUTE_ERROR_ON(!_memory_manager->pool_manager());
140 ARM_COMPUTE_ERROR_ON(_mappings.empty());
141 _pool->release(_mappings);
142 _memory_manager->pool_manager()->unlock_pool(_pool);
143 _pool = nullptr;
144 }
145}
146
147template <typename TensorType>
148inline MemoryMappings &MemoryGroupBase<TensorType>::mappings()
149{
150 return _mappings;
151}
152
153template <typename TensorType>
154inline void MemoryGroupBase<TensorType>::associate_memory_group(TensorType *)
155{
156 ARM_COMPUTE_ERROR("Must be implemented by child class");
157}
158} // arm_compute
159#endif /*__ARM_COMPUTE_MEMORYGROUPBASE_H__ */