From 9f50507681376daec44dc540802959094b0c7ff4 Mon Sep 17 00:00:00 2001 From: Vecko <36369090+VeckoTheGecko@users.noreply.github.com> Date: Tue, 12 Nov 2024 17:01:13 +0800 Subject: [PATCH 01/35] update contributing page --- docs/community/contributing.rst | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/docs/community/contributing.rst b/docs/community/contributing.rst index f8728d1df6..e68b6f5742 100644 --- a/docs/community/contributing.rst +++ b/docs/community/contributing.rst @@ -74,7 +74,18 @@ From there: - create a git branch, implement, commit, and push your changes - `create a pull request `_ (PR) into ``master`` of the original repo making sure to link to the issue that you are working on. Not yet finished with your feature but still want feedback on how you're going? Then mark it as "draft" and ``@ping`` a maintainer. See our `maintainer notes `_ to see our PR review workflow. -If you made changes to the documentation, and want to render a local version, you can run the command ``sphinx-autobuild --ignore "*.zip" docs docs/_build`` to create a server to automatically rebuild the documentation when you make changes. +Here is a short overview of a few different commands that we use during development: + +.. code-block:: bash + # Run unit-tests + pytest + + # Run typechecking + mypy + + # Build the documentation. This launches a server, automatically rebuilt when changes are made + sphinx-autobuild --ignore "*.zip" docs docs/_build + Code guidelines ~~~~~~~~~~~~~~~ From 0f5fa2a31e52b338ae3e130bed4e4c5250d360cf Mon Sep 17 00:00:00 2001 From: Vecko <36369090+VeckoTheGecko@users.noreply.github.com> Date: Tue, 12 Nov 2024 18:40:14 +0800 Subject: [PATCH 02/35] Update dev environment to use py3.10 --- environment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/environment.yml b/environment.yml index bd5b4e4549..4d67401fa2 100644 --- a/environment.yml +++ b/environment.yml @@ -2,7 +2,7 @@ name: parcels channels: - conda-forge dependencies: - - python>=3.10 + - python=3.10 - cgen - ffmpeg>=3.2.3 - git From 48ef81853f1778a351d5b1631835de7111adbf3c Mon Sep 17 00:00:00 2001 From: Vecko <36369090+VeckoTheGecko@users.noreply.github.com> Date: Tue, 12 Nov 2024 19:57:49 +0800 Subject: [PATCH 03/35] Add initial asv config --- .github/workflows/benchmarks.yml | 76 +++++++++++ .gitignore | 1 + .pre-commit-config.yaml | 1 + asv_bench/asv.conf.json | 206 ++++++++++++++++++++++++++++++ asv_bench/benchmarks/README_CI.md | 122 ++++++++++++++++++ asv_bench/benchmarks/__init__.py | 0 6 files changed, 406 insertions(+) create mode 100644 .github/workflows/benchmarks.yml create mode 100644 asv_bench/asv.conf.json create mode 100644 asv_bench/benchmarks/README_CI.md create mode 100644 asv_bench/benchmarks/__init__.py diff --git a/.github/workflows/benchmarks.yml b/.github/workflows/benchmarks.yml new file mode 100644 index 0000000000..5c801b11be --- /dev/null +++ b/.github/workflows/benchmarks.yml @@ -0,0 +1,76 @@ +# This workflow was adapted from xarray. See licenses/XARRAY_LICENSE for license details. +name: Benchmark + +on: + pull_request: + types: [opened, reopened, synchronize, labeled] + workflow_dispatch: + +env: + PR_HEAD_LABEL: ${{ github.event.pull_request.head.label }} + +jobs: + benchmark: + if: ${{ contains( github.event.pull_request.labels.*.name, 'run-benchmark') && github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch' }} + name: Linux + runs-on: ubuntu-20.04 + env: + ASV_DIR: "./asv_bench" + CONDA_ENV_FILE: environment.yml + + steps: + # We need the full repo to avoid this issue + # https://github.com/actions/checkout/issues/23 + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up conda environment + uses: mamba-org/setup-micromamba@v2 + with: + environment-file: ${{env.CONDA_ENV_FILE}} + environment-name: xarray-tests + cache-environment: true + cache-environment-key: "${{runner.os}}-${{runner.arch}}-py${{env.PYTHON_VERSION}}-${{env.TODAY}}-${{hashFiles(env.CONDA_ENV_FILE)}}-benchmark" + # add "build" because of https://github.com/airspeed-velocity/asv/issues/1385 + create-args: >- + python-build + mamba + + - name: Run benchmarks + shell: bash -l {0} + id: benchmark + env: + OPENBLAS_NUM_THREADS: 1 + MKL_NUM_THREADS: 1 + OMP_NUM_THREADS: 1 + ASV_FACTOR: 1.5 + ASV_SKIP_SLOW: 1 + run: | + set -x + # ID this runner + asv machine --yes + echo "Baseline: ${{ github.event.pull_request.base.sha }} (${{ github.event.pull_request.base.label }})" + echo "Contender: ${GITHUB_SHA} ($PR_HEAD_LABEL)" + # Run benchmarks for current commit against base + ASV_OPTIONS="--split --show-stderr --factor $ASV_FACTOR" + asv continuous $ASV_OPTIONS ${{ github.event.pull_request.base.sha }} ${GITHUB_SHA} \ + | sed "/Traceback \|failed$\|PERFORMANCE DECREASED/ s/^/::error::/" \ + | tee benchmarks.log + # Report and export results for subsequent steps + if grep "Traceback \|failed\|PERFORMANCE DECREASED" benchmarks.log > /dev/null ; then + exit 1 + fi + working-directory: ${{ env.ASV_DIR }} + + - name: Add instructions to artifact + if: always() + run: | + cp benchmarks/README_CI.md benchmarks.log .asv/results/ + working-directory: ${{ env.ASV_DIR }} + + - uses: actions/upload-artifact@v4 + if: always() + with: + name: asv-benchmark-results-${{ runner.os }} + path: ${{ env.ASV_DIR }}/.asv/results diff --git a/.gitignore b/.gitignore index c34fabf59c..d4c9651bc2 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,4 @@ dist/parcels*.egg parcels/examples/particle*.png parcels/_version_setup.py /.pytest_cache/ +.asv diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 96f33d1339..5b1cf0d621 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,6 +9,7 @@ repos: - id: check-json types: [text] files: \.(json|ipynb)$ + exclude: ^asv_bench/asv\.conf\.json$ - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.7.2 hooks: diff --git a/asv_bench/asv.conf.json b/asv_bench/asv.conf.json new file mode 100644 index 0000000000..4d54dee595 --- /dev/null +++ b/asv_bench/asv.conf.json @@ -0,0 +1,206 @@ +{ + // The version of the config file format. Do not change, unless + // you know what you are doing. + "version": 1, + + // The name of the project being benchmarked + "project": "parcels", + + // The project's homepage + "project_url": "http://docs.oceanparcels.org/", + + // The URL or local path of the source code repository for the + // project being benchmarked + "repo": "..", + + // The Python project's subdirectory in your repo. If missing or + // the empty string, the project is assumed to be located at the root + // of the repository. + // "repo_subdir": "", + + // Customizable commands for building, installing, and + // uninstalling the project. See asv.conf.json documentation. + // + // "install_command": ["in-dir={env_dir} python -mpip install {wheel_file}"], + // "uninstall_command": ["return-code=any python -mpip uninstall -y {project}"], + // fix for bad builds + // https://github.com/airspeed-velocity/asv/issues/1389#issuecomment-2076131185 + "build_command": [ + "python -m build", + "python -mpip wheel --no-deps --no-build-isolation --no-index -w {build_cache_dir} {build_dir}" + ], + // List of branches to benchmark. If not provided, defaults to "master" + // (for git) or "default" (for mercurial). + // "branches": ["master"], // for git + // "branches": ["default"], // for mercurial + + // The DVCS being used. If not set, it will be automatically + // determined from "repo" by looking at the protocol in the URL + // (if remote), or by looking for special directories, such as + // ".git" (if local). + "dvcs": "git", + + // The tool to use to create environments. May be "conda", + // "virtualenv" or other value depending on the plugins in use. + // If missing or the empty string, the tool will be automatically + // determined by looking for tools on the PATH environment + // variable. + "environment_type": "mamba", + "conda_channels": ["conda-forge"], + + // timeout in seconds for installing any dependencies in environment + // defaults to 10 min + "install_timeout": 600, + + // the base URL to show a commit for the project. + "show_commit_url": "https://github.com/oceanparcels/parcels/commit/", + + // The Pythons you'd like to test against. If not provided, defaults + // to the current version of Python used to run `asv`. + "pythons": ["3.11"], + + // The list of conda channel names to be searched for benchmark + // dependency packages in the specified order + // "conda_channels": ["conda-forge", "defaults"], + + // A conda environment file that is used for environment creation. + // "conda_environment_file": "environment.yml", + + // The matrix of dependencies to test. Each key of the "req" + // requirements dictionary is the name of a package (in PyPI) and + // the values are version numbers. An empty list or empty string + // indicates to just test against the default (latest) + // version. null indicates that the package is to not be + // installed. If the package to be tested is only available from + // PyPi, and the 'environment_type' is conda, then you can preface + // the package name by 'pip+', and the package will be installed + // via pip (with all the conda available packages installed first, + // followed by the pip installed packages). + // + // The ``@env`` and ``@env_nobuild`` keys contain the matrix of + // environment variables to pass to build and benchmark commands. + // An environment will be created for every combination of the + // cartesian product of the "@env" variables in this matrix. + // Variables in "@env_nobuild" will be passed to every environment + // during the benchmark phase, but will not trigger creation of + // new environments. A value of ``null`` means that the variable + // will not be set for the current combination. + // + // "matrix": { + // "req": { + // "numpy": ["1.6", "1.7"], + // "six": ["", null], // test with and without six installed + // "pip+emcee": [""] // emcee is only available for install with pip. + // }, + // "env": {"ENV_VAR_1": ["val1", "val2"]}, + // "env_nobuild": {"ENV_VAR_2": ["val3", null]}, + // }, + + // using dependencies listed in parcels feedstock + "matrix": { + "setuptools_scm": [""], + "cftime": [""], + "cgen": [""], + "dask": [""], + "matplotlib-base": [""], + "netcdf4": [""], + "numpy": [""], + "platformdirs": [""], + "psutil": [""], + "pymbolic": [""], + "pytest": [""], + "scipy": [""], + "trajan": [""], + "tqdm": [""], + "xarray": [""], + "zarr": [""] + }, + + // Combinations of libraries/python versions can be excluded/included + // from the set to test. Each entry is a dictionary containing additional + // key-value pairs to include/exclude. + // + // An exclude entry excludes entries where all values match. The + // values are regexps that should match the whole string. + // + // An include entry adds an environment. Only the packages listed + // are installed. The 'python' key is required. The exclude rules + // do not apply to includes. + // + // In addition to package names, the following keys are available: + // + // - python + // Python version, as in the *pythons* variable above. + // - environment_type + // Environment type, as above. + // - sys_platform + // Platform, as in sys.platform. Possible values for the common + // cases: 'linux2', 'win32', 'cygwin', 'darwin'. + // - req + // Required packages + // - env + // Environment variables + // - env_nobuild + // Non-build environment variables + // + // "exclude": [ + // {"python": "3.2", "sys_platform": "win32"}, // skip py3.2 on windows + // {"environment_type": "conda", "req": {"six": null}}, // don't run without six on conda + // {"env": {"ENV_VAR_1": "val2"}}, // skip val2 for ENV_VAR_1 + // ], + // + // "include": [ + // // additional env for python2.7 + // {"python": "2.7", "req": {"numpy": "1.8"}, "env_nobuild": {"FOO": "123"}}, + // // additional env if run on windows+conda + // {"platform": "win32", "environment_type": "conda", "python": "2.7", "req": {"libpython": ""}}, + // ], + + // The directory (relative to the current directory) that benchmarks are + // stored in. If not provided, defaults to "benchmarks" + // "benchmark_dir": "benchmarks", + + // The directory (relative to the current directory) to cache the Python + // environments in. If not provided, defaults to "env" + "env_dir": ".asv/env", + + // The directory (relative to the current directory) that raw benchmark + // results are stored in. If not provided, defaults to "results". + "results_dir": ".asv/results", + + // The directory (relative to the current directory) that the html tree + // should be written to. If not provided, defaults to "html". + "html_dir": ".asv/html" + + // The number of characters to retain in the commit hashes. + // "hash_length": 8, + + // `asv` will cache results of the recent builds in each + // environment, making them faster to install next time. This is + // the number of builds to keep, per environment. + // "build_cache_size": 2, + + // The commits after which the regression search in `asv publish` + // should start looking for regressions. Dictionary whose keys are + // regexps matching to benchmark names, and values corresponding to + // the commit (exclusive) after which to start looking for + // regressions. The default is to start from the first commit + // with results. If the commit is `null`, regression detection is + // skipped for the matching benchmark. + // + // "regressions_first_commits": { + // "some_benchmark": "352cdf", // Consider regressions only after this commit + // "another_benchmark": null, // Skip regression detection altogether + // }, + + // The thresholds for relative change in results, after which `asv + // publish` starts reporting regressions. Dictionary of the same + // form as in ``regressions_first_commits``, with values + // indicating the thresholds. If multiple entries match, the + // maximum is taken. If no entry matches, the default is 5%. + // + // "regressions_thresholds": { + // "some_benchmark": 0.01, // Threshold of 1% + // "another_benchmark": 0.5, // Threshold of 50% + // }, +} diff --git a/asv_bench/benchmarks/README_CI.md b/asv_bench/benchmarks/README_CI.md new file mode 100644 index 0000000000..9c35e8a93b --- /dev/null +++ b/asv_bench/benchmarks/README_CI.md @@ -0,0 +1,122 @@ +# Benchmark CI + + + + + +## How it works + +The `asv` suite can be run for any PR on GitHub Actions (check workflow `.github/workflows/benchmarks.yml`) by adding a `run-benchmark` label to said PR. This will trigger a job that will run the benchmarking suite for the current PR head (merged commit) against the PR base (usually `main`). + +We use `asv continuous` to run the job, which runs a relative performance measurement. This means that there's no state to be saved and that regressions are only caught in terms of performance ratio (absolute numbers are available but they are not useful since we do not use stable hardware over time). `asv continuous` will: + +- Compile `scikit-image` for _both_ commits. We use `ccache` to speed up the process, and `mamba` is used to create the build environments. +- Run the benchmark suite for both commits, _twice_ (since `processes=2` by default). +- Generate a report table with performance ratios: + - `ratio=1.0` -> performance didn't change. + - `ratio<1.0` -> PR made it slower. + - `ratio>1.0` -> PR made it faster. + +Due to the sensitivity of the test, we cannot guarantee that false positives are not produced. In practice, values between `(0.7, 1.5)` are to be considered part of the measurement noise. When in doubt, running the benchmark suite one more time will provide more information about the test being a false positive or not. + +## Running the benchmarks on GitHub Actions + +1. On a PR, add the label `run-benchmark`. +2. The CI job will be started. Checks will appear in the usual dashboard panel above the comment box. +3. If more commits are added, the label checks will be grouped with the last commit checks _before_ you added the label. +4. Alternatively, you can always go to the `Actions` tab in the repo and [filter for `workflow:Benchmark`](https://github.com/scikit-image/scikit-image/actions?query=workflow%3ABenchmark). Your username will be assigned to the `actor` field, so you can also filter the results with that if you need it. + +## The artifacts + +The CI job will also generate an artifact. This is the `.asv/results` directory compressed in a zip file. Its contents include: + +- `fv-xxxxx-xx/`. A directory for the machine that ran the suite. It contains three files: + - `.json`, `.json`: the benchmark results for each commit, with stats. + - `machine.json`: details about the hardware. +- `benchmarks.json`: metadata about the current benchmark suite. +- `benchmarks.log`: the CI logs for this run. +- This README. + +## Re-running the analysis + +Although the CI logs should be enough to get an idea of what happened (check the table at the end), one can use `asv` to run the analysis routines again. + +1. Uncompress the artifact contents in the repo, under `.asv/results`. This is, you should see `.asv/results/benchmarks.log`, not `.asv/results/something_else/benchmarks.log`. Write down the machine directory name for later. +2. Run `asv show` to see your available results. You will see something like this: + +``` +$> asv show + +Commits with results: + +Machine : Jaimes-MBP +Environment: conda-py3.9-cython-numpy1.20-scipy + + 00875e67 + +Machine : fv-az95-499 +Environment: conda-py3.7-cython-numpy1.17-pooch-scipy + + 8db28f02 + 3a305096 +``` + +3. We are interested in the commits for `fv-az95-499` (the CI machine for this run). We can compare them with `asv compare` and some extra options. `--sort ratio` will show largest ratios first, instead of alphabetical order. `--split` will produce three tables: improved, worsened, no changes. `--factor 1.5` tells `asv` to only complain if deviations are above a 1.5 ratio. `-m` is used to indicate the machine ID (use the one you wrote down in step 1). Finally, specify your commit hashes: baseline first, then contender! + +``` +$> asv compare --sort ratio --split --factor 1.5 -m fv-az95-499 8db28f02 3a305096 + +Benchmarks that have stayed the same: + + before after ratio + [8db28f02] [3a305096] + + n/a n/a n/a benchmark_restoration.RollingBall.time_rollingball_ndim + 1.23±0.04ms 1.37±0.1ms 1.12 benchmark_transform_warp.WarpSuite.time_to_float64(, 128, 3) + 5.07±0.1μs 5.59±0.4μs 1.10 benchmark_transform_warp.ResizeLocalMeanSuite.time_resize_local_mean(, (192, 192, 192), (192, 192, 192)) + 1.23±0.02ms 1.33±0.1ms 1.08 benchmark_transform_warp.WarpSuite.time_same_type(, 128, 3) + 9.45±0.2ms 10.1±0.5ms 1.07 benchmark_rank.Rank3DSuite.time_3d_filters('majority', (32, 32, 32)) + 23.0±0.9ms 24.6±1ms 1.07 benchmark_interpolation.InterpolationResize.time_resize((80, 80, 80), 0, 'symmetric', , True) + 38.7±1ms 41.1±1ms 1.06 benchmark_transform_warp.ResizeLocalMeanSuite.time_resize_local_mean(, (2048, 2048), (192, 192, 192)) + 4.97±0.2μs 5.24±0.2μs 1.05 benchmark_transform_warp.ResizeLocalMeanSuite.time_resize_local_mean(, (2048, 2048), (2048, 2048)) + 4.21±0.2ms 4.42±0.3ms 1.05 benchmark_rank.Rank3DSuite.time_3d_filters('gradient', (32, 32, 32)) + +... +``` + +If you want more details on a specific test, you can use `asv show`. Use `-b pattern` to filter which tests to show, and then specify a commit hash to inspect: + +``` +$> asv show -b time_to_float64 8db28f02 + +Commit: 8db28f02 + +benchmark_transform_warp.WarpSuite.time_to_float64 [fv-az95-499/conda-py3.7-cython-numpy1.17-pooch-scipy] + ok + =============== ============= ========== ============= ========== ============ ========== ============ ========== ============ + -- N / order + --------------- -------------------------------------------------------------------------------------------------------------- + dtype_in 128 / 0 128 / 1 128 / 3 1024 / 0 1024 / 1 1024 / 3 4096 / 0 4096 / 1 4096 / 3 + =============== ============= ========== ============= ========== ============ ========== ============ ========== ============ + numpy.uint8 2.56±0.09ms 523±30μs 1.28±0.05ms 130±3ms 28.7±2ms 81.9±3ms 2.42±0.01s 659±5ms 1.48±0.01s + numpy.uint16 2.48±0.03ms 530±10μs 1.28±0.02ms 130±1ms 30.4±0.7ms 81.1±2ms 2.44±0s 653±3ms 1.47±0.02s + numpy.float32 2.59±0.1ms 518±20μs 1.27±0.01ms 127±3ms 26.6±1ms 74.8±2ms 2.50±0.01s 546±10ms 1.33±0.02s + numpy.float64 2.48±0.04ms 513±50μs 1.23±0.04ms 134±3ms 30.7±2ms 85.4±2ms 2.55±0.01s 632±4ms 1.45±0.01s + =============== ============= ========== ============= ========== ============ ========== ============ ========== ============ + started: 2021-07-06 06:14:36, duration: 1.99m +``` + +## Other details + +### Skipping slow or demanding tests + +To minimize the time required to run the full suite, we trimmed the parameter matrix in some cases and, in others, directly skipped tests that ran for too long or require too much memory. Unlike `pytest`, `asv` does not have a notion of marks. However, you can `raise NotImplementedError` in the setup step to skip a test. In that vein, a new private function is defined at `benchmarks.__init__`: `_skip_slow`. This will check if the `ASV_SKIP_SLOW` environment variable has been defined. If set to `1`, it will raise `NotImplementedError` and skip the test. To implement this behavior in other tests, you can add the following attribute: + +```python +from . import _skip_slow # this function is defined in benchmarks.__init__ + +def time_something_slow(): + pass + +time_something.setup = _skip_slow +``` diff --git a/asv_bench/benchmarks/__init__.py b/asv_bench/benchmarks/__init__.py new file mode 100644 index 0000000000..e69de29bb2 From fcf2ff3aaccfa6204d5dd66a4a1b8c8e19f9b7a7 Mon Sep 17 00:00:00 2001 From: Vecko <36369090+VeckoTheGecko@users.noreply.github.com> Date: Wed, 13 Nov 2024 12:29:13 +0800 Subject: [PATCH 04/35] patch patch asv config and add docs --- .gitignore | 5 ++++- asv_bench/asv.conf.json | 2 +- asv_bench/benchmarks/__init__.py | 19 +++++++++++++++++ asv_bench/benchmarks/benchmarks.py | 34 ++++++++++++++++++++++++++++++ docs/community/benchmarking.md | 22 +++++++++++++++++++ docs/community/index.rst | 1 + 6 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 asv_bench/benchmarks/benchmarks.py create mode 100644 docs/community/benchmarking.md diff --git a/.gitignore b/.gitignore index d4c9651bc2..26f8a5a2e5 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,10 @@ lib/ bin/ parcels_examples +# asv environments +asv_bench/.asv +asv_bench/pkgs + *.so *.log *.nc @@ -31,4 +35,3 @@ dist/parcels*.egg parcels/examples/particle*.png parcels/_version_setup.py /.pytest_cache/ -.asv diff --git a/asv_bench/asv.conf.json b/asv_bench/asv.conf.json index 4d54dee595..bc57809c61 100644 --- a/asv_bench/asv.conf.json +++ b/asv_bench/asv.conf.json @@ -31,7 +31,7 @@ ], // List of branches to benchmark. If not provided, defaults to "master" // (for git) or "default" (for mercurial). - // "branches": ["master"], // for git + "branches": ["master"], // for git // "branches": ["default"], // for mercurial // The DVCS being used. If not set, it will be automatically diff --git a/asv_bench/benchmarks/__init__.py b/asv_bench/benchmarks/__init__.py index e69de29bb2..e91191dd19 100644 --- a/asv_bench/benchmarks/__init__.py +++ b/asv_bench/benchmarks/__init__.py @@ -0,0 +1,19 @@ +import os + + +def _skip_slow(): + """ + Use this function to skip slow or highly demanding tests. + + Use it as a `Class.setup` method or a `function.setup` attribute. + + Examples + -------- + >>> from . import _skip_slow + >>> def time_something_slow(): + ... pass + ... + >>> time_something.setup = _skip_slow + """ + if os.environ.get("ASV_SKIP_SLOW", "0") == "1": + raise NotImplementedError("Skipping this test...") diff --git a/asv_bench/benchmarks/benchmarks.py b/asv_bench/benchmarks/benchmarks.py new file mode 100644 index 0000000000..b21672ae4a --- /dev/null +++ b/asv_bench/benchmarks/benchmarks.py @@ -0,0 +1,34 @@ +# Write the benchmarking functions here. +# See "Writing benchmarks" in the asv docs for more information. + +# TODO: Write some benchmarks for parcels + + +class ExampleTimeSuite: + """ + An example benchmark that times the performance of various kinds + of iterating over dictionaries in Python. + """ + + def setup(self): + self.d = {} + for x in range(500): + self.d[x] = None + + def time_keys(self): + for _ in self.d.keys(): + pass + + def time_values(self): + for _ in self.d.values(): + pass + + def time_range(self): + d = self.d + for key in range(500): + _ = d[key] + + +class ExampleMemSuite: + def mem_list(self): + return [0] * 256 diff --git a/docs/community/benchmarking.md b/docs/community/benchmarking.md new file mode 100644 index 0000000000..d3b9e79921 --- /dev/null +++ b/docs/community/benchmarking.md @@ -0,0 +1,22 @@ +# Benchmarking + +Parcels comes with an [asv](https://asv.readthedocs.io/en/latest/) benchmarking suite to monitor the performance of the project over it's lifespan. + +The benchmarking is run in CI using GitHub Actions (similar to other projects like xarray, scikit-image, and pandas), using a ratio to determine performance regressions instead of raw outputs. More on the reliability of benchmarking in CI can be seen at [this blog post](https://labs.quansight.org/blog/2021/08/github-actions-benchmarks). Due to the reliance of CI for benchmarking, the benchmarks are small such that they test the core functionality of Parcels. Large scale simulation benchmarking is an avenue for future development. + +## Setup + +The asv benchmarks require these dependencies: + +`conda install -c conda-forge asv>0.6 libmambapy<2 conda-build` + +Progress on compatibility of asv with libmambapy 2 is documented at [this issue](https://github.com/airspeed-velocity/asv/issues/1438). + +## Running the benchmarks + +The benchmarks can be run locally using the following command: + +```bash +asv run + +``` diff --git a/docs/community/index.rst b/docs/community/index.rst index 3121d6cab9..c51561bdeb 100644 --- a/docs/community/index.rst +++ b/docs/community/index.rst @@ -18,3 +18,4 @@ See the sections in the primary sidebar and below to explore. :maxdepth: 1 maintainer + benchmarks From f8a37ef8d59a3e87a8d6dbf91878bda5ce3ceafd Mon Sep 17 00:00:00 2001 From: Vecko <36369090+VeckoTheGecko@users.noreply.github.com> Date: Wed, 13 Nov 2024 12:59:30 +0800 Subject: [PATCH 05/35] patch benchmarks ci --- .github/workflows/benchmarks.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/benchmarks.yml b/.github/workflows/benchmarks.yml index 5c801b11be..df9c5a11a1 100644 --- a/.github/workflows/benchmarks.yml +++ b/.github/workflows/benchmarks.yml @@ -28,14 +28,16 @@ jobs: - name: Set up conda environment uses: mamba-org/setup-micromamba@v2 with: + micromamba-version: "1.5.10-0" environment-file: ${{env.CONDA_ENV_FILE}} - environment-name: xarray-tests + environment-name: parcels-dev cache-environment: true cache-environment-key: "${{runner.os}}-${{runner.arch}}-py${{env.PYTHON_VERSION}}-${{env.TODAY}}-${{hashFiles(env.CONDA_ENV_FILE)}}-benchmark" # add "build" because of https://github.com/airspeed-velocity/asv/issues/1385 create-args: >- python-build - mamba + asv + mamba<=1.5.10 - name: Run benchmarks shell: bash -l {0} From 6d25f40258089794c98c36963638b634ca2a73c9 Mon Sep 17 00:00:00 2001 From: Vecko <36369090+VeckoTheGecko@users.noreply.github.com> Date: Wed, 13 Nov 2024 13:22:30 +0800 Subject: [PATCH 06/35] Revert "Update dev environment to use py3.10" This reverts commit 0f5fa2a31e52b338ae3e130bed4e4c5250d360cf. --- environment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/environment.yml b/environment.yml index 4d67401fa2..bd5b4e4549 100644 --- a/environment.yml +++ b/environment.yml @@ -2,7 +2,7 @@ name: parcels channels: - conda-forge dependencies: - - python=3.10 + - python>=3.10 - cgen - ffmpeg>=3.2.3 - git From 6a6b8bf749befc8c4c9fe74970be3e5132030b33 Mon Sep 17 00:00:00 2001 From: Vecko <36369090+VeckoTheGecko@users.noreply.github.com> Date: Wed, 13 Nov 2024 13:29:11 +0800 Subject: [PATCH 07/35] Update docs --- docs/community/benchmarking.md | 4 ++-- docs/community/contributing.rst | 3 +++ docs/community/index.rst | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/community/benchmarking.md b/docs/community/benchmarking.md index d3b9e79921..32b074c3f4 100644 --- a/docs/community/benchmarking.md +++ b/docs/community/benchmarking.md @@ -14,9 +14,9 @@ Progress on compatibility of asv with libmambapy 2 is documented at [this issue] ## Running the benchmarks -The benchmarks can be run locally using the following command: +The benchmarks are located in the `asv_bench` folder can be run locally using the following commands: ```bash +cd asv_bench asv run - ``` diff --git a/docs/community/contributing.rst b/docs/community/contributing.rst index e68b6f5742..c61da113ce 100644 --- a/docs/community/contributing.rst +++ b/docs/community/contributing.rst @@ -77,6 +77,7 @@ From there: Here is a short overview of a few different commands that we use during development: .. code-block:: bash + # Run unit-tests pytest @@ -87,6 +88,8 @@ Here is a short overview of a few different commands that we use during developm sphinx-autobuild --ignore "*.zip" docs docs/_build +To run performance benchmarks, see the `the benchmarking page `_. + Code guidelines ~~~~~~~~~~~~~~~ diff --git a/docs/community/index.rst b/docs/community/index.rst index c51561bdeb..0ccdd3bb12 100644 --- a/docs/community/index.rst +++ b/docs/community/index.rst @@ -18,4 +18,4 @@ See the sections in the primary sidebar and below to explore. :maxdepth: 1 maintainer - benchmarks + benchmarking From e659f4058b0d21d2c0d5b9724e3193dff5c3d474 Mon Sep 17 00:00:00 2001 From: Willi Rath Date: Tue, 11 Feb 2025 16:31:27 +0100 Subject: [PATCH 08/35] Add 3d advection benchmark --- asv_bench/benchmarks/benchmarks.py | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/asv_bench/benchmarks/benchmarks.py b/asv_bench/benchmarks/benchmarks.py index b21672ae4a..cf6e2896a2 100644 --- a/asv_bench/benchmarks/benchmarks.py +++ b/asv_bench/benchmarks/benchmarks.py @@ -4,6 +4,41 @@ # TODO: Write some benchmarks for parcels +import numpy as np +from parcels import FieldSet, ParticleSet, AdvectionRK4, ScipyParticle +from datetime import timedelta + + +class Advection3D: + """Benchmark running the Parcels brownian motion example.""" + + def time_run_whole_example(self): + """Flat 2D zonal flow that increases linearly with depth from 0 m/s to 1 m/s.""" + xdim = ydim = zdim = 2 + npart = 11 + dimensions = { + "lon": np.linspace(0.0, 1e4, xdim, dtype=np.float32), + "lat": np.linspace(0.0, 1e4, ydim, dtype=np.float32), + "depth": np.linspace(0.0, 1.0, zdim, dtype=np.float32), + } + data = { + "U": np.ones((xdim, ydim, zdim), dtype=np.float32), + "V": np.zeros((xdim, ydim, zdim), dtype=np.float32), + } + data["U"][:, :, 0] = 0.0 + fieldset = FieldSet.from_data(data, dimensions, mesh="flat", transpose=True) + + pset = ParticleSet( + fieldset, + pclass=ScipyParticle, + lon=np.zeros(npart), + lat=np.zeros(npart) + 1e2, + depth=np.linspace(0, 1, npart), + ) + pset.execute(AdvectionRK4, runtime=timedelta(hours=2), dt=timedelta(seconds=30)) + assert np.allclose(pset.depth * pset.time, pset.lon, atol=1.0e-1) + + class ExampleTimeSuite: """ An example benchmark that times the performance of various kinds From 60c7d5500531160493dee7b457521ad4ac1a02ad Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 11 Feb 2025 15:31:58 +0000 Subject: [PATCH 09/35] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- asv_bench/benchmarks/benchmarks.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/asv_bench/benchmarks/benchmarks.py b/asv_bench/benchmarks/benchmarks.py index cf6e2896a2..897c3b1396 100644 --- a/asv_bench/benchmarks/benchmarks.py +++ b/asv_bench/benchmarks/benchmarks.py @@ -4,10 +4,12 @@ # TODO: Write some benchmarks for parcels -import numpy as np -from parcels import FieldSet, ParticleSet, AdvectionRK4, ScipyParticle from datetime import timedelta +import numpy as np + +from parcels import AdvectionRK4, FieldSet, ParticleSet, ScipyParticle + class Advection3D: """Benchmark running the Parcels brownian motion example.""" From 84b8c2673184ef82531a5651e9ae73bb22b5fdcc Mon Sep 17 00:00:00 2001 From: Willi Rath Date: Wed, 12 Feb 2025 10:58:07 +0100 Subject: [PATCH 10/35] Add integration test benchmarks --- asv_bench/benchmarks/benchmarks.py | 71 ------------------- .../benchmarks/benchmarks_integration.py | 67 +++++++++++++++++ 2 files changed, 67 insertions(+), 71 deletions(-) delete mode 100644 asv_bench/benchmarks/benchmarks.py create mode 100644 asv_bench/benchmarks/benchmarks_integration.py diff --git a/asv_bench/benchmarks/benchmarks.py b/asv_bench/benchmarks/benchmarks.py deleted file mode 100644 index 897c3b1396..0000000000 --- a/asv_bench/benchmarks/benchmarks.py +++ /dev/null @@ -1,71 +0,0 @@ -# Write the benchmarking functions here. -# See "Writing benchmarks" in the asv docs for more information. - -# TODO: Write some benchmarks for parcels - - -from datetime import timedelta - -import numpy as np - -from parcels import AdvectionRK4, FieldSet, ParticleSet, ScipyParticle - - -class Advection3D: - """Benchmark running the Parcels brownian motion example.""" - - def time_run_whole_example(self): - """Flat 2D zonal flow that increases linearly with depth from 0 m/s to 1 m/s.""" - xdim = ydim = zdim = 2 - npart = 11 - dimensions = { - "lon": np.linspace(0.0, 1e4, xdim, dtype=np.float32), - "lat": np.linspace(0.0, 1e4, ydim, dtype=np.float32), - "depth": np.linspace(0.0, 1.0, zdim, dtype=np.float32), - } - data = { - "U": np.ones((xdim, ydim, zdim), dtype=np.float32), - "V": np.zeros((xdim, ydim, zdim), dtype=np.float32), - } - data["U"][:, :, 0] = 0.0 - fieldset = FieldSet.from_data(data, dimensions, mesh="flat", transpose=True) - - pset = ParticleSet( - fieldset, - pclass=ScipyParticle, - lon=np.zeros(npart), - lat=np.zeros(npart) + 1e2, - depth=np.linspace(0, 1, npart), - ) - pset.execute(AdvectionRK4, runtime=timedelta(hours=2), dt=timedelta(seconds=30)) - assert np.allclose(pset.depth * pset.time, pset.lon, atol=1.0e-1) - - -class ExampleTimeSuite: - """ - An example benchmark that times the performance of various kinds - of iterating over dictionaries in Python. - """ - - def setup(self): - self.d = {} - for x in range(500): - self.d[x] = None - - def time_keys(self): - for _ in self.d.keys(): - pass - - def time_values(self): - for _ in self.d.values(): - pass - - def time_range(self): - d = self.d - for key in range(500): - _ = d[key] - - -class ExampleMemSuite: - def mem_list(self): - return [0] * 256 diff --git a/asv_bench/benchmarks/benchmarks_integration.py b/asv_bench/benchmarks/benchmarks_integration.py new file mode 100644 index 0000000000..130e881649 --- /dev/null +++ b/asv_bench/benchmarks/benchmarks_integration.py @@ -0,0 +1,67 @@ +from parcels import AdvectionRK4, FieldSet, ParticleSet, ScipyParticle +import numpy as np +from datetime import timedelta + + +def time_advection2d(): + """Flat 2D zonal flow that increases linearly with depth from 0 m/s to 1 m/s. + + Time-taking variant. + """ + xdim = ydim = zdim = 2 + npart = 11 + + dimensions = { + "lon": np.linspace(0.0, 1e4, xdim, dtype=np.float32), + "lat": np.linspace(0.0, 1e4, ydim, dtype=np.float32), + "depth": np.linspace(0.0, 1.0, zdim, dtype=np.float32), + } + data = { + "U": np.ones((xdim, ydim, zdim), dtype=np.float32), + "V": np.zeros((xdim, ydim, zdim), dtype=np.float32), + } + data["U"][:, :, 0] = 0.0 + fieldset = FieldSet.from_data(data, dimensions, mesh="flat", transpose=True) + + pset = ParticleSet( + fieldset, + pclass=ScipyParticle, + lon=np.zeros(npart), + lat=np.zeros(npart) + 1e2, + depth=np.linspace(0, 1, npart), + ) + + pset.execute(AdvectionRK4, runtime=timedelta(hours=2), dt=timedelta(seconds=30)) + assert np.allclose(pset.depth * pset.time, pset.lon, atol=1.0e-1) + + +def peakmem_advection2d(): + """Flat 2D zonal flow that increases linearly with depth from 0 m/s to 1 m/s. + + Peak-Mem-taking variant. + """ + xdim = ydim = zdim = 2 + npart = 11 + + dimensions = { + "lon": np.linspace(0.0, 1e4, xdim, dtype=np.float32), + "lat": np.linspace(0.0, 1e4, ydim, dtype=np.float32), + "depth": np.linspace(0.0, 1.0, zdim, dtype=np.float32), + } + data = { + "U": np.ones((xdim, ydim, zdim), dtype=np.float32), + "V": np.zeros((xdim, ydim, zdim), dtype=np.float32), + } + data["U"][:, :, 0] = 0.0 + fieldset = FieldSet.from_data(data, dimensions, mesh="flat", transpose=True) + + pset = ParticleSet( + fieldset, + pclass=ScipyParticle, + lon=np.zeros(npart), + lat=np.zeros(npart) + 1e2, + depth=np.linspace(0, 1, npart), + ) + + pset.execute(AdvectionRK4, runtime=timedelta(hours=2), dt=timedelta(seconds=30)) + assert np.allclose(pset.depth * pset.time, pset.lon, atol=1.0e-1) From 5c41d41b115f7b9ea832d30b7c2e9a666f2f0587 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Feb 2025 10:00:04 +0000 Subject: [PATCH 11/35] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- asv_bench/benchmarks/benchmarks_integration.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/asv_bench/benchmarks/benchmarks_integration.py b/asv_bench/benchmarks/benchmarks_integration.py index 130e881649..27c9540b7e 100644 --- a/asv_bench/benchmarks/benchmarks_integration.py +++ b/asv_bench/benchmarks/benchmarks_integration.py @@ -1,7 +1,9 @@ -from parcels import AdvectionRK4, FieldSet, ParticleSet, ScipyParticle -import numpy as np from datetime import timedelta +import numpy as np + +from parcels import AdvectionRK4, FieldSet, ParticleSet, ScipyParticle + def time_advection2d(): """Flat 2D zonal flow that increases linearly with depth from 0 m/s to 1 m/s. From 6541d284b48f3e65261d77fe1d5c6025101858bd Mon Sep 17 00:00:00 2001 From: Willi Rath Date: Wed, 12 Feb 2025 11:13:30 +0100 Subject: [PATCH 12/35] Add particle execution timings --- .../benchmarks_particle_execution.py | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 asv_bench/benchmarks/benchmarks_particle_execution.py diff --git a/asv_bench/benchmarks/benchmarks_particle_execution.py b/asv_bench/benchmarks/benchmarks_particle_execution.py new file mode 100644 index 0000000000..ed44266179 --- /dev/null +++ b/asv_bench/benchmarks/benchmarks_particle_execution.py @@ -0,0 +1,77 @@ +from parcels import AdvectionRK4, FieldSet, ParticleSet, ScipyParticle, JITParticle +import numpy as np +from datetime import timedelta + + +class ParticleExecutionJIT: + def setup(self): + xdim = ydim = zdim = 2 + npart = 1_000 + + dimensions = { + "lon": np.linspace(0.0, 1e4, xdim, dtype=np.float32), + "lat": np.linspace(0.0, 1e4, ydim, dtype=np.float32), + "depth": np.linspace(0.0, 1.0, zdim, dtype=np.float32), + } + data = { + "U": np.ones((xdim, ydim, zdim), dtype=np.float32), + "V": np.zeros((xdim, ydim, zdim), dtype=np.float32), + } + data["U"][:, :, 0] = 0.0 + fieldset = FieldSet.from_data(data, dimensions, mesh="flat", transpose=True) + + self.pset = ParticleSet( + fieldset, + pclass=JITParticle, + lon=np.zeros(npart), + lat=np.zeros(npart) + 1e2, + depth=np.linspace(0, 1, npart), + ) + # trigger compilation + self.pset.execute(AdvectionRK4, runtime=0, dt=timedelta(seconds=30)) + + def time_run_single_timestep(self): + self.pset.execute( + AdvectionRK4, runtime=timedelta(seconds=1 * 30), dt=timedelta(seconds=30) + ) + + def time_run_many_timesteps(self): + self.pset.execute( + AdvectionRK4, runtime=timedelta(seconds=100 * 30), dt=timedelta(seconds=30) + ) + + +class ParticleExecutionScipy: + def setup(self): + xdim = ydim = zdim = 2 + npart = 1_000 + + dimensions = { + "lon": np.linspace(0.0, 1e4, xdim, dtype=np.float32), + "lat": np.linspace(0.0, 1e4, ydim, dtype=np.float32), + "depth": np.linspace(0.0, 1.0, zdim, dtype=np.float32), + } + data = { + "U": np.ones((xdim, ydim, zdim), dtype=np.float32), + "V": np.zeros((xdim, ydim, zdim), dtype=np.float32), + } + data["U"][:, :, 0] = 0.0 + fieldset = FieldSet.from_data(data, dimensions, mesh="flat", transpose=True) + + self.pset = ParticleSet( + fieldset, + pclass=ScipyParticle, + lon=np.zeros(npart), + lat=np.zeros(npart) + 1e2, + depth=np.linspace(0, 1, npart), + ) + + def time_run_single_timestep(self): + self.pset.execute( + AdvectionRK4, runtime=timedelta(seconds=1 * 30), dt=timedelta(seconds=30) + ) + + def time_run_many_timesteps(self): + self.pset.execute( + AdvectionRK4, runtime=timedelta(seconds=100 * 30), dt=timedelta(seconds=30) + ) From ce2a540a10c5eeb41105ffe0785ee1615082b5ca Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Feb 2025 10:13:55 +0000 Subject: [PATCH 13/35] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../benchmarks_particle_execution.py | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/asv_bench/benchmarks/benchmarks_particle_execution.py b/asv_bench/benchmarks/benchmarks_particle_execution.py index ed44266179..19d3244b10 100644 --- a/asv_bench/benchmarks/benchmarks_particle_execution.py +++ b/asv_bench/benchmarks/benchmarks_particle_execution.py @@ -1,7 +1,9 @@ -from parcels import AdvectionRK4, FieldSet, ParticleSet, ScipyParticle, JITParticle -import numpy as np from datetime import timedelta +import numpy as np + +from parcels import AdvectionRK4, FieldSet, JITParticle, ParticleSet, ScipyParticle + class ParticleExecutionJIT: def setup(self): @@ -31,14 +33,10 @@ def setup(self): self.pset.execute(AdvectionRK4, runtime=0, dt=timedelta(seconds=30)) def time_run_single_timestep(self): - self.pset.execute( - AdvectionRK4, runtime=timedelta(seconds=1 * 30), dt=timedelta(seconds=30) - ) + self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=1 * 30), dt=timedelta(seconds=30)) def time_run_many_timesteps(self): - self.pset.execute( - AdvectionRK4, runtime=timedelta(seconds=100 * 30), dt=timedelta(seconds=30) - ) + self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=100 * 30), dt=timedelta(seconds=30)) class ParticleExecutionScipy: @@ -67,11 +65,7 @@ def setup(self): ) def time_run_single_timestep(self): - self.pset.execute( - AdvectionRK4, runtime=timedelta(seconds=1 * 30), dt=timedelta(seconds=30) - ) + self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=1 * 30), dt=timedelta(seconds=30)) def time_run_many_timesteps(self): - self.pset.execute( - AdvectionRK4, runtime=timedelta(seconds=100 * 30), dt=timedelta(seconds=30) - ) + self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=100 * 30), dt=timedelta(seconds=30)) From c5d0f2f43fd07f1eb98447073d4c4ddf8754a539 Mon Sep 17 00:00:00 2001 From: Lizarbe Date: Tue, 18 Feb 2025 12:33:28 +0100 Subject: [PATCH 14/35] ArgoFloat benchmark --- asv_bench/benchmarks/Argofloat_benchmark.py | 104 ++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 asv_bench/benchmarks/Argofloat_benchmark.py diff --git a/asv_bench/benchmarks/Argofloat_benchmark.py b/asv_bench/benchmarks/Argofloat_benchmark.py new file mode 100644 index 0000000000..a31c9261c9 --- /dev/null +++ b/asv_bench/benchmarks/Argofloat_benchmark.py @@ -0,0 +1,104 @@ +## Argo float benchmark + +from datetime import timedelta + +import numpy as np + +from parcels import AdvectionRK4, FieldSet, JITParticle, ParticleSet, ScipyParticle, StatusCode, Variable + +def ArgoVerticalMovement(particle, fieldset, time): + driftdepth = 1000 # maximum depth in m + maxdepth = 2000 # maximum depth in m + vertical_speed = 0.10 # sink and rise speed in m/s + cycletime = 10 * 86400 # total time of cycle in seconds + drifttime = 9 * 86400 # time of deep drift in seconds + + if particle.cycle_phase == 0: + # Phase 0: Sinking with vertical_speed until depth is driftdepth + particle_ddepth += vertical_speed * particle.dt + if particle.depth + particle_ddepth >= driftdepth: + particle_ddepth = driftdepth - particle.depth + particle.cycle_phase = 1 + + elif particle.cycle_phase == 1: + # Phase 1: Drifting at depth for drifttime seconds + particle.drift_age += particle.dt + if particle.drift_age >= drifttime: + particle.drift_age = 0 # reset drift_age for next cycle + particle.cycle_phase = 2 + + elif particle.cycle_phase == 2: + # Phase 2: Sinking further to maxdepth + particle_ddepth += vertical_speed * particle.dt + if particle.depth + particle_ddepth >= maxdepth: + particle_ddepth = maxdepth - particle.depth + particle.cycle_phase = 3 + + elif particle.cycle_phase == 3: + # Phase 3: Rising with vertical_speed until at surface + particle_ddepth -= vertical_speed * particle.dt + # particle.temp = fieldset.temp[time, particle.depth, particle.lat, particle.lon] # if fieldset has temperature + if particle.depth + particle_ddepth <= fieldset.mindepth: + particle_ddepth = fieldset.mindepth - particle.depth + # particle.temp = 0./0. # reset temperature to NaN at end of sampling cycle + particle.cycle_phase = 4 + + elif particle.cycle_phase == 4: + # Phase 4: Transmitting at surface until cycletime is reached + if particle.cycle_age > cycletime: + particle.cycle_phase = 0 + particle.cycle_age = 0 + + if particle.state == StatusCode.Evaluate: + particle.cycle_age += particle.dt # update cycle_age + +class ArgoFloatJIT: + def setup(self): + xdim = ydim = zdim = 2 + + dimensions = { + "lon": "lon", + "lat": "lat", + "depth": "depth", + } + data = { + "U": np.ones((xdim, ydim, zdim), dtype=np.float32), + "V": np.zeros((xdim, ydim, zdim), dtype=np.float32), + } + data["U"][:, :, 0] = 0.0 + fieldset = FieldSet.from_data(data, dimensions, mesh="flat", transpose=True) + fieldset.mindepth = fieldset.U.depth[0] + + # Define a new Particle type including extra Variables + self.ArgoParticle = JITParticle.add_variables( + [ + # Phase of cycle: + # init_descend=0, + # drift=1, + # profile_descend=2, + # profile_ascend=3, + # transmit=4 + Variable("cycle_phase", dtype=np.int32, initial=0.0), + Variable("cycle_age", dtype=np.float32, initial=0.0), + Variable("drift_age", dtype=np.float32, initial=0.0), + # if fieldset has temperature + # Variable('temp', dtype=np.float32, initial=np.nan), + ] + ) + + self.pset=ParticleSet( + fieldset=fieldset, + pclass=ArgoParticle, + lon=[0], + lat=[0], + depth=[0] + ) + + # combine Argo vertical movement kernel with built-in Advection kernel + self.kernels = [ArgoVerticalMovement, AdvectionRK4] + + def time_run_single_timestep(self): + self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=1 * 30), dt=timedelta(seconds=30)) + + + From 51bf80adc6ae26d4756267d80dd090854087ce78 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 18 Feb 2025 11:35:20 +0000 Subject: [PATCH 15/35] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- asv_bench/benchmarks/Argofloat_benchmark.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/asv_bench/benchmarks/Argofloat_benchmark.py b/asv_bench/benchmarks/Argofloat_benchmark.py index a31c9261c9..17b525f1f4 100644 --- a/asv_bench/benchmarks/Argofloat_benchmark.py +++ b/asv_bench/benchmarks/Argofloat_benchmark.py @@ -4,9 +4,10 @@ import numpy as np -from parcels import AdvectionRK4, FieldSet, JITParticle, ParticleSet, ScipyParticle, StatusCode, Variable +from parcels import AdvectionRK4, FieldSet, JITParticle, ParticleSet, StatusCode, Variable -def ArgoVerticalMovement(particle, fieldset, time): + +def ArgoVerticalMovement(particle, fieldset, time): driftdepth = 1000 # maximum depth in m maxdepth = 2000 # maximum depth in m vertical_speed = 0.10 # sink and rise speed in m/s @@ -52,6 +53,7 @@ def ArgoVerticalMovement(particle, fieldset, time): if particle.state == StatusCode.Evaluate: particle.cycle_age += particle.dt # update cycle_age + class ArgoFloatJIT: def setup(self): xdim = ydim = zdim = 2 @@ -86,19 +88,10 @@ def setup(self): ] ) - self.pset=ParticleSet( - fieldset=fieldset, - pclass=ArgoParticle, - lon=[0], - lat=[0], - depth=[0] - ) + self.pset = ParticleSet(fieldset=fieldset, pclass=ArgoParticle, lon=[0], lat=[0], depth=[0]) # combine Argo vertical movement kernel with built-in Advection kernel self.kernels = [ArgoVerticalMovement, AdvectionRK4] - + def time_run_single_timestep(self): self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=1 * 30), dt=timedelta(seconds=30)) - - - From 7bbdcd65a0fe5340a70c5003582006cd4312e173 Mon Sep 17 00:00:00 2001 From: Lizarbe Date: Wed, 26 Feb 2025 11:31:05 +0100 Subject: [PATCH 16/35] Argo fixed --- ...at_benchmark.py => benchmark_argofloat.py} | 82 ++++++++++++------- 1 file changed, 53 insertions(+), 29 deletions(-) rename asv_bench/benchmarks/{Argofloat_benchmark.py => benchmark_argofloat.py} (55%) diff --git a/asv_bench/benchmarks/Argofloat_benchmark.py b/asv_bench/benchmarks/benchmark_argofloat.py similarity index 55% rename from asv_bench/benchmarks/Argofloat_benchmark.py rename to asv_bench/benchmarks/benchmark_argofloat.py index 17b525f1f4..6843423ffb 100644 --- a/asv_bench/benchmarks/Argofloat_benchmark.py +++ b/asv_bench/benchmarks/benchmark_argofloat.py @@ -1,9 +1,8 @@ -## Argo float benchmark - from datetime import timedelta import numpy as np - +import pandas as pd +import xarray as xr from parcels import AdvectionRK4, FieldSet, JITParticle, ParticleSet, StatusCode, Variable @@ -56,42 +55,67 @@ def ArgoVerticalMovement(particle, fieldset, time): class ArgoFloatJIT: def setup(self): - xdim = ydim = zdim = 2 + time = pd.date_range(start="2025-01-01", end="2025-02-16", freq="D") + lon = np.linspace(-180,180,120) + lat = np.linspace(-90,90,100) - dimensions = { - "lon": "lon", - "lat": "lat", - "depth": "depth", - } - data = { - "U": np.ones((xdim, ydim, zdim), dtype=np.float32), - "V": np.zeros((xdim, ydim, zdim), dtype=np.float32), + Lon,Lat = np.meshgrid(lon,lat) + + # Create large-scale gyre flow + U_gyre = np.cos(np.radians(Lat)) * np.sin(np.radians(Lon)) # Zonal flow + V_gyre = -np.sin(np.radians(Lat)) * np.cos(np.radians(Lon)) # Meridional flow + + f = 2 * 7.2921e-5 * np.sin(np.radians(Lat)) + + U_coriolis = U_gyre * (1 - 0.5 * np.abs(f)) + V_coriolis = V_gyre * (1 - 0.5 * np.abs(f)) + + noise_level = 0.1 # Adjust for more or less variability + U_noise = noise_level * np.random.randn(*U_coriolis.shape) + V_noise = noise_level * np.random.randn(*V_coriolis.shape) + + # Final realistic U and V velocity fields + U_final = U_coriolis + U_noise + V_final = V_coriolis + V_noise + + depth = np.linspace(0,2000,100) + + U_val = np.tile(U_final[None,None, :, :], (len(time), len(depth),1, 1)) # Repeat for each time step + V_val = np.tile(V_final[None,None, :, :], (len(time), len(depth),1, 1)) + + U = xr.DataArray(U_val, + dims = ['time','depth','lat','lon'], + coords = {'time':time, 'depth':depth,'lat':lat, 'lon':lon}, + name='U_velocity') + + V = xr.DataArray(V_val, + dims = ['time','depth','lat','lon'], + coords = {'time':time, 'depth':depth,'lat':lat, 'lon':lon}, + name='V_velocity') + + ds = xr.Dataset({"U":U, "V":V}) + + variables = { + "U": "U", + "V": "V", } - data["U"][:, :, 0] = 0.0 - fieldset = FieldSet.from_data(data, dimensions, mesh="flat", transpose=True) + dimensions = {"lat": "lat", "lon": "lon", "time": "time", "depth":"depth"} + fieldset = FieldSet.from_xarray_dataset(ds, variables, dimensions) + # uppermost layer in the hydrodynamic data fieldset.mindepth = fieldset.U.depth[0] - # Define a new Particle type including extra Variables - self.ArgoParticle = JITParticle.add_variables( + ArgoParticle = JITParticle.add_variables( [ - # Phase of cycle: - # init_descend=0, - # drift=1, - # profile_descend=2, - # profile_ascend=3, - # transmit=4 Variable("cycle_phase", dtype=np.int32, initial=0.0), Variable("cycle_age", dtype=np.float32, initial=0.0), Variable("drift_age", dtype=np.float32, initial=0.0), - # if fieldset has temperature - # Variable('temp', dtype=np.float32, initial=np.nan), ] ) - self.pset = ParticleSet(fieldset=fieldset, pclass=ArgoParticle, lon=[0], lat=[0], depth=[0]) + self.pset = ParticleSet(fieldset=fieldset, pclass=ArgoParticle, lon=[32], lat=[-31], depth=[0]) - # combine Argo vertical movement kernel with built-in Advection kernel - self.kernels = [ArgoVerticalMovement, AdvectionRK4] + + def time_run_many_timesteps(self): + self.pset.execute([ArgoVerticalMovement, AdvectionRK4], runtime=timedelta(seconds=1 * 30), dt=timedelta(seconds=30)) - def time_run_single_timestep(self): - self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=1 * 30), dt=timedelta(seconds=30)) + From 62cb3be000a6fe67dcc91dfd61c2496399f9534f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Feb 2025 10:37:38 +0000 Subject: [PATCH 17/35] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- asv_bench/benchmarks/benchmark_argofloat.py | 46 +++++++++++---------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/asv_bench/benchmarks/benchmark_argofloat.py b/asv_bench/benchmarks/benchmark_argofloat.py index 6843423ffb..3b9a165713 100644 --- a/asv_bench/benchmarks/benchmark_argofloat.py +++ b/asv_bench/benchmarks/benchmark_argofloat.py @@ -3,6 +3,7 @@ import numpy as np import pandas as pd import xarray as xr + from parcels import AdvectionRK4, FieldSet, JITParticle, ParticleSet, StatusCode, Variable @@ -56,10 +57,10 @@ def ArgoVerticalMovement(particle, fieldset, time): class ArgoFloatJIT: def setup(self): time = pd.date_range(start="2025-01-01", end="2025-02-16", freq="D") - lon = np.linspace(-180,180,120) - lat = np.linspace(-90,90,100) + lon = np.linspace(-180, 180, 120) + lat = np.linspace(-90, 90, 100) - Lon,Lat = np.meshgrid(lon,lat) + Lon, Lat = np.meshgrid(lon, lat) # Create large-scale gyre flow U_gyre = np.cos(np.radians(Lat)) * np.sin(np.radians(Lon)) # Zonal flow @@ -78,28 +79,32 @@ def setup(self): U_final = U_coriolis + U_noise V_final = V_coriolis + V_noise - depth = np.linspace(0,2000,100) - - U_val = np.tile(U_final[None,None, :, :], (len(time), len(depth),1, 1)) # Repeat for each time step - V_val = np.tile(V_final[None,None, :, :], (len(time), len(depth),1, 1)) + depth = np.linspace(0, 2000, 100) + + U_val = np.tile(U_final[None, None, :, :], (len(time), len(depth), 1, 1)) # Repeat for each time step + V_val = np.tile(V_final[None, None, :, :], (len(time), len(depth), 1, 1)) - U = xr.DataArray(U_val, - dims = ['time','depth','lat','lon'], - coords = {'time':time, 'depth':depth,'lat':lat, 'lon':lon}, - name='U_velocity') + U = xr.DataArray( + U_val, + dims=["time", "depth", "lat", "lon"], + coords={"time": time, "depth": depth, "lat": lat, "lon": lon}, + name="U_velocity", + ) - V = xr.DataArray(V_val, - dims = ['time','depth','lat','lon'], - coords = {'time':time, 'depth':depth,'lat':lat, 'lon':lon}, - name='V_velocity') + V = xr.DataArray( + V_val, + dims=["time", "depth", "lat", "lon"], + coords={"time": time, "depth": depth, "lat": lat, "lon": lon}, + name="V_velocity", + ) - ds = xr.Dataset({"U":U, "V":V}) + ds = xr.Dataset({"U": U, "V": V}) variables = { "U": "U", "V": "V", } - dimensions = {"lat": "lat", "lon": "lon", "time": "time", "depth":"depth"} + dimensions = {"lat": "lat", "lon": "lon", "time": "time", "depth": "depth"} fieldset = FieldSet.from_xarray_dataset(ds, variables, dimensions) # uppermost layer in the hydrodynamic data fieldset.mindepth = fieldset.U.depth[0] @@ -114,8 +119,7 @@ def setup(self): self.pset = ParticleSet(fieldset=fieldset, pclass=ArgoParticle, lon=[32], lat=[-31], depth=[0]) - def time_run_many_timesteps(self): - self.pset.execute([ArgoVerticalMovement, AdvectionRK4], runtime=timedelta(seconds=1 * 30), dt=timedelta(seconds=30)) - - + self.pset.execute( + [ArgoVerticalMovement, AdvectionRK4], runtime=timedelta(seconds=1 * 30), dt=timedelta(seconds=30) + ) From 1e08f59b1c0034515ff9317abf387e5557b3c199 Mon Sep 17 00:00:00 2001 From: Lizarbe Date: Wed, 26 Feb 2025 12:11:41 +0100 Subject: [PATCH 18/35] adapt runtime --- asv_bench/benchmarks/benchmark_argofloat.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/asv_bench/benchmarks/benchmark_argofloat.py b/asv_bench/benchmarks/benchmark_argofloat.py index 3b9a165713..6884d9778e 100644 --- a/asv_bench/benchmarks/benchmark_argofloat.py +++ b/asv_bench/benchmarks/benchmark_argofloat.py @@ -4,7 +4,7 @@ import pandas as pd import xarray as xr -from parcels import AdvectionRK4, FieldSet, JITParticle, ParticleSet, StatusCode, Variable +from parcels import AdvectionRK4, FieldSet, JITParticle, ParticleSet, StatusCode, Variable, ScipyParticle def ArgoVerticalMovement(particle, fieldset, time): @@ -55,8 +55,11 @@ def ArgoVerticalMovement(particle, fieldset, time): class ArgoFloatJIT: + particle_type = JITParticle + def setup(self): - time = pd.date_range(start="2025-01-01", end="2025-02-16", freq="D") + self.runtime_days = 45 + time = np.datetime64("2025-01-01") + np.arange(self.runtime_days + 1) * np.timedelta64(1, "D") lon = np.linspace(-180, 180, 120) lat = np.linspace(-90, 90, 100) @@ -109,7 +112,9 @@ def setup(self): # uppermost layer in the hydrodynamic data fieldset.mindepth = fieldset.U.depth[0] # Define a new Particle type including extra Variables - ArgoParticle = JITParticle.add_variables( + + + ArgoParticle = self.particle_type.add_variables( [ Variable("cycle_phase", dtype=np.int32, initial=0.0), Variable("cycle_age", dtype=np.float32, initial=0.0), @@ -121,5 +126,9 @@ def setup(self): def time_run_many_timesteps(self): self.pset.execute( - [ArgoVerticalMovement, AdvectionRK4], runtime=timedelta(seconds=1 * 30), dt=timedelta(seconds=30) + [ArgoVerticalMovement, AdvectionRK4], runtime=timedelta(days=self.runtime_days), dt=timedelta(seconds=30) ) + +# How do we derive benchmarks ? +# class ArgoFloatScipy(ArgoFloatJIT): +# particle_type = ScipyParticle From 7d3348f8ce1a6b0bb8ffa5632a544a8064b2d1b0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Feb 2025 11:16:02 +0000 Subject: [PATCH 19/35] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- asv_bench/benchmarks/benchmark_argofloat.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/asv_bench/benchmarks/benchmark_argofloat.py b/asv_bench/benchmarks/benchmark_argofloat.py index 6884d9778e..c795398ccc 100644 --- a/asv_bench/benchmarks/benchmark_argofloat.py +++ b/asv_bench/benchmarks/benchmark_argofloat.py @@ -1,10 +1,9 @@ from datetime import timedelta import numpy as np -import pandas as pd import xarray as xr -from parcels import AdvectionRK4, FieldSet, JITParticle, ParticleSet, StatusCode, Variable, ScipyParticle +from parcels import AdvectionRK4, FieldSet, JITParticle, ParticleSet, StatusCode, Variable def ArgoVerticalMovement(particle, fieldset, time): @@ -59,7 +58,7 @@ class ArgoFloatJIT: def setup(self): self.runtime_days = 45 - time = np.datetime64("2025-01-01") + np.arange(self.runtime_days + 1) * np.timedelta64(1, "D") + time = np.datetime64("2025-01-01") + np.arange(self.runtime_days + 1) * np.timedelta64(1, "D") lon = np.linspace(-180, 180, 120) lat = np.linspace(-90, 90, 100) @@ -112,7 +111,6 @@ def setup(self): # uppermost layer in the hydrodynamic data fieldset.mindepth = fieldset.U.depth[0] # Define a new Particle type including extra Variables - ArgoParticle = self.particle_type.add_variables( [ @@ -129,6 +127,7 @@ def time_run_many_timesteps(self): [ArgoVerticalMovement, AdvectionRK4], runtime=timedelta(days=self.runtime_days), dt=timedelta(seconds=30) ) -# How do we derive benchmarks ? + +# How do we derive benchmarks ? # class ArgoFloatScipy(ArgoFloatJIT): # particle_type = ScipyParticle From 579a1057ed6f9c8cfb9668c4b2f20d47ca0a1968 Mon Sep 17 00:00:00 2001 From: danliba Date: Wed, 26 Feb 2025 12:25:19 +0100 Subject: [PATCH 20/35] nemo start --- asv_bench/benchmarks/benchmarks_nemo_curvilinear.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 asv_bench/benchmarks/benchmarks_nemo_curvilinear.py diff --git a/asv_bench/benchmarks/benchmarks_nemo_curvilinear.py b/asv_bench/benchmarks/benchmarks_nemo_curvilinear.py new file mode 100644 index 0000000000..3642600204 --- /dev/null +++ b/asv_bench/benchmarks/benchmarks_nemo_curvilinear.py @@ -0,0 +1,7 @@ +class NemoCurvilinear: + def setup(self): + pass + + def time_run_experiment(self): + pass + \ No newline at end of file From 45599807a60bea73a94c3a4b87aa6627c5560f9c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Feb 2025 11:25:32 +0000 Subject: [PATCH 21/35] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- asv_bench/benchmarks/benchmarks_nemo_curvilinear.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/asv_bench/benchmarks/benchmarks_nemo_curvilinear.py b/asv_bench/benchmarks/benchmarks_nemo_curvilinear.py index 3642600204..d5e0a47430 100644 --- a/asv_bench/benchmarks/benchmarks_nemo_curvilinear.py +++ b/asv_bench/benchmarks/benchmarks_nemo_curvilinear.py @@ -3,5 +3,4 @@ def setup(self): pass def time_run_experiment(self): - pass - \ No newline at end of file + pass From 1f9c6c900f254291d4befa2b478407cbdff4ac4c Mon Sep 17 00:00:00 2001 From: danliba Date: Fri, 28 Feb 2025 15:55:32 +0100 Subject: [PATCH 22/35] nemo curvilinear --- .../benchmarks/benchmarks_nemo_curvilinear.py | 57 +++++++++++++++++-- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/asv_bench/benchmarks/benchmarks_nemo_curvilinear.py b/asv_bench/benchmarks/benchmarks_nemo_curvilinear.py index 3642600204..69f364b266 100644 --- a/asv_bench/benchmarks/benchmarks_nemo_curvilinear.py +++ b/asv_bench/benchmarks/benchmarks_nemo_curvilinear.py @@ -1,7 +1,56 @@ -class NemoCurvilinear: +from argparse import ArgumentParser +from datetime import timedelta +from glob import glob + +import numpy as np +import pytest +import parcels + +# ptype = {"scipy": parcels.ScipyParticle, "jit": parcels.JITParticle} +# advection = {"RK4": parcels.AdvectionRK4, "AA": parcels.AdvectionAnalytical} +path_nemo = "~/Documents/PhD/projects/2025-02_parcels_benchmarking/NemoCurvilinear_data" + +class NemoCurvilinearJIT: + particle_type = parcels.JITParticle + def setup(self): - pass + filenames = { + "U": { + "lon": f"{path_nemo}/mesh_mask.nc4", + "lat": f"{path_nemo}/mesh_mask.nc4", + "data": f"{path_nemo}/U_purely_zonal-ORCA025_grid_U.nc4", + }, + "V": { + "lon": f"{path_nemo}/mesh_mask.nc4", + "lat": f"{path_nemo}/mesh_mask.nc4", + "data": f"{path_nemo}/V_purely_zonal-ORCA025_grid_V.nc4", + }, + } + variables = {"U": "U", "V": "V"} + + dimensions = {"lon": "glamf", "lat": "gphif", "time": "time_counter"} + + fieldset = parcels.FieldSet.from_nemo( + filenames, variables, dimensions, allow_time_extrapolation=True + ) + + # Start 20 particles on a meridional line at 180W + npart = 20 + lonp = -180 * np.ones(npart) + latp = [i for i in np.linspace(-70, 85, npart)] + + self.pset = parcels.ParticleSet.from_list(fieldset, self.particle_type, lon=lonp, lat=latp) + # pfile = parcels.ParticleFile("nemo_particles", pset, outputdt=timedelta(days=1)) + + def time_run_experiment(self): - pass - \ No newline at end of file + self.pset.execute( + parcels.AdvectionRK4, + runtime=timedelta(days=30), + dt=timedelta(hours=6), + # output_file=pfile, + ) + +class NemoCurvilinearScipy(NemoCurvilinearJIT): + particle_type = parcels.ScipyParticle \ No newline at end of file From 28782dfb8ad52601ae8fc088c0de63dd04c5ce67 Mon Sep 17 00:00:00 2001 From: danliba Date: Fri, 28 Feb 2025 16:31:23 +0100 Subject: [PATCH 23/35] nemo curvilinear --- asv_bench/benchmarks/benchmarks_nemo_curvilinear.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/asv_bench/benchmarks/benchmarks_nemo_curvilinear.py b/asv_bench/benchmarks/benchmarks_nemo_curvilinear.py index 69f364b266..abd5260d61 100644 --- a/asv_bench/benchmarks/benchmarks_nemo_curvilinear.py +++ b/asv_bench/benchmarks/benchmarks_nemo_curvilinear.py @@ -8,7 +8,8 @@ # ptype = {"scipy": parcels.ScipyParticle, "jit": parcels.JITParticle} # advection = {"RK4": parcels.AdvectionRK4, "AA": parcels.AdvectionAnalytical} -path_nemo = "~/Documents/PhD/projects/2025-02_parcels_benchmarking/NemoCurvilinear_data" +# path_nemo = "~/Documents/PhD/projects/2025-02_parcels_benchmarking/NemoCurvilinear_data" +path_nemo = parcels.download_example_dataset("NemoCurvilinear_data") class NemoCurvilinearJIT: particle_type = parcels.JITParticle From 017753411e0da0103b5ee23f449d349c4766357a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 28 Feb 2025 15:38:32 +0000 Subject: [PATCH 24/35] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../benchmarks/benchmarks_nemo_curvilinear.py | 38 ++++++++----------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/asv_bench/benchmarks/benchmarks_nemo_curvilinear.py b/asv_bench/benchmarks/benchmarks_nemo_curvilinear.py index 9a41a3100c..70836030fc 100644 --- a/asv_bench/benchmarks/benchmarks_nemo_curvilinear.py +++ b/asv_bench/benchmarks/benchmarks_nemo_curvilinear.py @@ -1,9 +1,7 @@ -from argparse import ArgumentParser from datetime import timedelta -from glob import glob import numpy as np -import pytest + import parcels # ptype = {"scipy": parcels.ScipyParticle, "jit": parcels.JITParticle} @@ -11,30 +9,29 @@ # path_nemo = "~/Documents/PhD/projects/2025-02_parcels_benchmarking/NemoCurvilinear_data" path_nemo = parcels.download_example_dataset("NemoCurvilinear_data") + class NemoCurvilinearJIT: particle_type = parcels.JITParticle def setup(self): filenames = { "U": { - "lon": f"{path_nemo}/mesh_mask.nc4", - "lat": f"{path_nemo}/mesh_mask.nc4", - "data": f"{path_nemo}/U_purely_zonal-ORCA025_grid_U.nc4", - }, - "V": { - "lon": f"{path_nemo}/mesh_mask.nc4", - "lat": f"{path_nemo}/mesh_mask.nc4", - "data": f"{path_nemo}/V_purely_zonal-ORCA025_grid_V.nc4", - }, - } + "lon": f"{path_nemo}/mesh_mask.nc4", + "lat": f"{path_nemo}/mesh_mask.nc4", + "data": f"{path_nemo}/U_purely_zonal-ORCA025_grid_U.nc4", + }, + "V": { + "lon": f"{path_nemo}/mesh_mask.nc4", + "lat": f"{path_nemo}/mesh_mask.nc4", + "data": f"{path_nemo}/V_purely_zonal-ORCA025_grid_V.nc4", + }, + } variables = {"U": "U", "V": "V"} dimensions = {"lon": "glamf", "lat": "gphif", "time": "time_counter"} - fieldset = parcels.FieldSet.from_nemo( - filenames, variables, dimensions, allow_time_extrapolation=True - ) - + fieldset = parcels.FieldSet.from_nemo(filenames, variables, dimensions, allow_time_extrapolation=True) + # Start 20 particles on a meridional line at 180W npart = 20 lonp = -180 * np.ones(npart) @@ -43,17 +40,14 @@ def setup(self): self.pset = parcels.ParticleSet.from_list(fieldset, self.particle_type, lon=lonp, lat=latp) # pfile = parcels.ParticleFile("nemo_particles", pset, outputdt=timedelta(days=1)) - - def time_run_experiment(self): - - self.pset.execute( + self.pset.execute( parcels.AdvectionRK4, runtime=timedelta(days=30), dt=timedelta(hours=6), # output_file=pfile, ) + class NemoCurvilinearScipy(NemoCurvilinearJIT): particle_type = parcels.ScipyParticle - From 61f93a57dd26dcd62aa078740b789a133333cb59 Mon Sep 17 00:00:00 2001 From: Willi Rath Date: Wed, 2 Jul 2025 10:13:25 +0200 Subject: [PATCH 25/35] Fix benchmark workflow --- asv_bench/asv.conf.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asv_bench/asv.conf.json b/asv_bench/asv.conf.json index bc57809c61..5aa7dce77f 100644 --- a/asv_bench/asv.conf.json +++ b/asv_bench/asv.conf.json @@ -31,7 +31,7 @@ ], // List of branches to benchmark. If not provided, defaults to "master" // (for git) or "default" (for mercurial). - "branches": ["master"], // for git + "branches": ["main"], // for git // "branches": ["default"], // for mercurial // The DVCS being used. If not set, it will be automatically From e9b34e32dc5e20d14e64cbcad1cf74d530d1703f Mon Sep 17 00:00:00 2001 From: Willi Rath Date: Wed, 2 Jul 2025 10:14:41 +0200 Subject: [PATCH 26/35] Bump runner image --- .github/workflows/benchmarks.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/benchmarks.yml b/.github/workflows/benchmarks.yml index df9c5a11a1..39d8d4c7a9 100644 --- a/.github/workflows/benchmarks.yml +++ b/.github/workflows/benchmarks.yml @@ -13,7 +13,7 @@ jobs: benchmark: if: ${{ contains( github.event.pull_request.labels.*.name, 'run-benchmark') && github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch' }} name: Linux - runs-on: ubuntu-20.04 + runs-on: ubuntu-24.04 env: ASV_DIR: "./asv_bench" CONDA_ENV_FILE: environment.yml From 886af3dfa2ca4fa57f8fa6f5a4f9d022dcf0575d Mon Sep 17 00:00:00 2001 From: Willi Rath Date: Wed, 2 Jul 2025 10:45:24 +0200 Subject: [PATCH 27/35] Fix multistep execution --- asv_bench/benchmarks/benchmarks_particle_execution.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/asv_bench/benchmarks/benchmarks_particle_execution.py b/asv_bench/benchmarks/benchmarks_particle_execution.py index 19d3244b10..1ed9244fdb 100644 --- a/asv_bench/benchmarks/benchmarks_particle_execution.py +++ b/asv_bench/benchmarks/benchmarks_particle_execution.py @@ -30,13 +30,13 @@ def setup(self): depth=np.linspace(0, 1, npart), ) # trigger compilation - self.pset.execute(AdvectionRK4, runtime=0, dt=timedelta(seconds=30)) + self.pset.execute(AdvectionRK4, runtime=0, dt=timedelta(seconds=5)) def time_run_single_timestep(self): - self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=1 * 30), dt=timedelta(seconds=30)) + self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=1 * 5), dt=timedelta(seconds=5)) def time_run_many_timesteps(self): - self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=100 * 30), dt=timedelta(seconds=30)) + self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=100 * 5), dt=timedelta(seconds=5)) class ParticleExecutionScipy: From bf33c823b7b01f7f5646000957b14ab20425f2af Mon Sep 17 00:00:00 2001 From: Willi Rath Date: Wed, 2 Jul 2025 10:50:55 +0200 Subject: [PATCH 28/35] Also fix Scipy multi step execution --- asv_bench/benchmarks/benchmarks_particle_execution.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/asv_bench/benchmarks/benchmarks_particle_execution.py b/asv_bench/benchmarks/benchmarks_particle_execution.py index 1ed9244fdb..e42f3ed3ab 100644 --- a/asv_bench/benchmarks/benchmarks_particle_execution.py +++ b/asv_bench/benchmarks/benchmarks_particle_execution.py @@ -65,7 +65,7 @@ def setup(self): ) def time_run_single_timestep(self): - self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=1 * 30), dt=timedelta(seconds=30)) + self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=1 * 5), dt=timedelta(seconds=5)) def time_run_many_timesteps(self): - self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=100 * 30), dt=timedelta(seconds=30)) + self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=100 * 5), dt=timedelta(seconds=5)) From 0ad59a0476edc3f2bbe200c3c65ba87dccf028eb Mon Sep 17 00:00:00 2001 From: Willi Rath Date: Wed, 2 Jul 2025 11:12:54 +0200 Subject: [PATCH 29/35] Make asv verbose and skip bottleneck --- .github/workflows/benchmarks.yml | 2 +- asv_bench/asv.conf.json | 2 +- asv_bench/benchmarks/benchmarks_particle_execution.py | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/benchmarks.yml b/.github/workflows/benchmarks.yml index 39d8d4c7a9..2d49ccd855 100644 --- a/.github/workflows/benchmarks.yml +++ b/.github/workflows/benchmarks.yml @@ -55,7 +55,7 @@ jobs: echo "Baseline: ${{ github.event.pull_request.base.sha }} (${{ github.event.pull_request.base.label }})" echo "Contender: ${GITHUB_SHA} ($PR_HEAD_LABEL)" # Run benchmarks for current commit against base - ASV_OPTIONS="--split --show-stderr --factor $ASV_FACTOR" + ASV_OPTIONS="--split --show-stderr --factor $ASV_FACTOR --verbose" asv continuous $ASV_OPTIONS ${{ github.event.pull_request.base.sha }} ${GITHUB_SHA} \ | sed "/Traceback \|failed$\|PERFORMANCE DECREASED/ s/^/::error::/" \ | tee benchmarks.log diff --git a/asv_bench/asv.conf.json b/asv_bench/asv.conf.json index 5aa7dce77f..2504c2a6f2 100644 --- a/asv_bench/asv.conf.json +++ b/asv_bench/asv.conf.json @@ -110,7 +110,7 @@ "pymbolic": [""], "pytest": [""], "scipy": [""], - "trajan": [""], + // "trajan": [""], "tqdm": [""], "xarray": [""], "zarr": [""] diff --git a/asv_bench/benchmarks/benchmarks_particle_execution.py b/asv_bench/benchmarks/benchmarks_particle_execution.py index e42f3ed3ab..53dfe4bdf7 100644 --- a/asv_bench/benchmarks/benchmarks_particle_execution.py +++ b/asv_bench/benchmarks/benchmarks_particle_execution.py @@ -63,6 +63,7 @@ def setup(self): lat=np.zeros(npart) + 1e2, depth=np.linspace(0, 1, npart), ) + self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=1 * 5), dt=timedelta(seconds=5)) def time_run_single_timestep(self): self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=1 * 5), dt=timedelta(seconds=5)) From bda0028bcffb49c7dec7e0d8ce980509633efb44 Mon Sep 17 00:00:00 2001 From: Willi Rath Date: Wed, 2 Jul 2025 11:27:32 +0200 Subject: [PATCH 30/35] Set large timeout --- asv_bench/benchmarks/benchmarks_particle_execution.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/asv_bench/benchmarks/benchmarks_particle_execution.py b/asv_bench/benchmarks/benchmarks_particle_execution.py index 53dfe4bdf7..ec4411b822 100644 --- a/asv_bench/benchmarks/benchmarks_particle_execution.py +++ b/asv_bench/benchmarks/benchmarks_particle_execution.py @@ -6,6 +6,7 @@ class ParticleExecutionJIT: + timeout = 240 def setup(self): xdim = ydim = zdim = 2 npart = 1_000 @@ -40,6 +41,7 @@ def time_run_many_timesteps(self): class ParticleExecutionScipy: + timeout = 240 def setup(self): xdim = ydim = zdim = 2 npart = 1_000 @@ -67,6 +69,8 @@ def setup(self): def time_run_single_timestep(self): self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=1 * 5), dt=timedelta(seconds=5)) + print("single step", self.pset[0].time) def time_run_many_timesteps(self): self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=100 * 5), dt=timedelta(seconds=5)) + print("multi step", self.pset[0].time) From 5bd49d4685a4b24ac37b9e3ceca1201d462f6804 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Jul 2025 09:27:54 +0000 Subject: [PATCH 31/35] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- asv_bench/benchmarks/benchmarks_particle_execution.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/asv_bench/benchmarks/benchmarks_particle_execution.py b/asv_bench/benchmarks/benchmarks_particle_execution.py index ec4411b822..2f00ae8481 100644 --- a/asv_bench/benchmarks/benchmarks_particle_execution.py +++ b/asv_bench/benchmarks/benchmarks_particle_execution.py @@ -7,6 +7,7 @@ class ParticleExecutionJIT: timeout = 240 + def setup(self): xdim = ydim = zdim = 2 npart = 1_000 @@ -42,6 +43,7 @@ def time_run_many_timesteps(self): class ParticleExecutionScipy: timeout = 240 + def setup(self): xdim = ydim = zdim = 2 npart = 1_000 From 0a271e6bf7106f26975ca8e27dd63e83ba6aa0ee Mon Sep 17 00:00:00 2001 From: Willi Rath Date: Wed, 2 Jul 2025 12:01:18 +0200 Subject: [PATCH 32/35] Try avoiding git describe issue --- .github/workflows/benchmarks.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/benchmarks.yml b/.github/workflows/benchmarks.yml index 2d49ccd855..1bfbf1b575 100644 --- a/.github/workflows/benchmarks.yml +++ b/.github/workflows/benchmarks.yml @@ -56,7 +56,8 @@ jobs: echo "Contender: ${GITHUB_SHA} ($PR_HEAD_LABEL)" # Run benchmarks for current commit against base ASV_OPTIONS="--split --show-stderr --factor $ASV_FACTOR --verbose" - asv continuous $ASV_OPTIONS ${{ github.event.pull_request.base.sha }} ${GITHUB_SHA} \ + # asv continuous $ASV_OPTIONS ${{ github.event.pull_request.base.sha }} ${GITHUB_SHA} \ + asv continuous $ASV_OPTIONS ${GITHUB_SHA} \ | sed "/Traceback \|failed$\|PERFORMANCE DECREASED/ s/^/::error::/" \ | tee benchmarks.log # Report and export results for subsequent steps From 0e9a85b51b5603be23f63bf13c4fe6a2cd83e29f Mon Sep 17 00:00:00 2001 From: Willi Rath Date: Wed, 2 Jul 2025 12:16:13 +0200 Subject: [PATCH 33/35] Don't fail by grepping --- .github/workflows/benchmarks.yml | 11 +++++------ asv_bench/benchmarks/benchmarks_particle_execution.py | 2 -- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/.github/workflows/benchmarks.yml b/.github/workflows/benchmarks.yml index 1bfbf1b575..1ddf82d257 100644 --- a/.github/workflows/benchmarks.yml +++ b/.github/workflows/benchmarks.yml @@ -56,14 +56,13 @@ jobs: echo "Contender: ${GITHUB_SHA} ($PR_HEAD_LABEL)" # Run benchmarks for current commit against base ASV_OPTIONS="--split --show-stderr --factor $ASV_FACTOR --verbose" - # asv continuous $ASV_OPTIONS ${{ github.event.pull_request.base.sha }} ${GITHUB_SHA} \ - asv continuous $ASV_OPTIONS ${GITHUB_SHA} \ + asv continuous $ASV_OPTIONS ${{ github.event.pull_request.base.sha }} ${GITHUB_SHA} \ | sed "/Traceback \|failed$\|PERFORMANCE DECREASED/ s/^/::error::/" \ | tee benchmarks.log - # Report and export results for subsequent steps - if grep "Traceback \|failed\|PERFORMANCE DECREASED" benchmarks.log > /dev/null ; then - exit 1 - fi + # # Report and export results for subsequent steps + # if grep "Traceback \|failed\|PERFORMANCE DECREASED" benchmarks.log > /dev/null ; then + # exit 1 + # fi working-directory: ${{ env.ASV_DIR }} - name: Add instructions to artifact diff --git a/asv_bench/benchmarks/benchmarks_particle_execution.py b/asv_bench/benchmarks/benchmarks_particle_execution.py index 2f00ae8481..47a7b47846 100644 --- a/asv_bench/benchmarks/benchmarks_particle_execution.py +++ b/asv_bench/benchmarks/benchmarks_particle_execution.py @@ -71,8 +71,6 @@ def setup(self): def time_run_single_timestep(self): self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=1 * 5), dt=timedelta(seconds=5)) - print("single step", self.pset[0].time) def time_run_many_timesteps(self): self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=100 * 5), dt=timedelta(seconds=5)) - print("multi step", self.pset[0].time) From 557ea929921dcefd06490587258ad37e5591bdf9 Mon Sep 17 00:00:00 2001 From: Willi Rath Date: Wed, 2 Jul 2025 12:27:26 +0200 Subject: [PATCH 34/35] Be less verbose in benchmark --- .github/workflows/benchmarks.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/benchmarks.yml b/.github/workflows/benchmarks.yml index 1ddf82d257..bcd30af93c 100644 --- a/.github/workflows/benchmarks.yml +++ b/.github/workflows/benchmarks.yml @@ -55,7 +55,7 @@ jobs: echo "Baseline: ${{ github.event.pull_request.base.sha }} (${{ github.event.pull_request.base.label }})" echo "Contender: ${GITHUB_SHA} ($PR_HEAD_LABEL)" # Run benchmarks for current commit against base - ASV_OPTIONS="--split --show-stderr --factor $ASV_FACTOR --verbose" + ASV_OPTIONS="--split --factor $ASV_FACTOR" asv continuous $ASV_OPTIONS ${{ github.event.pull_request.base.sha }} ${GITHUB_SHA} \ | sed "/Traceback \|failed$\|PERFORMANCE DECREASED/ s/^/::error::/" \ | tee benchmarks.log From 68d8d9624c53a9a4a5ff806da0a9969367de7187 Mon Sep 17 00:00:00 2001 From: Willi Rath Date: Sun, 6 Jul 2025 12:59:34 +0200 Subject: [PATCH 35/35] Make repo url explicit --- asv_bench/asv.conf.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asv_bench/asv.conf.json b/asv_bench/asv.conf.json index 2504c2a6f2..c4f09d4bb4 100644 --- a/asv_bench/asv.conf.json +++ b/asv_bench/asv.conf.json @@ -11,7 +11,7 @@ // The URL or local path of the source code repository for the // project being benchmarked - "repo": "..", + "repo": "https://github.com/oceanparcels/parcels.git", // The Python project's subdirectory in your repo. If missing or // the empty string, the project is assumed to be located at the root