Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge remote-tracking branch 'origin/libdw2'
[simgrid.git] / teshsuite / smpi / mpich3-test / attr / attrend.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2008 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7 /*
8       The MPI-2 specification makes it clear that delect attributes are 
9       called on MPI_COMM_WORLD and MPI_COMM_SELF at the very beginning of
10       MPI_Finalize.  This is useful for tools that want to perform the MPI 
11       equivalent of an "at_exit" action.
12  */
13 #include <stdio.h>
14 #include "mpi.h"
15 #include "mpitest.h"
16
17 int exit_key = MPI_KEYVAL_INVALID;
18 int wasCalled = 0;
19 int foundError = 0;
20 /* #define DEBUG */
21 int delete_fn ( MPI_Comm, int, void *, void * );
22 #ifdef DEBUG
23 #define FFLUSH fflush(stdout);
24 #else
25 #define FFLUSH
26 #endif
27
28 int main( int argc, char **argv )
29 {
30     int errs = 0, wrank;
31
32     MTest_Init( &argc, &argv );
33
34     MPI_Comm_rank( MPI_COMM_WORLD, &wrank );
35     
36     /* create the keyval for the exit handler */
37     MPI_Keyval_create( MPI_NULL_COPY_FN, delete_fn, &exit_key, (void *)0 );
38
39     /* Attach to comm_self */
40     MPI_Attr_put( MPI_COMM_SELF, exit_key, (void*)0 );
41     /* We can free the key now */
42     MPI_Keyval_free( &exit_key );
43
44     /* Now, exit MPI */
45     /* MTest_Finalize( errs ); */
46     MPI_Finalize();
47
48     /* Check that the exit handler was called, and without error */
49     if (wrank == 0) {
50         /* In case more than one process exits MPI_Finalize */
51         if (wasCalled != 1) {
52             errs++;
53             printf( "Attribute delete function on MPI_COMM_SELF was not called\n" );
54         }
55         if (foundError != 0) {
56             errs++;
57             printf( "Found %d errors while executing delete function in MPI_COMM_SELF\n", foundError );
58         }
59         if (errs == 0) {
60             printf( " No Errors\n" );
61         }
62         else { 
63             printf( " Found %d errors\n", errs );
64         }
65         fflush(stdout );
66     }
67
68     return 0;
69 }
70
71 int delete_fn( MPI_Comm comm, int keyval, void *attribute_val, 
72                void *extra_state)
73 {
74     int flag;
75     wasCalled++;
76     MPI_Finalized( &flag );
77     if (flag) {
78         foundError++;
79     }
80     return MPI_SUCCESS;
81 }
82