|
| 1 | +# Copyright 2025 ACCESS-NRI and contributors. See the top-level COPYRIGHT file for details. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +from abc import ABC, abstractmethod |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import xarray as xr |
| 8 | + |
| 9 | +from access.profiling.metrics import ProfilingMetric |
| 10 | +from access.profiling.parser import ProfilingParser |
| 11 | +from access.profiling.scaling import plot_scaling_metrics |
| 12 | + |
| 13 | + |
| 14 | +class ProfilingLog: |
| 15 | + """Represents a profiling log file. |
| 16 | +
|
| 17 | + Args: |
| 18 | + filepath (Path): Path to the log file. |
| 19 | + parser (ProfilingParser): Parser to use for this log file. |
| 20 | + """ |
| 21 | + |
| 22 | + filepath: Path # Path to the log file |
| 23 | + parser: ProfilingParser # Parser to use for this log file |
| 24 | + |
| 25 | + def __init__(self, filepath: Path, parser: ProfilingParser): |
| 26 | + self.filepath = filepath |
| 27 | + self.parser = parser |
| 28 | + |
| 29 | + def parse(self) -> xr.Dataset: |
| 30 | + """Parses the log file and returns the profiling data as an xarray Dataset. |
| 31 | +
|
| 32 | + Returns: |
| 33 | + xr.Dataset: Parsed profiling data.""" |
| 34 | + path = self.filepath |
| 35 | + log = path.read_text() |
| 36 | + data = self.parser.read(log) |
| 37 | + return xr.Dataset( |
| 38 | + data_vars=dict( |
| 39 | + zip( |
| 40 | + self.parser.metrics, |
| 41 | + [ |
| 42 | + xr.DataArray(data[metric], dims=["region"]).pint.quantify(metric.units) |
| 43 | + for metric in self.parser.metrics |
| 44 | + ], |
| 45 | + strict=True, |
| 46 | + ) |
| 47 | + ), |
| 48 | + coords={"region": data["region"]}, |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +class ProfilingManager(ABC): |
| 53 | + """Abstract base class to handle profiling data and workflows. |
| 54 | +
|
| 55 | + This high-level class defines methods to parse different types of profiling data. Currently, |
| 56 | + it supports parsing and plotting scaling data. |
| 57 | + """ |
| 58 | + |
| 59 | + data: dict[str, xr.Dataset] = {} # Dictionary mapping component names to their profiling datasets. |
| 60 | + |
| 61 | + @abstractmethod |
| 62 | + def parse_profiling_data(self, path: Path) -> dict[str, xr.Dataset]: |
| 63 | + """Parses profiling data from the specified path. |
| 64 | +
|
| 65 | + Args: |
| 66 | + path (Path): Path to the experiment directory. |
| 67 | +
|
| 68 | + Returns: |
| 69 | + dict[str, xr.Dataset]: Dictionary mapping component names to their profiling datasets. |
| 70 | + """ |
| 71 | + |
| 72 | + @abstractmethod |
| 73 | + def parse_ncpus(self, path: Path) -> int: |
| 74 | + """Parses the number of CPUs used in a given experiment in the specified path. |
| 75 | +
|
| 76 | + Args: |
| 77 | + path (Path): Path to the experiment directory. |
| 78 | +
|
| 79 | + Returns: |
| 80 | + int: Number of CPUs used in the experiment. |
| 81 | + """ |
| 82 | + |
| 83 | + def parse_scaling_data(self, paths: list[Path]): |
| 84 | + """Parses profiling data from a list of experiment directories. |
| 85 | +
|
| 86 | + Args: |
| 87 | + paths (list[Path]): List of paths to experiment directories. |
| 88 | + """ |
| 89 | + self.data = {} |
| 90 | + for path in paths: |
| 91 | + # Parse data |
| 92 | + datasets = self.parse_profiling_data(path) |
| 93 | + |
| 94 | + # Find number of cpus used |
| 95 | + ncpus = self.parse_ncpus(path) |
| 96 | + |
| 97 | + # Add ncpus dimension and concatenate with existing data |
| 98 | + for name, ds in datasets.items(): |
| 99 | + ds = ds.expand_dims({"ncpus": 1}).assign_coords({"ncpus": [ncpus]}) |
| 100 | + if name in self.data: |
| 101 | + self.data[name] = xr.concat([self.data[name], ds], dim="ncpus", join="outer") |
| 102 | + else: |
| 103 | + self.data[name] = ds |
| 104 | + |
| 105 | + def plot_scaling_data( |
| 106 | + self, |
| 107 | + components: list[str], |
| 108 | + regions: list[list[str]], |
| 109 | + metric: ProfilingMetric, |
| 110 | + region_relabel_map: dict | None = None, |
| 111 | + ): |
| 112 | + """Plots scaling data for the specified components, regions and metric. |
| 113 | +
|
| 114 | + Args: |
| 115 | + components (list[str]): List of component names to plot. |
| 116 | + regions (list[list[str]]): List of regions to plot for each component. |
| 117 | + metric (ProfilingMetric): Metric to use for the scaling plots. |
| 118 | + region_relabel_map (dict | None): Optional mapping to relabel regions in the plots. |
| 119 | + """ |
| 120 | + plot_scaling_metrics([self.data[c] for c in components], regions, metric, region_relabel_map=region_relabel_map) |
0 commit comments