2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "TensorManager.h"
19 #include <util/logging.h>
28 TensorManager::TensorManager()
29 : _const_mgr{new cpu_common::DynamicMemoryManager()},
30 _nonconst_mgr{new cpu_common::MemoryManager()}
35 void TensorManager::allocateConsts(void)
37 for (auto &pair : _tensors)
39 const auto &ind = pair.first;
40 auto tensor = pair.second;
41 if (_as_constants[ind])
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;
53 void TensorManager::allocateNonconsts(void)
55 _nonconst_mgr->allocate();
57 for (auto &pair : _tensors)
59 const auto &ind = pair.first;
60 auto tensor = pair.second;
61 if (!_as_constants[ind])
63 auto *buffer = _nonconst_mgr->getBuffer(ind);
64 tensor->setBuffer(buffer);
66 VERBOSE(CPU_TENSORMANAGER) << "TENSOR(#" << ind.value()
67 << "): " << static_cast<void *>(buffer) << std::endl;
72 void TensorManager::deallocateConsts(void) { _const_mgr->deallocate(); }
74 void TensorManager::deallocateNonconsts(void) { _nonconst_mgr->deallocate(); }
76 void TensorManager::buildTensor(const ir::OperandIndex &ind, const ir::OperandInfo &tensor_info,
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;
85 void TensorManager::claimPlan(const ir::OperandIndex &ind, uint32_t size)
87 assert(_tensors.find(ind) != _tensors.end());
88 if (!_as_constants[ind])
89 _nonconst_mgr->claimPlan(ind, size);
92 void TensorManager::releasePlan(const ir::OperandIndex &ind)
94 assert(_tensors.find(ind) != _tensors.end());
95 if (!_as_constants[ind])
96 _nonconst_mgr->releasePlan(ind);
99 std::shared_ptr<operand::Tensor> TensorManager::at(const ir::OperandIndex &ind)
101 if (_tensors.find(ind) == _tensors.end())
103 return _tensors.at(ind);
106 void TensorManager::iterate(const std::function<void(const ir::OperandIndex &)> &fn)
108 for (const auto &it : _tensors)
113 } // namespace backend