Blame src/mpi/attr/comm_set_attr.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
Packit Service c5cf8c
#include "mpiimpl.h"
Packit Service c5cf8c
#include "attr.h"
Packit Service c5cf8c
Packit Service c5cf8c
/* -- Begin Profiling Symbol Block for routine MPI_Comm_set_attr */
Packit Service c5cf8c
#if defined(HAVE_PRAGMA_WEAK)
Packit Service c5cf8c
#pragma weak MPI_Comm_set_attr = PMPI_Comm_set_attr
Packit Service c5cf8c
#elif defined(HAVE_PRAGMA_HP_SEC_DEF)
Packit Service c5cf8c
#pragma _HP_SECONDARY_DEF PMPI_Comm_set_attr  MPI_Comm_set_attr
Packit Service c5cf8c
#elif defined(HAVE_PRAGMA_CRI_DUP)
Packit Service c5cf8c
#pragma _CRI duplicate MPI_Comm_set_attr as PMPI_Comm_set_attr
Packit Service c5cf8c
#elif defined(HAVE_WEAK_ATTRIBUTE)
Packit Service c5cf8c
int MPI_Comm_set_attr(MPI_Comm comm, int comm_keyval, void *attribute_val)
Packit Service c5cf8c
    __attribute__ ((weak, alias("PMPI_Comm_set_attr")));
Packit Service c5cf8c
#endif
Packit Service c5cf8c
/* -- End Profiling Symbol Block */
Packit Service c5cf8c
Packit Service c5cf8c
/* Define MPICH_MPI_FROM_PMPI if weak symbols are not supported to build
Packit Service c5cf8c
   the MPI routines */
Packit Service c5cf8c
#ifndef MPICH_MPI_FROM_PMPI
Packit Service c5cf8c
#undef MPI_Comm_set_attr
Packit Service c5cf8c
#define MPI_Comm_set_attr PMPI_Comm_set_attr
Packit Service c5cf8c
Packit Service c5cf8c
#undef FUNCNAME
Packit Service c5cf8c
#define FUNCNAME MPIR_Comm_set_attr_impl
Packit Service c5cf8c
#undef FCNAME
Packit Service c5cf8c
#define FCNAME MPL_QUOTE(FUNCNAME)
Packit Service c5cf8c
int MPIR_Comm_set_attr_impl(MPIR_Comm * comm_ptr, int comm_keyval, void *attribute_val,
Packit Service c5cf8c
                            MPIR_Attr_type attrType)
Packit Service c5cf8c
{
Packit Service c5cf8c
    int mpi_errno = MPI_SUCCESS;
Packit Service c5cf8c
    MPII_Keyval *keyval_ptr = NULL;
Packit Service c5cf8c
    MPIR_Attribute *p;
Packit Service c5cf8c
Packit Service c5cf8c
    MPIR_ERR_CHKANDJUMP(comm_keyval == MPI_KEYVAL_INVALID, mpi_errno, MPI_ERR_KEYVAL,
Packit Service c5cf8c
                        "**keyvalinvalid");
Packit Service c5cf8c
Packit Service c5cf8c
    /* CHANGE FOR MPI 2.2:  Look for attribute.  They are ordered by when they
Packit Service c5cf8c
     * were added, with the most recent first. This uses
Packit Service c5cf8c
     * a simple linear list algorithm because few applications use more than a
Packit Service c5cf8c
     * handful of attributes */
Packit Service c5cf8c
Packit Service c5cf8c
    MPII_Keyval_get_ptr(comm_keyval, keyval_ptr);
Packit Service c5cf8c
    MPIR_Assert(keyval_ptr != NULL);
Packit Service c5cf8c
Packit Service c5cf8c
    /* printf("Setting attr val to %x\n", attribute_val); */
Packit Service c5cf8c
    p = comm_ptr->attributes;
Packit Service c5cf8c
    while (p) {
Packit Service c5cf8c
        if (p->keyval->handle == keyval_ptr->handle) {
Packit Service c5cf8c
            /* If found, call the delete function before replacing the
Packit Service c5cf8c
             * attribute */
Packit Service c5cf8c
            mpi_errno = MPIR_Call_attr_delete(comm_ptr->handle, p);
Packit Service c5cf8c
            if (mpi_errno) {
Packit Service c5cf8c
                goto fn_fail;
Packit Service c5cf8c
            }
Packit Service c5cf8c
            p->attrType = attrType;
Packit Service c5cf8c
            /* FIXME: This code is incorrect in some cases, particularly
Packit Service c5cf8c
             * in the case where intptr_t is different from MPI_Aint,
Packit Service c5cf8c
             * since in that case, the Fortran 9x interface will provide
Packit Service c5cf8c
             * more bytes in the attribute_val than this allows. The
Packit Service c5cf8c
             * dual casts are a sign that this is faulty. This will
Packit Service c5cf8c
             * need to be fixed in the type/win set_attr routines as
Packit Service c5cf8c
             * well. */
Packit Service c5cf8c
            p->value = (MPII_Attr_val_t) (intptr_t) attribute_val;
Packit Service c5cf8c
            /* printf("Updating attr at %x\n", &p->value); */
Packit Service c5cf8c
            /* Does not change the reference count on the keyval */
Packit Service c5cf8c
            break;
Packit Service c5cf8c
        }
Packit Service c5cf8c
        p = p->next;
Packit Service c5cf8c
    }
Packit Service c5cf8c
    /* CHANGE FOR MPI 2.2: If not found, add at the beginning */
Packit Service c5cf8c
    if (!p) {
Packit Service c5cf8c
        MPIR_Attribute *new_p = MPID_Attr_alloc();
Packit Service c5cf8c
        MPIR_ERR_CHKANDJUMP(!new_p, mpi_errno, MPI_ERR_OTHER, "**nomem");
Packit Service c5cf8c
        /* Did not find in list.  Add at end */
Packit Service c5cf8c
        new_p->keyval = keyval_ptr;
Packit Service c5cf8c
        new_p->attrType = attrType;
Packit Service c5cf8c
        new_p->pre_sentinal = 0;
Packit Service c5cf8c
        /* FIXME: See the comment above on this dual cast. */
Packit Service c5cf8c
        new_p->value = (MPII_Attr_val_t) (intptr_t) attribute_val;
Packit Service c5cf8c
        new_p->post_sentinal = 0;
Packit Service c5cf8c
        new_p->next = comm_ptr->attributes;
Packit Service c5cf8c
        MPII_Keyval_add_ref(keyval_ptr);
Packit Service c5cf8c
        comm_ptr->attributes = new_p;
Packit Service c5cf8c
        /* printf("Creating attr at %x\n", &new_p->value); */
Packit Service c5cf8c
    }
Packit Service c5cf8c
Packit Service c5cf8c
    /* Here is where we could add a hook for the device to detect attribute
Packit Service c5cf8c
     * value changes, using something like
Packit Service c5cf8c
     * MPID_Comm_attr_hook(comm_ptr, keyval, attribute_val);
Packit Service c5cf8c
     */
Packit Service c5cf8c
Packit Service c5cf8c
Packit Service c5cf8c
  fn_exit:
Packit Service c5cf8c
    return mpi_errno;
Packit Service c5cf8c
  fn_fail:
Packit Service c5cf8c
    goto fn_exit;
Packit Service c5cf8c
}
Packit Service c5cf8c
Packit Service c5cf8c
Packit Service c5cf8c
#undef FUNCNAME
Packit Service c5cf8c
#define FUNCNAME MPII_Comm_set_attr
Packit Service c5cf8c
#undef FCNAME
Packit Service c5cf8c
#define FCNAME MPL_QUOTE(FUNCNAME)
Packit Service c5cf8c
int MPII_Comm_set_attr(MPI_Comm comm, int comm_keyval, void *attribute_val, MPIR_Attr_type attrType)
Packit Service c5cf8c
{
Packit Service c5cf8c
    int mpi_errno = MPI_SUCCESS;
Packit Service c5cf8c
    MPIR_Comm *comm_ptr = NULL;
Packit Service c5cf8c
    MPIR_FUNC_TERSE_STATE_DECL(MPID_STATE_MPIR_COMM_SET_ATTR);
Packit Service c5cf8c
Packit Service c5cf8c
    MPIR_ERRTEST_INITIALIZED_ORDIE();
Packit Service c5cf8c
Packit Service c5cf8c
    MPID_THREAD_CS_ENTER(GLOBAL, MPIR_THREAD_GLOBAL_ALLFUNC_MUTEX);
Packit Service c5cf8c
    MPIR_FUNC_TERSE_ENTER(MPID_STATE_MPIR_COMM_SET_ATTR);
Packit Service c5cf8c
Packit Service c5cf8c
    /* Validate parameters, especially handles needing to be converted */
Packit Service c5cf8c
#ifdef HAVE_ERROR_CHECKING
Packit Service c5cf8c
    {
Packit Service c5cf8c
        MPID_BEGIN_ERROR_CHECKS;
Packit Service c5cf8c
        {
Packit Service c5cf8c
            MPIR_ERRTEST_COMM(comm, mpi_errno);
Packit Service c5cf8c
            MPIR_ERRTEST_KEYVAL(comm_keyval, MPIR_COMM, "communicator", mpi_errno);
Packit Service c5cf8c
            MPIR_ERRTEST_KEYVAL_PERM(comm_keyval, mpi_errno);
Packit Service c5cf8c
        }
Packit Service c5cf8c
        MPID_END_ERROR_CHECKS;
Packit Service c5cf8c
    }
Packit Service c5cf8c
#endif
Packit Service c5cf8c
Packit Service c5cf8c
    /* Convert MPI object handles to object pointers */
Packit Service c5cf8c
    MPIR_Comm_get_ptr(comm, comm_ptr);
Packit Service c5cf8c
Packit Service c5cf8c
    /* Validate parameters and objects (post conversion) */
Packit Service c5cf8c
#ifdef HAVE_ERROR_CHECKING
Packit Service c5cf8c
    {
Packit Service c5cf8c
        MPID_BEGIN_ERROR_CHECKS;
Packit Service c5cf8c
        {
Packit Service c5cf8c
            MPII_Keyval *keyval_ptr = NULL;
Packit Service c5cf8c
Packit Service c5cf8c
            /* Validate comm_ptr */
Packit Service c5cf8c
            MPIR_Comm_valid_ptr(comm_ptr, mpi_errno, TRUE);
Packit Service c5cf8c
            /* If comm_ptr is not valid, it will be reset to null */
Packit Service c5cf8c
            /* Validate keyval_ptr */
Packit Service c5cf8c
            MPII_Keyval_get_ptr(comm_keyval, keyval_ptr);
Packit Service c5cf8c
            MPII_Keyval_valid_ptr(keyval_ptr, mpi_errno);
Packit Service c5cf8c
            if (mpi_errno)
Packit Service c5cf8c
                goto fn_fail;
Packit Service c5cf8c
        }
Packit Service c5cf8c
        MPID_END_ERROR_CHECKS;
Packit Service c5cf8c
    }
Packit Service c5cf8c
#endif /* HAVE_ERROR_CHECKING */
Packit Service c5cf8c
Packit Service c5cf8c
    /* ... body of routine ...  */
Packit Service c5cf8c
    mpi_errno = MPIR_Comm_set_attr_impl(comm_ptr, comm_keyval, attribute_val, attrType);
Packit Service c5cf8c
    if (mpi_errno)
Packit Service c5cf8c
        goto fn_fail;
Packit Service c5cf8c
Packit Service c5cf8c
    /* ... end of body of routine ... */
Packit Service c5cf8c
Packit Service c5cf8c
  fn_exit:
Packit Service c5cf8c
    MPIR_FUNC_TERSE_EXIT(MPID_STATE_MPIR_COMM_SET_ATTR);
Packit Service c5cf8c
    MPID_THREAD_CS_EXIT(GLOBAL, MPIR_THREAD_GLOBAL_ALLFUNC_MUTEX);
Packit Service c5cf8c
    return mpi_errno;
Packit Service c5cf8c
Packit Service c5cf8c
  fn_fail:
Packit Service c5cf8c
    /* --BEGIN ERROR HANDLING-- */
Packit Service c5cf8c
#ifdef HAVE_ERROR_CHECKING
Packit Service c5cf8c
    {
Packit Service c5cf8c
        mpi_errno =
Packit Service c5cf8c
            MPIR_Err_create_code(mpi_errno, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_OTHER,
Packit Service c5cf8c
                                 "**mpi_comm_set_attr", "**mpi_comm_set_attr %C %d %p", comm,
Packit Service c5cf8c
                                 comm_keyval, attribute_val);
Packit Service c5cf8c
    }
Packit Service c5cf8c
#endif
Packit Service c5cf8c
    mpi_errno = MPIR_Err_return_comm(comm_ptr, FCNAME, mpi_errno);
Packit Service c5cf8c
    goto fn_exit;
Packit Service c5cf8c
    /* --END ERROR HANDLING-- */
Packit Service c5cf8c
}
Packit Service c5cf8c
#endif /* MPICH_MPI_FROM_PMPI */
Packit Service c5cf8c
Packit Service c5cf8c
#undef FUNCNAME
Packit Service c5cf8c
#define FUNCNAME MPI_Comm_set_attr
Packit Service c5cf8c
#undef FCNAME
Packit Service c5cf8c
#define FCNAME MPL_QUOTE(FUNCNAME)
Packit Service c5cf8c
/*@
Packit Service c5cf8c
   MPI_Comm_set_attr - Stores attribute value associated with a key
Packit Service c5cf8c
Packit Service c5cf8c
Input Parameters:
Packit Service c5cf8c
+ comm - communicator to which attribute will be attached (handle)
Packit Service c5cf8c
. comm_keyval - key value, as returned by  'MPI_Comm_create_keyval' (integer)
Packit Service c5cf8c
- attribute_val - attribute value
Packit Service c5cf8c
Packit Service c5cf8c
Notes:
Packit Service c5cf8c
Values of the permanent attributes 'MPI_TAG_UB', 'MPI_HOST', 'MPI_IO',
Packit Service c5cf8c
'MPI_WTIME_IS_GLOBAL', 'MPI_UNIVERSE_SIZE', 'MPI_LASTUSEDCODE', and
Packit Service c5cf8c
'MPI_APPNUM' may not be changed.
Packit Service c5cf8c
Packit Service c5cf8c
The type of the attribute value depends on whether C, C++, or Fortran
Packit Service c5cf8c
is being used.
Packit Service c5cf8c
In C and C++, an attribute value is a pointer ('void *'); in Fortran, it is an
Packit Service c5cf8c
address-sized integer.
Packit Service c5cf8c
Packit Service c5cf8c
If an attribute is already present, the delete function (specified when the
Packit Service c5cf8c
corresponding keyval was created) will be called.
Packit Service c5cf8c
Packit Service c5cf8c
.N ThreadSafe
Packit Service c5cf8c
Packit Service c5cf8c
.N Fortran
Packit Service c5cf8c
Packit Service c5cf8c
.N Errors
Packit Service c5cf8c
.N MPI_SUCCESS
Packit Service c5cf8c
.N MPI_ERR_COMM
Packit Service c5cf8c
.N MPI_ERR_KEYVAL
Packit Service c5cf8c
.N MPI_ERR_PERM_KEY
Packit Service c5cf8c
Packit Service c5cf8c
.seealso MPI_Comm_create_keyval, MPI_Comm_delete_attr
Packit Service c5cf8c
@*/
Packit Service c5cf8c
int MPI_Comm_set_attr(MPI_Comm comm, int comm_keyval, void *attribute_val)
Packit Service c5cf8c
{
Packit Service c5cf8c
    int mpi_errno = MPI_SUCCESS;
Packit Service c5cf8c
    MPIR_Comm *comm_ptr = NULL;
Packit Service c5cf8c
    MPIR_FUNC_TERSE_STATE_DECL(MPID_STATE_MPI_COMM_SET_ATTR);
Packit Service c5cf8c
Packit Service c5cf8c
    MPID_THREAD_CS_ENTER(GLOBAL, MPIR_THREAD_GLOBAL_ALLFUNC_MUTEX);
Packit Service c5cf8c
    MPIR_FUNC_TERSE_ENTER(MPID_STATE_MPI_COMM_SET_ATTR);
Packit Service c5cf8c
Packit Service c5cf8c
    /* Validate parameters, especially handles needing to be converted */
Packit Service c5cf8c
#ifdef HAVE_ERROR_CHECKING
Packit Service c5cf8c
    {
Packit Service c5cf8c
        MPID_BEGIN_ERROR_CHECKS;
Packit Service c5cf8c
        {
Packit Service c5cf8c
            MPIR_ERRTEST_COMM(comm, mpi_errno);
Packit Service c5cf8c
            MPIR_ERRTEST_KEYVAL(comm_keyval, MPIR_COMM, "communicator", mpi_errno);
Packit Service c5cf8c
            MPIR_ERRTEST_KEYVAL_PERM(comm_keyval, mpi_errno);
Packit Service c5cf8c
        }
Packit Service c5cf8c
        MPID_END_ERROR_CHECKS;
Packit Service c5cf8c
    }
Packit Service c5cf8c
#endif
Packit Service c5cf8c
Packit Service c5cf8c
    /* Convert MPI object handles to object pointers */
Packit Service c5cf8c
    MPIR_Comm_get_ptr(comm, comm_ptr);
Packit Service c5cf8c
Packit Service c5cf8c
    /* Validate parameters and objects (post conversion) */
Packit Service c5cf8c
#ifdef HAVE_ERROR_CHECKING
Packit Service c5cf8c
    {
Packit Service c5cf8c
        MPID_BEGIN_ERROR_CHECKS;
Packit Service c5cf8c
        {
Packit Service c5cf8c
            MPII_Keyval *keyval_ptr = NULL;
Packit Service c5cf8c
Packit Service c5cf8c
            /* Validate comm_ptr */
Packit Service c5cf8c
            MPIR_Comm_valid_ptr(comm_ptr, mpi_errno, TRUE);
Packit Service c5cf8c
            /* If comm_ptr is not valid, it will be reset to null */
Packit Service c5cf8c
            /* Validate keyval_ptr */
Packit Service c5cf8c
            MPII_Keyval_get_ptr(comm_keyval, keyval_ptr);
Packit Service c5cf8c
            MPII_Keyval_valid_ptr(keyval_ptr, mpi_errno);
Packit Service c5cf8c
            if (mpi_errno)
Packit Service c5cf8c
                goto fn_fail;
Packit Service c5cf8c
        }
Packit Service c5cf8c
        MPID_END_ERROR_CHECKS;
Packit Service c5cf8c
    }
Packit Service c5cf8c
#endif /* HAVE_ERROR_CHECKING */
Packit Service c5cf8c
Packit Service c5cf8c
    /* ... body of routine ...  */
Packit Service c5cf8c
    mpi_errno = MPIR_Comm_set_attr_impl(comm_ptr, comm_keyval, attribute_val, MPIR_ATTR_PTR);
Packit Service c5cf8c
    if (mpi_errno)
Packit Service c5cf8c
        goto fn_fail;
Packit Service c5cf8c
    /* ... end of body of routine ... */
Packit Service c5cf8c
Packit Service c5cf8c
  fn_exit:
Packit Service c5cf8c
    MPIR_FUNC_TERSE_EXIT(MPID_STATE_MPI_COMM_SET_ATTR);
Packit Service c5cf8c
    MPID_THREAD_CS_EXIT(GLOBAL, MPIR_THREAD_GLOBAL_ALLFUNC_MUTEX);
Packit Service c5cf8c
    return mpi_errno;
Packit Service c5cf8c
Packit Service c5cf8c
  fn_fail:
Packit Service c5cf8c
    /* --BEGIN ERROR HANDLING-- */
Packit Service c5cf8c
#ifdef HAVE_ERROR_CHECKING
Packit Service c5cf8c
    {
Packit Service c5cf8c
        mpi_errno =
Packit Service c5cf8c
            MPIR_Err_create_code(mpi_errno, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_OTHER,
Packit Service c5cf8c
                                 "**mpi_comm_set_attr", "**mpi_comm_set_attr %C %d %p", comm,
Packit Service c5cf8c
                                 comm_keyval, attribute_val);
Packit Service c5cf8c
    }
Packit Service c5cf8c
#endif
Packit Service c5cf8c
    goto fn_exit;
Packit Service c5cf8c
    /* --END ERROR HANDLING-- */
Packit Service c5cf8c
}