|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Concatenate multiple benchmark result tables into a single comprehensive table. |
| 4 | +
|
| 5 | +This script finds and combines related benchmark results from different datasets |
| 6 | +(mesh, canada, uniform_01) into a single LaTeX table for easier comparison. |
| 7 | +""" |
| 8 | +import os |
| 9 | +import re |
| 10 | +import argparse |
| 11 | +import pandas as pd |
| 12 | + |
| 13 | + |
| 14 | +def parse_tex_table(filepath): |
| 15 | + """Parse a LaTeX table file into a pandas DataFrame.""" |
| 16 | + with open(filepath, 'r') as file: |
| 17 | + lines = file.readlines() |
| 18 | + data_start = False |
| 19 | + parsed = [] |
| 20 | + for line in lines: |
| 21 | + if "\\midrule" in line: |
| 22 | + data_start = True |
| 23 | + continue |
| 24 | + if "\\bottomrule" in line: |
| 25 | + break |
| 26 | + if data_start and '&' in line: |
| 27 | + row = [x.strip().strip('\\') for x in line.split('&')] |
| 28 | + if len(row) == 4: |
| 29 | + parsed.append({ |
| 30 | + 'algorithm': row[0], |
| 31 | + 'ns/f': row[1], |
| 32 | + 'ins/f': row[2], |
| 33 | + 'ins/c': row[3] |
| 34 | + }) |
| 35 | + return pd.DataFrame(parsed) |
| 36 | + |
| 37 | + |
| 38 | +def clean_cpu_name(cpu_name): |
| 39 | + """Clean CPU name for better display in tables.""" |
| 40 | + cpu_cleaned = cpu_name.replace("Ryzen9900x", "Ryzen 9900X") |
| 41 | + cpu_cleaned = cpu_cleaned.replace("_Platinum", "") |
| 42 | + cpu_cleaned = re.sub(r"_\d+-Core_Processor", "", cpu_cleaned) |
| 43 | + cpu_cleaned = re.sub(r"_CPU__\d+\.\d+GHz", "", cpu_cleaned) |
| 44 | + cpu_cleaned = re.sub(r"\(R\)", "", cpu_cleaned) |
| 45 | + return cpu_cleaned.replace("_", " ").replace(" ", " ").strip() |
| 46 | + |
| 47 | + |
| 48 | +def format_latex_table(df, cpu_name, compiler, float_bits, microarch=None, |
| 49 | + exclude_algos=None): |
| 50 | + """Format the combined data as a LaTeX table.""" |
| 51 | + if exclude_algos is None: |
| 52 | + exclude_algos = set() |
| 53 | + |
| 54 | + cpu_cleaned = clean_cpu_name(cpu_name) |
| 55 | + caption = f"{cpu_cleaned} results ({compiler}, {float_bits}-bit floats" |
| 56 | + if microarch: |
| 57 | + caption += f", {microarch}" |
| 58 | + caption += ")" |
| 59 | + label = f"tab:{re.sub(r'[^a-zA-Z0-9]+', '', cpu_name.lower())}results" |
| 60 | + header = ( |
| 61 | + "\\begin{table}\n" |
| 62 | + " \\centering\n" |
| 63 | + f" \\caption{{{caption}}}%\n" |
| 64 | + f" \\label{{{label}}}\n" |
| 65 | + " \\begin{tabular}{lccccccccc}\n" |
| 66 | + " \\toprule\n" |
| 67 | + " \\multirow{1}{*}{Name} & \\multicolumn{3}{c|}{mesh} & " |
| 68 | + "\\multicolumn{3}{c|}{canada} & \\multicolumn{3}{c}{unit} \\\\\n" |
| 69 | + " & {ns/f} & {ins/f} & {ins/c} & " |
| 70 | + "{ns/f} & {ins/f} & {ins/c} & {ns/f} & {ins/f} & {ins/c} \\\\ " |
| 71 | + "\\midrule\n" |
| 72 | + ) |
| 73 | + body = "" |
| 74 | + for _, row in df.iterrows(): |
| 75 | + if row['algorithm'] in exclude_algos: |
| 76 | + continue |
| 77 | + line = ( |
| 78 | + f" {row['algorithm']} & {row['ns/f_mesh']} & " |
| 79 | + f"{row['ins/f_mesh']} & {row['ins/c_mesh']} & " |
| 80 | + f"{row['ns/f_canada']} & {row['ins/f_canada']} & " |
| 81 | + f"{row['ins/c_canada']} & " |
| 82 | + f"{row['ns/f_unit']} & {row['ins/f_unit']} & " |
| 83 | + f"{row['ins/c_unit']} \\\\\n" |
| 84 | + ) |
| 85 | + body += line |
| 86 | + footer = ( |
| 87 | + " \\bottomrule\n" |
| 88 | + " \\end{tabular}\\restartrowcolors\n" |
| 89 | + "\\end{table}\n" |
| 90 | + ) |
| 91 | + return header + body + footer |
| 92 | + |
| 93 | + |
| 94 | +def find_combinations(root, pattern=None): |
| 95 | + """Find all combinations of benchmark result files that can be combined.""" |
| 96 | + if pattern is None: |
| 97 | + pattern = re.compile( |
| 98 | + r"(.*?)_(g\+\+|clang\+\+)_(mesh|canada|uniform_01)_(none|s)" |
| 99 | + r"(?:_(x86-64|x86-64-v2|x86-64-v3|x86-64-v4|native))?\.tex" |
| 100 | + ) |
| 101 | + # group(1)=cpu, 2=compiler, 3=dataset, 4=variant, 5=microarch (optional) |
| 102 | + |
| 103 | + combos = [] |
| 104 | + for dirpath, _, filenames in os.walk(root): |
| 105 | + tex_files = [f for f in filenames if f.endswith('.tex')] |
| 106 | + table = {} |
| 107 | + for f in tex_files: |
| 108 | + m = pattern.match(f) |
| 109 | + if m: |
| 110 | + cpu, compiler, dataset, variant, microarch = m.groups() |
| 111 | + key = (dirpath, cpu, compiler, variant, microarch) |
| 112 | + if key not in table: |
| 113 | + table[key] = {} |
| 114 | + table[key][dataset] = os.path.join(dirpath, f) |
| 115 | + for (dirpath, cpu, compiler, variant, microarch), files in table.items(): |
| 116 | + if {"mesh", "canada", "uniform_01"}.issubset(files.keys()): |
| 117 | + combos.append((dirpath, cpu, compiler, variant, microarch, files)) |
| 118 | + return combos |
| 119 | + |
| 120 | + |
| 121 | +def main(): |
| 122 | + parser = argparse.ArgumentParser( |
| 123 | + description="Concatenate benchmark tables into comprehensive tables") |
| 124 | + parser.add_argument( |
| 125 | + "--input-dir", "-i", default="./outputs", |
| 126 | + help="Directory containing benchmark .tex files") |
| 127 | + parser.add_argument( |
| 128 | + "--output-dir", "-o", |
| 129 | + help="Output directory for combined tables (defaults to input directory)") |
| 130 | + parser.add_argument( |
| 131 | + "--exclude", "-e", nargs="+", |
| 132 | + default=["netlib", "teju\\_jagua", "yy\\_double", "snprintf", "abseil"], |
| 133 | + help="Algorithms to exclude from the output tables") |
| 134 | + args = parser.parse_args() |
| 135 | + |
| 136 | + input_dir = args.input_dir |
| 137 | + output_dir = args.output_dir if args.output_dir else input_dir |
| 138 | + exclude_algos = set(args.exclude) |
| 139 | + |
| 140 | + # Create output directory if it doesn't exist |
| 141 | + if not os.path.exists(output_dir): |
| 142 | + os.makedirs(output_dir) |
| 143 | + |
| 144 | + combos = find_combinations(input_dir) |
| 145 | + if not combos: |
| 146 | + print(f"No matching benchmark files found in {input_dir}") |
| 147 | + return |
| 148 | + |
| 149 | + print(f"Found {len(combos)} combinations to process") |
| 150 | + |
| 151 | + for dirpath, cpu, compiler, variant, microarch, paths in combos: |
| 152 | + df_mesh = parse_tex_table(paths['mesh']) |
| 153 | + df_canada = parse_tex_table(paths['canada']) |
| 154 | + df_unit = parse_tex_table(paths['uniform_01']) |
| 155 | + df_merged = df_mesh.merge( |
| 156 | + df_canada, on='algorithm', suffixes=('_mesh', '_canada')) |
| 157 | + df_merged = df_merged.merge(df_unit, on='algorithm') |
| 158 | + df_merged.rename(columns={ |
| 159 | + 'ns/f': 'ns/f_unit', |
| 160 | + 'ins/f': 'ins/f_unit', |
| 161 | + 'ins/c': 'ins/c_unit' |
| 162 | + }, inplace=True) |
| 163 | + |
| 164 | + float_bits = "32" if variant == "s" else "64" |
| 165 | + tex_code = format_latex_table( |
| 166 | + df_merged, cpu, compiler, float_bits, microarch, exclude_algos) |
| 167 | + |
| 168 | + suffix = f"_{microarch}" if microarch else "" |
| 169 | + out_path = os.path.join( |
| 170 | + output_dir, f"{cpu}_{compiler}_all_{variant}{suffix}.tex") |
| 171 | + with open(out_path, "w") as f: |
| 172 | + f.write(tex_code) |
| 173 | + print(f"[OK] {out_path}") |
| 174 | + |
| 175 | + |
| 176 | +if __name__ == "__main__": |
| 177 | + main() |
0 commit comments