Blame src/mpi/coll/scan/scan_intra_smp.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
Packit Service c5cf8c
#undef FUNCNAME
Packit Service c5cf8c
#define FUNCNAME MPIR_Scan_intra_smp
Packit Service c5cf8c
#undef FCNAME
Packit Service c5cf8c
#define FCNAME MPL_QUOTE(FUNCNAME)
Packit Service c5cf8c
int MPIR_Scan_intra_smp(const void *sendbuf, void *recvbuf, int count,
Packit Service c5cf8c
                        MPI_Datatype datatype, MPI_Op op, MPIR_Comm * comm_ptr,
Packit Service c5cf8c
                        MPIR_Errflag_t * errflag)
Packit Service c5cf8c
{
Packit Service c5cf8c
    int mpi_errno = MPI_SUCCESS;
Packit Service c5cf8c
    int mpi_errno_ret = MPI_SUCCESS;
Packit Service c5cf8c
    MPIR_CHKLMEM_DECL(3);
Packit Service c5cf8c
    int rank = comm_ptr->rank;
Packit Service c5cf8c
    MPI_Status status;
Packit Service c5cf8c
    void *tempbuf = NULL, *localfulldata = NULL, *prefulldata = NULL;
Packit Service c5cf8c
    MPI_Aint true_lb, true_extent, extent;
Packit Service c5cf8c
    int noneed = 1;             /* noneed=1 means no need to bcast tempbuf and
Packit Service c5cf8c
                                 * reduce tempbuf & recvbuf */
Packit Service c5cf8c
Packit Service c5cf8c
    MPIR_Type_get_true_extent_impl(datatype, &true_lb, &true_extent);
Packit Service c5cf8c
Packit Service c5cf8c
    MPIR_Datatype_get_extent_macro(datatype, extent);
Packit Service c5cf8c
Packit Service c5cf8c
    MPIR_Ensure_Aint_fits_in_pointer(count * MPL_MAX(extent, true_extent));
Packit Service c5cf8c
Packit Service c5cf8c
    MPIR_CHKLMEM_MALLOC(tempbuf, void *, count * (MPL_MAX(extent, true_extent)),
Packit Service c5cf8c
                        mpi_errno, "temporary buffer", MPL_MEM_BUFFER);
Packit Service c5cf8c
    tempbuf = (void *) ((char *) tempbuf - true_lb);
Packit Service c5cf8c
Packit Service c5cf8c
    /* Create prefulldata and localfulldata on local roots of all nodes */
Packit Service c5cf8c
    if (comm_ptr->node_roots_comm != NULL) {
Packit Service c5cf8c
        MPIR_CHKLMEM_MALLOC(prefulldata, void *, count * (MPL_MAX(extent, true_extent)),
Packit Service c5cf8c
                            mpi_errno, "prefulldata for scan", MPL_MEM_BUFFER);
Packit Service c5cf8c
        prefulldata = (void *) ((char *) prefulldata - true_lb);
Packit Service c5cf8c
Packit Service c5cf8c
        if (comm_ptr->node_comm != NULL) {
Packit Service c5cf8c
            MPIR_CHKLMEM_MALLOC(localfulldata, void *, count * (MPL_MAX(extent, true_extent)),
Packit Service c5cf8c
                                mpi_errno, "localfulldata for scan", MPL_MEM_BUFFER);
Packit Service c5cf8c
            localfulldata = (void *) ((char *) localfulldata - true_lb);
Packit Service c5cf8c
        }
Packit Service c5cf8c
    }
Packit Service c5cf8c
Packit Service c5cf8c
    /* perform intranode scan to get temporary result in recvbuf. if there is only
Packit Service c5cf8c
     * one process, just copy the raw data. */
Packit Service c5cf8c
    if (comm_ptr->node_comm != NULL) {
Packit Service c5cf8c
        mpi_errno = MPIR_Scan(sendbuf, recvbuf, count, datatype, op, comm_ptr->node_comm, 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 if (sendbuf != MPI_IN_PLACE) {
Packit Service c5cf8c
        mpi_errno = MPIR_Localcopy(sendbuf, count, datatype, recvbuf, count, datatype);
Packit Service c5cf8c
        if (mpi_errno)
Packit Service c5cf8c
            MPIR_ERR_POP(mpi_errno);
Packit Service c5cf8c
    }
Packit Service c5cf8c
Packit Service c5cf8c
    /* get result from local node's last processor which
Packit Service c5cf8c
     * contains the reduce result of the whole node. Name it as
Packit Service c5cf8c
     * localfulldata. For example, localfulldata from node 1 contains
Packit Service c5cf8c
     * reduced data of rank 1,2,3. */
Packit Service c5cf8c
    if (comm_ptr->node_roots_comm != NULL && comm_ptr->node_comm != NULL) {
Packit Service c5cf8c
        mpi_errno = MPIC_Recv(localfulldata, count, datatype,
Packit Service c5cf8c
                              comm_ptr->node_comm->local_size - 1, MPIR_SCAN_TAG,
Packit Service c5cf8c
                              comm_ptr->node_comm, &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 if (comm_ptr->node_roots_comm == NULL &&
Packit Service c5cf8c
               comm_ptr->node_comm != NULL &&
Packit Service c5cf8c
               MPIR_Get_intranode_rank(comm_ptr, rank) == comm_ptr->node_comm->local_size - 1) {
Packit Service c5cf8c
        mpi_errno = MPIC_Send(recvbuf, count, datatype,
Packit Service c5cf8c
                              0, MPIR_SCAN_TAG, comm_ptr->node_comm, 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 if (comm_ptr->node_roots_comm != NULL) {
Packit Service c5cf8c
        localfulldata = recvbuf;
Packit Service c5cf8c
    }
Packit Service c5cf8c
Packit Service c5cf8c
    /* do scan on localfulldata to prefulldata. for example,
Packit Service c5cf8c
     * prefulldata on rank 4 contains reduce result of ranks
Packit Service c5cf8c
     * 1,2,3,4,5,6. it will be sent to rank 7 which is master
Packit Service c5cf8c
     * process of node 3. */
Packit Service c5cf8c
    if (comm_ptr->node_roots_comm != NULL) {
Packit Service c5cf8c
        mpi_errno = MPIR_Scan(localfulldata, prefulldata, count, datatype,
Packit Service c5cf8c
                              op, comm_ptr->node_roots_comm, 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 (MPIR_Get_internode_rank(comm_ptr, rank) != comm_ptr->node_roots_comm->local_size - 1) {
Packit Service c5cf8c
            mpi_errno = MPIC_Send(prefulldata, count, datatype,
Packit Service c5cf8c
                                  MPIR_Get_internode_rank(comm_ptr, rank) + 1,
Packit Service c5cf8c
                                  MPIR_SCAN_TAG, comm_ptr->node_roots_comm, 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 (MPIR_Get_internode_rank(comm_ptr, rank) != 0) {
Packit Service c5cf8c
            mpi_errno = MPIC_Recv(tempbuf, count, datatype,
Packit Service c5cf8c
                                  MPIR_Get_internode_rank(comm_ptr, rank) - 1,
Packit Service c5cf8c
                                  MPIR_SCAN_TAG, comm_ptr->node_roots_comm, &status, errflag);
Packit Service c5cf8c
            noneed = 0;
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
    /* now tempbuf contains all the data needed to get the correct
Packit Service c5cf8c
     * scan result. for example, to node 3, it will have reduce result
Packit Service c5cf8c
     * of rank 1,2,3,4,5,6 in tempbuf.
Packit Service c5cf8c
     * then we should broadcast this result in the local node, and
Packit Service c5cf8c
     * reduce it with recvbuf to get final result if nessesary. */
Packit Service c5cf8c
Packit Service c5cf8c
    if (comm_ptr->node_comm != NULL) {
Packit Service c5cf8c
        mpi_errno = MPIR_Bcast(&noneed, 1, MPI_INT, 0, comm_ptr->node_comm, 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
    if (noneed == 0) {
Packit Service c5cf8c
        if (comm_ptr->node_comm != NULL) {
Packit Service c5cf8c
            mpi_errno = MPIR_Bcast(tempbuf, count, datatype, 0, comm_ptr->node_comm, 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
        mpi_errno = MPIR_Reduce_local(tempbuf, recvbuf, count, datatype, op);
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
}