-
Notifications
You must be signed in to change notification settings - Fork 1
WIP: Namespaced AppContext
#16
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
Draft
JHopeCollins
wants to merge
22
commits into
main
Choose a base branch
from
JHopeCollins/appctx
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.
Draft
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
8d680b1
appctx
JHopeCollins 321fefa
Merge branch 'main' into JHopeCollins/appctx
JHopeCollins 54cd6db
AppContext in __init__
JHopeCollins 4604f3d
Merge branch 'main' into JHopeCollins/appctx
JHopeCollins c7e366a
Hide AppContext internal keys from user
JHopeCollins 913eb56
updates
JHopeCollins cfe9954
appctx docstrings
JHopeCollins ec9c900
test appctx
JHopeCollins 0a4ba8f
tidy up appctx keygen
JHopeCollins 8104028
review comments - hide key from user completely
JHopeCollins d231b36
move import to top of test file
JHopeCollins 36587cc
Update petsctools/appctx.py
JHopeCollins 3c1b6cf
appctx: hide key_from_option from user.
JHopeCollins 5975b82
global appctx stack
JHopeCollins d51cd25
numpy import inside test
JHopeCollins 0c54caa
Merge branch 'main' into JHopeCollins/appctx
JHopeCollins 3e7928b
use petsc.vec.norm rather than numpy.allclose for test
JHopeCollins 660475a
trailing whitespace
JHopeCollins e042462
attach the appctx directly to the OptionsManager
JHopeCollins ffb03ef
Separate global AppContext and scoped AppContextManager
JHopeCollins f893063
AppContext()[option] = value?
JHopeCollins 65f166a
make the AppContextKey a helpfully named string
JHopeCollins 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| from typing import Any | ||
| import itertools | ||
| from functools import cached_property | ||
| from contextlib import contextmanager | ||
| from petsctools.exceptions import PetscToolsAppctxException | ||
|
|
||
| _global_appctx_data = {} | ||
| """The global storage for user data with arbitrary python types.""" | ||
|
|
||
|
|
||
| class AppContextKey(str): | ||
| """A custom key type for AppContext.""" | ||
|
|
||
| _count = itertools.count() | ||
|
|
||
| @classmethod | ||
| def _generate_key(cls): | ||
| return f"petsctools_appctx_key_{next(cls._count)}" | ||
|
|
||
|
|
||
| class AppContext: | ||
| def __init__(self, prefix: str | None = None): | ||
| from petsctools.options import _validate_prefix | ||
|
|
||
| # possibly append underscore or cast to str | ||
| self._prefix = _validate_prefix(prefix or "") | ||
|
|
||
| @property | ||
| def prefix(self) -> str: | ||
| return self._prefix | ||
|
|
||
| @cached_property | ||
| def options_object(self): | ||
| """A PETSc.Options instance.""" | ||
| from petsc4py import PETSc | ||
|
|
||
| return PETSc.Options() | ||
|
|
||
| def _key_from_option(self, option: str) -> AppContextKey: | ||
| """ | ||
| Return the internal key for the PETSc option `option`. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| option | ||
| The PETSc option. | ||
|
|
||
| Returns | ||
| ------- | ||
| key | ||
| An internal key corresponding to ``option``. | ||
| """ | ||
| return AppContextKey( | ||
| self.options_object.getString(self.prefix + option) | ||
| ) | ||
|
|
||
| def __getitem__(self, option: str | AppContextKey, /) -> Any: | ||
| """ | ||
| Return the value with the key saved in ``PETSc.Options()[option]``. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| option : | ||
| The PETSc option or key. | ||
|
|
||
| Returns | ||
| ------- | ||
| val : | ||
| The value for the key `option`. | ||
|
|
||
| Raises | ||
| ------ | ||
| PetscToolsAppctxException | ||
| If the AppContext does contain a value for `option`. | ||
| """ | ||
| try: | ||
| return _global_appctx_data[self._key_from_option(option)] | ||
| except KeyError: | ||
| raise PetscToolsAppctxException( | ||
| f"AppContext does not have an entry for {option}" | ||
| ) | ||
|
|
||
| def __setitem__(self, option: str, value: Any, /): | ||
| key = AppContextKey._generate_key() | ||
| self.options_object[self.prefix + option] = key | ||
| _global_appctx_data[key] = value | ||
|
|
||
| def get( | ||
| self, option: str | AppContextKey, default: Any | None = None | ||
| ) -> Any: | ||
| """ | ||
| Return the value with the key saved in ``PETSc.Options()[option]``, | ||
| or if it does not exist return default. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| option : | ||
| The PETSc option or key. | ||
| default : | ||
| The value to return if ``option`` is not in the ``AppContext`` | ||
|
|
||
| Returns | ||
| ------- | ||
| val : | ||
| The value for the key ``option``, or ``default``. | ||
| """ | ||
| try: | ||
| return self[option] | ||
| except PetscToolsAppctxException: | ||
| return default | ||
|
|
||
|
|
||
| class AppContextManager: | ||
| """ | ||
| Class for passing non-primitive types to PETSc python contexts. | ||
|
|
||
| The PETSc.Options dictionary can only contain primitive types (str, | ||
| int, float, bool) as values. The AppContext allows other types to be | ||
| passed into PETSc solvers while still making use of the namespacing | ||
| provided by options prefixing. | ||
|
|
||
| A typical usage is shown below. In this example we have a python PC | ||
| type `MyCustomPC` which requires additional data in the form of a | ||
| `MyCustomData` instance. | ||
| We can add the data to the AppContext with the `appctx.add` method, | ||
| but we need to tell `MyCustomPC` how to retrieve that data. The | ||
| `add` method returns a key which is a valid PETSc.Options entry, | ||
| i.e. a primitive type instance. This key is passed via PETSc.Options | ||
| with the 'custompc_somedata' prefix. | ||
|
|
||
| NB: The user should never handle this key directly, it should only | ||
| ever be placed directly into the options dictionary. | ||
|
|
||
| The data can be retrieved by giving the AppContext the (fully | ||
| prefixed) option for the key, in which case the AppContext will | ||
| internally fetch the key from the PETSc.Options and return the data. | ||
|
|
||
| .. code-block:: python3 | ||
|
|
||
| appctx = AppContext() | ||
| some_data = MyCustomData(5) | ||
|
|
||
| opts = OptionsManager( | ||
| parameters={ | ||
| 'pc_type': 'python', | ||
| 'pc_python_type': 'MyCustomPC', | ||
| 'custompc_somedata': appctx.add(some_data)}, | ||
| options_prefix='solver') | ||
|
|
||
| with opts.inserted_options(): | ||
| default = MyCustomData(10) | ||
| data = appctx.get('solver_custompc_somedata', default) | ||
|
JHopeCollins marked this conversation as resolved.
|
||
| """ | ||
|
|
||
| def __init__(self): | ||
| self._data = {} | ||
|
|
||
| def add(self, val: Any) -> AppContextKey: | ||
| """ | ||
| Add a value to the application context and | ||
| return the autogenerated key for that value. | ||
|
|
||
| The autogenerated key should be used as the value for the | ||
| corresponding entry in the solver_parameters dictionary. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| val | ||
| The value to add to the AppContext. | ||
|
|
||
| Returns | ||
| ------- | ||
| key | ||
| The key to put into the PETSc Options dictionary. | ||
| """ | ||
| key = AppContextKey._generate_key() | ||
| self._data[key] = val | ||
| return key | ||
|
|
||
| @contextmanager | ||
| def inserted_appctx(self): | ||
| # We don't overwrite existing entries in the global data, | ||
| # so we need to keep track of what we do actually put in | ||
| # so we don't accidentally remove something we shouldn't. | ||
| to_delete = set() | ||
| try: | ||
| for k, v in self._data.items(): | ||
| if k not in _global_appctx_data: | ||
| _global_appctx_data[k] = v | ||
| to_delete.add(k) | ||
| yield | ||
| finally: | ||
| for k in self._data: | ||
| if k in to_delete: | ||
| del _global_appctx_data[k] | ||
| to_delete.remove(k) | ||
| assert len(to_delete) == 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
Oops, something went wrong.
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.
split this docstring.