1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2017-2020 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "arm_compute/runtime/ISimpleLifetimeManager.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/core/Error.h"
27*c217d954SCole Faust #include "arm_compute/runtime/IAllocator.h"
28*c217d954SCole Faust #include "arm_compute/runtime/IMemory.h"
29*c217d954SCole Faust #include "arm_compute/runtime/IMemoryGroup.h"
30*c217d954SCole Faust #include "arm_compute/runtime/IMemoryPool.h"
31*c217d954SCole Faust
32*c217d954SCole Faust #include <algorithm>
33*c217d954SCole Faust #include <cmath>
34*c217d954SCole Faust #include <map>
35*c217d954SCole Faust #include <vector>
36*c217d954SCole Faust
37*c217d954SCole Faust namespace arm_compute
38*c217d954SCole Faust {
ISimpleLifetimeManager()39*c217d954SCole Faust ISimpleLifetimeManager::ISimpleLifetimeManager()
40*c217d954SCole Faust : _active_group(nullptr), _active_elements(), _free_blobs(), _occupied_blobs(), _finalized_groups()
41*c217d954SCole Faust {
42*c217d954SCole Faust }
43*c217d954SCole Faust
register_group(IMemoryGroup * group)44*c217d954SCole Faust void ISimpleLifetimeManager::register_group(IMemoryGroup *group)
45*c217d954SCole Faust {
46*c217d954SCole Faust if(_active_group == nullptr)
47*c217d954SCole Faust {
48*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(group == nullptr);
49*c217d954SCole Faust _active_group = group;
50*c217d954SCole Faust }
51*c217d954SCole Faust }
52*c217d954SCole Faust
release_group(IMemoryGroup * group)53*c217d954SCole Faust bool ISimpleLifetimeManager::release_group(IMemoryGroup *group)
54*c217d954SCole Faust {
55*c217d954SCole Faust if(group == nullptr)
56*c217d954SCole Faust {
57*c217d954SCole Faust return false;
58*c217d954SCole Faust }
59*c217d954SCole Faust const bool status = bool(_finalized_groups.erase(group));
60*c217d954SCole Faust if(status)
61*c217d954SCole Faust {
62*c217d954SCole Faust group->mappings().clear();
63*c217d954SCole Faust }
64*c217d954SCole Faust return status;
65*c217d954SCole Faust }
66*c217d954SCole Faust
start_lifetime(void * obj)67*c217d954SCole Faust void ISimpleLifetimeManager::start_lifetime(void *obj)
68*c217d954SCole Faust {
69*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(obj == nullptr);
70*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_MSG(_active_elements.find(obj) != std::end(_active_elements), "Memory object is already registered!");
71*c217d954SCole Faust
72*c217d954SCole Faust // Check if there is a free blob
73*c217d954SCole Faust if(_free_blobs.empty())
74*c217d954SCole Faust {
75*c217d954SCole Faust _occupied_blobs.emplace_front(Blob{ obj, 0, 0, { obj } });
76*c217d954SCole Faust }
77*c217d954SCole Faust else
78*c217d954SCole Faust {
79*c217d954SCole Faust _occupied_blobs.splice(std::begin(_occupied_blobs), _free_blobs, std::begin(_free_blobs));
80*c217d954SCole Faust _occupied_blobs.front().id = obj;
81*c217d954SCole Faust }
82*c217d954SCole Faust
83*c217d954SCole Faust // Insert object in groups and mark its finalized state to false
84*c217d954SCole Faust _active_elements.insert(std::make_pair(obj, obj));
85*c217d954SCole Faust }
86*c217d954SCole Faust
end_lifetime(void * obj,IMemory & obj_memory,size_t size,size_t alignment)87*c217d954SCole Faust void ISimpleLifetimeManager::end_lifetime(void *obj, IMemory &obj_memory, size_t size, size_t alignment)
88*c217d954SCole Faust {
89*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(obj == nullptr);
90*c217d954SCole Faust
91*c217d954SCole Faust // Find object
92*c217d954SCole Faust auto active_object_it = _active_elements.find(obj);
93*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(active_object_it == std::end(_active_elements));
94*c217d954SCole Faust
95*c217d954SCole Faust // Update object fields and mark object as complete
96*c217d954SCole Faust Element &el = active_object_it->second;
97*c217d954SCole Faust el.handle = &obj_memory;
98*c217d954SCole Faust el.size = size;
99*c217d954SCole Faust el.alignment = alignment;
100*c217d954SCole Faust el.status = true;
101*c217d954SCole Faust
102*c217d954SCole Faust // Find object in the occupied lists
103*c217d954SCole Faust auto occupied_blob_it = std::find_if(std::begin(_occupied_blobs), std::end(_occupied_blobs), [&obj](const Blob & b)
104*c217d954SCole Faust {
105*c217d954SCole Faust return obj == b.id;
106*c217d954SCole Faust });
107*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(occupied_blob_it == std::end(_occupied_blobs));
108*c217d954SCole Faust
109*c217d954SCole Faust // Update occupied blob and return as free
110*c217d954SCole Faust occupied_blob_it->bound_elements.insert(obj);
111*c217d954SCole Faust occupied_blob_it->max_size = std::max(occupied_blob_it->max_size, size);
112*c217d954SCole Faust occupied_blob_it->max_alignment = std::max(occupied_blob_it->max_alignment, alignment);
113*c217d954SCole Faust occupied_blob_it->id = nullptr;
114*c217d954SCole Faust _free_blobs.splice(std::begin(_free_blobs), _occupied_blobs, occupied_blob_it);
115*c217d954SCole Faust
116*c217d954SCole Faust // Check if all objects are finalized and reset active group
117*c217d954SCole Faust if(are_all_finalized())
118*c217d954SCole Faust {
119*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(!_occupied_blobs.empty());
120*c217d954SCole Faust
121*c217d954SCole Faust // Update blobs and group mappings
122*c217d954SCole Faust update_blobs_and_mappings();
123*c217d954SCole Faust
124*c217d954SCole Faust // Update finalized groups
125*c217d954SCole Faust _finalized_groups[_active_group].insert(std::begin(_active_elements), std::end(_active_elements));
126*c217d954SCole Faust
127*c217d954SCole Faust // Reset state
128*c217d954SCole Faust _active_elements.clear();
129*c217d954SCole Faust _active_group = nullptr;
130*c217d954SCole Faust _free_blobs.clear();
131*c217d954SCole Faust }
132*c217d954SCole Faust }
133*c217d954SCole Faust
are_all_finalized() const134*c217d954SCole Faust bool ISimpleLifetimeManager::are_all_finalized() const
135*c217d954SCole Faust {
136*c217d954SCole Faust return !std::any_of(std::begin(_active_elements), std::end(_active_elements), [](const std::pair<void *, Element> &e)
137*c217d954SCole Faust {
138*c217d954SCole Faust return !e.second.status;
139*c217d954SCole Faust });
140*c217d954SCole Faust }
141*c217d954SCole Faust } // namespace arm_compute
142