Triggering function call on MPI process termination
Was flipping through the MPI-2 Standards when I stumbled upon this:
"There are times in which it would be convenient to have actions happen when an MPI process finishes. For example, a routine may do initializations that are useful until the MPI job (or that part of the job that being terminated in the case of dynamically created processes) is finished. This can be accomplished in MPI-2 by attaching an attribute to MPI_COMM_SELF with a callback function."
Looked like good fun. Here's a quick code to see it in action.
#include <mpi.h>
/* callback function triggered when Communicator is freed */
int comm_self_delete_fn (MPI_Comm comm, int keyval, \
void *attr_val, void *extra_state) {
printf("[%d] In callback function\n", *((int *)attr_val));
}
int main (int argc, char **argv) {
int onquit;
int rank;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
/* Create attribute with callback function */
MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, \
comm_self_delete_fn, &onquit, (void *)0);
/* Assign attribute to MPI_COMM_SELF with tank as value */
MPI_Comm_set_attr(MPI_COMM_SELF, onquit, &rank);
MPI_Comm_free_keyval(&onquit);
printf("[%d] Before finalize\n", rank);
MPI_Finalize();
printf("[%d] After finalize\n", rank);
return 0;
}
Notes:
- MPI_COMM_SELF is used (instead of, say MPI_COMM_WORLD) because that is the first object freed during MPI_Finalize() before any other parts of MPI are affected.
- MPI-2 introduces attribute caching functions for Windows and Datatypes (previously only available for Communicators): MPI_{Comm,Win,Type}_{create,set}_keyval.
- Callback function for COPY used when Communicator (or windows/datatypes) are duplicated.
No comments:
Post a Comment