Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
We don't support MPI_Init( 0, 0 ), actually, and crash when this happens.
[simgrid.git] / teshsuite / smpi / mpich3-test / init / attrself.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2009 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7 #include "mpi.h"
8 #include <stdio.h>
9 #include "mpitest.h"
10
11 /*
12 static char MTestDescrip[] = "Test creating and inserting attributes in \
13 different orders to ensure that the list management code handles all cases.";
14 */
15
16 int checkAttrs( MPI_Comm, int, int [], int [] );
17 int delete_fn( MPI_Comm, int, void *, void *);
18
19 #define NKEYS 5
20 static int key[NKEYS];      /* Keys in creation order */
21 static int keyorder[NKEYS]; /* Index (into key) of keys in order added to comm 
22                             (key[keyorder[0]] is first set) */
23 static int nkeys = 0;
24 static int ncall = 0;
25 static int errs  = 0;
26 /* 
27  * Test that attributes on comm self are deleted in LIFO order 
28  */
29
30 int main( int argc, char *argv[] )
31 {
32     int      attrval[10];
33     int      wrank, i;
34     MPI_Comm comm;
35
36     MPI_Init( &argc, &argv );
37
38     MPI_Comm_rank( MPI_COMM_WORLD, &wrank );
39
40     comm = MPI_COMM_SELF;
41     
42     /* Create key values */
43     for (nkeys=0; nkeys<NKEYS; nkeys++) {
44         MPI_Comm_create_keyval( MPI_NULL_COPY_FN, delete_fn,
45                                 &key[nkeys], (void *)0 );
46         attrval[nkeys] = 1024 * nkeys;
47     }
48     
49     /* Insert attribute in several orders.  Test after put with get,
50        then delete, then confirm delete with get. */
51     
52     MPI_Comm_set_attr( comm, key[3], &attrval[3] ); keyorder[0] = 3;
53     MPI_Comm_set_attr( comm, key[2], &attrval[2] ); keyorder[1] = 2;
54     MPI_Comm_set_attr( comm, key[0], &attrval[0] ); keyorder[2] = 0;
55     MPI_Comm_set_attr( comm, key[1], &attrval[1] ); keyorder[3] = 1;
56     MPI_Comm_set_attr( comm, key[4], &attrval[4] ); keyorder[4] = 4;
57     
58     errs += checkAttrs( comm, NKEYS, key, attrval );
59     
60     for (i=0; i<NKEYS; i++) {
61         /* Save the key value so that we can compare it in the 
62            delete function */
63         int keyval = key[i];
64         MPI_Comm_free_keyval( &keyval );
65     }
66         
67     MPI_Finalize();
68     
69     if (wrank == 0) {
70         if (ncall != nkeys) {
71             printf( "Deleted %d keys but should have deleted %d\n", 
72                     ncall, nkeys );
73             errs++;
74         }
75         if (errs == 0) printf( " No Errors\n" );
76         else printf( " Found %d errors\n", errs );
77     }
78     return 0;
79   
80 }
81
82 int checkAttrs( MPI_Comm comm, int n, int lkey[], int attrval[] )
83 {
84     int lerrs = 0;
85     int i, flag, *val_p;
86
87     for (i=0; i<n; i++) {
88         MPI_Comm_get_attr( comm, lkey[i], &val_p, &flag );
89         if (!flag) {
90             lerrs++;
91             fprintf( stderr, "Attribute for key %d not set\n", i );
92         }
93         else if (val_p != &attrval[i]) {
94             lerrs++;
95             fprintf( stderr, "Attribute value for key %d not correct\n",
96                      i );
97         }
98     }
99
100     return lerrs;
101 }
102
103 /* We *should* be deleting key[keyorder[nkeys-ncall]] */
104 int delete_fn( MPI_Comm comm, int keyval, void *attribute_val, 
105                void *extra_state)
106 {
107     if (ncall >= nkeys) {
108         printf( "delete function called too many times!\n" );
109         errs++;
110     }
111
112     /* As of MPI 2.2, the order of deletion of attributes on 
113        MPI_COMM_SELF is defined */
114     if (MPI_VERSION > 2 || (MPI_VERSION == 2 && MPI_SUBVERSION >= 2)) {
115         if (keyval != key[keyorder[nkeys-1-ncall]]) {
116             printf( "Expected key # %d but found key with value %d\n", 
117                     keyorder[nkeys-1-ncall], keyval );
118             errs++;
119         }
120     }
121     ncall++;
122     return MPI_SUCCESS;
123 }
124
125 /*
126 int checkNoAttrs( MPI_Comm comm, int n, int lkey[] )
127 {
128     int lerrs = 0;
129     int i, flag, *val_p;
130
131     for (i=0; i<n; i++) {
132         MPI_Comm_get_attr( comm, lkey[i], &val_p, &flag );
133         if (flag) {
134             lerrs++;
135             fprintf( stderr, "Attribute for key %d set but should be deleted\n", i );
136         }
137     }
138
139     return lerrs;
140 }
141 */