-
Notifications
You must be signed in to change notification settings - Fork 1
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
JDBetteridge/parallel assert #6
Conversation
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.
There are quite a few small things that need fixing and comments that should be addressed. In general this seems good though.
@connorjward and I were chatting yesterday and I think we're both pretty happy with this, it's something we really need. Changes are:
def parallel_assert(assertion: Union[bool, None], msg: str=""):
"""Make an assertion across MPI.COMM_WORLD
Parameters:
-----------
assertion:
Boolean that will be tested for truthyness. This should be `None`
on any rank that is not participating in the assertion.
msg:
A informative message to be printed if the assertion fails on any rank.
Raises:
---------
AssertionError
Raised on all ranks if the :assertion: argument is :False: on any rank.
Example:
--------
Where in serial code one would have previously written:
```python
x = f()
assert x < 5
```
Now write, if all ranks are participating:
```python
x = f()
parallel_assert(x < 5)
```
Or if only the first 2 ranks are participating, and with a helpful message:
```python
x = f()
rank = MPI.COMM_WORLD.rank
parallel_assert(x < 5 if (rank < 3) else None,
msg="x is not less than 5")
```
"""
if assertion is None:
assertion = True
all_assertions = MPI.COMM_WORLD.allgather(assertion)
if not all(all_assertions):
raise AssertionError(
"Parallel assertion failed on ranks:"
f"{[ii for ii, b in enumerate(all_assertions) if not b]}\n" + msg
) |
Co-authored-by: Josh Hope-Collins <[email protected]>
Add a parallel assertion feature