820cad38ac6b07eb75970d8ac6be03fd72a7b432
[platform/core/ml/nnfw.git] / runtime / onert / core / src / backend / cpu_common / StaticTensorManager.cc
1 /*
2  * Copyright (c) 2019 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 "backend/cpu_common/StaticTensorManager.h"
18
19 #include "backend/cpu_common/DynamicTensorManager.h"
20 #include <util/logging.h>
21
22 namespace onert
23 {
24 namespace backend
25 {
26 namespace cpu_common
27 {
28
29 StaticTensorManager::StaticTensorManager(const std::shared_ptr<TensorRegistry> &reg)
30     : _const_mgr{new DynamicMemoryManager()}, _nonconst_mgr{new MemoryManager()}, _tensors{reg}
31 {
32   // DO NOTHING
33 }
34
35 void StaticTensorManager::allocateConsts(void)
36 {
37   for (auto &pair : _tensors->native_tensors())
38   {
39     const auto &ind = pair.first;
40     auto tensor = pair.second;
41     if (_as_constants[ind])
42     {
43       auto mem_alloc = _const_mgr->allocate(ind, tensor->total_size());
44       tensor->setBuffer(mem_alloc);
45       auto buffer = mem_alloc->base();
46       VERBOSE(CPU_COMMON_StaticTensorManager) << "CONSTANT TENSOR(#" << ind.value()
47                                               << "): " << static_cast<void *>(buffer)
48                                               << "size : " << tensor->total_size() << std::endl;
49     }
50   }
51 }
52
53 void StaticTensorManager::allocateNonconsts(void)
54 {
55   _nonconst_mgr->allocate();
56
57   for (auto &pair : _tensors->native_tensors())
58   {
59     const auto &ind = pair.first;
60     auto tensor = pair.second;
61     if (!_as_constants[ind] && !tensor->is_dynamic())
62     {
63       auto *buffer = _nonconst_mgr->getBuffer(ind);
64       tensor->setBuffer(buffer);
65
66       VERBOSE(CPU_COMMON_StaticTensorManager) << "TENSOR(#" << ind.value()
67                                               << "): " << static_cast<void *>(buffer) << std::endl;
68     }
69   }
70 }
71
72 void StaticTensorManager::deallocateConsts(void) { _const_mgr->deallocate(); }
73
74 void StaticTensorManager::deallocateNonconsts(void) { _nonconst_mgr->deallocate(); }
75
76 void StaticTensorManager::buildTensor(const ir::OperandIndex &ind,
77                                       const ir::OperandInfo &tensor_info, ir::Layout backend_layout,
78                                       bool as_const)
79 {
80   assert(!_tensors->getNativeTensor(ind));
81   auto tensor = std::make_shared<Tensor>(tensor_info, backend_layout, nullptr);
82   _tensors->setNativeTensor(ind, tensor);
83   _as_constants[ind] = as_const;
84 }
85
86 void StaticTensorManager::claimPlan(const ir::OperandIndex &ind, uint32_t size)
87 {
88   assert(_tensors->getNativeTensor(ind));
89
90   // This method is called only when a tensor has proper shape
91   assert(!_tensors->getNativeTensor(ind)->is_dynamic());
92
93   if (!_as_constants[ind])
94     _nonconst_mgr->claimPlan(ind, size);
95 }
96
97 void StaticTensorManager::releasePlan(const ir::OperandIndex &ind)
98 {
99   assert(_tensors->getNativeTensor(ind));
100
101   // This method is called only when a tensor has proper shape
102   assert(!_tensors->getNativeTensor(ind)->is_dynamic());
103
104   if (!_as_constants[ind])
105     _nonconst_mgr->releasePlan(ind);
106 }
107
108 void StaticTensorManager::iterate(const std::function<void(const ir::OperandIndex &)> &fn)
109 {
110   for (const auto &it : _tensors->native_tensors())
111     fn(it.first);
112 }
113
114 } // namespace cpu_common
115 } // namespace backend
116 } // namespace onert