Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://github.com/mpoquet/simgrid
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / fkeyvalwin.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 <stdlib.h>
10 #include "mpitestconf.h"
11 #include "mpitest.h"
12 #ifdef HAVE_STRING_H
13 #include <string.h>
14 #endif
15
16 /*
17 static char MTestDescrip[] = "Test freeing keyvals while still attached to \
18 a win, then make sure that the keyval delete code are still \
19 executed";
20 */
21
22 /* Copy increments the attribute value */
23 /* Note that we can really ignore this because there is no win dup */
24 int copy_fn( MPI_Win oldwin, int keyval, void *extra_state,
25              void *attribute_val_in, void *attribute_val_out, 
26              int *flag);
27 int copy_fn( MPI_Win oldwin, int keyval, void *extra_state,
28              void *attribute_val_in, void *attribute_val_out, 
29              int *flag)
30 {
31     /* Copy the address of the attribute */
32     *(void **)attribute_val_out = attribute_val_in;
33     /* Change the value */
34     *(int *)attribute_val_in = *(int *)attribute_val_in + 1;
35     *flag = 1;
36     return MPI_SUCCESS;
37 }
38
39 /* Delete decrements the attribute value */
40 int delete_fn( MPI_Win win, int keyval, void *attribute_val, 
41                void *extra_state);
42 int delete_fn( MPI_Win win, int keyval, void *attribute_val, 
43                void *extra_state)
44 {
45     *(int *)attribute_val = *(int *)attribute_val - 1;
46     return MPI_SUCCESS;
47 }
48
49 int main( int argc, char *argv[] )
50 {
51     int errs = 0;
52     int attrval;
53     int i, key[32], keyval, saveKeyval;
54     MPI_Win win;
55     MTest_Init( &argc, &argv );
56
57     while (MTestGetWin( &win, 0 )) {
58         if (win == MPI_WIN_NULL) continue;
59
60         MPI_Win_create_keyval( copy_fn, delete_fn, &keyval, (void *)0 );
61         saveKeyval = keyval;   /* in case we need to free explicitly */
62         attrval = 1;
63         MPI_Win_set_attr( win, 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_Win_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_Win_create_keyval( MPI_NULL_COPY_FN, MPI_NULL_DELETE_FN,
72                                &key[i], (void *)0 );
73         }
74
75         MTestFreeWin(&win);
76
77         /* Check that the original attribute was freed */
78         if (attrval != 0) {
79             errs++;
80             printf( "Attribute not decremented when win %s freed\n",
81                     MTestGetWinName() );
82         }
83         /* Free those other keyvals */
84         for (i=0; i<32; i++) {
85             MPI_Win_free_keyval( &key[i] );
86         }
87
88     }
89     MTest_Finalize( errs );
90     MPI_Finalize();
91
92     return 0;
93 }