blob: 1cbfce3f10acaf550f0a8cf94e952c5cca5e3c70 [file] [log] [blame]
Tim Hall79d07d22020-04-27 18:20:16 +01001# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
2#
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the License); you may
6# not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an AS IS BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Tim Hall79d07d22020-04-27 18:20:16 +010016# Description:
17# Allocate tensor addresses using a greedy algorithm.
Tim Hall79d07d22020-04-27 18:20:16 +010018from . import numeric_util
19
20
21class GreedyAllocator:
22 def __init__(self, nng, arch, live_ranges, mem_area):
23 self.nng = nng
24 self.arch = arch
25 self.mem_area = mem_area
26
27 self.live_ranges = live_ranges
28 self.memory_required = 0
29
30 self.current_allocs = []
31
32 def alloc(self, new_lr):
33 size = new_lr.size
34 current_top = 0
35 if self.current_allocs:
36 current_top = max(start_addr + lr.size for start_addr, lr in self.current_allocs)
37 best_offset = numeric_util.round_up(current_top, new_lr.get_alignment())
38 best_offset_fit = (1 << 64) - 1
39
40 current_offset = 0
41 for start_addr, lr in self.current_allocs:
42 aligned_current_offset = numeric_util.round_up(current_offset, new_lr.get_alignment())
43 if aligned_current_offset + size <= start_addr and start_addr - current_offset < best_offset_fit:
44 best_offset = current_offset
45 best_offset_fit = start_addr - current_offset
46
47 current_offset = start_addr + lr.size
48
Charles Xu5b3dcd72020-05-28 07:20:52 +020049 best_offset = new_lr.set_address(best_offset)
Tim Hall79d07d22020-04-27 18:20:16 +010050 self.memory_required = max(self.memory_required, best_offset + size)
Tim Hall79d07d22020-04-27 18:20:16 +010051 self.current_allocs.append((best_offset, new_lr))
52 self.current_allocs = list(sorted(self.current_allocs))
53
54 def dealloc(self, lr_to_dealloc):
55 self.current_allocs = [(start_addr, lr) for start_addr, lr in self.current_allocs if lr != lr_to_dealloc]
56
57 def allocate_live_ranges(self, verbose_allocation):
58 lrs = set()
59 for lr in self.live_ranges.ranges.values():
60 lrs.add((lr.start_time, lr.end_time, lr))
61
62 lrs = sorted(lrs)
63
64 for curr_time, _, new_lr in lrs:
65 for _, lr in list(self.current_allocs):
66 if lr.end_time < curr_time:
67 self.dealloc(lr)
68
69 self.alloc(new_lr)
70
71 assert self.verify_allocation()
72 return self.memory_required
73
74 def verify_allocation(self):
75 lrs = list(self.live_ranges.ranges.values())
76 for n in lrs:
77 for m in lrs:
78 if n != m and n.overlaps_ranges(m):
79 overlap, tens_n, tens_m = n.overlaps_address(m)
Louis Verhaard0b8268a2020-08-05 16:11:29 +020080 if overlap and not (tens_n.equivalent(tens_m) and tens_n.address == tens_m.address):
Tim Hall79d07d22020-04-27 18:20:16 +010081 print("Solution failed, overlapping buffer!")
82 print(tens_n.address, tens_n.address + n.size, n.name)
83 print(tens_m.address, tens_m.address + m.size, m.name)
84 print()
85 return False
86
87 return True
88
89
90def allocate_live_ranges(nng, arch, live_ranges, mem_area, verbose_allocation=False):
91 g = GreedyAllocator(nng, arch, live_ranges, mem_area)
92 return g.allocate_live_ranges(verbose_allocation)