In der Funktion my_function soll ein eigener Errorhandler verwendet werden, der an den jeweiligen übergebenen Kommunikator gebunden wird. Der vorher gültige Errorhandler wird zwischengespeichert und später wieder restauriert.
#include "mpi.h"
#include <stdio.h>
void my_errhandler( MPI_Comm *comm, int *errcode);
int my_function( MPI_Comm comm, int root);
int main( int argc, char *argv[]) {
int myid;
MPI_Init(&argc, &argv);
MPI_Comm_rank( MPI_COMM_WORLD, &myid);
/* Fehler provozieren mit root = 999 für den Broadcast in my_function */
my_function( MPI_COMM_WORLD, 999);
MPI_Finalize();
}
void my_errhandler (MPI_Comm *Comm, int *errcode)
{
char hostname[80];
char errstring[MPI_MAX_ERROR_STRING];
int resultlen, myid;
gethostname( hostname, 79);
MPI_Comm_rank( MPI_COMM_WORLD, &myid);
MPI_Error_string( *errcode, errstring, &resultlen);
printf("Proc %d on %-15.12s: UserErrorhandler - ErrorCode %d Message %s\n",
myid, hostname, *errcode, errstring);
}
int my_function( MPI_Comm comm, int root) {
int msg, myid;
MPI_Errhandler errh, store_errh;
MPI_Comm_rank( comm, &myid);
MPI_Errhandler_get(comm, &store_errh);
MPI_Errhandler_create((MPI_Handler_function *)(&my_errhandler), &errh);
MPI_Errhandler_set( comm, errh);
msg=-1;
MPI_Bcast( &msg, 1, MPI_INT, root, comm);
MPI_Errhandler_free( &errh);
MPI_Errhandler_set( comm, store_errh);
}
| Makefile | C-Quelltext |