-
Notifications
You must be signed in to change notification settings - Fork 708
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
Allow external initialization of MPI and p4est #14884
Draft
sloede
wants to merge
1
commit into
dealii:master
Choose a base branch
from
sloede:msl/allow-external-initialization
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
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.
+56
−19
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -737,7 +737,9 @@ namespace Utilities | |
|
||
MPI_InitFinalize::MPI_InitFinalize(int & argc, | ||
char **& argv, | ||
const unsigned int max_num_threads) | ||
const unsigned int max_num_threads, | ||
const bool allow_external_initialization_) | ||
: allow_external_initialization(allow_external_initialization_) | ||
{ | ||
static bool constructor_has_already_run = false; | ||
(void)constructor_has_already_run; | ||
|
@@ -753,17 +755,25 @@ namespace Utilities | |
int MPI_has_been_started = 0; | ||
ierr = MPI_Initialized(&MPI_has_been_started); | ||
AssertThrowMPI(ierr); | ||
AssertThrow(MPI_has_been_started == 0, | ||
ExcMessage("MPI error. You can only start MPI once!")); | ||
|
||
int provided; | ||
// this works like ierr = MPI_Init (&argc, &argv); but tells MPI that | ||
// we might use several threads but never call two MPI functions at the | ||
// same time. For an explanation see on why we do this see | ||
// http://www.open-mpi.org/community/lists/users/2010/03/12244.php | ||
int wanted = MPI_THREAD_SERIALIZED; | ||
ierr = MPI_Init_thread(&argc, &argv, wanted, &provided); | ||
AssertThrowMPI(ierr); | ||
|
||
if (MPI_has_been_started == 1 && allow_external_initialization) | ||
{ | ||
finalize_mpi = false; | ||
} | ||
else | ||
{ | ||
AssertThrow(MPI_has_been_started == 0, | ||
ExcMessage("MPI error. You can only start MPI once!")); | ||
|
||
int provided; | ||
// this works like ierr = MPI_Init (&argc, &argv); but tells MPI that | ||
// we might use several threads but never call two MPI functions at the | ||
// same time. For an explanation see on why we do this see | ||
// http://www.open-mpi.org/community/lists/users/2010/03/12244.php | ||
int wanted = MPI_THREAD_SERIALIZED; | ||
ierr = MPI_Init_thread(&argc, &argv, wanted, &provided); | ||
AssertThrowMPI(ierr); | ||
} | ||
|
||
// disable for now because at least some implementations always return | ||
// MPI_THREAD_SINGLE. | ||
|
@@ -834,9 +844,19 @@ namespace Utilities | |
// MPI_Comm_create_group (see cburstedde/p4est#30). | ||
// Disabling it leads to more verbose p4est error messages | ||
// which should be fine. | ||
sc_init(MPI_COMM_WORLD, 0, 0, nullptr, SC_LP_SILENT); | ||
if (sc_package_id >= 0 && allow_external_initialization) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
{ | ||
finalize_sc = false; | ||
} | ||
else | ||
{ | ||
sc_init(MPI_COMM_WORLD, 0, 0, nullptr, SC_LP_SILENT); | ||
} | ||
# endif | ||
p4est_init(nullptr, SC_LP_SILENT); | ||
if (p4est_package_id < 0 || !allow_external_initialization) | ||
{ | ||
p4est_init(nullptr, SC_LP_SILENT); | ||
} | ||
#endif | ||
|
||
constructor_has_already_run = true; | ||
|
@@ -1011,7 +1031,10 @@ namespace Utilities | |
#ifdef DEAL_II_WITH_P4EST | ||
// now end p4est and libsc | ||
// Note: p4est has no finalize function | ||
sc_finalize(); | ||
if (finalize_sc) | ||
{ | ||
sc_finalize(); | ||
} | ||
#endif | ||
|
||
|
||
|
@@ -1035,9 +1058,12 @@ namespace Utilities | |
} | ||
else | ||
{ | ||
const int ierr = MPI_Finalize(); | ||
(void)ierr; | ||
AssertNothrow(ierr == MPI_SUCCESS, dealii::ExcMPI(ierr)); | ||
if (finalize_mpi) | ||
{ | ||
const int ierr = MPI_Finalize(); | ||
(void)ierr; | ||
AssertNothrow(ierr == MPI_SUCCESS, dealii::ExcMPI(ierr)); | ||
} | ||
} | ||
} | ||
#endif | ||
|
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.
I don't understand why you need to require an extra flag
allow_external_initialization
. For example, the PETSc/SLEPC part can be initalized outside of this function and deal.ii does not complain. What is different for MPI?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.
Currently in
master
, deal.II does not allow MPI to be initialized outside of deal.II:dealii/source/base/mpi.cc
Lines 753 to 757 in 3c37cfe
I thus assumed that the design goals for this function were that any abnormal usage by the user should result in a fast and hard failure, even though in this case it would have been possible to, e.g., first check the current thread level and see if it is sufficient for deal.II. This makes sense to me - I have used the same approach many times before, assuming that if anything is happening out of the ordinary, the user should probably be informed immediately instead of potentially running an expensive simulation and failing after a couple of hours.
My approach in this PR was therefore motivated by wanting to keep this strict safety against user errors as the default. I therefore made them set a flag explicitly, which indicates to deal.II "I know what I'm doing" but also provides the feedback to the users "if you had to set a flag for this, you're probably in dragon land now".
However, I am not a regular committer to deal.II, so everything I wrote was based only on assumptions. I'd be more than happy to make this work without a flag, if this is so desired. However, from the convo in this PR, I already got the feeling that a very different approach is to be followed for allowing external initialization, so I don't know if this question will not be rendered moot anyway.