ONE - On-device Neural Engine
Loading...
Searching...
No Matches
luci_interpreter::BuddyMemoryManager Class Reference

#include <BuddyMemoryManager.h>

Collaboration diagram for luci_interpreter::BuddyMemoryManager:

Public Member Functions

 BuddyMemoryManager (uint8_t *memory_start, int32_t memSize)
 
void allocate_memory (luci_interpreter::Tensor &tensor) final
 
void release_memory (luci_interpreter::Tensor &tensor) final
 
- Public Member Functions inherited from luci_interpreter::IMemoryManager
virtual ~IMemoryManager ()=default
 

Detailed Description

Definition at line 24 of file BuddyMemoryManager.h.

Constructor & Destructor Documentation

◆ BuddyMemoryManager()

luci_interpreter::BuddyMemoryManager::BuddyMemoryManager ( uint8_t memory_start,
int32_t  memSize 
)

Definition at line 22 of file BuddyMemoryManager.cpp.

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)
39
40 addToBlocks(_start_block, p);
41}
T must_cast(loco::Node *node)
Configuration p

References luci::must_cast(), and p.

Member Function Documentation

◆ allocate_memory()

void luci_interpreter::BuddyMemoryManager::allocate_memory ( luci_interpreter::Tensor tensor)
finalvirtual

Implements luci_interpreter::IMemoryManager.

Definition at line 43 of file BuddyMemoryManager.cpp.

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}
const T * data(const std::vector< T, Alloc > &v)
size_t getDataTypeSize(DataType data_type)
Definition DataType.h:33
int32_t size[5]
Definition Slice.cpp:35

References flatbuffers::data(), luci_interpreter::getDataTypeSize(), luci::must_cast(), and size.

◆ release_memory()

void luci_interpreter::BuddyMemoryManager::release_memory ( luci_interpreter::Tensor tensor)
finalvirtual

Implements luci_interpreter::IMemoryManager.

Definition at line 87 of file BuddyMemoryManager.cpp.

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}

References flatbuffers::data(), and luci::must_cast().


The documentation for this class was generated from the following files: