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, int *flag);
20 int delete_fn(MPI_Comm comm, int keyval, void *attribute_val, void *extra_state);
22 /* Copy increments the attribute value */
23 int copy_fn(MPI_Comm oldcomm, int keyval, void *extra_state,
24 void *attribute_val_in, void *attribute_val_out, int *flag)
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 */
36 /* Delete decrements the attribute value */
37 int delete_fn(MPI_Comm comm, int keyval, void *attribute_val, void *extra_state)
39 *(int *) attribute_val = *(int *) attribute_val - 1;
43 int main(int argc, char *argv[])
47 int i, key[32], keyval, saveKeyval;
48 MPI_Comm comm, dupcomm;
49 MTest_Init(&argc, &argv);
51 while (MTestGetIntracomm(&comm, 1)) {
52 if (comm == MPI_COMM_NULL)
55 MPI_Comm_create_keyval(copy_fn, delete_fn, &keyval, (void *) 0);
56 saveKeyval = keyval; /* in case we need to free explicitly */
58 MPI_Comm_set_attr(comm, keyval, (void *) &attrval);
59 /* See MPI-1, 5.7.1. Freeing the keyval does not remove it if it
60 * is in use in an attribute */
61 MPI_Comm_free_keyval(&keyval);
63 /* We create some dummy keyvals here in case the same keyval
65 for (i = 0; i < 32; i++) {
66 MPI_Comm_create_keyval(MPI_NULL_COPY_FN, MPI_NULL_DELETE_FN, &key[i], (void *) 0);
69 MPI_Comm_dup(comm, &dupcomm);
70 /* Check that the attribute was copied */
73 printf("Attribute not incremented when comm dup'ed (%s)\n", MTestGetIntracommName());
75 MPI_Comm_free(&dupcomm);
78 printf("Attribute not decremented when dupcomm %s freed\n", MTestGetIntracommName());
80 /* Check that the attribute was freed in the dupcomm */
82 if (comm != MPI_COMM_WORLD && comm != MPI_COMM_SELF) {
84 /* Check that the original attribute was freed */
87 printf("Attribute not decremented when comm %s freed\n", MTestGetIntracommName());
91 /* Explicitly delete the attributes from world and self */
92 MPI_Comm_delete_attr(comm, saveKeyval);
94 /* Free those other keyvals */
95 for (i = 0; i < 32; i++) {
96 MPI_Comm_free_keyval(&key[i]);
102 /* The attributes on comm self and world were deleted by finalize
103 * (see separate test) */