Blame test/mpi/attr/attrend.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *
Packit 0848f5
 *  (C) 2008 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
/*
Packit 0848f5
      The MPI-2 specification makes it clear that delect attributes are
Packit 0848f5
      called on MPI_COMM_WORLD and MPI_COMM_SELF at the very beginning of
Packit 0848f5
      MPI_Finalize.  This is useful for tools that want to perform the MPI
Packit 0848f5
      equivalent of an "at_exit" action.
Packit 0848f5
 */
Packit 0848f5
#include <stdio.h>
Packit 0848f5
#include "mpi.h"
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
Packit 0848f5
int exit_key = MPI_KEYVAL_INVALID;
Packit 0848f5
int wasCalled = 0;
Packit 0848f5
int foundError = 0;
Packit 0848f5
/* #define DEBUG */
Packit 0848f5
int delete_fn(MPI_Comm, int, void *, void *);
Packit 0848f5
#ifdef DEBUG
Packit 0848f5
#define FFLUSH fflush(stdout);
Packit 0848f5
#else
Packit 0848f5
#define FFLUSH
Packit 0848f5
#endif
Packit 0848f5
Packit 0848f5
int main(int argc, char **argv)
Packit 0848f5
{
Packit 0848f5
    int errs = 0, wrank;
Packit 0848f5
Packit 0848f5
    MTest_Init(&argc, &argv);
Packit 0848f5
Packit 0848f5
    MPI_Comm_rank(MPI_COMM_WORLD, &wrank);
Packit 0848f5
Packit 0848f5
    /* create the keyval for the exit handler */
Packit 0848f5
    MPI_Keyval_create(MPI_NULL_COPY_FN, delete_fn, &exit_key, (void *) 0);
Packit 0848f5
Packit 0848f5
    /* Attach to comm_self */
Packit 0848f5
    MPI_Attr_put(MPI_COMM_SELF, exit_key, (void *) 0);
Packit 0848f5
    /* We can free the key now */
Packit 0848f5
    MPI_Keyval_free(&exit_key);
Packit 0848f5
Packit 0848f5
    /* Now, exit MPI */
Packit 0848f5
    /* MTest_Finalize(errs); */
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
Packit 0848f5
    /* Check that the exit handler was called, and without error */
Packit 0848f5
    if (wrank == 0) {
Packit 0848f5
        /* In case more than one process exits MPI_Finalize */
Packit 0848f5
        if (wasCalled != 1) {
Packit 0848f5
            errs++;
Packit 0848f5
            printf("Attribute delete function on MPI_COMM_SELF was not called\n");
Packit 0848f5
        }
Packit 0848f5
        if (foundError != 0) {
Packit 0848f5
            errs++;
Packit 0848f5
            printf("Found %d errors while executing delete function in MPI_COMM_SELF\n",
Packit 0848f5
                   foundError);
Packit 0848f5
        }
Packit 0848f5
        if (errs == 0) {
Packit 0848f5
            printf(" No Errors\n");
Packit 0848f5
        }
Packit 0848f5
        else {
Packit 0848f5
            printf(" Found %d errors\n", errs);
Packit 0848f5
        }
Packit 0848f5
        fflush(stdout);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    return 0;
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
int delete_fn(MPI_Comm comm, int keyval, void *attribute_val, void *extra_state)
Packit 0848f5
{
Packit 0848f5
    int flag;
Packit 0848f5
    wasCalled++;
Packit 0848f5
    MPI_Finalized(&flag;;
Packit 0848f5
    if (flag) {
Packit 0848f5
        foundError++;
Packit 0848f5
    }
Packit 0848f5
    return MPI_SUCCESS;
Packit 0848f5
}