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
18
19namespace luci_interpreter
20{
21
23{
24 int32_t p = lowerLog2(memSize);
25
26 // We assume that the requested size of memory does not exceed 4 GB
27 assert(p < 32);
28 memSize = 1 << p;
29
30 _start_block = reinterpret_cast<Block *>(memory_start);
31 _start_block->size = memSize - sizeof(Block);
32 _start_block->is_free = true;
33 _start_block->self = _start_block;
34 _num_blocks = 0;
35 _size = _start_block->size;
36
37 for (auto &_free_block : _free_blocks)
38 _free_block = nullptr;
39
40 addToBlocks(_start_block, p);
41}
42
44{
45 const size_t element_size = getDataTypeSize(tensor.element_type());
46 const int64_t num_elements = tensor.shape().large_num_elements();
47
48 // Check for integer overflow in size calculation
49 if (num_elements < 0 || static_cast<uint64_t>(num_elements) > SIZE_MAX / element_size)
50 {
51 throw std::runtime_error("Integer overflow in size calculation");
52 }
53
54 const int64_t total_size = num_elements * element_size;
55 auto size = static_cast<size_t>(total_size);
56 auto footprint = size + sizeof(Block);
57 auto l = (footprint & (footprint - 1)) == 0
58 ? lowerLog2(footprint)
59 : lowerLog2(footprint) + 1; // check footprint is pow_of_2
60
61 while (l < 32 && !_free_blocks[l])
62 l++;
63
64 if (l >= 32)
65 {
66 throw std::runtime_error{"Memory limit exceeded"};
67 }
68
69 Block *tmp;
70 tmp = _free_blocks[l];
71 removeFromBlocks(tmp, l);
72
73 while ((tmp->size + sizeof(Block)) / 2 >= size + sizeof(Block))
74 {
75 divideBlock(tmp, l);
76 l--;
77 }
78
79 tmp->is_free = false;
80 tmp->self = tmp;
81 _num_blocks++;
82
83 auto *data = (uint8_t *)(tmp + 1);
84 tensor.set_data_buffer(data);
85}
86
88{
89 auto data = tensor.data<void>();
90 auto *tmp = (Block *)((uint8_t *)data - sizeof(Block));
91
92 assert(tmp->self == tmp);
93
94 tmp->is_free = true;
95 addToBlocks(tmp, lowerLog2(tmp->size + sizeof(Block)));
96
97 while (tmp)
98 if (tmp->size == _size)
99 break;
100 else
101 tmp = mergeBlock(tmp);
102
103 _num_blocks--;
104 tensor.set_data_buffer(nullptr);
105}
106
107} // namespace luci_interpreter
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
T must_cast(loco::Node *node)
int32_t size[5]
Definition Slice.cpp:35
Configuration p