Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3 more smells
[simgrid.git] / teshsuite / smpi / mpich3-test / attr / fkeyvaltype.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 #include "stdlib.h"
11
12 /*
13 static char MTestDescrip[] = "Test freeing keyvals while still attached to \
14 a datatype, then make sure that the keyval delete and copy code are still \
15 executed";
16 */
17
18 /* Copy increments the attribute value */
19 int copy_fn(MPI_Datatype oldtype, int keyval, void *extra_state,
20             void *attribute_val_in, void *attribute_val_out, int *flag);
21 int copy_fn(MPI_Datatype oldtype, int keyval, void *extra_state,
22             void *attribute_val_in, void *attribute_val_out, int *flag)
23 {
24     /* Copy the address of the attribute */
25     *(void **) attribute_val_out = attribute_val_in;
26     /* Change the value */
27     *(int *) attribute_val_in = *(int *) attribute_val_in + 1;
28     /* set flag to 1 to tell comm dup to insert this attribute
29      * into the new communicator */
30     *flag = 1;
31     return MPI_SUCCESS;
32 }
33
34 /* Delete decrements the attribute value */
35 int delete_fn(MPI_Datatype type, int keyval, void *attribute_val, void *extra_state);
36 int delete_fn(MPI_Datatype type, int keyval, void *attribute_val, void *extra_state)
37 {
38     *(int *) attribute_val = *(int *) attribute_val - 1;
39     return MPI_SUCCESS;
40 }
41
42 int main(int argc, char *argv[])
43 {
44     int errs = 0;
45     int attrval;
46     int i, key[32], keyval, saveKeyval;
47     MPI_Datatype type, duptype;
48     MTestDatatype mstype, mrtype;
49     char typename[MPI_MAX_OBJECT_NAME];
50     int tnlen;
51
52     MTest_Init(&argc, &argv);
53
54     while (MTestGetDatatypes(&mstype, &mrtype, 1)) {
55         type = mstype.datatype;
56         MPI_Type_create_keyval(copy_fn, delete_fn, &keyval, (void *) 0);
57         saveKeyval = keyval;    /* in case we need to free explicitly */
58         attrval = 1;
59         MPI_Type_set_attr(type, keyval, (void *) &attrval);
60         /* See MPI-1, 5.7.1.  Freeing the keyval does not remove it if it
61          * is in use in an attribute */
62         MPI_Type_free_keyval(&keyval);
63
64         /* We create some dummy keyvals here in case the same keyval
65          * is reused */
66         for (i = 0; i < 1; i++) {
67             MPI_Type_create_keyval(MPI_NULL_COPY_FN, MPI_NULL_DELETE_FN, &key[i], (void *) 0);
68         }
69
70         if (attrval != 1) {
71             errs++;
72             MPI_Type_get_name(type, typename, &tnlen);
73             printf("attrval is %d, should be 1, before dup in type %s\n", attrval, typename);
74         }
75         MPI_Type_dup(type, &duptype);
76         /* Check that the attribute was copied */
77         if (attrval != 2) {
78             errs++;
79             MPI_Type_get_name(type, typename, &tnlen);
80             printf("Attribute not incremented when type dup'ed (%s)\n", typename);
81         }
82         MPI_Type_free(&duptype);
83         if (attrval != 1) {
84             errs++;
85             MPI_Type_get_name(type, typename, &tnlen);
86             printf("Attribute not decremented when duptype %s freed\n", typename);
87         }
88         /* Check that the attribute was freed in the duptype */
89
90         if (!mstype.isBasic) {
91             MPI_Type_get_name(type, typename, &tnlen);
92             MTestFreeDatatype(&mstype);
93             /* Check that the original attribute was freed */
94             if (attrval != 0) {
95                 errs++;
96                 printf("Attribute not decremented when type %s freed\n", typename);
97             }
98         }
99         else {
100             /* Explicitly delete the attributes from world and self */
101             MPI_Type_delete_attr(type, saveKeyval);
102             if (mstype.buf) {
103                 free(mstype.buf);
104                 mstype.buf = 0;
105             }
106         }
107         /* Free those other keyvals */
108         for (i = 0; i < 1; i++) {
109             MPI_Type_free_keyval(&key[i]);
110         }
111         MTestFreeDatatype(&mrtype);
112     }
113     MTest_Finalize(errs);
114     MPI_Finalize();
115
116     return 0;
117
118 }