Blame test/mpi/attr/fkeyval.c

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