Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add MPI_Comm_*keyval and MPI_Comm*attr functions
[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, 
21              int *flag);
22 int copy_fn( MPI_Datatype oldtype, int keyval, void *extra_state,
23              void *attribute_val_in, void *attribute_val_out, 
24              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_Datatype type, int keyval, void *attribute_val, 
38                void *extra_state);
39 int delete_fn( MPI_Datatype type, int keyval, void *attribute_val, 
40                void *extra_state)
41 {
42     *(int *)attribute_val = *(int *)attribute_val - 1;
43     return MPI_SUCCESS;
44 }
45
46 int main( int argc, char *argv[] )
47 {
48     int errs = 0;
49     int attrval;
50     int i, key[32], keyval, saveKeyval;
51     MPI_Datatype type, duptype;
52     MTestDatatype mstype, mrtype;
53     char typename[MPI_MAX_OBJECT_NAME];
54     int tnlen;
55
56     MTest_Init( &argc, &argv );
57
58     while (MTestGetDatatypes( &mstype, &mrtype, 1 )) {
59         type = mstype.datatype;
60         MPI_Type_create_keyval( copy_fn, delete_fn, &keyval, (void *)0 );
61         saveKeyval = keyval;   /* in case we need to free explicitly */
62         attrval = 1;
63         MPI_Type_set_attr( type, keyval, (void*)&attrval );
64         /* See MPI-1, 5.7.1.  Freeing the keyval does not remove it if it
65            is in use in an attribute */
66         MPI_Type_free_keyval( &keyval );
67         
68         /* We create some dummy keyvals here in case the same keyval
69            is reused */
70         for (i=0; i<32; i++) {
71             MPI_Type_create_keyval( MPI_NULL_COPY_FN, MPI_NULL_DELETE_FN,
72                                &key[i], (void *)0 );
73         }
74
75         if (attrval != 1) {
76             errs++;
77             MPI_Type_get_name( type, typename, &tnlen );
78             printf( "attrval is %d, should be 1, before dup in type %s\n",
79                      attrval, typename );
80         }
81         MPI_Type_dup( type, &duptype );
82         /* Check that the attribute was copied */
83         if (attrval != 2) {
84             errs++;
85             MPI_Type_get_name( type, typename, &tnlen );
86             printf( "Attribute not incremented when type dup'ed (%s)\n",
87                      typename );
88         }
89         MPI_Type_free( &duptype );
90         if (attrval != 1) {
91             errs++;
92             MPI_Type_get_name( type, typename, &tnlen );
93             printf( "Attribute not decremented when duptype %s freed\n",
94                     typename );
95         }
96         /* Check that the attribute was freed in the duptype */
97
98         if (!mstype.isBasic) {
99             MPI_Type_get_name( type, typename, &tnlen );
100             MTestFreeDatatype(&mstype);
101             /* Check that the original attribute was freed */
102             if (attrval != 0) {
103                 errs++;
104                 printf( "Attribute not decremented when type %s freed\n",
105                         typename );
106             }
107         }
108         else {
109             /* Explicitly delete the attributes from world and self */
110             MPI_Type_delete_attr( type, saveKeyval );
111             if (mstype.buf) {
112                 free(mstype.buf);
113                 mstype.buf = 0;
114             }
115         }
116         /* Free those other keyvals */
117         for (i=0; i<32; i++) {
118             MPI_Type_free_keyval( &key[i] );
119         }
120         MTestFreeDatatype(&mrtype);
121     }
122     MTest_Finalize( errs );
123     MPI_Finalize();
124
125     return 0;
126   
127 }