-
Notifications
You must be signed in to change notification settings - Fork 3
64 safely checking for mpi4py import #126
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
jeinstei
wants to merge
7
commits into
develop
Choose a base branch
from
64-safely-checking-for-mpi4py-import
base: develop
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
7 commits
Select commit
Hold shift + click to select a range
548adc2
Removing MPI initialization from module testing
jeinstei 38ca43d
Adding same change to mpi.py per request
jeinstei 92a2c22
Fixing bug pointed out by @cchall
jeinstei d698629
Updating with subprocess-based MPI checking
jeinstei a04deaf
Updating MPI handling with a global
jeinstei 4a77a37
fixing up environment checking
jeinstei aaaa4ec
Moving imports to beginning of file for mpi.py
jeinstei 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from mpi4py import MPI | ||
| MPI.Init() |
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 |
|---|---|---|
| @@ -1,18 +1,62 @@ | ||
| import os | ||
| import subprocess | ||
| from inspect import currentframe, getframeinfo | ||
| import rsopt | ||
|
|
||
| __active_env = None | ||
|
|
||
| def get_mpi_environment(): | ||
| """Checks MPI environment and whether or not MPI is initialized | ||
|
|
||
| Params: | ||
| None | ||
|
|
||
| Returns: | ||
| None if mpi is unavailable; else a dict representing the active MPI environment""" | ||
| global __active_env | ||
|
|
||
| # Test for mpi4py install | ||
| try: | ||
| import mpi4py | ||
| mpi4py.rc.initialize = False | ||
| from mpi4py import MPI | ||
| except ModuleNotFoundError: | ||
| # mpi4py not installed so it can't be used | ||
| return | ||
| __active_env = "no_mpi" | ||
|
|
||
| if __active_env == "no_mpi": | ||
| return None | ||
|
|
||
| # If we already ran this process and have an environment, return the active environment | ||
| if __active_env: | ||
| return __active_env | ||
|
|
||
| frameinfo = getframeinfo(currentframe()) | ||
| print(f"Initializing MPI from {frameinfo.filename}:L{frameinfo.lineno}", flush=True) | ||
|
|
||
| #import faulthandler | ||
| #import sys | ||
| #faulthandler.enable(file=sys.stderr, all_threads=True) | ||
|
|
||
| # Test MPI intialization in another thread | ||
| fname = os.path.dirname(rsopt.__file__) + "/__main__.py" | ||
| pp = subprocess.run(["python", fname]) | ||
|
|
||
| if pp.returncode != 0: | ||
| __active_env = "no_mpi" | ||
| return None | ||
|
|
||
| if not MPI.COMM_WORLD.Get_size() - 1: | ||
| # MPI not being used | ||
| # (if user did start MPI with size 1 this would be an illegal configuration since: main + 1 worker = 2 ranks) | ||
| return | ||
| __active_env = "no_mpi" | ||
| return None | ||
|
|
||
| nworkers = MPI.COMM_WORLD.Get_size() - 1 | ||
| is_manager = MPI.COMM_WORLD.Get_rank() == 0 | ||
| mpi_environment = {'mpi_comm': MPI.COMM_WORLD, 'comms': 'mpi', 'nworkers': nworkers, 'is_manager': is_manager} | ||
|
|
||
| return mpi_environment | ||
| # Save global environment | ||
| __active_env = mpi_environment | ||
|
|
||
| return mpi_environment |
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
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.
Uh oh!
There was an error while loading. Please reload this page.