Handle multiple libraries interacting with p4est/sc
sloede opened this issue ยท 8 comments
Description
If I understand correctly, sc_init
/p4est_init
and sc_finalize
are only supposed to be called once per process. This makes it hard to use p4est in a setting where multiple libraries are linked in an executable which individually use p4est, since as far as I can tell, there is no official way to check if sc/p4est have already been initialized/finalized. Thus, each library that uses p4est has either to assume that it is the only library to do so and call p4est_init
unconditionally, or they have to provide for the possibility that it is initialized elsewhere and manually disable the call to p4est_init
.
For easier composability of libraries, it would thus be great if sc/p4est were to provide an API that provides this information and allows one to safely compose multiple libraries that make use of p4est.
Proposed solution
Following the MPI standard and its API for MPI_Initialized
and MPI_Finalized
, one possible solution would be if sc/p4est were to provide the following public API,
void p4est_initialized(int* flag);
void sc_initialized(int* flag);
void sc_finalized(int* flag);
where one can call each function with a pointer to an int
that will either be 0
or 1
afterwards, depending on whether sc/p4est have already been initialized. This would allow to guard the init/finalization code in libraries with something like
int initialized;
sc_initialized(&initialized);
if (!initialized) {
sc_init(MPI_COMM_WORLD, 0, 0, NULL, SC_LP_ERROR);
}
p4est_initialized(&initialized);
if (!initialized) {
p4est_init(NULL, SC_LP_ERROR);
}
[...]
int finalized;
sc_finalized(&finalized);
if (!finalized) {
sc_finalize();
}
If API similarity to MPI is not relevant/desirable, alternative APIs could be
- a function like
p4est_is_initialized
, which directly returns true or false, or - a function like
p4est_init_idempotent
, which internally checks for initialization and does nothing if already initialized.
The init and finalize functions are somewhat soft in that they are not strictly required. However, they do set the internal rank variable, which is used in logging output, and then they catch some signals if so desired. I agree this should be done once per program and not once per sub-application.
I like the is_initialized and is_finalized method the best. Idempotent versions may be written by any user.
Can be closed @sloede?
The SC part is still missing... I didn't get around to implementing it, but feel free to give it a shot of you're interested!
Ah yes, I missed that. Sorry.
The sc part is merged into develop. The PR on p4est is still open, basically just due to the history being requested to simplify. Thanks the @jmark for preparing it to this point!
We have p4est_is_initialized. Is this good enough for the time being?
Yes, thank you very much!
Yes! Thanks!