blob: 173ced4cac602cd2f733c79531e509fedec9d111 [file] [log] [blame]
Francis Murtaghca49a242021-09-28 15:30:31 +01001//
2// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
Jim Flynne1fdd282021-10-26 21:26:10 +01006#include <unordered_map>
7#include <iostream>
8#include "StrategyValidator.hpp"
Francis Murtaghca49a242021-09-28 15:30:31 +01009
10namespace armnn
11{
12
Jim Flynne1fdd282021-10-26 21:26:10 +010013std::vector<MemBin> StrategyValidator::Optimize(std::vector<MemBlock>& memBlocks)
Francis Murtaghca49a242021-09-28 15:30:31 +010014{
15 // Condition #1: All Memblocks have been assigned to a MemBin
16
17 // Condition #2: No Memblock is assigned to multiple MemBins
18
19 // Condition #3: No two Memblocks in a MemBin overlap in both the X and Y axis
20 // Memblocks in a MemBin can overlap on the X axis for SingleAxisPacking
21 // Memblocks in a MemBin can overlap on the Y axis or the X for MultiAxisPacking but not both
22
Jim Flynne1fdd282021-10-26 21:26:10 +010023 std::unordered_map<unsigned int, bool> validationMap;
24
25 for (auto memBlock : memBlocks)
26 {
27 validationMap[memBlock.m_Index] = false;
28 }
29
Francis Murtaghca49a242021-09-28 15:30:31 +010030 auto memBinVect = m_Strategy->Optimize(memBlocks);
31
32 // Compare each of the input memblocks against every assignedBlock in each bin
33 // if we get through all bins without finding a block return
34 // if at any stage the block is found twice return
35
Jim Flynne1fdd282021-10-26 21:26:10 +010036 for (auto memBin : memBinVect)
Francis Murtaghca49a242021-09-28 15:30:31 +010037 {
Jim Flynne1fdd282021-10-26 21:26:10 +010038 for (auto block : memBin.m_MemBlocks)
Francis Murtaghca49a242021-09-28 15:30:31 +010039 {
Jim Flynne1fdd282021-10-26 21:26:10 +010040 try
Francis Murtaghca49a242021-09-28 15:30:31 +010041 {
Jim Flynne1fdd282021-10-26 21:26:10 +010042 if (!validationMap.at(block.m_Index))
Francis Murtaghca49a242021-09-28 15:30:31 +010043 {
Jim Flynne1fdd282021-10-26 21:26:10 +010044 validationMap.at(block.m_Index) = true;
45 }
46 else
47 {
48 throw MemoryValidationException("Condition #2: Memblock is assigned to multiple MemBins");
Francis Murtaghca49a242021-09-28 15:30:31 +010049 }
50 }
Jim Flynne1fdd282021-10-26 21:26:10 +010051 catch (const std::out_of_range&)
52 {
53 throw MemoryValidationException("Unknown index ");
54 }
Francis Murtaghca49a242021-09-28 15:30:31 +010055 }
Jim Flynne1fdd282021-10-26 21:26:10 +010056 }
57
58 for (auto memBlock : memBlocks)
59 {
60 if (!validationMap.at(memBlock.m_Index))
Francis Murtaghca49a242021-09-28 15:30:31 +010061 {
Jim Flynne1fdd282021-10-26 21:26:10 +010062 throw MemoryValidationException("Condition #1: Block not found in any bin");
Francis Murtaghca49a242021-09-28 15:30:31 +010063 }
64 }
65
66 // Check for overlaps once we know blocks are all assigned and no duplicates
67 for (auto bin : memBinVect)
68 {
69 for (unsigned int i = 0; i < bin.m_MemBlocks.size(); ++i)
70 {
Finn Williamse933c382021-11-10 19:43:51 +000071 auto Block1 = bin.m_MemBlocks[i];
72 auto B1Left = Block1.m_Offset;
73 auto B1Right = Block1.m_Offset + Block1.m_MemSize;
Francis Murtaghca49a242021-09-28 15:30:31 +010074
Finn Williamse933c382021-11-10 19:43:51 +000075 auto B1Top = Block1.m_StartOfLife;
76 auto B1Bottom = Block1.m_EndOfLife;
Francis Murtaghca49a242021-09-28 15:30:31 +010077
78 // Only compare with blocks after the current one as previous have already been checked
79 for (unsigned int j = i + 1; j < bin.m_MemBlocks.size(); ++j)
80 {
Finn Williamse933c382021-11-10 19:43:51 +000081 auto Block2 = bin.m_MemBlocks[j];
82 auto B2Left = Block2.m_Offset;
83 auto B2Right = Block2.m_Offset + Block2.m_MemSize;
Francis Murtaghca49a242021-09-28 15:30:31 +010084
Finn Williamse933c382021-11-10 19:43:51 +000085 auto B2Top = Block2.m_StartOfLife;
86 auto B2Bottom = Block2.m_EndOfLife;
Francis Murtaghca49a242021-09-28 15:30:31 +010087
88 switch (m_Strategy->GetMemBlockStrategyType())
89 {
90 case (MemBlockStrategyType::SingleAxisPacking):
91 {
Finn Williamse933c382021-11-10 19:43:51 +000092 if (B1Top <= B2Bottom && B1Bottom >= B2Top)
Francis Murtaghca49a242021-09-28 15:30:31 +010093 {
Jim Flynne1fdd282021-10-26 21:26:10 +010094 throw MemoryValidationException("Condition #3: "
95 "invalid as two Memblocks overlap on the Y axis for SingleAxisPacking");
96
Francis Murtaghca49a242021-09-28 15:30:31 +010097 }
98 break;
99 }
100 case (MemBlockStrategyType::MultiAxisPacking):
101 {
Finn Williamse933c382021-11-10 19:43:51 +0000102 // If overlapping on both X and Y then invalid
103 if (B1Left <= B2Right && B1Right >= B2Left &&
104 B1Top <= B2Bottom && B1Bottom >= B2Top)
105 {
106 // Condition #3: two Memblocks overlap on both the X and Y axis
107 throw MemoryValidationException("Condition #3: "
108 "two Memblocks overlap on both the X and Y axis");
109 }
Francis Murtaghca49a242021-09-28 15:30:31 +0100110 break;
111 }
112 default:
Jim Flynne1fdd282021-10-26 21:26:10 +0100113 throw MemoryValidationException("Unknown MemBlockStrategyType");
Francis Murtaghca49a242021-09-28 15:30:31 +0100114 }
115 }
Francis Murtaghca49a242021-09-28 15:30:31 +0100116 }
117 }
118
119 // None of the conditions broken so return true
Jim Flynne1fdd282021-10-26 21:26:10 +0100120 return memBinVect;
Francis Murtaghca49a242021-09-28 15:30:31 +0100121}
122
123} // namespace armnn