From e5f2e3fcc70c148ad767ff97e54453ad97149eed Mon Sep 17 00:00:00 2001 From: vladaindjic Date: Tue, 1 Mar 2022 23:47:57 +0100 Subject: [PATCH] Benchmark dynamic config can be passed with the benchmark source code. --- .../agent_py/datasets/__init__.py | 21 ++++++++++++++++++ .../agent_py/datasets/hpctoolkit_dataset.py | 22 +++++++++++++++++++ .../benchmarks/cpu-benchmarks/simple_pow.c | 21 ++++++++++++++++++ examples/hpctoolkit_service/demo_perf.py | 1 + .../service_py/benchmark_builder.py | 2 +- 5 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 examples/hpctoolkit_service/benchmarks/cpu-benchmarks/simple_pow.c diff --git a/examples/hpctoolkit_service/agent_py/datasets/__init__.py b/examples/hpctoolkit_service/agent_py/datasets/__init__.py index e69de29bb..9e865c28c 100644 --- a/examples/hpctoolkit_service/agent_py/datasets/__init__.py +++ b/examples/hpctoolkit_service/agent_py/datasets/__init__.py @@ -0,0 +1,21 @@ +from compiler_gym.datasets import Benchmark +from compiler_gym.datasets.uri import BenchmarkUri +from compiler_gym.service.proto import Benchmark as BenchmarkProto, BenchmarkDynamicConfig +from compiler_gym.service.proto import File +from typing import Union + + +def benchmark_from_file_contents(uri: Union[str, BenchmarkUri], data: bytes, + dynamic_config: BenchmarkDynamicConfig): + """Construct a benchmark from raw data. + + :param uri: The URI of the benchmark. + + :param data: An array of bytes that will be passed to the compiler + service. + + :param dynamic_config: BenchmarkDynamicConfig that specifies build, pre_run, run, + post_run commands. + """ + return Benchmark(proto=BenchmarkProto(uri=str(uri), program=File(contents=data), + dynamic_config=dynamic_config)) diff --git a/examples/hpctoolkit_service/agent_py/datasets/hpctoolkit_dataset.py b/examples/hpctoolkit_service/agent_py/datasets/hpctoolkit_dataset.py index 719d390fe..7fe6350fa 100644 --- a/examples/hpctoolkit_service/agent_py/datasets/hpctoolkit_dataset.py +++ b/examples/hpctoolkit_service/agent_py/datasets/hpctoolkit_dataset.py @@ -10,6 +10,9 @@ sys.path.insert(0, Path(__file__).parent.parent.parent.resolve()) import utils +from . import benchmark_from_file_contents +from compiler_gym.service.proto import BenchmarkDynamicConfig, Command + # BENCHMARKS_PATH: Path = runfiles_path("examples/hpctoolkit_service/benchmarks") BENCHMARKS_PATH: Path = Path( @@ -39,6 +42,25 @@ def __init__(self, *args, **kwargs): "benchmark://hpctoolkit-cpu-v0/nanosleep", self.preprocess(BENCHMARKS_PATH / "nanosleep.c"), ), + "benchmark://hpctoolkit-cpu-v0/simple_pow": benchmark_from_file_contents( + "benchmark://hpctoolkit-cpu-v0/simple_pow", + self.preprocess(BENCHMARKS_PATH / "simple_pow.c"), + BenchmarkDynamicConfig( + build_cmd=Command( + # $CC is replaced with clang command, + # $IN is replaced with benchmark path + # Following are linking flags (only one in this case). + argument=["$CC", "$IN", "-lm"], + timeout_seconds=60, + outfile=["a.out"], + ), + run_cmd=Command( + argument=["./a.out"], + timeout_seconds=300, + infile=["a.out"], + ) + ) + ), } @staticmethod diff --git a/examples/hpctoolkit_service/benchmarks/cpu-benchmarks/simple_pow.c b/examples/hpctoolkit_service/benchmarks/cpu-benchmarks/simple_pow.c new file mode 100644 index 000000000..0a84d42a6 --- /dev/null +++ b/examples/hpctoolkit_service/benchmarks/cpu-benchmarks/simple_pow.c @@ -0,0 +1,21 @@ +// #include +#include +#include +#include + +#define ITER 10 + +__attribute__((noinline)) +double test_bench_func() { + double sum = 0.0; + for (int ic = 1; ic <= ITER; ic++) { + sum += pow(ic, 3); + } + return sum; +} + +__attribute__((optnone)) int main(int argc, char* argv[]) { + double ret = test_bench_func(); + printf("*** The bench result is: %f\n", ret); + return 0; +} diff --git a/examples/hpctoolkit_service/demo_perf.py b/examples/hpctoolkit_service/demo_perf.py index 74773639e..8ef5afa49 100644 --- a/examples/hpctoolkit_service/demo_perf.py +++ b/examples/hpctoolkit_service/demo_perf.py @@ -75,6 +75,7 @@ def main(): benchmark_to_process = [ # from benchmarks directory + "benchmark://hpctoolkit-cpu-v0/simple_pow", # "benchmark://hpctoolkit-cpu-v0/offsets1", "benchmark://hpctoolkit-cpu-v0/conv2d", # "benchmark://hpctoolkit-cpu-v0/nanosleep", diff --git a/examples/hpctoolkit_service/service_py/benchmark_builder.py b/examples/hpctoolkit_service/service_py/benchmark_builder.py index e2c478306..c32b1e96b 100644 --- a/examples/hpctoolkit_service/service_py/benchmark_builder.py +++ b/examples/hpctoolkit_service/service_py/benchmark_builder.py @@ -51,7 +51,7 @@ def __init__(self, working_directory: Path, benchmark: Benchmark, timeout_sec: f self.bc_path, "-o", self.exe_path, - "-lm", + # "-lm", ], # "-nostartfiles" "save": [self.llvm_dis, "-o", self.llvm_new_path, self.bc_path], }