1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
4 * (C) 2001 by Argonne National Laboratory.
5 * See COPYRIGHT in top-level directory.
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 \
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,
21 int delete_fn( MPI_Comm comm, int keyval, void *attribute_val,
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,
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 */
39 /* Delete decrements the attribute value */
40 int delete_fn( MPI_Comm comm, int keyval, void *attribute_val,
43 *(int *)attribute_val = *(int *)attribute_val - 1;
47 int main( int argc, char *argv[] )
51 int i, key[32], keyval, saveKeyval;
52 MPI_Comm comm, dupcomm;
53 MTest_Init( &argc, &argv );
55 while (MTestGetIntracomm( &comm, 1 )) {
56 if (comm == MPI_COMM_NULL) continue;
58 MPI_Comm_create_keyval( copy_fn, delete_fn, &keyval, (void *)0 );
59 saveKeyval = keyval; /* in case we need to free explicitly */
61 MPI_Comm_set_attr( 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_Comm_free_keyval( &keyval );
66 /* We create some dummy keyvals here in case the same keyval
68 for (i=0; i<32; i++) {
69 MPI_Comm_create_keyval( MPI_NULL_COPY_FN, MPI_NULL_DELETE_FN,
73 MPI_Comm_dup( comm, &dupcomm );
74 /* Check that the attribute was copied */
77 printf( "Attribute not incremented when comm dup'ed (%s)\n",
78 MTestGetIntracommName() );
80 MPI_Comm_free( &dupcomm );
83 printf( "Attribute not decremented when dupcomm %s freed\n",
84 MTestGetIntracommName() );
86 /* Check that the attribute was freed in the dupcomm */
88 if (comm != MPI_COMM_WORLD && comm != MPI_COMM_SELF) {
89 MPI_Comm_free( &comm );
90 /* Check that the original attribute was freed */
93 printf( "Attribute not decremented when comm %s freed\n",
94 MTestGetIntracommName() );
98 /* Explicitly delete the attributes from world and self */
99 MPI_Comm_delete_attr( comm, saveKeyval );
101 /* Free those other keyvals */
102 for (i=0; i<32; i++) {
103 MPI_Comm_free_keyval( &key[i] );
106 MTest_Finalize( errs );
109 /* The attributes on comm self and world were deleted by finalize
110 (see separate test) */