Logo AND Algorithmique Numérique Distribuée

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