Louis Verhaard | 9bfe0f8 | 2020-12-03 12:26:25 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2020 Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| 7 | * not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include <cstdint> |
| 20 | #include <iostream> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "search_allocator.h" |
| 24 | |
| 25 | using namespace std; |
| 26 | |
| 27 | /** |
| 28 | * Reads live ranges from the input, and then performs allocation. |
| 29 | * The input has format: |
| 30 | |
| 31 | <number of live ranges> |
| 32 | <start_time> <end_time> <size> |
| 33 | ... |
| 34 | |
| 35 | * e.g.: |
| 36 | 4 |
| 37 | 0 20 4096 |
| 38 | 2 8 16000 |
| 39 | 4 10 800 |
| 40 | 6 20 1024 |
| 41 | */ |
| 42 | int main() { |
| 43 | int lr_count; |
| 44 | cin >> lr_count; |
| 45 | cin.ignore(); |
| 46 | vector<uint32_t> input; |
| 47 | vector<uint32_t> output; |
| 48 | for (int i = 0; i < lr_count; ++i) { |
| 49 | LiveRange lr; |
| 50 | cin >> lr.start_time >> lr.end_time >> lr.size; |
| 51 | lr.id = i; |
| 52 | cin.ignore(); |
| 53 | input.push_back(lr.start_time); |
| 54 | input.push_back(lr.end_time); |
| 55 | input.push_back(lr.size); |
| 56 | } |
| 57 | vector<LiveRange> lrs; |
| 58 | for (size_t i = 0; i < input.size(); i += 3) { |
| 59 | LiveRange lr; |
| 60 | lr.start_time = input[i]; |
| 61 | lr.end_time = input[i+1]; |
| 62 | lr.size = input[i+2]; |
| 63 | lrs.push_back(lr); |
| 64 | } |
| 65 | SearchAllocator allocator(lrs, 0); |
| 66 | uint32_t result = allocator.allocate(output); |
| 67 | printf("Output:\n"); |
| 68 | for (int i = 0; i < lr_count; ++i) { |
| 69 | printf("%4d: %6d %4d-%4d size %6d\n", i, output[i], input[3*i], input[3*i+1], input[3*i+2]); |
| 70 | } |
| 71 | uint32_t min_size = allocator.get_min_required_size(); |
| 72 | double overhead = 100.0 * (result - min_size)/(double)min_size; |
| 73 | printf("Total size: %d (%1.1f K), minimum required size: %d, overhead: %1.2f%%\n", |
| 74 | result, result/1024.0, min_size, overhead); |
| 75 | printf("Search used %ld iterations\n", (long)allocator.get_nr_iterations()); |
| 76 | return 0; |
| 77 | } |