Skip to content
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

Fix parallel optimisation and increase coverage #299

Merged
merged 8 commits into from
Apr 22, 2024

Conversation

BradyPlanden
Copy link
Member

@BradyPlanden BradyPlanden commented Apr 18, 2024

Description

Bugfix optimisation.parallel, add coverage tests, initial multiprocessing import and context set.

Issue reference

Fixes #298

Review

Before you mark your PR as ready for review, please ensure that you've considered the following:

  • Updated the CHANGELOG.md in reverse chronological order (newest at the top) with a concise description of the changes, including the PR number.
  • Noted any breaking changes, including details on how it might impact existing functionality.

Type of change

  • New Feature: A non-breaking change that adds new functionality.
  • Optimization: A code change that improves performance.
  • Examples: A change to existing or additional examples.
  • Bug Fix: A non-breaking change that addresses an issue.
  • Documentation: Updates to documentation or new documentation for new features.
  • Refactoring: Non-functional changes that improve the codebase.
  • Style: Non-functional changes related to code style (formatting, naming, etc).
  • Testing: Additional tests to improve coverage or confirm functionality.
  • Other: (Insert description of change)

Key checklist:

  • No style issues: $ pre-commit run (or $ nox -s pre-commit) (see CONTRIBUTING.md for how to set this up to run automatically when committing locally, in just two lines of code)
  • All unit tests pass: $ nox -s tests
  • The documentation builds: $ nox -s doctest

You can run integration tests, unit tests, and doctests together at once, using $ nox -s quick.

Further checks:

  • Code is well-commented, especially in complex or unclear areas.
  • Added tests that prove my fix is effective or that my feature works.
  • Checked that coverage remains or improves, and added tests if necessary to maintain or increase coverage.

Thank you for contributing to our project! Your efforts help us to deliver great software.

@BradyPlanden BradyPlanden linked an issue Apr 18, 2024 that may be closed by this pull request
Copy link

codecov bot commented Apr 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.79%. Comparing base (0273723) to head (fed4a12).
Report is 8 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #299      +/-   ##
===========================================
+ Coverage    95.49%   95.79%   +0.30%     
===========================================
  Files           38       38              
  Lines         2040     2045       +5     
===========================================
+ Hits          1948     1959      +11     
+ Misses          92       86       -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@BradyPlanden
Copy link
Member Author

BradyPlanden commented Apr 19, 2024

This PR is now ready for review. There is a larger term discussion on how we handle Windows systems, as the multiprocessing doesn't work without wrapping the call structure within a __main__. If we can sort a method out for this,it would greatly increase our optimisation performance across all operating systems. A few results from local timing (Macbook Pro M3) of the spm_PSO.py example with iterations = 200.

Time
Current ~9.9s
PyBaMM "fast with events" Solver ~9.0s
optim.set_parallel(True) ~3.3s

At the moment, this adds multiprocessing for Linux, MacOS, and Windows with WSL.

Copy link
Member

@agriyakhetarpal agriyakhetarpal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks interesting, @BradyPlanden, nice! I wonder if something like Dask can be used to fix what has been happening on Windows here (and also, given the recent parallel discussion on PyBaMM). It could be too much work to add, though 😬

tests/unit/test_import.py Outdated Show resolved Hide resolved
tests/unit/test_import.py Outdated Show resolved Hide resolved
@agriyakhetarpal
Copy link
Member

agriyakhetarpal commented Apr 22, 2024

Actually, this won't entirely work and might break further sometime. For example, if one does

import pybop
import sys

and then something like this

print([k for k in sys.modules.keys() if "pybop" in k])

gives us this:

tap to view output
['pybop._version',
 'pybop._utils',
 'pybop._problem',
 'pybop.costs',
 'pybop.costs.base_cost',
 'pybop.observers',
 'pybop.models',
 'pybop.models.base_model',
 'pybop.parameters',
 'pybop.parameters.parameter',
 'pybop.observers.observer',
 'pybop.costs.fitting_costs',
 'pybop.costs.design_costs',
 'pybop.costs._likelihoods',
 'pybop._dataset',
 'pybop.models.lithium_ion.echem_base',
 'pybop.models.lithium_ion.echem',
 'pybop.models.lithium_ion',
 'pybop.models.empirical.ecm_base',
 'pybop.models.empirical.ecm',
 'pybop.models.empirical',
 'pybop._experiment',
 'pybop._optimisation',
 'pybop.optimisers',
 'pybop.optimisers.base_optimiser',
 'pybop.optimisers.scipy_optimisers',
 'pybop.optimisers.pints_optimisers',
 'pybop.parameters.parameter_set',
 'pybop.parameters.priors',
 'pybop.observers.unscented_kalman',
 'pybop.plotting',
 'pybop.plotting.plotly_manager',
 'pybop.plotting.quick_plot',
 'pybop.plotting.plot2d',
 'pybop.plotting.plot_dataset',
 'pybop.plotting.plot_convergence',
 'pybop.plotting.plot_parameters',
 'pybop.plotting.plot_problem',
 'pybop']

so, running del sys.modules["pybop"] and running the above list comprehension just returns the above list, which is almost the same except the last entry, which gets removed:

tap to view output
['pybop._version',
 'pybop._utils',
 'pybop._problem',
 'pybop.costs',
 'pybop.costs.base_cost',
 'pybop.observers',
 'pybop.models',
 'pybop.models.base_model',
 'pybop.parameters',
 'pybop.parameters.parameter',
 'pybop.observers.observer',
 'pybop.costs.fitting_costs',
 'pybop.costs.design_costs',
 'pybop.costs._likelihoods',
 'pybop._dataset',
 'pybop.models.lithium_ion.echem_base',
 'pybop.models.lithium_ion.echem',
 'pybop.models.lithium_ion',
 'pybop.models.empirical.ecm_base',
 'pybop.models.empirical.ecm',
 'pybop.models.empirical',
 'pybop._experiment',
 'pybop._optimisation',
 'pybop.optimisers',
 'pybop.optimisers.base_optimiser',
 'pybop.optimisers.scipy_optimisers',
 'pybop.optimisers.pints_optimisers',
 'pybop.parameters.parameter_set',
 'pybop.parameters.priors',
 'pybop.observers.unscented_kalman',
 'pybop.plotting',
 'pybop.plotting.plotly_manager',
 'pybop.plotting.quick_plot',
 'pybop.plotting.plot2d',
 'pybop.plotting.plot_dataset',
 'pybop.plotting.plot_convergence',
 'pybop.plotting.plot_parameters',
 'pybop.plotting.plot_problem']

Therefore, we should delete all of these entries (i.e., pybop.*) before importing pybop again.

@BradyPlanden
Copy link
Member Author

Thanks for the feedback @agriyakhetarpal. I've updated now, I believe this fully unloads the module. Let me know if you catch any other issues!

tests/unit/test_import.py Outdated Show resolved Hide resolved
Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com>
@BradyPlanden BradyPlanden merged commit 692b4d5 into develop Apr 22, 2024
28 checks passed
@BradyPlanden BradyPlanden deleted the 298-add-coverage-for-optimisationparallel branch April 22, 2024 21:10
@BradyPlanden BradyPlanden changed the title [WIP]: Fix parallel optimisation and increase coverage Fix parallel optimisation and increase coverage Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add coverage for optimisation.parallel()
3 participants