From 045890d9b056f56e3e53835b352c2ac9d176c7d9 Mon Sep 17 00:00:00 2001 From: Felix Thaler Date: Wed, 26 Jun 2024 08:22:16 +0200 Subject: [PATCH 1/2] Replace os.path usage by pathlib --- setup.py | 6 +++--- .../stencils/cuda_hip/mixin.py | 8 +++----- .../stencils/openmp/mixin.py | 7 +++---- .../stencils/openmp_blocked/mixin.py | 4 ++-- .../benchmarks_collection/stream/cuda_hip.py | 6 ++---- .../benchmarks_collection/stream/mc_calpin.py | 5 ++--- .../unstructured/openmp/mixin.py | 8 +++----- stencil_benchmarks/scripts/sbench_analyze.py | 15 ++++++--------- stencil_benchmarks/tools/template.py | 12 ++++++------ 9 files changed, 30 insertions(+), 41 deletions(-) diff --git a/setup.py b/setup.py index 8aa7fd8..51f0b21 100644 --- a/setup.py +++ b/setup.py @@ -30,16 +30,16 @@ # POSSIBILITY OF SUCH DAMAGE. # # SPDX-License-Identifier: BSD-3-Clause -import os import re +from pathlib import Path import setuptools def read_file(*path): """Read file content.""" - package_path = os.path.abspath(os.path.dirname(__file__)) - with open(os.path.join(package_path, *path)) as text_file: + package_path = Path(__file__).parent.resolve() + with open(package_path.joinpath(*path)) as text_file: return text_file.read() diff --git a/stencil_benchmarks/benchmarks_collection/stencils/cuda_hip/mixin.py b/stencil_benchmarks/benchmarks_collection/stencils/cuda_hip/mixin.py index 2852c24..43288f3 100644 --- a/stencil_benchmarks/benchmarks_collection/stencils/cuda_hip/mixin.py +++ b/stencil_benchmarks/benchmarks_collection/stencils/cuda_hip/mixin.py @@ -33,8 +33,8 @@ import abc import contextlib import ctypes -import os import warnings +from pathlib import Path from stencil_benchmarks.benchmark import ( Benchmark, @@ -63,10 +63,8 @@ def setup(self): if self.backend == "cuda" and self.timers == "hip-ext": raise ParameterError("hip-ext timers are not compatible with CUDA") - template_file = os.path.join( - os.path.dirname(os.path.abspath(__file__)), - "templates", - self.template_file(), + template_file = ( + Path(__file__).parent.resolve() / "templates" / self.template_file() ) code = template.render(template_file, **self.template_args()) code = cpphelpers.format_code(code, line_numbers=False) diff --git a/stencil_benchmarks/benchmarks_collection/stencils/openmp/mixin.py b/stencil_benchmarks/benchmarks_collection/stencils/openmp/mixin.py index 8c407c2..5c92535 100644 --- a/stencil_benchmarks/benchmarks_collection/stencils/openmp/mixin.py +++ b/stencil_benchmarks/benchmarks_collection/stencils/openmp/mixin.py @@ -34,6 +34,7 @@ import ctypes import os import warnings +from pathlib import Path from stencil_benchmarks.benchmark import Benchmark, ExecutionError, Parameter from stencil_benchmarks.tools import compilation, cpphelpers, template @@ -63,9 +64,7 @@ class StencilMixin(Benchmark): def setup(self): super().setup() - template_file = os.path.join( - self.template_path(), "templates", self.template_file() - ) + template_file = self.template_path() / "templates" / self.template_file() code = template.render(template_file, **self.template_args()) code = cpphelpers.format_code(code, line_numbers=False) @@ -84,7 +83,7 @@ def setup(self): ) def template_path(self): - return os.path.dirname(os.path.abspath(__file__)) + return Path(__file__).parent.resolve() def compile_command(self): command = [self.compiler] diff --git a/stencil_benchmarks/benchmarks_collection/stencils/openmp_blocked/mixin.py b/stencil_benchmarks/benchmarks_collection/stencils/openmp_blocked/mixin.py index 7c30120..b9a3cc7 100644 --- a/stencil_benchmarks/benchmarks_collection/stencils/openmp_blocked/mixin.py +++ b/stencil_benchmarks/benchmarks_collection/stencils/openmp_blocked/mixin.py @@ -31,7 +31,7 @@ # # SPDX-License-Identifier: BSD-3-Clause import contextlib -import os +from pathlib import Path import numpy as np @@ -60,7 +60,7 @@ def setup(self): ) def template_path(self): - return os.path.dirname(os.path.abspath(__file__)) + return Path(__file__).parent.resolve() @property def blocked_domain(self): diff --git a/stencil_benchmarks/benchmarks_collection/stream/cuda_hip.py b/stencil_benchmarks/benchmarks_collection/stream/cuda_hip.py index db27c22..b367a65 100644 --- a/stencil_benchmarks/benchmarks_collection/stream/cuda_hip.py +++ b/stencil_benchmarks/benchmarks_collection/stream/cuda_hip.py @@ -30,9 +30,9 @@ # POSSIBILITY OF SUCH DAMAGE. # # SPDX-License-Identifier: BSD-3-Clause -import os import re import warnings +from pathlib import Path from ...benchmark import Benchmark, ExecutionError, Parameter from ...tools import compilation, cpphelpers, template @@ -70,9 +70,7 @@ def setup(self): (self.array_size + elements_per_block - 1) // elements_per_block ) * elements_per_block - template_file = os.path.join( - os.path.dirname(os.path.abspath(__file__)), "cuda_hip.j2" - ) + template_file = Path(__file__).parent.resolve() / "cuda_hip.j2" code = template.render(template_file, **self.template_args()) if self.print_code: print(cpphelpers.format_code(code)) diff --git a/stencil_benchmarks/benchmarks_collection/stream/mc_calpin.py b/stencil_benchmarks/benchmarks_collection/stream/mc_calpin.py index 891a906..df97dad 100644 --- a/stencil_benchmarks/benchmarks_collection/stream/mc_calpin.py +++ b/stencil_benchmarks/benchmarks_collection/stream/mc_calpin.py @@ -32,6 +32,7 @@ # SPDX-License-Identifier: BSD-3-Clause import os import re +from pathlib import Path import numpy as np @@ -59,9 +60,7 @@ def setup(self): if self.compiler.endswith("icpc"): os.environ["KMP_INIT_AT_FORK"] = "0" - template_file = os.path.join( - os.path.dirname(os.path.abspath(__file__)), self.template_file() - ) + template_file = Path(__file__).parent.resolve() / self.template_file() code = template.render(template_file, **self.template_args()) if self.print_kernels: print( diff --git a/stencil_benchmarks/benchmarks_collection/unstructured/openmp/mixin.py b/stencil_benchmarks/benchmarks_collection/unstructured/openmp/mixin.py index 87c9ce0..ae6e67c 100644 --- a/stencil_benchmarks/benchmarks_collection/unstructured/openmp/mixin.py +++ b/stencil_benchmarks/benchmarks_collection/unstructured/openmp/mixin.py @@ -34,6 +34,7 @@ import ctypes import os import warnings +from pathlib import Path from stencil_benchmarks.benchmark import Benchmark, ExecutionError, Parameter from stencil_benchmarks.tools import compilation, cpphelpers, template @@ -64,8 +65,8 @@ class UnstructuredMixin(Benchmark): def setup(self): super().setup() - template_file = os.path.join( - self.template_path(), "templates", self.template_file() + template_file = ( + Path(__file__).parent.resolve() / "templates" / self.template_file() ) code = template.render(template_file, **self.template_args()) code = cpphelpers.format_code(code, line_numbers=False) @@ -84,9 +85,6 @@ def setup(self): "false negatives for stencils with read-write fields" ) - def template_path(self): - return os.path.dirname(os.path.abspath(__file__)) - def compile_command(self): command = [self.compiler] if self.platform_preset != "none": diff --git a/stencil_benchmarks/scripts/sbench_analyze.py b/stencil_benchmarks/scripts/sbench_analyze.py index f78c72d..695bb5c 100644 --- a/stencil_benchmarks/scripts/sbench_analyze.py +++ b/stencil_benchmarks/scripts/sbench_analyze.py @@ -31,9 +31,9 @@ # # SPDX-License-Identifier: BSD-3-Clause -import os import re import textwrap +from pathlib import Path import click import numpy as np @@ -84,7 +84,7 @@ def arrange(df, query, groups, aggregation, unstack, select, sort): @main.command(name="print") -@click.argument("csv", type=click.Path(exists=True)) +@click.argument("csv", type=click.Path(exists=True, path_type=Path)) @click.option( "--common/--non-common", "-c", @@ -134,7 +134,7 @@ def print_csv( @main.command() -@click.argument("csv", type=click.Path(exists=True)) +@click.argument("csv", type=click.Path(exists=True, path_type=Path)) @click.option("--uniform/--non-uniform", help="Equidistant placement of x-axis ticks.") @click.option("--ylim", type=float, nargs=2, help="Y-axis limits.") @click.option("--title", "-t", help="Plot title.") @@ -166,7 +166,7 @@ def print_csv( help="Search and replace a pattern in the final labels, " "input as /pattern/repl/ in Python regex syntax.", ) -@click.option("--output", "-o", type=click.Path(), help="Output file.") +@click.option("--output", "-o", type=click.Path(path_type=Path), help="Output file.") @click.option("--dpi", default=300, type=int, help="Output DPI (dots per inch).") def plot( csv, @@ -186,9 +186,6 @@ def plot( dpi, ): """Plot output of sbench. - - X is the data column name for the values used for the x-axis in the plot, Y - is the column name for the y-axis. """ import cycler from matplotlib import pyplot as plt @@ -247,7 +244,7 @@ def plot( if relative_to: plt.gca().yaxis.set_major_formatter(ticker.PercentFormatter(1.0)) if not title: - title = os.path.split(csv)[-1] + title = csv.name plt.title(title) subtitle = ", ".join(f"{k}: {v}" for k, v in common.items()) plt.text( @@ -269,7 +266,7 @@ def plot( @main.command() -@click.argument("csv", type=click.Path(exists=True), nargs=-1) +@click.argument("csv", type=click.Path(exists=True, path_type=Path), nargs=-1) @click.argument("output", type=click.File(mode="w")) def merge(csv, output): """Merge multiple CSV files produces by sbench.""" diff --git a/stencil_benchmarks/tools/template.py b/stencil_benchmarks/tools/template.py index 0ef63b6..ffb0c06 100644 --- a/stencil_benchmarks/tools/template.py +++ b/stencil_benchmarks/tools/template.py @@ -30,14 +30,14 @@ # POSSIBILITY OF SUCH DAMAGE. # # SPDX-License-Identifier: BSD-3-Clause -import os -from typing import Any, Dict +from pathlib import Path +from typing import Any, Dict, Union import jinja2 -def render(template_file: str, **kwargs: Dict[str, Any]) -> str: - template_path, template_file = os.path.split(template_file) - env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_path)) - template = env.get_template(template_file) +def render(template_file: Union[str, Path], **kwargs: Dict[str, Any]) -> str: + template_file = Path(template_file) + env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_file.parent)) + template = env.get_template(template_file.name) return template.render(**kwargs) From 96c300b4357bc1083032167e9f83fbf827121551 Mon Sep 17 00:00:00 2001 From: Felix Thaler Date: Wed, 26 Jun 2024 08:40:28 +0200 Subject: [PATCH 2/2] Fix plotting of single-series data --- stencil_benchmarks/scripts/sbench_analyze.py | 31 ++++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/stencil_benchmarks/scripts/sbench_analyze.py b/stencil_benchmarks/scripts/sbench_analyze.py index 695bb5c..9e09ec1 100644 --- a/stencil_benchmarks/scripts/sbench_analyze.py +++ b/stencil_benchmarks/scripts/sbench_analyze.py @@ -185,8 +185,7 @@ def plot( output, dpi, ): - """Plot output of sbench. - """ + """Plot output of sbench.""" import cycler from matplotlib import pyplot as plt from matplotlib import ticker @@ -215,20 +214,28 @@ def plot( pattern, repl = regex[1:-1].split(splitter, 1) regexes.append((re.compile(pattern), repl)) - for index, row in df.iterrows(): + def plot_data(index, row): x = np.arange(len(row.index)) if uniform else row.index y = row.values / relative_to if relative_to else row.values - if isinstance(index, tuple): - label = ", ".join( - f"{name}={value}" for name, value in zip(df.index.names, index) - ) - else: - label = str(index) + label = ( + ", ".join(f"{name}={value}" for name, value in zip(df.index.names, index)) + if isinstance(index, tuple) + else str(index) + ) for regex, repl in regexes: label = regex.sub(repl, label) plt.plot(x, y, label=label) - if uniform: - plt.xticks(x, row.index, rotation=45) + if uniform: + plt.xticks(x, row.index, rotation=45) + + if isinstance(df, pd.Series): + plot_data(df.index, df) + plt.xlabel(df.index.name) + else: + for index, row in df.iterrows(): + plot_data(index, row) + plt.xlabel(df.columns.name) + plt.legend() for i, ref in enumerate(reference): label, value = ref.split("=", 1) @@ -236,8 +243,6 @@ def plot( dashes = (5 * (i + 1) + 3 * i, 3) plt.axhline(value, color="k", ls=(0, dashes), label=label) - plt.legend() - plt.xlabel(df.columns.name) plt.ylabel(select) if ylim: plt.ylim(ylim)