Skip to content

Commit

Permalink
Add helper functions to run specifc profiling analysis, starting with…
Browse files Browse the repository at this point in the history
… parallel scaling.
  • Loading branch information
micaeljtoliveira committed Apr 9, 2024
1 parent 191c370 commit 7bba5f8
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 32 deletions.
23 changes: 23 additions & 0 deletions om3utils/profiling_analyses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from pathlib import Path
import xarray as xr

from om3utils.payu_config_yaml import read_payu_config_yaml


def scaling_ncpus(run_dir):
config_file = Path(run_dir) / "config.yaml"
config = read_payu_config_yaml(config_file.as_posix())
return config["ncpus"]


def scaling_speedup(stats: xr.Dataset) -> xr.Dataset:
speedup = stats.tavg.sel(ncpus=stats["ncpus"].min()) / stats.tavg
speedup.name = "speedup"
return speedup


def scaling_efficiency(stats: xr.Dataset) -> xr.Dataset:
speedup = scaling_speedup(stats)
eff = speedup / speedup.ncpus * 100 * stats["ncpus"].min()
eff.name = "parallel efficiency [%]"
return eff
34 changes: 34 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest
import xarray as xr


@pytest.fixture()
def profiling_data():
regions = ["Total runtime", "Ocean Initialization"]
ncpus = [1, 2, 4]
hits = []
tmin = []
tmax = []
tavg = []
for n in ncpus:
hits.append([1, 2])
tmin.append([value / min(n, 2) for value in [138.600364, 2.344926]])
tmax.append([value / min(n, 2) for value in [138.600366, 2.345701]])
tavg.append([value / min(n, 2) for value in [600365, 2.345388]])

return regions, ncpus, hits, tmin, tmax, tavg


@pytest.fixture()
def simple_scaling_data(profiling_data):
regions, ncpus, hits, tmin, tmax, tavg = profiling_data

return xr.Dataset(
data_vars=dict(
hits=(["ncpus", "region"], hits),
tmin=(["ncpus", "region"], tmin),
tmax=(["ncpus", "region"], tmax),
tavg=(["ncpus", "region"], tavg),
),
coords=dict(region=regions, ncpus=ncpus),
)
32 changes: 0 additions & 32 deletions tests/test_profiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,6 @@ def read(self, path: Path) -> dict:
return self._data[path.name]


@pytest.fixture()
def profiling_data():
regions = ["Total runtime", "Ocean Initialization"]
ncpus = [1, 2, 4]
hits = []
tmin = []
tmax = []
tavg = []
for n in ncpus:
hits.append([1, 2])
tmin.append([value / min(n, 2) for value in [138.600364, 2.344926]])
tmax.append([value / min(n, 2) for value in [138.600366, 2.345701]])
tavg.append([value / min(n, 2) for value in [600365, 2.345388]])

return regions, ncpus, hits, tmin, tmax, tavg


@pytest.fixture()
def simple_scaling_runs(tmp_path, profiling_data):
regions, ncpus, hits, tmin, tmax, tavg = profiling_data
Expand All @@ -55,21 +38,6 @@ def simple_scaling_runs(tmp_path, profiling_data):
return run_dirs, parser


@pytest.fixture()
def simple_scaling_data(profiling_data):
regions, ncpus, hits, tmin, tmax, tavg = profiling_data

return xr.Dataset(
data_vars=dict(
hits=(["ncpus", "region"], hits),
tmin=(["ncpus", "region"], tmin),
tmax=(["ncpus", "region"], tmax),
tavg=(["ncpus", "region"], tavg),
),
coords=dict(region=regions, ncpus=ncpus),
)


def test_parse_profiling_data(tmp_path, simple_scaling_runs, simple_scaling_data):
run_dirs, parser = simple_scaling_runs

Expand Down
27 changes: 27 additions & 0 deletions tests/test_scaling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest

from om3utils.profiling_analyses import scaling_ncpus, scaling_speedup, scaling_efficiency


@pytest.fixture()
def scaling_run_dir(tmp_path):
file = tmp_path / "config.yaml"
file.write_text("ncpus: 4")


def test_scaling_ncpus(tmp_path, scaling_run_dir):
ncpus = scaling_ncpus(tmp_path)

assert ncpus == 4


def test_scaling_speedup(simple_scaling_data):
speedup = scaling_speedup(simple_scaling_data)

assert (speedup.sel(region="Total runtime").values[:] == [1.0, 2.0, 2.0]).all()


def test_efficiency(simple_scaling_data):
efficiency = scaling_efficiency(simple_scaling_data)

assert (efficiency.sel(region="Total runtime").values[:] == [100.0, 100.0, 50.0]).all()

0 comments on commit 7bba5f8

Please sign in to comment.