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

Revert lru_cache to functional form; Add lru-dict for integrators #2823

Merged
merged 3 commits into from
Mar 29, 2023

Conversation

jsbrittain
Copy link
Contributor

Description

Fix for excessive RAM consumption when running back-to-back simulations. As outlined in #1442, three problems were identified:

  1. Use of the decorator @lru_cache on methods prevent the class instance from being garbage collected. Declaring lru_cache methods functionally in the initialiser resolves this problem. However, note that the function is no longer picklable, and instead the cache relationship must be removed and reinstated during pickling / unpickling.
  2. Integrators are stored in a dictionary for re-use in CasadiSolver. Successive simulations with the same solver can cause the Solver object to grow unbound. A new LRU dictionary (LRUDict) has been introduced to limit the total number of items stored (along with appropriate test code). Default behaviour remains unchanged, but user-specification of an integrator limit permits simulations to run in a loop without unbound RAM consumption. Documentation has been updated to reflect the new parameter.
  3. Successive caching of Solution histories, which can be solved by passing solution.last_state to Simulation.solve() (but requires resolution of [Bug]: Solution last_state can't be used as a starting solution #2788).

Fixes #1442 (Excessive RAM consumption when running multiple simulations)

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.

  • New feature (non-breaking change which adds functionality)
  • Optimization (back-end change that speeds up the code)
  • Bug fix (non-breaking change which fixes an issue)

Key checklist:

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

You can run unit and doctests together at once, using $ python run-tests.py --quick.

Further checks:

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

@codecov

This comment was marked as outdated.

@jsbrittain jsbrittain force-pushed the issue-1442-ram branch 2 times, most recently from 55880c2 to 04170e3 Compare March 28, 2023 18:20
@jsbrittain jsbrittain marked this pull request as ready for review March 28, 2023 19:29
Copy link
Member

@brosaplanella brosaplanella left a comment

Choose a reason for hiding this comment

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

Looks good, thanks! Just a small comment on the CHANGELOG.

CHANGELOG.md Outdated
@@ -8,13 +8,15 @@
- Added "Open-circuit voltage [V]", which is the open-circuit voltage as calculated from the bulk particle concentrations. The old variable "Measured open circuit voltage [V]", which referred to the open-circuit potential as calculated from the surface particle concentrations, has been renamed to "Surface open-circuit voltage [V]". ([#2740](https://github.com/pybamm-team/PyBaMM/pull/2740))
- Added an example for `plot_voltage_components`, explaining what the different voltage components are. ([#2740](https://github.com/pybamm-team/PyBaMM/pull/2740))
- Added method to calculate maximum theoretical energy. ([#2777](https://github.com/pybamm-team/PyBaMM/pull/2777)) and add to summary variables ([#2781](https://github.com/pybamm-team/PyBaMM/pull/2781))
- Added option to limit the number of integrators stored in CasadiSolver, which is particularly relevant when running simulations back-to-back [#2823](https://github.com/pybamm-team/PyBaMM/pull/2823)
Copy link
Member

Choose a reason for hiding this comment

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

Can you move the comment further up (we follow a decreasing PR number sorting)? I realise some of the existing lines do not follow the order actually, so if you can sort them that would be great hahah.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, will do!

@brosaplanella
Copy link
Member

@all-contributors add @jsbrittain for code and tests

@allcontributors
Copy link
Contributor

@brosaplanella

I've put up a pull request to add @jsbrittain! 🎉

Copy link
Contributor

@martinjrobins martinjrobins left a comment

Choose a reason for hiding this comment

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

thanks @jsbrittain. The one comment I had was the default of "unbounded" for number of stored integrators. What is the effect of having a finite bound by default (e.g. 100?), it feels like it would be nice to have memory consumption bounded by default.

@jsbrittain
Copy link
Contributor Author

thanks @jsbrittain. The one comment I had was the default of "unbounded" for number of stored integrators. What is the effect of having a finite bound by default (e.g. 100?), it feels like it would be nice to have memory consumption bounded by default.

The default is unbound as this mimics the original code behaviour, but happy to apply an (arbitrary) bound, such as 100, if preferred.

@martinjrobins
Copy link
Contributor

Thanks @jsbrittain, happy to merge.

@martinjrobins martinjrobins merged commit 2d561b7 into develop Mar 29, 2023
@martinjrobins martinjrobins deleted the issue-1442-ram branch March 29, 2023 18:40
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.

Excessive RAM consumption when running multiple simulations (Solver Issue)
3 participants