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

Allow external initialization of MPI and p4est #14884

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion include/deal.II/base/mpi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,8 @@ namespace Utilities
MPI_InitFinalize(
int & argc,
char **& argv,
const unsigned int max_num_threads = numbers::invalid_unsigned_int);
const unsigned int max_num_threads = numbers::invalid_unsigned_int,
const bool allow_external_initialization_ = false);

/**
* Destructor. Calls <tt>MPI_Finalize()</tt> in case this class owns the
Expand Down Expand Up @@ -1206,9 +1207,19 @@ namespace Utilities
*/
static std::set<MPI_Request *> requests;

bool allow_external_initialization = false;

#ifdef DEAL_II_WITH_PETSC
bool finalize_petscslepc;
#endif

#ifdef DEAL_II_WITH_MPI
bool finalize_mpi = true;
#endif

#ifdef DEAL_II_WITH_P4EST
bool finalize_sc = true;
#endif
};

/**
Expand Down
62 changes: 44 additions & 18 deletions source/base/mpi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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!"));
Comment on lines +765 to +766
Copy link
Contributor

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?

Copy link
Contributor Author

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

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!"));

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.


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.
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
Expand Down Expand Up @@ -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


Expand All @@ -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
Expand Down