|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# pylint: disable=C0114,C0116 |
| 3 | +# |
| 4 | +# Copyright 2022-2024 by Wilson Snyder. Verilator is free software; you |
| 5 | +# can redistribute it and/or modify it under the terms of either the GNU Lesser |
| 6 | +# General Public License Version 3 or the Apache License 2.0. |
| 7 | +# SPDX-License-Identifier: LGPL-3.0-only OR Apache-2.0 |
| 8 | +# |
| 9 | +# Usage Example: |
| 10 | +# |
| 11 | +# ./nodist/gen_group_scores \ |
| 12 | +# 200x15-100 2x999 16x50 2x999 100x50-150 500 4-33 8x0-999 0-7 \ |
| 13 | +# > test_files_list.txt |
| 14 | +# ./bin/verilator --debugi-V3EmitMk 6 --output-groups 10 \ |
| 15 | +# --debug-test-concatenation ./test_files_list.txt |
| 16 | + |
| 17 | +import argparse |
| 18 | +import random |
| 19 | +import re |
| 20 | +import sys |
| 21 | + |
| 22 | + |
| 23 | +def parse_sequence_params(sequence_params: str) -> tuple[int, int, int] | None: |
| 24 | + m = re.match(r"^(?:([1-9][0-9]*)x)?(0|[1-9][0-9]*)(?:-(0|[1-9][0-9]*))?$", |
| 25 | + sequence_params) |
| 26 | + |
| 27 | + repeat = 1 |
| 28 | + min_score = 1 |
| 29 | + max_score = 1 |
| 30 | + |
| 31 | + if not m: |
| 32 | + return None |
| 33 | + |
| 34 | + if m.group(1) is not None: |
| 35 | + repeat = int(m.group(1)) |
| 36 | + |
| 37 | + min_score = int(m.group(2)) |
| 38 | + max_score = int(m.group(2)) |
| 39 | + |
| 40 | + if m.group(3) is not None: |
| 41 | + max_score = int(m.group(3)) |
| 42 | + |
| 43 | + if max_score < min_score: |
| 44 | + min_score, max_score = max_score, min_score |
| 45 | + |
| 46 | + return (repeat, min_score, max_score) |
| 47 | + |
| 48 | + |
| 49 | +def gen_sequence_scores(repeat: int, min_score: int, |
| 50 | + max_score: int) -> list[int]: |
| 51 | + result: list[int] = [] |
| 52 | + for _ in range(repeat): |
| 53 | + score = min_score |
| 54 | + if min_score != max_score: |
| 55 | + score = random.randrange(min_score, max_score) |
| 56 | + result.append(score) |
| 57 | + return result |
| 58 | + |
| 59 | + |
| 60 | +def main(): |
| 61 | + parser = argparse.ArgumentParser( |
| 62 | + allow_abbrev=False, |
| 63 | + formatter_class=argparse.RawTextHelpFormatter, |
| 64 | + description="Generates input list for .cpp file concatenation test", |
| 65 | + epilog="Copyright 2024 by Wilson Snyder. Verilator is free software;\n" |
| 66 | + "you can redistribute it and/or modify it under the terms of either the GNU\n" |
| 67 | + "Lesser General Public License Version 3 or the Apache License 2.0.\n" |
| 68 | + "SPDX-License-Identifier: LGPL-3.0-only OR Apache-2.0\n") |
| 69 | + |
| 70 | + parser.add_argument( |
| 71 | + "sequence_params", |
| 72 | + type=str, |
| 73 | + nargs="+", |
| 74 | + help="Defines how to generate a sequence of entries. Syntax:\n" |
| 75 | + "\n" |
| 76 | + " [{repeat}x]{score}\n" + " [{repeat}x]{min_score}-{max_score}\n" |
| 77 | + "\n" |
| 78 | + "Where:\n" + |
| 79 | + " repeat number of entries in the list (1 by default)\n" |
| 80 | + " score use `score` for each entry\n" + |
| 81 | + " min_score/max_score use random score between min and max\n" |
| 82 | + " for each entry\n" |
| 83 | + "\n" |
| 84 | + "Examples:\n" |
| 85 | + " 120x5-80 120 entries, each with a random score between 5 and 80\n" |
| 86 | + " (inclusive) each.\n" |
| 87 | + " 1000-2000 1 entry with a random score between 1000 and 2000\n" |
| 88 | + " (inclusive).\n" |
| 89 | + " 10x24 10 entries, each with score 24.\n" |
| 90 | + " 9000 1 entry with score 9000.\n") |
| 91 | + |
| 92 | + cmdline = parser.parse_args() |
| 93 | + |
| 94 | + all_sequence_params: list[tuple[str, int, int, int]] = [] |
| 95 | + for params_str in cmdline.sequence_params: |
| 96 | + sequence_params = parse_sequence_params(params_str) |
| 97 | + if sequence_params is None: |
| 98 | + # Exit with error message |
| 99 | + parser.error(f"Invalid sequence_params value: {params_str}") |
| 100 | + |
| 101 | + all_sequence_params.append((params_str, *sequence_params)) |
| 102 | + |
| 103 | + abs_ent_idx = 0 |
| 104 | + for seq_idx, (params_str, repeat, min_score, |
| 105 | + max_score) in enumerate(all_sequence_params): |
| 106 | + for seq_ent_idx, score in enumerate( |
| 107 | + gen_sequence_scores(repeat, min_score, max_score)): |
| 108 | + print(f"{score:>12}\t" |
| 109 | + f"dummy_cpp_file_{abs_ent_idx:04}" |
| 110 | + f".seq_{seq_idx:02}_{params_str}_{seq_ent_idx}" |
| 111 | + f".score_{score}") |
| 112 | + abs_ent_idx += 1 |
| 113 | + |
| 114 | + return 0 |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + sys.exit(main()) |
0 commit comments