Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / teshsuite / smpi / mpich3-test / attr / fkeyval.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2001 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 freeing keyvals while still attached to \
13 a communicator, then make sure that the keyval delete and copy code are still \
14 executed";
15 */
16
17 /* Function prototypes to keep compilers happy */
18 int copy_fn(MPI_Comm oldcomm, int keyval, void *extra_state,
19             void *attribute_val_in, void *attribute_val_out, int *flag);
20 int delete_fn(MPI_Comm comm, int keyval, void *attribute_val, void *extra_state);
21
22 /* Copy increments the attribute value */
23 int copy_fn(MPI_Comm oldcomm, int keyval, void *extra_state,
24             void *attribute_val_in, void *attribute_val_out, int *flag)
25 {
26     /* Copy the address of the attribute */
27     *(void **) attribute_val_out = attribute_val_in;
28     /* Change the value */
29     *(int *) attribute_val_in = *(int *) attribute_val_in + 1;
30     /* set flag to 1 to tell comm dup to insert this attribute
31      * into the new communicator */
32     *flag = 1;
33     return MPI_SUCCESS;
34 }
35
36 /* Delete decrements the attribute value */
37 int delete_fn(MPI_Comm comm, int keyval, void *attribute_val, void *extra_state)
38 {
39     *(int *) attribute_val = *(int *) attribute_val - 1;
40     return MPI_SUCCESS;
41 }
42
43 int main(int argc, char *argv[])
44 {
45     int errs = 0;
46     int attrval;
47     int i, key[32], keyval, saveKeyval;
48     MPI_Comm comm, dupcomm;
49     MTest_Init(&argc, &argv);
50
51     while (MTestGetIntracomm(&comm, 1)) {
52         if (comm == MPI_COMM_NULL)
53             continue;
54
55         MPI_Keyval_create(copy_fn, delete_fn, &keyval, (void *) 0);
56         saveKeyval = keyval;    /* in case we need to free explicitly */
57         attrval = 1;
58         MPI_Attr_put(comm, keyval, (void *) &attrval);
59         /* See MPI-1, 5.7.1.  Freeing the keyval does not remove it if it
60          * is in use in an attribute */
61         MPI_Keyval_free(&keyval);
62
63         /* We create some dummy keyvals here in case the same keyval
64          * is reused */
65         for (i = 0; i < 32; i++) {
66             MPI_Keyval_create(MPI_NULL_COPY_FN, MPI_NULL_DELETE_FN, &key[i], (void *) 0);
67         }
68
69         MPI_Comm_dup(comm, &dupcomm);
70         /* Check that the attribute was copied */
71         if (attrval != 2) {
72             errs++;
73             printf("Attribute not incremented when comm dup'ed (%s)\n", MTestGetIntracommName());
74         }
75         MPI_Comm_free(&dupcomm);
76         if (attrval != 1) {
77             errs++;
78             printf("Attribute not decremented when dupcomm %s freed\n", MTestGetIntracommName());
79         }
80         /* Check that the attribute was freed in the dupcomm */
81
82         if (comm != MPI_COMM_WORLD && comm != MPI_COMM_SELF) {
83             MPI_Comm_free(&comm);
84             /* Check that the original attribute was freed */
85             if (attrval != 0) {
86                 errs++;
87                 printf("Attribute not decremented when comm %s freed\n", MTestGetIntracommName());
88             }
89         }
90         else {
91             /* Explicitly delete the attributes from world and self */
92             MPI_Attr_delete(comm, saveKeyval);
93         }
94         /* Free those other keyvals */
95         for (i = 0; i < 32; i++) {
96             MPI_Keyval_free(&key[i]);
97         }
98     }
99     MTest_Finalize(errs);
100     MPI_Finalize();
101
102     /* The attributes on comm self and world were deleted by finalize
103      * (see separate test) */
104
105     return 0;
106 }