Blame src/mpi/comm/comm_dup.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 "mpicomm.h"
Packit Service c5cf8c
Packit Service c5cf8c
/* -- Begin Profiling Symbol Block for routine MPI_Comm_dup */
Packit Service c5cf8c
#if defined(HAVE_PRAGMA_WEAK)
Packit Service c5cf8c
#pragma weak MPI_Comm_dup = PMPI_Comm_dup
Packit Service c5cf8c
#elif defined(HAVE_PRAGMA_HP_SEC_DEF)
Packit Service c5cf8c
#pragma _HP_SECONDARY_DEF PMPI_Comm_dup  MPI_Comm_dup
Packit Service c5cf8c
#elif defined(HAVE_PRAGMA_CRI_DUP)
Packit Service c5cf8c
#pragma _CRI duplicate MPI_Comm_dup as PMPI_Comm_dup
Packit Service c5cf8c
#elif defined(HAVE_WEAK_ATTRIBUTE)
Packit Service c5cf8c
int MPI_Comm_dup(MPI_Comm comm, MPI_Comm * newcomm) __attribute__ ((weak, alias("PMPI_Comm_dup")));
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_dup
Packit Service c5cf8c
#define MPI_Comm_dup PMPI_Comm_dup
Packit Service c5cf8c
Packit Service c5cf8c
#undef FUNCNAME
Packit Service c5cf8c
#define FUNCNAME MPIR_Comm_dup_impl
Packit Service c5cf8c
#undef FCNAME
Packit Service c5cf8c
#define FCNAME MPL_QUOTE(FUNCNAME)
Packit Service c5cf8c
int MPIR_Comm_dup_impl(MPIR_Comm * comm_ptr, MPIR_Comm ** newcomm_ptr)
Packit Service c5cf8c
{
Packit Service c5cf8c
    int mpi_errno = MPI_SUCCESS;
Packit Service c5cf8c
    MPIR_Attribute *new_attributes = 0;
Packit Service c5cf8c
Packit Service c5cf8c
    /* Copy attributes, executing the attribute copy functions */
Packit Service c5cf8c
    /* This accesses the attribute dup function through the perprocess
Packit Service c5cf8c
     * structure to prevent comm_dup from forcing the linking of the
Packit Service c5cf8c
     * attribute functions.  The actual function is (by default)
Packit Service c5cf8c
     * MPIR_Attr_dup_list
Packit Service c5cf8c
     */
Packit Service c5cf8c
    if (MPIR_Process.attr_dup) {
Packit Service c5cf8c
        mpi_errno = MPIR_Process.attr_dup(comm_ptr->handle, comm_ptr->attributes, &new_attributes);
Packit Service c5cf8c
        if (mpi_errno)
Packit Service c5cf8c
            MPIR_ERR_POP(mpi_errno);
Packit Service c5cf8c
    }
Packit Service c5cf8c
Packit Service c5cf8c
Packit Service c5cf8c
    /* Generate a new context value and a new communicator structure */
Packit Service c5cf8c
    /* We must use the local size, because this is compared to the
Packit Service c5cf8c
     * rank of the process in the communicator.  For intercomms,
Packit Service c5cf8c
     * this must be the local size */
Packit Service c5cf8c
    mpi_errno = MPII_Comm_copy(comm_ptr, comm_ptr->local_size, newcomm_ptr);
Packit Service c5cf8c
    if (mpi_errno)
Packit Service c5cf8c
        MPIR_ERR_POP(mpi_errno);
Packit Service c5cf8c
Packit Service c5cf8c
    (*newcomm_ptr)->attributes = new_attributes;
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
#endif
Packit Service c5cf8c
Packit Service c5cf8c
#undef FUNCNAME
Packit Service c5cf8c
#define FUNCNAME MPI_Comm_dup
Packit Service c5cf8c
#undef FCNAME
Packit Service c5cf8c
#define FCNAME MPL_QUOTE(FUNCNAME)
Packit Service c5cf8c
/*@
Packit Service c5cf8c
Packit Service c5cf8c
MPI_Comm_dup - Duplicates an existing communicator with all its cached
Packit Service c5cf8c
               information
Packit Service c5cf8c
Packit Service c5cf8c
Input Parameters:
Packit Service c5cf8c
. comm - Communicator to be duplicated (handle)
Packit Service c5cf8c
Packit Service c5cf8c
Output Parameters:
Packit Service c5cf8c
. newcomm - A new communicator over the same group as 'comm' but with a new
Packit Service c5cf8c
  context. See notes.  (handle)
Packit Service c5cf8c
Packit Service c5cf8c
Notes:
Packit Service c5cf8c
  This routine is used to create a new communicator that has a new
Packit Service c5cf8c
  communication context but contains the same group of processes as
Packit Service c5cf8c
  the input communicator.  Since all MPI communication is performed
Packit Service c5cf8c
  within a communicator (specifies as the group of processes `plus`
Packit Service c5cf8c
  the context), this routine provides an effective way to create a
Packit Service c5cf8c
  private communicator for use by a software module or library.  In
Packit Service c5cf8c
  particular, no library routine should use 'MPI_COMM_WORLD' as the
Packit Service c5cf8c
  communicator; instead, a duplicate of a user-specified communicator
Packit Service c5cf8c
  should always be used.  For more information, see Using MPI, 2nd
Packit Service c5cf8c
  edition.
Packit Service c5cf8c
Packit Service c5cf8c
  Because this routine essentially produces a copy of a communicator,
Packit Service c5cf8c
  it also copies any attributes that have been defined on the input
Packit Service c5cf8c
  communicator, using the attribute copy function specified by the
Packit Service c5cf8c
  'copy_function' argument to 'MPI_Keyval_create'.  This is
Packit Service c5cf8c
  particularly useful for (a) attributes that describe some property
Packit Service c5cf8c
  of the group associated with the communicator, such as its
Packit Service c5cf8c
  interconnection topology and (b) communicators that are given back
Packit Service c5cf8c
  to the user; the attibutes in this case can track subsequent
Packit Service c5cf8c
  'MPI_Comm_dup' operations on this communicator.
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
Packit Service c5cf8c
.seealso: MPI_Comm_free, MPI_Keyval_create, MPI_Attr_put, MPI_Attr_delete,
Packit Service c5cf8c
 MPI_Comm_create_keyval, MPI_Comm_set_attr, MPI_Comm_delete_attr
Packit Service c5cf8c
@*/
Packit Service c5cf8c
int MPI_Comm_dup(MPI_Comm comm, MPI_Comm * newcomm)
Packit Service c5cf8c
{
Packit Service c5cf8c
    int mpi_errno = MPI_SUCCESS;
Packit Service c5cf8c
    MPIR_Comm *comm_ptr = NULL, *newcomm_ptr;
Packit Service c5cf8c
    MPIR_FUNC_TERSE_STATE_DECL(MPID_STATE_MPI_COMM_DUP);
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_MPI_COMM_DUP);
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
        }
Packit Service c5cf8c
        MPID_END_ERROR_CHECKS;
Packit Service c5cf8c
    }
Packit Service c5cf8c
#endif /* HAVE_ERROR_CHECKING */
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
            /* Validate comm_ptr */
Packit Service c5cf8c
            MPIR_Comm_valid_ptr(comm_ptr, mpi_errno, FALSE);
Packit Service c5cf8c
            if (mpi_errno)
Packit Service c5cf8c
                goto fn_fail;
Packit Service c5cf8c
            /* If comm_ptr is not valid, it will be reset to null */
Packit Service c5cf8c
            MPIR_ERRTEST_ARGNULL(newcomm, "newcomm", mpi_errno);
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
Packit Service c5cf8c
    mpi_errno = MPIR_Comm_dup_impl(comm_ptr, &newcomm_ptr);
Packit Service c5cf8c
    if (mpi_errno)
Packit Service c5cf8c
        MPIR_ERR_POP(mpi_errno);
Packit Service c5cf8c
Packit Service c5cf8c
    MPIR_OBJ_PUBLISH_HANDLE(*newcomm, newcomm_ptr->handle);
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_DUP);
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_dup", "**mpi_comm_dup %C %p", comm, newcomm);
Packit Service c5cf8c
    }
Packit Service c5cf8c
#endif
Packit Service c5cf8c
    if (newcomm)
Packit Service c5cf8c
        *newcomm = MPI_COMM_NULL;
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
}