Skip to content

Commit

Permalink
Merge branch 'main' of github.com:scitools/iris into cube-type-hints
Browse files Browse the repository at this point in the history
  • Loading branch information
bouweandela committed Sep 16, 2024
2 parents 91d221b + 2acfd92 commit 9f2f2b1
Show file tree
Hide file tree
Showing 33 changed files with 1,647 additions and 906 deletions.
9 changes: 6 additions & 3 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
# Check for updates to GitHub Actions every weekday
interval: "daily"
# Check later in the week - the upstream dependabot check in `workflows` runs deliberately early in the week.
# Therefore allowing time for the `workflows` update to be merged-and-released first.
interval: "weekly"
day: "thursday"
time: "01:00"
timezone: "Europe/London"
labels:
- "New: Pull Request"
- "Bot"
2 changes: 1 addition & 1 deletion .github/workflows/ci-manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ concurrency:
jobs:
manifest:
name: "check-manifest"
uses: scitools/workflows/.github/workflows/ci-manifest.yml@2024.08.1
uses: scitools/workflows/.github/workflows/ci-manifest.yml@2024.09.4
13 changes: 6 additions & 7 deletions .github/workflows/ci-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,29 @@ jobs:
- name: "data cache"
uses: ./.github/workflows/composite/iris-data-cache
with:
cache_build: 0
cache_build: 5
env_name: ${{ env.ENV_NAME }}
version: ${{ env.IRIS_TEST_DATA_VERSION }}

- name: "conda package cache"
uses: ./.github/workflows/composite/conda-pkg-cache
with:
cache_build: 0
cache_build: 5
cache_period: ${{ env.CACHE_PERIOD }}
env_name: ${{ env.ENV_NAME }}

- name: "conda install"
uses: conda-incubator/setup-miniconda@v3
with:
miniforge-version: latest
channels: conda-forge,defaults
channels: conda-forge
activate-environment: ${{ env.ENV_NAME }}
auto-update-conda: false
use-only-tar-bz2: true

- name: "conda environment cache"
uses: ./.github/workflows/composite/conda-env-cache
with:
cache_build: 0
cache_build: 5
cache_period: ${{ env.CACHE_PERIOD }}
env_name: ${{ env.ENV_NAME }}
install_packages: "cartopy nox pip"
Expand All @@ -104,14 +103,14 @@ jobs:
- name: "cartopy cache"
uses: ./.github/workflows/composite/cartopy-cache
with:
cache_build: 0
cache_build: 5
cache_period: ${{ env.CACHE_PERIOD }}
env_name: ${{ env.ENV_NAME }}

- name: "nox cache"
uses: ./.github/workflows/composite/nox-cache
with:
cache_build: 2
cache_build: 5
env_name: ${{ env.ENV_NAME }}
lock_file: ${{ env.LOCK_FILE }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/refresh-lockfiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ on:

jobs:
refresh_lockfiles:
uses: scitools/workflows/.github/workflows/refresh-lockfiles.yml@2024.08.1
uses: scitools/workflows/.github/workflows/refresh-lockfiles.yml@2024.09.4
secrets: inherit
6 changes: 6 additions & 0 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ repeats _between_ `setup()` calls using the `repeat` attribute.
`warmup_time = 0` is also advisable since ASV performs independent re-runs to
estimate run-time, and these will still be subject to the original problem.

### Custom benchmarks

Iris benchmarking implements custom benchmark types, such as a `tracemalloc`
benchmark to measure memory growth. See [custom_bms/](./custom_bms) for more
detail.

### Scaling / non-Scaling Performance Differences

**(We no longer advocate the below for benchmarks run during CI, given the
Expand Down
7 changes: 5 additions & 2 deletions benchmarks/asv.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@
"command_comment": [
"We know that the Nox command takes care of installation in each",
"environment, and in the case of Iris no specialised uninstall or",
"build commands are needed to get it working."
"build commands are needed to get it working.",

"We do however need to install the custom benchmarks for them to be",
"usable."
],
"install_command": [],
"uninstall_command": [],
"build_command": []
"build_command": ["python {conf_dir}/custom_bms/install.py"]
}
105 changes: 0 additions & 105 deletions benchmarks/benchmarks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,111 +37,6 @@ def disable_repeat_between_setup(benchmark_object):
return benchmark_object


class TrackAddedMemoryAllocation:
"""Measures by how much process resident memory grew, during execution.
Context manager which measures by how much process resident memory grew,
during execution of its enclosed code block.
Obviously limited as to what it actually measures : Relies on the current
process not having significant unused (de-allocated) memory when the
tested codeblock runs, and only reliable when the code allocates a
significant amount of new memory.
Example:
with TrackAddedMemoryAllocation() as mb:
initial_call()
other_call()
result = mb.addedmem_mb()
Attributes
----------
RESULT_MINIMUM_MB : float
The smallest result that should ever be returned, in Mb. Results
fluctuate from run to run (usually within 1Mb) so if a result is
sufficiently small this noise will produce a before-after ratio over
AVD's detection threshold and be treated as 'signal'. Results
smaller than this value will therefore be returned as equal to this
value, ensuring fractionally small noise / no noise at all.
Defaults to 1.0
RESULT_ROUND_DP : int
Number of decimal places of rounding on result values (in Mb).
Defaults to 1
"""

RESULT_MINIMUM_MB = 0.2
RESULT_ROUND_DP = 1 # I.E. to nearest 0.1 Mb

def __enter__(self):
tracemalloc.start()
return self

def __exit__(self, *_):
_, peak_mem_bytes = tracemalloc.get_traced_memory()
tracemalloc.stop()
# Save peak-memory allocation, scaled from bytes to Mb.
self._peak_mb = peak_mem_bytes * (2.0**-20)

def addedmem_mb(self):
"""Return measured memory growth, in Mb."""
result = self._peak_mb
# Small results are too vulnerable to noise being interpreted as signal.
result = max(self.RESULT_MINIMUM_MB, result)
# Rounding makes results easier to read.
result = np.round(result, self.RESULT_ROUND_DP)
return result

@staticmethod
def decorator(decorated_func):
"""Benchmark to track growth in resident memory during execution.
Intended for use on ASV ``track_`` benchmarks. Applies the
:class:`TrackAddedMemoryAllocation` context manager to the benchmark
code, sets the benchmark ``unit`` attribute to ``Mb``.
"""

def _wrapper(*args, **kwargs):
assert decorated_func.__name__[:6] == "track_"
# Run the decorated benchmark within the added memory context
# manager.
with TrackAddedMemoryAllocation() as mb:
decorated_func(*args, **kwargs)
return mb.addedmem_mb()

decorated_func.unit = "Mb"
return _wrapper

@staticmethod
def decorator_repeating(repeats=3):
"""Benchmark to track growth in resident memory during execution.
Tracks memory for repeated calls of decorated function.
Intended for use on ASV ``track_`` benchmarks. Applies the
:class:`TrackAddedMemoryAllocation` context manager to the benchmark
code, sets the benchmark ``unit`` attribute to ``Mb``.
"""

def decorator(decorated_func):
def _wrapper(*args, **kwargs):
assert decorated_func.__name__[:6] == "track_"
# Run the decorated benchmark within the added memory context
# manager.
with TrackAddedMemoryAllocation() as mb:
for _ in range(repeats):
decorated_func(*args, **kwargs)
return mb.addedmem_mb()

decorated_func.unit = "Mb"
return _wrapper

return decorator


def on_demand_benchmark(benchmark_object):
"""Disable these benchmark(s) unless ON_DEMAND_BENCHARKS env var is set.
Expand Down
5 changes: 2 additions & 3 deletions benchmarks/benchmarks/cperf/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from iris import save

from .. import TrackAddedMemoryAllocation, on_demand_benchmark
from .. import on_demand_benchmark
from ..generate_data.ugrid import make_cube_like_2d_cubesphere, make_cube_like_umfield
from . import _N_CUBESPHERE_UM_EQUIVALENT, _UM_DIMS_YX

Expand Down Expand Up @@ -36,6 +36,5 @@ def _save_data(self, cube):
def time_save_data_netcdf(self, data_type):
self._save_data(self.cube)

@TrackAddedMemoryAllocation.decorator
def track_addedmem_save_data_netcdf(self, data_type):
def tracemalloc_save_data_netcdf(self, data_type):
self._save_data(self.cube)
41 changes: 26 additions & 15 deletions benchmarks/benchmarks/merge_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
# See LICENSE in the root of the repository for full licensing details.
"""Benchmarks relating to :meth:`iris.cube.CubeList.merge` and ``concatenate``."""

import warnings

import numpy as np

from iris.cube import CubeList
from iris.warnings import IrisVagueMetadataWarning

from . import TrackAddedMemoryAllocation
from .generate_data.stock import realistic_4d_w_everything


Expand All @@ -34,28 +36,37 @@ def setup(self):
def time_merge(self):
_ = self.cube_list.merge_cube()

@TrackAddedMemoryAllocation.decorator_repeating()
def track_mem_merge(self):
def tracemalloc_merge(self):
_ = self.cube_list.merge_cube()

tracemalloc_merge.number = 3 # type: ignore[attr-defined]


class Concatenate:
# TODO: Improve coverage.

cube_list: CubeList

def setup(self):
source_cube = realistic_4d_w_everything()
second_cube = source_cube.copy()
first_dim_coord = second_cube.coord(dimensions=0, dim_coords=True)
first_dim_coord.points = (
first_dim_coord.points + np.ptp(first_dim_coord.points) + 1
)
self.cube_list = CubeList([source_cube, second_cube])

def time_concatenate(self):
params = [[False, True]]
param_names = ["Lazy operations"]

def setup(self, lazy_run: bool):
warnings.filterwarnings("ignore", message="Ignoring a datum")
warnings.filterwarnings("ignore", category=IrisVagueMetadataWarning)
source_cube = realistic_4d_w_everything(lazy=lazy_run)
self.cube_list = CubeList([source_cube])
for _ in range(24):
next_cube = self.cube_list[-1].copy()
first_dim_coord = next_cube.coord(dimensions=0, dim_coords=True)
first_dim_coord.points = (
first_dim_coord.points + np.ptp(first_dim_coord.points) + 1
)
self.cube_list.append(next_cube)

def time_concatenate(self, _):
_ = self.cube_list.concatenate_cube()

@TrackAddedMemoryAllocation.decorator_repeating()
def track_mem_merge(self):
def tracemalloc_concatenate(self, _):
_ = self.cube_list.concatenate_cube()

tracemalloc_concatenate.number = 3 # type: ignore[attr-defined]
13 changes: 4 additions & 9 deletions benchmarks/benchmarks/mesh/utils/regions_combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from iris import load, load_cube, save
from iris.mesh.utils import recombine_submeshes

from ... import TrackAddedMemoryAllocation
from ...generate_data.ugrid import make_cube_like_2d_cubesphere


Expand Down Expand Up @@ -169,8 +168,7 @@ def setup(self, n_cubesphere):
def time_create_combined_cube(self, n_cubesphere):
self.recombine()

@TrackAddedMemoryAllocation.decorator
def track_addedmem_create_combined_cube(self, n_cubesphere):
def tracemalloc_create_combined_cube(self, n_cubesphere):
self.recombine()


Expand All @@ -180,8 +178,7 @@ class CombineRegionsComputeRealData(MixinCombineRegions):
def time_compute_data(self, n_cubesphere):
_ = self.recombined_cube.data

@TrackAddedMemoryAllocation.decorator
def track_addedmem_compute_data(self, n_cubesphere):
def tracemalloc_compute_data(self, n_cubesphere):
_ = self.recombined_cube.data


Expand All @@ -199,8 +196,7 @@ def time_save(self, n_cubesphere):
# Save to disk, which must compute data + stream it to file.
save(self.recombined_cube, "tmp.nc")

@TrackAddedMemoryAllocation.decorator
def track_addedmem_save(self, n_cubesphere):
def tracemalloc_save(self, n_cubesphere):
save(self.recombined_cube, "tmp.nc")

def track_filesize_saved(self, n_cubesphere):
Expand All @@ -227,6 +223,5 @@ def time_stream_file2file(self, n_cubesphere):
# Save to disk, which must compute data + stream it to file.
save(self.recombined_cube, "tmp.nc")

@TrackAddedMemoryAllocation.decorator
def track_addedmem_stream_file2file(self, n_cubesphere):
def tracemalloc_stream_file2file(self, n_cubesphere):
save(self.recombined_cube, "tmp.nc")
17 changes: 9 additions & 8 deletions benchmarks/benchmarks/regridding.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
from iris.analysis import AreaWeighted, PointInCell
from iris.coords import AuxCoord

from . import TrackAddedMemoryAllocation


class HorizontalChunkedRegridding:
def setup(self) -> None:
Expand Down Expand Up @@ -53,20 +51,22 @@ def time_regrid_area_w_new_grid(self) -> None:
# Realise data
out.data

@TrackAddedMemoryAllocation.decorator_repeating()
def track_mem_regrid_area_w(self) -> None:
def tracemalloc_regrid_area_w(self) -> None:
# Regrid the chunked cube
out = self.cube.regrid(self.template_cube, self.scheme_area_w)
# Realise data
out.data

@TrackAddedMemoryAllocation.decorator_repeating()
def track_mem_regrid_area_w_new_grid(self) -> None:
tracemalloc_regrid_area_w.number = 3 # type: ignore[attr-defined]

def tracemalloc_regrid_area_w_new_grid(self) -> None:
# Regrid the chunked cube
out = self.chunked_cube.regrid(self.template_cube, self.scheme_area_w)
# Realise data
out.data

tracemalloc_regrid_area_w_new_grid.number = 3 # type: ignore[attr-defined]


class CurvilinearRegridding:
def setup(self) -> None:
Expand Down Expand Up @@ -110,9 +110,10 @@ def time_regrid_pic(self) -> None:
# Realise the data
out.data

@TrackAddedMemoryAllocation.decorator_repeating()
def track_mem_regrid_pic(self) -> None:
def tracemalloc_regrid_pic(self) -> None:
# Regrid the cube onto the template.
out = self.cube.regrid(self.template_cube, self.scheme_pic)
# Realise the data
out.data

tracemalloc_regrid_pic.number = 3 # type: ignore[attr-defined]
Loading

0 comments on commit 9f2f2b1

Please sign in to comment.