Imported Upstream version 1.4.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / TensorManager.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 "TensorManager.h"
18
19 #include <util/logging.h>
20
21 namespace onert
22 {
23 namespace backend
24 {
25 namespace cpu
26 {
27
28 TensorManager::TensorManager()
29     : _const_mgr{new cpu_common::DynamicMemoryManager()},
30       _nonconst_mgr{new cpu_common::MemoryManager()}
31 {
32   // DO NOTHING
33 }
34
35 void TensorManager::allocateConsts(void)
36 {
37   for (auto &pair : _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_TENSORMANAGER) << "CONSTANT TENSOR(#" << ind.value()
47                                  << "): " << static_cast<void *>(buffer)
48                                  << "size : " << tensor->total_size() << std::endl;
49     }
50   }
51 }
52
53 void TensorManager::allocateNonconsts(void)
54 {
55   _nonconst_mgr->allocate();
56
57   for (auto &pair : _tensors)
58   {
59     const auto &ind = pair.first;
60     auto tensor = pair.second;
61     if (!_as_constants[ind])
62     {
63       auto *buffer = _nonconst_mgr->getBuffer(ind);
64       tensor->setBuffer(buffer);
65
66       VERBOSE(CPU_TENSORMANAGER) << "TENSOR(#" << ind.value()
67                                  << "): " << static_cast<void *>(buffer) << std::endl;
68     }
69   }
70 }
71
72 void TensorManager::deallocateConsts(void) { _const_mgr->deallocate(); }
73
74 void TensorManager::deallocateNonconsts(void) { _nonconst_mgr->deallocate(); }
75
76 void TensorManager::buildTensor(const ir::OperandIndex &ind, const ir::OperandInfo &tensor_info,
77                                 bool as_const)
78 {
79   assert(_tensors.find(ind) == _tensors.end());
80   auto tensor = std::make_shared<operand::Tensor>(tensor_info);
81   _tensors[ind] = tensor;
82   _as_constants[ind] = as_const;
83 }
84
85 void TensorManager::claimPlan(const ir::OperandIndex &ind, uint32_t size)
86 {
87   assert(_tensors.find(ind) != _tensors.end());
88   if (!_as_constants[ind])
89     _nonconst_mgr->claimPlan(ind, size);
90 }
91
92 void TensorManager::releasePlan(const ir::OperandIndex &ind)
93 {
94   assert(_tensors.find(ind) != _tensors.end());
95   if (!_as_constants[ind])
96     _nonconst_mgr->releasePlan(ind);
97 }
98
99 std::shared_ptr<operand::Tensor> TensorManager::at(const ir::OperandIndex &ind)
100 {
101   if (_tensors.find(ind) == _tensors.end())
102     return nullptr;
103   return _tensors.at(ind);
104 }
105
106 void TensorManager::iterate(const std::function<void(const ir::OperandIndex &)> &fn)
107 {
108   for (const auto &it : _tensors)
109     fn(it.first);
110 }
111
112 } // namespace cpu
113 } // namespace backend
114 } // namespace onert