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)
38 _free_block = nullptr;
39
40 addToBlocks(_start_block, p);
41}

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 int32_t num_elements = tensor.shape().num_elements();
47 auto size = num_elements * element_size;
48 auto footprint = size + sizeof(Block);
49 auto l = (footprint & (footprint - 1)) == 0
50 ? lowerLog2(footprint)
51 : lowerLog2(footprint) + 1; // check footprint is pow_of_2
52
53 while (l < 32 && !_free_blocks[l])
54 l++;
55
56 if (l >= 32)
57 {
58 throw std::runtime_error{"Memory limit exceeded"};
59 }
60
61 Block *tmp;
62 tmp = _free_blocks[l];
63 removeFromBlocks(tmp, l);
64
65 while ((tmp->size + sizeof(Block)) / 2 >= size + sizeof(Block))
66 {
67 divideBlock(tmp, l);
68 l--;
69 }
70
71 tmp->is_free = false;
72 tmp->self = tmp;
73 _num_blocks++;
74
75 auto *data = (uint8_t *)(tmp + 1);
76 tensor.set_data_buffer(data);
77}
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

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

◆ release_memory()

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

Implements luci_interpreter::IMemoryManager.

Definition at line 79 of file BuddyMemoryManager.cpp.

80{
81 auto data = tensor.data<void>();
82 auto *tmp = (Block *)((uint8_t *)data - sizeof(Block));
83
84 assert(tmp->self == tmp);
85
86 tmp->is_free = true;
87 addToBlocks(tmp, lowerLog2(tmp->size + sizeof(Block)));
88
89 while (tmp)
90 if (tmp->size == _size)
91 break;
92 else
93 tmp = mergeBlock(tmp);
94
95 _num_blocks--;
96 tensor.set_data_buffer(nullptr);
97}

References flatbuffers::data().


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