Blame src/mpi_t/mpit_finalize.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *  (C) 2011 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
Packit 0848f5
#include "mpiimpl.h"
Packit 0848f5
Packit 0848f5
/* -- Begin Profiling Symbol Block for routine MPI_T_finalize */
Packit 0848f5
#if defined(HAVE_PRAGMA_WEAK)
Packit 0848f5
#pragma weak MPI_T_finalize = PMPI_T_finalize
Packit 0848f5
#elif defined(HAVE_PRAGMA_HP_SEC_DEF)
Packit 0848f5
#pragma _HP_SECONDARY_DEF PMPI_T_finalize  MPI_T_finalize
Packit 0848f5
#elif defined(HAVE_PRAGMA_CRI_DUP)
Packit 0848f5
#pragma _CRI duplicate MPI_T_finalize as PMPI_T_finalize
Packit 0848f5
#elif defined(HAVE_WEAK_ATTRIBUTE)
Packit 0848f5
int MPI_T_finalize(void) __attribute__((weak,alias("PMPI_T_finalize")));
Packit 0848f5
#endif
Packit 0848f5
/* -- End Profiling Symbol Block */
Packit 0848f5
Packit 0848f5
/* Define MPICH_MPI_FROM_PMPI if weak symbols are not supported to build
Packit 0848f5
   the MPI routines */
Packit 0848f5
#ifndef MPICH_MPI_FROM_PMPI
Packit 0848f5
#undef MPI_T_finalize
Packit 0848f5
#define MPI_T_finalize PMPI_T_finalize
Packit 0848f5
Packit 0848f5
/* any non-MPI functions go here, especially non-static ones */
Packit 0848f5
Packit 0848f5
static void MPIR_T_enum_env_finalize(void)
Packit 0848f5
{
Packit 0848f5
    int i, j;
Packit 0848f5
    MPIR_T_enum_t *e;
Packit 0848f5
    enum_item_t *item;
Packit 0848f5
Packit 0848f5
    if (enum_table) {
Packit 0848f5
        /* Free all entries */
Packit 0848f5
        for (i = 0; i < utarray_len(enum_table); i++) {
Packit 0848f5
            e = (MPIR_T_enum_t *)utarray_eltptr(enum_table, i);
Packit 0848f5
            MPIU_Free((void *)e->name);
Packit 0848f5
Packit 0848f5
            /* Free items in this enum */
Packit 0848f5
            for (j = 0; j < utarray_len(e->items); j++) {
Packit 0848f5
                item = (enum_item_t *)utarray_eltptr(e->items, j);
Packit 0848f5
                MPIU_Free((void *)item->name);
Packit 0848f5
            }
Packit 0848f5
Packit 0848f5
            utarray_free(e->items);
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        /* Free enum_table itself */
Packit 0848f5
        utarray_free(enum_table);
Packit 0848f5
        enum_table = NULL;
Packit 0848f5
    }
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
static void MPIR_T_cat_env_finalize(void)
Packit 0848f5
{
Packit 0848f5
    int i;
Packit 0848f5
    cat_table_entry_t *cat;
Packit 0848f5
Packit 0848f5
    if (cat_table) {
Packit 0848f5
        /* Free all entries */
Packit 0848f5
        for (i = 0; i < utarray_len(cat_table); i++) {
Packit 0848f5
            cat = (cat_table_entry_t *)utarray_eltptr(cat_table, i);
Packit 0848f5
            MPIU_Free((void *)cat->name);
Packit 0848f5
            MPIU_Free((void *)cat->desc);
Packit 0848f5
            utarray_free(cat->cvar_indices);
Packit 0848f5
            utarray_free(cat->pvar_indices);
Packit 0848f5
            utarray_free(cat->subcat_indices);
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        /* Free cat_table itself */
Packit 0848f5
        utarray_free(cat_table);
Packit 0848f5
        cat_table = NULL;
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    if (cat_hash) {
Packit 0848f5
        name2index_hash_t *current, *tmp;
Packit 0848f5
        /* Free all entries */
Packit 0848f5
        HASH_ITER(hh, cat_hash, current, tmp) {
Packit 0848f5
            HASH_DEL(cat_hash, current);
Packit 0848f5
            MPIU_Free(current);
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        /* Free cat_hash itself */
Packit 0848f5
        HASH_CLEAR(hh, cat_hash);
Packit 0848f5
        cat_hash = NULL;
Packit 0848f5
    }
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
Packit 0848f5
static void MPIR_T_cvar_env_finalize(void)
Packit 0848f5
{
Packit 0848f5
    int i;
Packit 0848f5
    cvar_table_entry_t *cvar;
Packit 0848f5
Packit 0848f5
    MPIR_T_cvar_finalize();
Packit 0848f5
Packit 0848f5
    if (cvar_table) {
Packit 0848f5
        /* Free all entries */
Packit 0848f5
        for (i = 0; i < utarray_len(cvar_table); i++) {
Packit 0848f5
            cvar = (cvar_table_entry_t *)utarray_eltptr(cvar_table, i);
Packit 0848f5
            MPIU_Free((void *)cvar->name);
Packit 0848f5
            MPIU_Free((void *)cvar->desc);
Packit 0848f5
            if (cvar->datatype == MPI_CHAR) MPIU_Free(cvar->addr);
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        /* Free pvar_table itself */
Packit 0848f5
        utarray_free(cvar_table);
Packit 0848f5
        cvar_table = NULL;
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    if (cvar_hash) {
Packit 0848f5
        name2index_hash_t *current, *tmp;
Packit 0848f5
        /* Free all entries */
Packit 0848f5
        HASH_ITER(hh, cvar_hash, current, tmp) {
Packit 0848f5
            HASH_DEL(cvar_hash, current);
Packit 0848f5
            MPIU_Free(current);
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        /* Free cvar_hash itself */
Packit 0848f5
        HASH_CLEAR(hh, cvar_hash);
Packit 0848f5
        cvar_hash = NULL;
Packit 0848f5
    }
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
static void MPIR_T_pvar_env_finalize(void)
Packit 0848f5
{
Packit 0848f5
    int i;
Packit 0848f5
    pvar_table_entry_t *pvar;
Packit 0848f5
Packit 0848f5
    if (pvar_table) {
Packit 0848f5
        /* Free all entries */
Packit 0848f5
        for (i = 0; i < utarray_len(pvar_table); i++) {
Packit 0848f5
            pvar = (pvar_table_entry_t *)utarray_eltptr(pvar_table, i);
Packit 0848f5
            MPIU_Free((void *)pvar->name);
Packit 0848f5
            MPIU_Free((void *)pvar->desc);
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        /* Free pvar_table itself */
Packit 0848f5
        utarray_free(pvar_table);
Packit 0848f5
        pvar_table = NULL;
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    for (i = 0; i < MPIR_T_PVAR_CLASS_NUMBER; i++) {
Packit 0848f5
        if (pvar_hashs[i]) {
Packit 0848f5
            name2index_hash_t *current, *tmp;
Packit 0848f5
            /* Free all entries */
Packit 0848f5
            HASH_ITER(hh, pvar_hashs[i], current, tmp) {
Packit 0848f5
                HASH_DEL(pvar_hashs[i], current);
Packit 0848f5
                MPIU_Free(current);
Packit 0848f5
            }
Packit 0848f5
Packit 0848f5
            /* Free pvar_hashs[i] itself */
Packit 0848f5
            HASH_CLEAR(hh, pvar_hashs[i]);
Packit 0848f5
            pvar_hashs[i] = NULL;
Packit 0848f5
        }
Packit 0848f5
    }
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
void MPIR_T_env_finalize(void)
Packit 0848f5
{
Packit 0848f5
    MPIR_T_enum_env_finalize();
Packit 0848f5
    MPIR_T_cvar_env_finalize();
Packit 0848f5
    MPIR_T_pvar_env_finalize();
Packit 0848f5
    MPIR_T_cat_env_finalize();
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
#endif /* MPICH_MPI_FROM_PMPI */
Packit 0848f5
Packit 0848f5
#undef FUNCNAME
Packit 0848f5
#define FUNCNAME MPI_T_finalize
Packit 0848f5
#undef FCNAME
Packit 0848f5
#define FCNAME MPL_QUOTE(FUNCNAME)
Packit 0848f5
/*@
Packit 0848f5
MPI_T_finalize - Finalize the MPI tool information interface
Packit 0848f5
Packit 0848f5
Notes:
Packit 0848f5
This routine may be called as often as the corresponding MPI_T_init_thread() routine
Packit 0848f5
up to the current point of execution. Calling it more times returns a corresponding
Packit 0848f5
error code. As long as the number of calls to MPI_T_finalize() is smaller than the
Packit 0848f5
number of calls to MPI_T_init_thread() up to the current point of execution, the MPI
Packit 0848f5
tool information interface remains initialized and calls to its routines are permissible.
Packit 0848f5
Further, additional calls to MPI_T_init_thread() after one or more calls to MPI_T_finalize()
Packit 0848f5
are permissible. Once MPI_T_finalize() is called the same number of times as the routine
Packit 0848f5
MPI_T_init_thread() up to the current point of execution, the MPI tool information
Packit 0848f5
interface is no longer initialized. The interface can be reinitialized by subsequent calls
Packit 0848f5
to MPI_T_init_thread().
Packit 0848f5
Packit 0848f5
At the end of the program execution, unless MPI_Abort() is called, an application must
Packit 0848f5
have called MPI_T_init_thread() and MPI_T_finalize() an equal number of times.
Packit 0848f5
Packit 0848f5
.N ThreadSafe
Packit 0848f5
Packit 0848f5
.N Errors
Packit 0848f5
.N MPI_SUCCESS
Packit 0848f5
.N MPI_T_ERR_NOT_INITIALIZED
Packit 0848f5
Packit 0848f5
.seealso MPI_T_init_thread
Packit 0848f5
@*/
Packit 0848f5
int MPI_T_finalize(void)
Packit 0848f5
{
Packit 0848f5
    int mpi_errno = MPI_SUCCESS;
Packit 0848f5
Packit 0848f5
    MPID_MPI_STATE_DECL(MPID_STATE_MPI_T_FINALIZE);
Packit 0848f5
    MPID_MPI_FUNC_ENTER(MPID_STATE_MPI_T_FINALIZE);
Packit 0848f5
Packit 0848f5
    /* ... body of routine ...  */
Packit 0848f5
Packit 0848f5
    --MPIR_T_init_balance;
Packit 0848f5
    if (MPIR_T_init_balance < 0) {
Packit 0848f5
        mpi_errno = MPI_T_ERR_NOT_INITIALIZED;
Packit 0848f5
        goto fn_fail;
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    if (MPIR_T_init_balance == 0) {
Packit 0848f5
        MPIR_T_THREAD_CS_FINALIZE();
Packit 0848f5
        MPIR_T_env_finalize();
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    /* ... end of body of routine ... */
Packit 0848f5
Packit 0848f5
fn_exit:
Packit 0848f5
    MPID_MPI_FUNC_EXIT(MPID_STATE_MPI_T_FINALIZE);
Packit 0848f5
    return mpi_errno;
Packit 0848f5
Packit 0848f5
fn_fail:
Packit 0848f5
    /* --BEGIN ERROR HANDLING-- */
Packit 0848f5
#   ifdef HAVE_ERROR_CHECKING
Packit 0848f5
    {
Packit 0848f5
        mpi_errno = MPIR_Err_create_code(
Packit 0848f5
            mpi_errno, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_OTHER,
Packit 0848f5
            "**mpi_t_finalize", NULL);
Packit 0848f5
    }
Packit 0848f5
#   endif
Packit 0848f5
    mpi_errno = MPIR_Err_return_comm(NULL, FCNAME, mpi_errno);
Packit 0848f5
    goto fn_exit;
Packit 0848f5
    /* --END ERROR HANDLING-- */
Packit 0848f5
}