|
| 1 | +# Copyright (C) 2024 Intel Corporation |
| 2 | +# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. |
| 3 | +# See LICENSE.TXT |
| 4 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | + |
| 6 | +import random |
| 7 | +from utils.utils import git_clone |
| 8 | +from .base import Benchmark, Suite |
| 9 | +from .result import Result |
| 10 | +from utils.utils import run, create_build_path |
| 11 | +from .options import options |
| 12 | +from .oneapi import get_oneapi |
| 13 | +import os |
| 14 | +import csv |
| 15 | +import io |
| 16 | + |
| 17 | +def isUMFAvailable(): |
| 18 | + return options.umf is not None |
| 19 | + |
| 20 | +class UMFSuite(Suite): |
| 21 | + def __init__(self, directory): |
| 22 | + self.directory = directory |
| 23 | + if not isUMFAvailable(): |
| 24 | + print("UMF not provided. Related benchmarks will not run") |
| 25 | + |
| 26 | + def setup(self): |
| 27 | + if not isUMFAvailable(): |
| 28 | + return [] |
| 29 | + self.built = True |
| 30 | + |
| 31 | + def benchmarks(self) -> list[Benchmark]: |
| 32 | + if not isUMFAvailable(): |
| 33 | + return |
| 34 | + |
| 35 | + benches = [ |
| 36 | + GBench(self), |
| 37 | + ] |
| 38 | + |
| 39 | + return benches |
| 40 | + |
| 41 | +class ComputeUMFBenchmark(Benchmark): |
| 42 | + def __init__(self, bench, name): |
| 43 | + self.bench = bench |
| 44 | + self.bench_name = name |
| 45 | + self.oneapi = get_oneapi() |
| 46 | + |
| 47 | + self.col_name = None |
| 48 | + self.col_iterations = None |
| 49 | + self.col_real_time = None |
| 50 | + self.col_cpu_time = None |
| 51 | + self.col_time_unit = None |
| 52 | + |
| 53 | + self.col_statistics_time = None |
| 54 | + |
| 55 | + super().__init__(bench.directory) |
| 56 | + |
| 57 | + def bin_args(self) -> list[str]: |
| 58 | + return [] |
| 59 | + |
| 60 | + def extra_env_vars(self) -> dict: |
| 61 | + return {} |
| 62 | + |
| 63 | + def setup(self): |
| 64 | + if not isUMFAvailable(): |
| 65 | + print("UMF prefix path not provided") |
| 66 | + return |
| 67 | + |
| 68 | + self.benchmark_bin = os.path.join(options.umf, 'benchmark', self.bench_name) |
| 69 | + |
| 70 | + def run(self, env_vars) -> list[Result]: |
| 71 | + command = [ |
| 72 | + f"{self.benchmark_bin}", |
| 73 | + ] |
| 74 | + |
| 75 | + command += self.bin_args() |
| 76 | + env_vars.update(self.extra_env_vars()) |
| 77 | + |
| 78 | + result = self.run_bench(command, env_vars, add_sycl=False, ld_library=[self.oneapi.tbb_lib()]) |
| 79 | + parsed = self.parse_output(result) |
| 80 | + results = [] |
| 81 | + for r in parsed: |
| 82 | + (config, pool, mean) = r |
| 83 | + label = f"{config} {pool}" |
| 84 | + results.append(Result(label=label, value=mean, command=command, env=env_vars, stdout=result, unit="ns", explicit_group=config)) |
| 85 | + return results |
| 86 | + |
| 87 | + # Implementation with self.col_* indices could lead to the division by None |
| 88 | + def get_mean(self, datarow): |
| 89 | + raise NotImplementedError() |
| 90 | + |
| 91 | + def teardown(self): |
| 92 | + return |
| 93 | + |
| 94 | +class GBench(ComputeUMFBenchmark): |
| 95 | + def __init__(self, bench): |
| 96 | + super().__init__(bench, "umf-benchmark") |
| 97 | + |
| 98 | + self.col_name = 0 |
| 99 | + self.col_iterations = 1 |
| 100 | + self.col_real_time = 2 |
| 101 | + self.col_cpu_time = 3 |
| 102 | + self.col_time_unit = 4 |
| 103 | + |
| 104 | + self.idx_pool = 0 |
| 105 | + self.idx_config = 1 |
| 106 | + self.name_separator = '/' |
| 107 | + |
| 108 | + self.col_statistics_time = self.col_real_time |
| 109 | + |
| 110 | + def name(self): |
| 111 | + return self.bench_name |
| 112 | + |
| 113 | + # --benchmark_format describes stdout output |
| 114 | + # --benchmark_out=<file> and --benchmark_out_format=<format> |
| 115 | + # describe output to a file |
| 116 | + def bin_args(self): |
| 117 | + return ["--benchmark_format=csv"] |
| 118 | + |
| 119 | + # the default unit |
| 120 | + # might be changed globally with --benchmark_time_unit={ns|us|ms|s} |
| 121 | + # the change affects only benchmark where time unit has not been set |
| 122 | + # explicitly |
| 123 | + def unit(self): |
| 124 | + return "ns" |
| 125 | + |
| 126 | + # these benchmarks are not stable, so set this at a large value |
| 127 | + def stddev_threshold(self) -> float: |
| 128 | + return 0.2 # 20% |
| 129 | + |
| 130 | + def get_pool_and_config(self, full_name): |
| 131 | + list_split = full_name.split(self.name_separator, 1) |
| 132 | + if len(list_split) != 2: |
| 133 | + raise ValueError("Incorrect benchmark name format: ", full_name) |
| 134 | + |
| 135 | + return list_split[self.idx_pool], list_split[self.idx_config] |
| 136 | + |
| 137 | + def get_mean(self, datarow): |
| 138 | + return float(datarow[self.col_statistics_time]) |
| 139 | + |
| 140 | + def parse_output(self, output): |
| 141 | + csv_file = io.StringIO(output) |
| 142 | + reader = csv.reader(csv_file) |
| 143 | + |
| 144 | + data_row = next(reader, None) |
| 145 | + if data_row is None: |
| 146 | + raise ValueError("Benchmark output does not contain data.") |
| 147 | + |
| 148 | + results = [] |
| 149 | + for row in reader: |
| 150 | + try: |
| 151 | + full_name = row[self.col_name] |
| 152 | + pool, config = self.get_pool_and_config(full_name) |
| 153 | + mean = self.get_mean(row) |
| 154 | + results.append((config, pool, mean)) |
| 155 | + except KeyError as e: |
| 156 | + raise ValueError(f"Error parsing output: {e}") |
| 157 | + |
| 158 | + return results |
0 commit comments