-
Notifications
You must be signed in to change notification settings - Fork 33
Speed up CI and test adjoint in parallel #438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cpjordan
wants to merge
3
commits into
main
Choose a base branch
from
speed-up-CI-main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,7 @@ htmlcov/ | |
| nosetests.xml | ||
| coverage.xml | ||
| *,cover | ||
| *.errcode | ||
|
|
||
| # Translations | ||
| *.mo | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| import glob | ||
| import sys | ||
| import shutil | ||
| import runpy | ||
|
|
||
| # set environment flag | ||
| # can be used in examples to reduce cpu cost | ||
|
|
@@ -68,18 +69,65 @@ | |
| all_examples = glob.glob(os.path.join(examples_dir, '*/*.py')) | ||
| all_examples = [f for f in all_examples if f not in exclude_files] | ||
|
|
||
| # Examples that should be exercised under MPI in the main parallel CI job. | ||
| # Keyed by path relative to `examples_dir` with the requested nprocs. | ||
| parallel_examples = { | ||
| os.path.join('discrete_turbines', 'tidal_array.py'): 2, | ||
| } | ||
|
|
||
| @pytest.fixture(params=all_examples) | ||
| for relpath, nprocs in parallel_examples.items(): | ||
| abspath = os.path.join(examples_dir, relpath) | ||
| try: | ||
| idx = all_examples.index(abspath) | ||
| except ValueError: | ||
| continue | ||
| all_examples[idx] = pytest.param(abspath, marks=pytest.mark.parallel(nprocs)) | ||
|
|
||
|
|
||
| @pytest.fixture(params=all_examples, | ||
| ids=lambda x: os.path.relpath(x, examples_dir)) | ||
| def example_file(request): | ||
| return os.path.abspath(request.param) | ||
|
|
||
|
|
||
| def test_examples(example_file, tmpdir, monkeypatch): | ||
| def test_examples(example_file, tmp_path, monkeypatch, request): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do this sort of thing by importing the file: https://github.com/firedrakeproject/firedrake/blob/main/tests/firedrake/demos/test_demos_run.py#L129 This seems totally fine though. |
||
| assert os.path.isfile(example_file), 'File not found {:}'.format(example_file) | ||
| # copy mesh files | ||
| source = os.path.dirname(example_file) | ||
| for f in glob.glob(os.path.join(source, '*.msh')): | ||
| shutil.copy(f, str(tmpdir)) | ||
| # change workdir to temporary dir | ||
| monkeypatch.chdir(tmpdir) | ||
| subprocess.check_call([sys.executable, example_file]) | ||
|
|
||
| if request.node.get_closest_marker("parallel") is None: | ||
| # Serial example: copy mesh files and run in a subprocess. | ||
| for f in glob.glob(os.path.join(source, '*.msh')): | ||
| shutil.copy(f, str(tmp_path)) | ||
| # change workdir to temporary dir | ||
| monkeypatch.chdir(tmp_path) | ||
| subprocess.check_call([sys.executable, example_file]) | ||
| return | ||
|
|
||
| # Parallel example: run the script under the current MPI communicator. | ||
| # In CI this test is selected only in the main parallel job (outer mpiexec -n 2), | ||
| # so we must not spawn mpiexec here (no nested MPI). We also coordinate a shared | ||
| # working directory across ranks to avoid each rank creating its own tmp_path. | ||
| from mpi4py import MPI | ||
| comm = MPI.COMM_WORLD | ||
| if comm.rank == 0: | ||
| workdir = tmp_path | ||
| for f in glob.glob(os.path.join(source, '*.msh')): | ||
| shutil.copy(f, str(workdir)) | ||
| else: | ||
| workdir = None | ||
| workdir = comm.bcast(str(workdir) if workdir is not None else None, root=0) | ||
| comm.barrier() | ||
| monkeypatch.chdir(workdir) | ||
| # Make local imports like `import turbine_callback` work the same way they do | ||
| # when running `python /abs/path/to/example.py` (which prepends the script dir | ||
| # to sys.path). | ||
| added_to_syspath = False | ||
| if source not in sys.path: | ||
| sys.path.insert(0, source) | ||
| added_to_syspath = True | ||
| try: | ||
| runpy.run_path(example_file, run_name="__main__") | ||
| finally: | ||
| if added_to_syspath and sys.path and sys.path[0] == source: | ||
|
stephankramer marked this conversation as resolved.
|
||
| sys.path.pop(0) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """ | ||
| Runs all adjoint example scripts. Only tests whether examples can be executed. | ||
| """ | ||
| import pytest | ||
| import os | ||
| import subprocess | ||
| import glob | ||
| import sys | ||
| import shutil | ||
| import runpy | ||
|
|
||
| # set environment flag | ||
| # can be used in examples to reduce cpu cost | ||
| os.environ['THETIS_REGRESSION_TEST'] = "1" | ||
|
|
||
| # list of all adjoint examples to run | ||
| adjoint_files_serial = [ | ||
| 'tidalfarm/tidalfarm.py', | ||
| 'channel_inversion/inverse_problem.py', | ||
| 'headland_inversion/inverse_problem.py', | ||
| 'tohoku_inversion/inverse_problem.py', | ||
| ] | ||
|
|
||
| adjoint_files_parallel = [ | ||
| 'discrete_turbines/channel-optimisation.py', | ||
| ] | ||
|
|
||
| cwd = os.path.abspath(os.path.dirname(__file__)) | ||
| examples_dir = os.path.abspath(os.path.join(cwd, '..', '..', 'examples')) | ||
|
|
||
| all_examples = ( | ||
| [os.path.join(examples_dir, f) for f in adjoint_files_serial] | ||
| + [ | ||
| pytest.param(os.path.join(examples_dir, f), marks=pytest.mark.parallel(2)) | ||
| for f in adjoint_files_parallel | ||
| ] | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture(params=all_examples, | ||
| ids=lambda x: os.path.relpath(x, examples_dir)) | ||
| def example_file(request): | ||
| return os.path.abspath(request.param) | ||
|
|
||
|
|
||
| def test_examples(example_file, tmp_path, tmp_path_factory, monkeypatch, request): | ||
| assert os.path.isfile(example_file), 'File not found {:}'.format(example_file) | ||
| # copy mesh files | ||
| source = os.path.dirname(example_file) | ||
|
|
||
| if request.node.get_closest_marker("parallel") is None: | ||
| # Serial example: copy mesh files and run in a subprocess. | ||
| for f in glob.glob(os.path.join(source, '*.msh')): | ||
| shutil.copy(f, str(tmp_path)) | ||
| # change workdir to temporary dir | ||
| monkeypatch.chdir(tmp_path) | ||
| subprocess.check_call([sys.executable, example_file]) | ||
| return | ||
|
|
||
| # Parallel example: run the script under the current MPI communicator. | ||
| # In CI this test is selected only in the adjoint-parallel job (outer mpiexec -n 2), | ||
| # so we must not spawn mpiexec here (no nested MPI). We also coordinate a shared | ||
| # working directory across ranks to avoid each rank creating its own tmpdir. | ||
| from mpi4py import MPI | ||
| comm = MPI.COMM_WORLD | ||
| if comm.rank == 0: | ||
| workdir = tmp_path_factory.mktemp("thetis-adjoint-example-channel-optimisation") | ||
| for f in glob.glob(os.path.join(source, '*.msh')): | ||
| shutil.copy(f, str(workdir)) | ||
| else: | ||
| workdir = None | ||
| workdir = comm.bcast(str(workdir) if workdir is not None else None, root=0) | ||
| comm.barrier() | ||
| monkeypatch.chdir(workdir) | ||
| runpy.run_path(example_file, run_name="__main__") |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can set this at the top level of the file (example)