Blame test/mpi/attr/fkeyvalcomm.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *
Packit 0848f5
 *  (C) 2001 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
#include "mpi.h"
Packit 0848f5
#include <stdio.h>
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
Packit 0848f5
/*
Packit 0848f5
static char MTestDescrip[] = "Test freeing keyvals while still attached to \
Packit 0848f5
a communicator, then make sure that the keyval delete and copy code are still \
Packit 0848f5
executed";
Packit 0848f5
*/
Packit 0848f5
Packit 0848f5
/* Function prototypes to keep compilers happy */
Packit 0848f5
int copy_fn(MPI_Comm oldcomm, int keyval, void *extra_state,
Packit 0848f5
            void *attribute_val_in, void *attribute_val_out, int *flag);
Packit 0848f5
int delete_fn(MPI_Comm comm, int keyval, void *attribute_val, void *extra_state);
Packit 0848f5
Packit 0848f5
/* Copy increments the attribute value */
Packit 0848f5
int copy_fn(MPI_Comm oldcomm, int keyval, void *extra_state,
Packit 0848f5
            void *attribute_val_in, void *attribute_val_out, int *flag)
Packit 0848f5
{
Packit 0848f5
    /* Copy the address of the attribute */
Packit 0848f5
    *(void **) attribute_val_out = attribute_val_in;
Packit 0848f5
    /* Change the value */
Packit 0848f5
    *(int *) attribute_val_in = *(int *) attribute_val_in + 1;
Packit 0848f5
    /* set flag to 1 to tell comm dup to insert this attribute
Packit 0848f5
     * into the new communicator */
Packit 0848f5
    *flag = 1;
Packit 0848f5
    return MPI_SUCCESS;
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
/* Delete decrements the attribute value */
Packit 0848f5
int delete_fn(MPI_Comm comm, int keyval, void *attribute_val, void *extra_state)
Packit 0848f5
{
Packit 0848f5
    *(int *) attribute_val = *(int *) attribute_val - 1;
Packit 0848f5
    return MPI_SUCCESS;
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
int main(int argc, char *argv[])
Packit 0848f5
{
Packit 0848f5
    int errs = 0;
Packit 0848f5
    int attrval;
Packit 0848f5
    int i, key[32], keyval, saveKeyval;
Packit 0848f5
    MPI_Comm comm, dupcomm;
Packit 0848f5
    MTest_Init(&argc, &argv);
Packit 0848f5
Packit 0848f5
    while (MTestGetIntracomm(&comm, 1)) {
Packit 0848f5
        if (comm == MPI_COMM_NULL)
Packit 0848f5
            continue;
Packit 0848f5
Packit 0848f5
        MPI_Comm_create_keyval(copy_fn, delete_fn, &keyval, (void *) 0);
Packit 0848f5
        saveKeyval = keyval;    /* in case we need to free explicitly */
Packit 0848f5
        attrval = 1;
Packit 0848f5
        MPI_Comm_set_attr(comm, keyval, (void *) &attrval);
Packit 0848f5
        /* See MPI-1, 5.7.1.  Freeing the keyval does not remove it if it
Packit 0848f5
         * is in use in an attribute */
Packit 0848f5
        MPI_Comm_free_keyval(&keyval);
Packit 0848f5
Packit 0848f5
        /* We create some dummy keyvals here in case the same keyval
Packit 0848f5
         * is reused */
Packit 0848f5
        for (i = 0; i < 32; i++) {
Packit 0848f5
            MPI_Comm_create_keyval(MPI_NULL_COPY_FN, MPI_NULL_DELETE_FN, &key[i], (void *) 0);
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        MPI_Comm_dup(comm, &dupcomm);
Packit 0848f5
        /* Check that the attribute was copied */
Packit 0848f5
        if (attrval != 2) {
Packit 0848f5
            errs++;
Packit 0848f5
            printf("Attribute not incremented when comm dup'ed (%s)\n", MTestGetIntracommName());
Packit 0848f5
        }
Packit 0848f5
        MPI_Comm_free(&dupcomm);
Packit 0848f5
        if (attrval != 1) {
Packit 0848f5
            errs++;
Packit 0848f5
            printf("Attribute not decremented when dupcomm %s freed\n", MTestGetIntracommName());
Packit 0848f5
        }
Packit 0848f5
        /* Check that the attribute was freed in the dupcomm */
Packit 0848f5
Packit 0848f5
        if (comm != MPI_COMM_WORLD && comm != MPI_COMM_SELF) {
Packit 0848f5
            MPI_Comm_free(&comm);
Packit 0848f5
            /* Check that the original attribute was freed */
Packit 0848f5
            if (attrval != 0) {
Packit 0848f5
                errs++;
Packit 0848f5
                printf("Attribute not decremented when comm %s freed\n", MTestGetIntracommName());
Packit 0848f5
            }
Packit 0848f5
        }
Packit 0848f5
        else {
Packit 0848f5
            /* Explicitly delete the attributes from world and self */
Packit 0848f5
            MPI_Comm_delete_attr(comm, saveKeyval);
Packit 0848f5
        }
Packit 0848f5
        /* Free those other keyvals */
Packit 0848f5
        for (i = 0; i < 32; i++) {
Packit 0848f5
            MPI_Comm_free_keyval(&key[i]);
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
    MTest_Finalize(errs);
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
Packit 0848f5
    /* The attributes on comm self and world were deleted by finalize
Packit 0848f5
     * (see separate test) */
Packit 0848f5
Packit 0848f5
    return 0;
Packit 0848f5
Packit 0848f5
}