ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MemoryManager.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "MemoryManager.h"
18
20
21#include <util/ConfigSource.h>
22
23#include <cassert>
24
26{
27
29 : _optim_vars_count{optim_vars_count}
30{
31 // DO NOTHING
32}
33
35{
36 _mem_alloc = std::make_shared<basic::Allocator>(_mem_planner->capacity());
37 assert(_mem_alloc->base());
38
39 const auto vars_capacity = _mem_planner->capacity() * _optim_vars_count;
40 _var_mem_alloc = std::make_shared<basic::Allocator>(vars_capacity);
41}
42
44 uint32_t pos_var) const
45{
46 assert(_mem_planner->memory_plans().find(ind) != _mem_planner->memory_plans().end());
47 const auto var_offset = pos_var * _mem_planner->capacity();
48 const auto &mem_blk = _mem_planner->memory_plans().at(ind);
49 return _var_mem_alloc->base() + var_offset + mem_blk.offset;
50}
51
52DisposableMemoryManager::DisposableMemoryManager() : _mem_planner{createMemoryPlanner()}
53{
54 // DO NOTHING
55}
56
57basic::IMemoryPlanner<DisposableTensorIndex> *DisposableMemoryManager::createMemoryPlanner()
58{
59 auto planner_id = util::getConfigString(util::config::CPU_MEMORY_PLANNER);
60 return MemoryPlannerFactory<DisposableTensorIndex>::get().create(planner_id);
61}
62
64DisposableMemoryManager::createMemoryPlanner(const std::string planner_id)
65{
66 return MemoryPlannerFactory<DisposableTensorIndex>::get().create(planner_id);
67}
68
70{
71 _mem_planner->claim(ind, size);
72}
73
75{
76 _mem_planner->release(ind);
77}
78
80{
81 _mem_alloc = std::make_shared<basic::Allocator>(_mem_planner->capacity());
82 assert(_mem_alloc->base());
83}
84
86{
87 assert(_mem_planner->memory_plans().find(ind) != _mem_planner->memory_plans().end());
88 const auto &mem_blk = _mem_planner->memory_plans().at(ind);
89 return _mem_alloc->base() + mem_blk.offset;
90}
91
92LayerScopeMemoryManager::LayerScopeMemoryManager() : _mem_planner{createMemoryPlanner()}
93{
94 // DO NOTHING
95}
96
97basic::IMemoryPlanner<LayerScopeTensorIndex> *LayerScopeMemoryManager::createMemoryPlanner()
98{
99 auto planner_id = util::getConfigString(util::config::CPU_MEMORY_PLANNER);
100 return MemoryPlannerFactory<LayerScopeTensorIndex>::get().create(planner_id);
101}
102
104{
105 _mem_alloc = std::make_shared<basic::Allocator>(_mem_planner->capacity());
106 assert(_mem_alloc->base());
107}
108
110{
111 assert(_mem_planner->memory_plans().find(ind) != _mem_planner->memory_plans().end());
112 const auto &mem_blk = _mem_planner->memory_plans().at(ind);
113 return _mem_alloc->base() + mem_blk.offset;
114}
115
116void LayerScopeMemoryManager::deallocate(void) { _mem_alloc->release(); }
117
119{
120 _mem_planner->claim(ind, size);
121}
122
124{
125 _mem_planner->release(ind);
126}
127
128} // namespace onert::backend::train
std::shared_ptr< Allocator > _mem_alloc
std::shared_ptr< IMemoryPlanner< ir::OperandIndex > > _mem_planner
void releasePlan(const DisposableTensorIndex &ind)
uint8_t * getBuffer(const DisposableTensorIndex &ind) const
void claimPlan(const DisposableTensorIndex &ind, uint32_t size)
Class that is index of DisposableTensor.
void claimPlan(const LayerScopeTensorIndex &ind, uint32_t size)
uint8_t * getBuffer(const LayerScopeTensorIndex &ind) const
void releasePlan(const LayerScopeTensorIndex &ind)
static MemoryPlannerFactory< Index > & get()
TrainableMemoryManager(uint32_t optimizer_vars_count)
uint8_t * getOptVarBuffer(const ir::OperandIndex &ind, uint32_t pos_var) const
std::string getConfigString(const std::string &key)
int32_t size[5]
Definition Slice.cpp:35