Blame test/mpi/cxx/attr/fkeyvalcommx.cxx

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
Packit Service c5cf8c
#include "mpitestconf.h"
Packit Service c5cf8c
#ifdef HAVE_IOSTREAM
Packit Service c5cf8c
// Not all C++ compilers have iostream instead of iostream.h
Packit Service c5cf8c
#include <iostream>
Packit Service c5cf8c
#ifdef HAVE_NAMESPACE_STD
Packit Service c5cf8c
// Those that do often need the std namespace; otherwise, a bare "cout"
Packit Service c5cf8c
// is likely to fail to compile
Packit Service c5cf8c
using namespace std;
Packit Service c5cf8c
#endif
Packit Service c5cf8c
#else
Packit Service c5cf8c
#include <iostream.h>
Packit Service c5cf8c
#endif
Packit Service c5cf8c
#include <stdio.h>
Packit Service c5cf8c
#include "mpitestcxx.h"
Packit Service c5cf8c
#ifdef HAVE_STRING_H
Packit Service c5cf8c
#include <string.h>
Packit Service c5cf8c
#endif
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
/* Copy increments the attribute value */
Packit Service c5cf8c
int copy_fn(const MPI::Comm & oldcomm, int keyval, void *extra_state,
Packit Service c5cf8c
            void *attribute_val_in, void *attribute_val_out, bool & 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::Intracomm comm, dupcomm;
Packit Service c5cf8c
Packit Service c5cf8c
    MTest_Init();
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
        keyval = MPI::Comm::Create_keyval(copy_fn, delete_fn, (void *) 0);
Packit Service c5cf8c
        saveKeyval = keyval;    /* in case we need to free explicitly */
Packit Service c5cf8c
        attrval = 1;
Packit Service c5cf8c
        comm.Set_attr(keyval, &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::Comm::Free_keyval(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
            key[i] = MPI::Comm::Create_keyval(MPI::Comm::NULL_COPY_FN,
Packit Service c5cf8c
                                              MPI::Comm::NULL_DELETE_FN, (void *) 0);
Packit Service c5cf8c
        }
Packit Service c5cf8c
Packit Service c5cf8c
        dupcomm = comm.Dup();
Packit Service c5cf8c
        /* Check that the attribute was copied */
Packit Service c5cf8c
        if (attrval != 2) {
Packit Service c5cf8c
            errs++;
Packit Service c5cf8c
            cout << "Attribute not incremented when comm dup'ed, attrval = " <<
Packit Service c5cf8c
                attrval << " should be 2 in communicator " << MTestGetIntracommName() << "\n";
Packit Service c5cf8c
        }
Packit Service c5cf8c
        dupcomm.Free();
Packit Service c5cf8c
        if (attrval != 1) {
Packit Service c5cf8c
            errs++;
Packit Service c5cf8c
            cout << "Attribute not decremented when comm dup'ed, attrval = " <<
Packit Service c5cf8c
                attrval << " should be 1 in communicator " << MTestGetIntracommName() << "\n";
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
            comm.Free();
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
            comm.Delete_attr(saveKeyval);
Packit Service c5cf8c
        }
Packit Service c5cf8c
        /* Free those other keyvals */
Packit Service c5cf8c
        for (i = 0; i < 32; i++) {
Packit Service c5cf8c
            MPI::Comm::Free_keyval(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 0;
Packit Service c5cf8c
Packit Service c5cf8c
}