ONE - On-device Neural Engine
Loading...
Searching...
No Matches
BuddyMemoryManager.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 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#if 0
18
19#include "BuddyMemoryManager.h"
20
21namespace luci_interpreter
22{
23
24BuddyMemoryManager::BuddyMemoryManager(uint8_t *memory_start, int32_t memSize)
25{
26 int32_t p = lowerLog2(memSize);
27
28 // We assume that the requested size of memory does not exceed 4 GB
29 assert(p < 32);
30 memSize = 1 << p;
31
32 _start_block = reinterpret_cast<Block *>(memory_start);
33 _start_block->size = memSize - sizeof(Block);
34 _start_block->is_free = true;
35 _start_block->self = _start_block;
36 _num_blocks = 0;
37 _size = _start_block->size;
38
39 for (auto &_free_block : _free_blocks)
40 _free_block = nullptr;
41
42 addToBlocks(_start_block, p);
43}
44
46{
47 const size_t element_size = getDataTypeSize(tensor.element_type());
48 const int32_t num_elements = tensor.shape().num_elements();
49 auto size = num_elements * element_size;
50 auto footprint = size + sizeof(Block);
51 auto l = (footprint & (footprint - 1)) == 0
52 ? lowerLog2(footprint)
53 : lowerLog2(footprint) + 1; // check footprint is pow_of_2
54
55 while (l < 32 && !_free_blocks[l])
56 l++;
57
58 assert(l < 32);
59
60 Block *tmp;
61 tmp = _free_blocks[l];
62 removeFromBlocks(tmp, l);
63
64 while ((tmp->size + sizeof(Block)) / 2 >= size + sizeof(Block))
65 {
66 divideBlock(tmp, l);
67 l--;
68 }
69
70 tmp->is_free = false;
71 tmp->self = tmp;
72 _num_blocks++;
73
74 auto *data = (uint8_t *)(tmp + 1);
75 tensor.set_data_buffer(data);
76}
77
79{
80 auto data = tensor.data<void>();
81 auto *tmp = (Block *)((uint8_t *)data - sizeof(Block));
82
83 assert(tmp->self == tmp);
84
85 tmp->is_free = true;
86 addToBlocks(tmp, lowerLog2(tmp->size + sizeof(Block)));
87
88 while (tmp)
89 if (tmp->size == _size)
90 break;
91 else
92 tmp = mergeBlock(tmp);
93
94 _num_blocks--;
95 tensor.set_data_buffer(nullptr);
96}
97
98} // namespace luci_interpreter
99
100#endif
void allocate_memory(luci_interpreter::Tensor &tensor) final
void release_memory(luci_interpreter::Tensor &tensor) final
BuddyMemoryManager(uint8_t *memory_start, int32_t memSize)
const T * data(const std::vector< T, Alloc > &v)
size_t getDataTypeSize(DataType data_type)
Definition DataType.h:33
uint32_t num_elements(const Shape &shape)
The number of elements of a feature map of a given shape.
Definition Shape.h:59
int32_t size[5]
Definition Slice.cpp:35