Blame src/mpi/coll/ialltoallw/ialltoallw_intra_inplace.c

Packit Service c5cf8c
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit Service c5cf8c
/*
Packit Service c5cf8c
 *  (C) 2017 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
/* Algorithm: Inplace Alltoallw
Packit Service c5cf8c
 *
Packit Service c5cf8c
 * We use pair-wise sendrecv_replace in order to conserve memory usage, which
Packit Service c5cf8c
 * is keeping with the spirit of the MPI-2.2 Standard.  But because of this
Packit Service c5cf8c
 * approach all processes must agree on the global schedule of sendrecv_replace
Packit Service c5cf8c
 * operations to avoid deadlock.
Packit Service c5cf8c
 *
Packit Service c5cf8c
 * Note that this is not an especially efficient algorithm in terms of time and
Packit Service c5cf8c
 * there will be multiple repeated malloc/free's rather than maintaining a
Packit Service c5cf8c
 * single buffer across the whole loop.  Something like MADRE is probably the
Packit Service c5cf8c
 * best solution for the MPI_IN_PLACE scenario.
Packit Service c5cf8c
 */
Packit Service c5cf8c
#undef FUNCNAME
Packit Service c5cf8c
#define FUNCNAME MPIR_Ialltoallw_sched_intra_inplace
Packit Service c5cf8c
#undef FCNAME
Packit Service c5cf8c
#define FCNAME MPL_QUOTE(FUNCNAME)
Packit Service c5cf8c
int MPIR_Ialltoallw_sched_intra_inplace(const void *sendbuf, const int sendcounts[],
Packit Service c5cf8c
                                        const int sdispls[], const MPI_Datatype sendtypes[],
Packit Service c5cf8c
                                        void *recvbuf, const int recvcounts[], const int rdispls[],
Packit Service c5cf8c
                                        const MPI_Datatype recvtypes[], MPIR_Comm * comm_ptr,
Packit Service c5cf8c
                                        MPIR_Sched_t s)
Packit Service c5cf8c
{
Packit Service c5cf8c
    int mpi_errno = MPI_SUCCESS;
Packit Service c5cf8c
    int comm_size, i, j;
Packit Service c5cf8c
    int dst, rank;
Packit Service c5cf8c
    int recv_extent;
Packit Service c5cf8c
    MPI_Aint true_extent, true_lb;
Packit Service c5cf8c
    int max_size;
Packit Service c5cf8c
    void *tmp_buf = NULL, *adj_tmp_buf = NULL;
Packit Service c5cf8c
    MPIR_SCHED_CHKPMEM_DECL(1);
Packit Service c5cf8c
Packit Service c5cf8c
    comm_size = comm_ptr->local_size;
Packit Service c5cf8c
    rank = comm_ptr->rank;
Packit Service c5cf8c
Packit Service c5cf8c
    /* The regular MPI_Alltoallw handles MPI_IN_PLACE using pairwise
Packit Service c5cf8c
     * sendrecv_replace calls.  We don't have a sendrecv_replace, so just
Packit Service c5cf8c
     * malloc the maximum of the counts array entries and then perform the
Packit Service c5cf8c
     * pairwise exchanges manually with schedule barriers instead.
Packit Service c5cf8c
     *
Packit Service c5cf8c
     * Because of this approach all processes must agree on the global
Packit Service c5cf8c
     * schedule of "sendrecv_replace" operations to avoid deadlock.
Packit Service c5cf8c
     *
Packit Service c5cf8c
     * This keeps with the spirit of the MPI-2.2 standard, which is to
Packit Service c5cf8c
     * conserve memory when using MPI_IN_PLACE for these routines.
Packit Service c5cf8c
     * Something like MADRE would probably generate a more optimal
Packit Service c5cf8c
     * algorithm. */
Packit Service c5cf8c
    max_size = 0;
Packit Service c5cf8c
    for (i = 0; i < comm_size; ++i) {
Packit Service c5cf8c
        /* only look at recvtypes/recvcounts because the send vectors are
Packit Service c5cf8c
         * ignored when sendbuf==MPI_IN_PLACE */
Packit Service c5cf8c
        MPIR_Type_get_true_extent_impl(recvtypes[i], &true_lb, &true_extent);
Packit Service c5cf8c
        MPIR_Datatype_get_extent_macro(recvtypes[i], recv_extent);
Packit Service c5cf8c
        max_size = MPL_MAX(max_size, recvcounts[i] * MPL_MAX(recv_extent, true_extent));
Packit Service c5cf8c
    }
Packit Service c5cf8c
    MPIR_SCHED_CHKPMEM_MALLOC(tmp_buf, void *, max_size, mpi_errno, "Ialltoallw tmp_buf",
Packit Service c5cf8c
                              MPL_MEM_BUFFER);
Packit Service c5cf8c
Packit Service c5cf8c
    for (i = 0; i < comm_size; ++i) {
Packit Service c5cf8c
        /* start inner loop at i to avoid re-exchanging data */
Packit Service c5cf8c
        for (j = i; j < comm_size; ++j) {
Packit Service c5cf8c
            if (rank == i && rank == j) {
Packit Service c5cf8c
                /* no need to "sendrecv_replace" for ourselves */
Packit Service c5cf8c
            } else if (rank == i || rank == j) {
Packit Service c5cf8c
                if (rank == i)
Packit Service c5cf8c
                    dst = j;
Packit Service c5cf8c
                else
Packit Service c5cf8c
                    dst = i;
Packit Service c5cf8c
Packit Service c5cf8c
                MPIR_Type_get_true_extent_impl(recvtypes[i], &true_lb, &true_extent);
Packit Service c5cf8c
                adj_tmp_buf = (void *) ((char *) tmp_buf - true_lb);
Packit Service c5cf8c
Packit Service c5cf8c
                mpi_errno = MPIR_Sched_send(((char *) recvbuf + rdispls[dst]),
Packit Service c5cf8c
                                            recvcounts[dst], recvtypes[dst], dst, comm_ptr, s);
Packit Service c5cf8c
                if (mpi_errno)
Packit Service c5cf8c
                    MPIR_ERR_POP(mpi_errno);
Packit Service c5cf8c
                mpi_errno =
Packit Service c5cf8c
                    MPIR_Sched_recv(adj_tmp_buf, recvcounts[dst], recvtypes[dst], dst, comm_ptr, s);
Packit Service c5cf8c
                if (mpi_errno)
Packit Service c5cf8c
                    MPIR_ERR_POP(mpi_errno);
Packit Service c5cf8c
                MPIR_SCHED_BARRIER(s);
Packit Service c5cf8c
Packit Service c5cf8c
                mpi_errno = MPIR_Sched_copy(adj_tmp_buf, recvcounts[dst], recvtypes[dst],
Packit Service c5cf8c
                                            ((char *) recvbuf + rdispls[dst]),
Packit Service c5cf8c
                                            recvcounts[dst], recvtypes[dst], s);
Packit Service c5cf8c
                if (mpi_errno)
Packit Service c5cf8c
                    MPIR_ERR_POP(mpi_errno);
Packit Service c5cf8c
                MPIR_SCHED_BARRIER(s);
Packit Service c5cf8c
            }
Packit Service c5cf8c
        }
Packit Service c5cf8c
    }
Packit Service c5cf8c
Packit Service c5cf8c
    MPIR_SCHED_CHKPMEM_COMMIT(s);
Packit Service c5cf8c
  fn_exit:
Packit Service c5cf8c
    return mpi_errno;
Packit Service c5cf8c
  fn_fail:
Packit Service c5cf8c
    MPIR_SCHED_CHKPMEM_REAP(s);
Packit Service c5cf8c
    goto fn_exit;
Packit Service c5cf8c
}