Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix+activate rma test
[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, &key[nkeys], (void *) 0);
45         attrval[nkeys] = 1024 * nkeys;
46     }
47
48     /* Insert attribute in several orders.  Test after put with get,
49      * then delete, then confirm delete with get. */
50
51     MPI_Comm_set_attr(comm, key[3], &attrval[3]);
52     keyorder[0] = 3;
53     MPI_Comm_set_attr(comm, key[2], &attrval[2]);
54     keyorder[1] = 2;
55     MPI_Comm_set_attr(comm, key[0], &attrval[0]);
56     keyorder[2] = 0;
57     MPI_Comm_set_attr(comm, key[1], &attrval[1]);
58     keyorder[3] = 1;
59     MPI_Comm_set_attr(comm, key[4], &attrval[4]);
60     keyorder[4] = 4;
61
62     errs += checkAttrs(comm, NKEYS, key, attrval);
63
64     for (i = 0; i < NKEYS; i++) {
65         /* Save the key value so that we can compare it in the
66          * delete function */
67         int keyval = key[i];
68         MPI_Comm_free_keyval(&keyval);
69     }
70
71     MPI_Finalize();
72
73     if (wrank == 0) {
74         if (ncall != nkeys) {
75             printf("Deleted %d keys but should have deleted %d\n", ncall, nkeys);
76             errs++;
77         }
78         if (errs == 0)
79             printf(" No Errors\n");
80         else
81             printf(" Found %d errors\n", errs);
82     }
83     return 0;
84
85 }
86
87 int checkAttrs(MPI_Comm comm, int n, int lkey[], int attrval[])
88 {
89     int lerrs = 0;
90     int i, flag, *val_p;
91
92     for (i = 0; i < n; i++) {
93         MPI_Comm_get_attr(comm, lkey[i], &val_p, &flag);
94         if (!flag) {
95             lerrs++;
96             fprintf(stderr, "Attribute for key %d not set\n", i);
97         }
98         else if (val_p != &attrval[i]) {
99             lerrs++;
100             fprintf(stderr, "Atribute value for key %d not correct\n", i);
101         }
102     }
103
104     return lerrs;
105 }
106
107 /* We *should* be deleting key[keyorder[nkeys-ncall]] */
108 int delete_fn(MPI_Comm comm, int keyval, void *attribute_val, void *extra_state)
109 {
110     if (ncall >= nkeys) {
111         printf("delete function called too many times!\n");
112         errs++;
113     }
114
115     /* As of MPI 2.2, the order of deletion of attributes on
116      * MPI_COMM_SELF is defined */
117     if (MPI_VERSION > 2 || (MPI_VERSION == 2 && MPI_SUBVERSION >= 2)) {
118         if (keyval != key[keyorder[nkeys - 1 - ncall]]) {
119             printf("Expected key # %d but found key with value %d\n",
120                    keyorder[nkeys - 1 - ncall], keyval);
121             errs++;
122         }
123     }
124     ncall++;
125     return MPI_SUCCESS;
126 }
127
128 /*
129 int checkNoAttrs(MPI_Comm comm, int n, int lkey[])
130 {
131     int lerrs = 0;
132     int i, flag, *val_p;
133
134     for (i=0; i<n; i++) {
135         MPI_Comm_get_attr(comm, lkey[i], &val_p, &flag);
136         if (flag) {
137             lerrs++;
138             fprintf(stderr, "Attribute for key %d set but should be deleted\n", i);
139         }
140     }
141
142     return lerrs;
143 }
144 */