Blame test/mpi/cxx/rma/fkeyvalwinx.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
Packit Service c5cf8c
static char MTestDescrip[] = "Test freeing keyvals while still attached to \
Packit Service c5cf8c
a win, then make sure that the keyval delete code are still \
Packit Service c5cf8c
executed";
Packit Service c5cf8c
Packit Service c5cf8c
/* Copy increments the attribute value */
Packit Service c5cf8c
/* Note that we can really ignore this because there is no win dup */
Packit Service c5cf8c
int copy_fn(const MPI::Win & oldwin, 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
    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::Win & win, 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::Win win;
Packit Service c5cf8c
    MTest_Init();
Packit Service c5cf8c
Packit Service c5cf8c
    while (MTestGetWin(win, 0)) {
Packit Service c5cf8c
        if (win == MPI::WIN_NULL)
Packit Service c5cf8c
            continue;
Packit Service c5cf8c
Packit Service c5cf8c
        keyval = MPI::Win::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
        win.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::Win::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] =
Packit Service c5cf8c
                MPI::Win::Create_keyval(MPI::Win::NULL_COPY_FN, MPI::Win::NULL_DELETE_FN,
Packit Service c5cf8c
                                        (void *) 0);
Packit Service c5cf8c
        }
Packit Service c5cf8c
Packit Service c5cf8c
        MTestFreeWin(win);
Packit Service c5cf8c
        /* Check that the original attribute was freed */
Packit Service c5cf8c
        if (attrval != 0) {
Packit Service c5cf8c
            errs++;
Packit Service c5cf8c
            cout << "Attribute not decremented when win " << MTestGetWinName() << " freed\n";
Packit Service c5cf8c
        }
Packit Service c5cf8c
        /* Free those other keyvals */
Packit Service c5cf8c
        for (i = 0; i < 32; i++) {
Packit Service c5cf8c
            MPI::Win::Free_keyval(key[i]);
Packit Service c5cf8c
        }
Packit Service c5cf8c
    }
Packit Service c5cf8c
    MTest_Finalize(errs);
Packit Service c5cf8c
Packit Service c5cf8c
    return 0;
Packit Service c5cf8c
}