1 /*
2  * Copyright (C) by Argonne National Laboratory
3  *     See COPYRIGHT in top-level directory
4  */
5 
6 /*
7       The MPI-2.2 specification makes it clear that attributes are called on
8       MPI_COMM_WORLD and MPI_COMM_SELF at the very beginning of MPI_Finalize in
9       LIFO order with respect to the order in which they are set.  This is
10       useful for tools that want to perform the MPI equivalent of an "at_exit"
11       action.
12  */
13 #include <stdio.h>
14 #include "mpi.h"
15 #include "mpitest.h"
16 
17 /* 20 ought to be enough attributes to ensure that hash-table based MPI
18  * implementations do not accidentally pass the test except by being extremely
19  * "lucky".  There are (20!) possible permutations which means that there is
20  * about a 1 in 2.43e18 chance of getting LIFO ordering out of a hash table,
21  * assuming a decent hash function is used. */
22 #define NUM_TEST_ATTRS (20)
23 
24 static int exit_keys[NUM_TEST_ATTRS];   /* init to MPI_KEYVAL_INVALID */
25 static int was_called[NUM_TEST_ATTRS];
26 int foundError = 0;
27 int delete_fn(MPI_Comm, int, void *, void *);
28 
main(int argc,char ** argv)29 int main(int argc, char **argv)
30 {
31     int errs = 0, wrank;
32     int i;
33 
34     MTest_Init(&argc, &argv);
35 
36     MPI_Comm_rank(MPI_COMM_WORLD, &wrank);
37 
38 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
39     for (i = 0; i < NUM_TEST_ATTRS; ++i) {
40         exit_keys[i] = MPI_KEYVAL_INVALID;
41         was_called[i] = 0;
42 
43         /* create the keyval for the exit handler */
44         MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, delete_fn, &exit_keys[i], NULL);
45         /* attach to comm_self */
46         MPI_Comm_set_attr(MPI_COMM_SELF, exit_keys[i], (void *) (long) i);
47     }
48 
49     /* we can free the keys now */
50     for (i = 0; i < NUM_TEST_ATTRS; ++i) {
51         MPI_Comm_free_keyval(&exit_keys[i]);
52     }
53 
54     /* now, exit MPI */
55     MPI_Finalize();
56 
57     /* check that the exit handlers were called in LIFO order, and without error */
58     if (wrank == 0) {
59         /* In case more than one process exits MPI_Finalize */
60         for (i = 0; i < NUM_TEST_ATTRS; ++i) {
61             if (was_called[i] < 1) {
62                 errs++;
63                 printf("Attribute delete function on MPI_COMM_SELF was not called for idx=%d\n", i);
64             } else if (was_called[i] > 1) {
65                 errs++;
66                 printf
67                     ("Attribute delete function on MPI_COMM_SELF was called multiple times for idx=%d\n",
68                      i);
69             }
70         }
71         if (foundError != 0) {
72             errs++;
73             printf("Found %d errors while executing delete function in MPI_COMM_SELF\n",
74                    foundError);
75         }
76         if (errs == 0) {
77             printf(" No Errors\n");
78         } else {
79             printf(" Found %d errors\n", errs);
80         }
81         fflush(stdout);
82     }
83 #else /* this is a pre-MPI-2.2 implementation, ordering is not defined */
84     MPI_Finalize();
85     if (wrank == 0)
86         printf(" No Errors\n");
87 #endif
88 
89     return MTestReturnValue(errs);
90 }
91 
delete_fn(MPI_Comm comm,int keyval,void * attribute_val,void * extra_state)92 int delete_fn(MPI_Comm comm, int keyval, void *attribute_val, void *extra_state)
93 {
94     int flag;
95     int i;
96     int my_idx = (int) (long) attribute_val;
97 
98     if (my_idx < 0 || my_idx > NUM_TEST_ATTRS) {
99         printf("internal error, my_idx=%d is invalid!\n", my_idx);
100         fflush(stdout);
101     }
102 
103     was_called[my_idx]++;
104 
105     MPI_Finalized(&flag);
106     if (flag) {
107         printf("my_idx=%d, MPI_Finalized returned %d, should have been 0", my_idx, flag);
108         foundError++;
109     }
110 
111     /* since attributes were added in 0..(NUM_TEST_ATTRS-1) order, they will be
112      * called in (NUM_TEST_ATTRS-1)..0 order */
113     for (i = 0; i < my_idx; ++i) {
114         if (was_called[i] != 0) {
115             printf("my_idx=%d, was_called[%d]=%d but should be 0\n", my_idx, i, was_called[i]);
116             foundError++;
117         }
118     }
119     for (i = my_idx; i < NUM_TEST_ATTRS; ++i) {
120         if (was_called[i] != 1) {
121             printf("my_idx=%d, was_called[%d]=%d but should be 1\n", my_idx, i, was_called[i]);
122             foundError++;
123         }
124     }
125 
126     return MPI_SUCCESS;
127 }
128