Blame src/mpi/coll/gather/gather_inter_local_gather_remote_send.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
Packit Service c5cf8c
/* Local gather remote send
Packit Service c5cf8c
 *
Packit Service c5cf8c
 * Remote group does a local intracommunicator gather to rank 0. Rank
Packit Service c5cf8c
 * 0 then sends data to root.
Packit Service c5cf8c
 *
Packit Service c5cf8c
 * Cost: (lgp+1).alpha + n.((p-1)/p).beta + n.beta
Packit Service c5cf8c
 */
Packit Service c5cf8c
Packit Service c5cf8c
#undef FUNCNAME
Packit Service c5cf8c
#define FUNCNAME MPIR_Gather_inter_local_gather_remote_send
Packit Service c5cf8c
#undef FCNAME
Packit Service c5cf8c
#define FCNAME MPL_QUOTE(FUNCNAME)
Packit Service c5cf8c
int MPIR_Gather_inter_local_gather_remote_send(const void *sendbuf, int sendcount,
Packit Service c5cf8c
                                               MPI_Datatype sendtype, void *recvbuf, int recvcount,
Packit Service c5cf8c
                                               MPI_Datatype recvtype, int root,
Packit Service c5cf8c
                                               MPIR_Comm * comm_ptr, MPIR_Errflag_t * errflag)
Packit Service c5cf8c
{
Packit Service c5cf8c
    int rank, local_size, remote_size, mpi_errno = MPI_SUCCESS;
Packit Service c5cf8c
    int mpi_errno_ret = MPI_SUCCESS;
Packit Service c5cf8c
    MPI_Status status;
Packit Service c5cf8c
    MPI_Aint extent, true_extent, true_lb = 0;
Packit Service c5cf8c
    void *tmp_buf = NULL;
Packit Service c5cf8c
    MPIR_Comm *newcomm_ptr = NULL;
Packit Service c5cf8c
    MPIR_CHKLMEM_DECL(1);
Packit Service c5cf8c
Packit Service c5cf8c
    if (root == MPI_PROC_NULL) {
Packit Service c5cf8c
        /* local processes other than root do nothing */
Packit Service c5cf8c
        return MPI_SUCCESS;
Packit Service c5cf8c
    }
Packit Service c5cf8c
Packit Service c5cf8c
    remote_size = comm_ptr->remote_size;
Packit Service c5cf8c
    local_size = comm_ptr->local_size;
Packit Service c5cf8c
Packit Service c5cf8c
    if (root == MPI_ROOT) {
Packit Service c5cf8c
        /* root receives data from rank 0 on remote group */
Packit Service c5cf8c
        mpi_errno =
Packit Service c5cf8c
            MPIC_Recv(recvbuf, recvcount * remote_size, recvtype, 0, MPIR_GATHER_TAG, comm_ptr,
Packit Service c5cf8c
                      &status, errflag);
Packit Service c5cf8c
        if (mpi_errno) {
Packit Service c5cf8c
            /* for communication errors, just record the error but continue */
Packit Service c5cf8c
            *errflag =
Packit Service c5cf8c
                MPIX_ERR_PROC_FAILED ==
Packit Service c5cf8c
                MPIR_ERR_GET_CLASS(mpi_errno) ? MPIR_ERR_PROC_FAILED : MPIR_ERR_OTHER;
Packit Service c5cf8c
            MPIR_ERR_SET(mpi_errno, *errflag, "**fail");
Packit Service c5cf8c
            MPIR_ERR_ADD(mpi_errno_ret, mpi_errno);
Packit Service c5cf8c
        }
Packit Service c5cf8c
    } else {
Packit Service c5cf8c
        /* remote group. Rank 0 allocates temporary buffer, does
Packit Service c5cf8c
         * local intracommunicator gather, and then sends the data
Packit Service c5cf8c
         * to root. */
Packit Service c5cf8c
Packit Service c5cf8c
        rank = comm_ptr->rank;
Packit Service c5cf8c
Packit Service c5cf8c
        if (rank == 0) {
Packit Service c5cf8c
            MPIR_Type_get_true_extent_impl(sendtype, &true_lb, &true_extent);
Packit Service c5cf8c
            MPIR_Datatype_get_extent_macro(sendtype, extent);
Packit Service c5cf8c
Packit Service c5cf8c
            MPIR_Ensure_Aint_fits_in_pointer(sendcount * local_size *
Packit Service c5cf8c
                                             (MPL_MAX(extent, true_extent)));
Packit Service c5cf8c
            MPIR_CHKLMEM_MALLOC(tmp_buf, void *,
Packit Service c5cf8c
                                sendcount * local_size * (MPL_MAX(extent, true_extent)), mpi_errno,
Packit Service c5cf8c
                                "tmp_buf", MPL_MEM_BUFFER);
Packit Service c5cf8c
            /* adjust for potential negative lower bound in datatype */
Packit Service c5cf8c
            tmp_buf = (void *) ((char *) tmp_buf - true_lb);
Packit Service c5cf8c
        }
Packit Service c5cf8c
Packit Service c5cf8c
        /* all processes in remote group form new intracommunicator */
Packit Service c5cf8c
        if (!comm_ptr->local_comm) {
Packit Service c5cf8c
            mpi_errno = MPII_Setup_intercomm_localcomm(comm_ptr);
Packit Service c5cf8c
            if (mpi_errno)
Packit Service c5cf8c
                MPIR_ERR_POP(mpi_errno);
Packit Service c5cf8c
        }
Packit Service c5cf8c
Packit Service c5cf8c
        newcomm_ptr = comm_ptr->local_comm;
Packit Service c5cf8c
Packit Service c5cf8c
        /* now do the a local gather on this intracommunicator */
Packit Service c5cf8c
        mpi_errno =
Packit Service c5cf8c
            MPIR_Gather(sendbuf, sendcount, sendtype, tmp_buf, sendcount, sendtype, 0, newcomm_ptr,
Packit Service c5cf8c
                        errflag);
Packit Service c5cf8c
        if (mpi_errno) {
Packit Service c5cf8c
            /* for communication errors, just record the error but continue */
Packit Service c5cf8c
            *errflag =
Packit Service c5cf8c
                MPIX_ERR_PROC_FAILED ==
Packit Service c5cf8c
                MPIR_ERR_GET_CLASS(mpi_errno) ? MPIR_ERR_PROC_FAILED : MPIR_ERR_OTHER;
Packit Service c5cf8c
            MPIR_ERR_SET(mpi_errno, *errflag, "**fail");
Packit Service c5cf8c
            MPIR_ERR_ADD(mpi_errno_ret, mpi_errno);
Packit Service c5cf8c
        }
Packit Service c5cf8c
Packit Service c5cf8c
        if (rank == 0) {
Packit Service c5cf8c
            mpi_errno =
Packit Service c5cf8c
                MPIC_Send(tmp_buf, sendcount * local_size, sendtype, root, MPIR_GATHER_TAG,
Packit Service c5cf8c
                          comm_ptr, errflag);
Packit Service c5cf8c
            if (mpi_errno) {
Packit Service c5cf8c
                /* for communication errors, just record the error but continue */
Packit Service c5cf8c
                *errflag =
Packit Service c5cf8c
                    MPIX_ERR_PROC_FAILED ==
Packit Service c5cf8c
                    MPIR_ERR_GET_CLASS(mpi_errno) ? MPIR_ERR_PROC_FAILED : MPIR_ERR_OTHER;
Packit Service c5cf8c
                MPIR_ERR_SET(mpi_errno, *errflag, "**fail");
Packit Service c5cf8c
                MPIR_ERR_ADD(mpi_errno_ret, mpi_errno);
Packit Service c5cf8c
            }
Packit Service c5cf8c
        }
Packit Service c5cf8c
    }
Packit Service c5cf8c
Packit Service c5cf8c
  fn_exit:
Packit Service c5cf8c
    MPIR_CHKLMEM_FREEALL();
Packit Service c5cf8c
    if (mpi_errno_ret)
Packit Service c5cf8c
        mpi_errno = mpi_errno_ret;
Packit Service c5cf8c
    else if (*errflag != MPIR_ERR_NONE)
Packit Service c5cf8c
        MPIR_ERR_SET(mpi_errno, *errflag, "**coll_fail");
Packit Service c5cf8c
    return mpi_errno;
Packit Service c5cf8c
Packit Service c5cf8c
  fn_fail:
Packit Service c5cf8c
    goto fn_exit;
Packit Service c5cf8c
}