xref: /aosp_15_r20/external/armnn/include/armnn/backends/IMemoryOptimizerStrategy.hpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include <armnn/Types.hpp>
8 
9 namespace armnn
10 {
11 
12 // A MemBlock represents a memory usage requirement in time and space and can be seen as essentially a rectangle
13 struct MemBlock
14 {
MemBlockarmnn::MemBlock15     MemBlock(const unsigned int startOfLife,
16              const unsigned int endOfLife,
17              const size_t memSize,
18              size_t offset,
19              const unsigned int index)
20     : m_StartOfLife(startOfLife), m_EndOfLife(endOfLife), m_MemSize(memSize), m_Offset(offset), m_Index(index) {}
21 
22     const unsigned int m_StartOfLife; // Y start inclusive
23     const unsigned int m_EndOfLife; // Y end inclusive
24 
25     const size_t m_MemSize; // Offset + Memsize = X end
26     size_t m_Offset; // X start
27 
28     const unsigned int m_Index; // Index to keep order
29 };
30 
31 // A MemBin represents a single contiguous area of memory that can store 1-n number of MemBlocks
32 struct MemBin
33 {
34     std::vector<MemBlock> m_MemBlocks;
35     size_t m_MemSize;
36 };
37 
38 // IMemoryOptimizerStrategy will set m_Offset of the MemBlocks,
39 // sort them into 1-n bins, then pair each bin of MemBlocks with an int specifying it's total size
40 // A IMemoryOptimizerStrategy must ensure that
41 // 1: All MemBlocks have been assigned to a MemBin
42 // 2: No MemBlock is assigned to multiple MemBins
43 // 3: No two Memblocks in a MemBin overlap in both the X and Y axis
44 //    (a strategy cannot change the y axis or length of a MemBlock)
45 class IMemoryOptimizerStrategy
46 {
47 public:
~IMemoryOptimizerStrategy()48     virtual ~IMemoryOptimizerStrategy() {}
49 
50     virtual std::string GetName() const = 0;
51 
52     virtual MemBlockStrategyType GetMemBlockStrategyType() const = 0;
53 
54     virtual std::vector<MemBin> Optimize(std::vector<MemBlock>& memBlocks) = 0;
55 };
56 
57 } // namespace armnn