⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gatherv.c

📁 mpi并行计算的c++代码 可用vc或gcc编译通过 可以用来搭建并行计算试验环境
💻 C
字号:
/* -*- Mode: C; c-basic-offset:4 ; -*- *//* * *  (C) 2001 by Argonne National Laboratory. *      See COPYRIGHT in top-level directory. */#include "mpiimpl.h"/* -- Begin Profiling Symbol Block for routine MPI_Gatherv */#if defined(HAVE_PRAGMA_WEAK)#pragma weak MPI_Gatherv = PMPI_Gatherv#elif defined(HAVE_PRAGMA_HP_SEC_DEF)#pragma _HP_SECONDARY_DEF PMPI_Gatherv  MPI_Gatherv#elif defined(HAVE_PRAGMA_CRI_DUP)#pragma _CRI duplicate MPI_Gatherv as PMPI_Gatherv#endif/* -- End Profiling Symbol Block *//* Define MPICH_MPI_FROM_PMPI if weak symbols are not supported to build   the MPI routines */#ifndef MPICH_MPI_FROM_PMPI#define MPI_Gatherv PMPI_Gatherv/* This is the default implementation of gatherv. The algorithm is:      Algorithm: MPI_Gatherv   Since the array of recvcounts is valid only on the root, we cannot   do a tree algorithm without first communicating the recvcounts to   other processes. Therefore, we simply use a linear algorithm for the   gather, which takes (p-1) steps versus lgp steps for the tree   algorithm. The bandwidth requirement is the same for both algorithms.   Cost = (p-1).alpha + n.((p-1)/p).beta   Possible improvements:    End Algorithm: MPI_Gatherv*//* not declared static because it is called in intercommunicator allgatherv */int MPIR_Gatherv ( 	void *sendbuf, 	int sendcnt,  	MPI_Datatype sendtype, 	void *recvbuf, 	int *recvcnts, 	int *displs, 	MPI_Datatype recvtype, 	int root, 	MPID_Comm *comm_ptr ){    static const char FCNAME[] = "MPIR_Gatherv";    int        comm_size, rank, remote_comm_size;    int        mpi_errno = MPI_SUCCESS;    MPI_Comm comm;    MPI_Aint       extent;    int            i;        comm = comm_ptr->handle;    rank = comm_ptr->rank;        /* check if multiple threads are calling this collective function */    MPIDU_ERR_CHECK_MULTIPLE_THREADS_ENTER( comm_ptr );        /* If rank == root, then I recv lots, otherwise I send */    if ((comm_ptr->comm_kind == MPID_INTRACOMM) && (rank == root)) {        /* intracomm root */        comm_size = comm_ptr->local_size;        MPID_Datatype_get_extent_macro(recvtype, extent);        for ( i=0; i<root; i++ ) {            if (recvcnts[i]) {                mpi_errno = MPIC_Recv(((char *)recvbuf+displs[i]*extent),                                       recvcnts[i], recvtype, i,                                      MPIR_GATHERV_TAG, comm,                                      MPI_STATUS_IGNORE);		/* --BEGIN ERROR HANDLING-- */                if (mpi_errno)		{		    mpi_errno = MPIR_Err_create_code(mpi_errno, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_OTHER, "**fail", 0);		    return mpi_errno;		}		/* --END ERROR HANDLING-- */            }        }        if (sendbuf != MPI_IN_PLACE) {            if (recvcnts[rank]) {                mpi_errno = MPIR_Localcopy(sendbuf, sendcnt, sendtype,                                           ((char *)recvbuf+displs[rank]*extent),                                            recvcnts[rank], recvtype);		/* --BEGIN ERROR HANDLING-- */                if (mpi_errno)		{		    mpi_errno = MPIR_Err_create_code(mpi_errno, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_OTHER, "**fail", 0);		    return mpi_errno;		}		/* --END ERROR HANDLING-- */            }        }        for ( i=root+1; i<comm_size; i++ ) {            if (recvcnts[i]) {                mpi_errno = MPIC_Recv(((char *)recvbuf+displs[i]*extent),                                       recvcnts[i], recvtype, i,                                      MPIR_GATHERV_TAG, comm,                                      MPI_STATUS_IGNORE);		/* --BEGIN ERROR HANDLING-- */                if (mpi_errno)		{		    mpi_errno = MPIR_Err_create_code(mpi_errno, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_OTHER, "**fail", 0);		    return mpi_errno;		}		/* --END ERROR HANDLING-- */            }        }    }        else if ((comm_ptr->comm_kind == MPID_INTERCOMM) && (root == MPI_ROOT)) {        /* intercommunicator root */        remote_comm_size = comm_ptr->remote_size;        MPID_Datatype_get_extent_macro(recvtype, extent);        for (i=0; i<remote_comm_size; i++) {            if (recvcnts[i]) {                mpi_errno = MPIC_Recv(((char *)recvbuf+displs[i]*extent),                                       recvcnts[i], recvtype, i,                                      MPIR_GATHERV_TAG, comm,                                      MPI_STATUS_IGNORE);		/* --BEGIN ERROR HANDLING-- */                if (mpi_errno)		{		    mpi_errno = MPIR_Err_create_code(mpi_errno, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_OTHER, "**fail", 0);		    return mpi_errno;		}		/* --END ERROR HANDLING-- */            }                        }    }    else if (root != MPI_PROC_NULL) { /* non-root nodes, and in the intercomm. case, non-root nodes on remote side */        if (sendcnt)            mpi_errno = MPIC_Send(sendbuf, sendcnt, sendtype, root,                                   MPIR_GATHERV_TAG, comm);    }        /* check if multiple threads are calling this collective function */    MPIDU_ERR_CHECK_MULTIPLE_THREADS_EXIT( comm_ptr );        return (mpi_errno);}#endif#undef FUNCNAME#define FUNCNAME MPI_Gatherv/*@MPI_Gatherv - Gathers into specified locations from all processes in a groupInput Parameters:+ sendbuf - starting address of send buffer (choice) . sendcount - number of elements in send buffer (integer) . sendtype - data type of send buffer elements (handle) . recvcounts - integer array (of length group size) containing the number of elements that are received from each process(significant only at 'root') . displs - integer array (of length group size). Entry  'i'  specifies the displacement relative to recvbuf  atwhich to place the incoming data from process  'i'  (significant onlyat root) . recvtype - data type of recv buffer elements (significant only at 'root') (handle) . root - rank of receiving process (integer) - comm - communicator (handle) Output Parameter:. recvbuf - address of receive buffer (choice, significant only at 'root') .N ThreadSafe.N Fortran.N Errors.N MPI_SUCCESS.N MPI_ERR_COMM.N MPI_ERR_TYPE.N MPI_ERR_BUFFER@*/int MPI_Gatherv(void *sendbuf, int sendcnt, MPI_Datatype sendtype,                 void *recvbuf, int *recvcnts, int *displs,                 MPI_Datatype recvtype, int root, MPI_Comm comm){    static const char FCNAME[] = "MPI_Gatherv";    int mpi_errno = MPI_SUCCESS;    MPID_Comm *comm_ptr = NULL;    MPID_MPI_STATE_DECL(MPID_STATE_MPI_GATHERV);    MPIR_ERRTEST_INITIALIZED_ORDIE();        MPID_CS_ENTER();    MPID_MPI_COLL_FUNC_ENTER(MPID_STATE_MPI_GATHERV);    /* Validate parameters, especially handles needing to be converted */#   ifdef HAVE_ERROR_CHECKING    {        MPID_BEGIN_ERROR_CHECKS;        {	    MPIR_ERRTEST_COMM(comm, mpi_errno);            if (mpi_errno != MPI_SUCCESS) goto fn_fail;	}        MPID_END_ERROR_CHECKS;    }#   endif /* HAVE_ERROR_CHECKING */    /* Convert MPI object handles to object pointers */    MPID_Comm_get_ptr( comm, comm_ptr );    /* Validate parameters and objects (post conversion) */#   ifdef HAVE_ERROR_CHECKING    {        MPID_BEGIN_ERROR_CHECKS;        {	    MPID_Datatype *sendtype_ptr=NULL, *recvtype_ptr=NULL;            int i, rank, comm_size;	                MPID_Comm_valid_ptr( comm_ptr, mpi_errno );            if (mpi_errno != MPI_SUCCESS) goto fn_fail;	    if (comm_ptr->comm_kind == MPID_INTRACOMM) {		MPIR_ERRTEST_INTRA_ROOT(comm_ptr, root, mpi_errno);                if (sendbuf != MPI_IN_PLACE) {                    MPIR_ERRTEST_COUNT(sendcnt, mpi_errno);                    MPIR_ERRTEST_DATATYPE(sendtype, "sendtype", mpi_errno);                    if (HANDLE_GET_KIND(sendtype) != HANDLE_KIND_BUILTIN) {                        MPID_Datatype_get_ptr(sendtype, sendtype_ptr);                        MPID_Datatype_valid_ptr( sendtype_ptr, mpi_errno );                        MPID_Datatype_committed_ptr( sendtype_ptr, mpi_errno );                    }                    MPIR_ERRTEST_USERBUFFER(sendbuf,sendcnt,sendtype,mpi_errno);                }                rank = comm_ptr->rank;                if (rank == root) {                    comm_size = comm_ptr->local_size;                    for (i=0; i<comm_size; i++) {                        MPIR_ERRTEST_COUNT(recvcnts[i], mpi_errno);                        MPIR_ERRTEST_DATATYPE(recvtype, "recvtype", mpi_errno);                    }                    if (HANDLE_GET_KIND(recvtype) != HANDLE_KIND_BUILTIN) {                        MPID_Datatype_get_ptr(recvtype, recvtype_ptr);                        MPID_Datatype_valid_ptr( recvtype_ptr, mpi_errno );                        MPID_Datatype_committed_ptr( recvtype_ptr, mpi_errno );                    }                    for (i=0; i<comm_size; i++) {                        if (recvcnts[i] > 0) {                            MPIR_ERRTEST_RECVBUF_INPLACE(recvbuf, recvcnts[i], mpi_errno);                            MPIR_ERRTEST_USERBUFFER(recvbuf,recvcnts[i],recvtype,mpi_errno);                             break;                        }                    }                }                else                    MPIR_ERRTEST_SENDBUF_INPLACE(sendbuf, sendcnt, mpi_errno);            }	    if (comm_ptr->comm_kind == MPID_INTERCOMM) {		MPIR_ERRTEST_INTER_ROOT(comm_ptr, root, mpi_errno);                if (root == MPI_ROOT) {                    comm_size = comm_ptr->remote_size;                    for (i=0; i<comm_size; i++) {                        MPIR_ERRTEST_COUNT(recvcnts[i], mpi_errno);                        MPIR_ERRTEST_DATATYPE(recvtype, "recvtype", mpi_errno);                    }                    if (HANDLE_GET_KIND(recvtype) != HANDLE_KIND_BUILTIN) {                        MPID_Datatype_get_ptr(recvtype, recvtype_ptr);                        MPID_Datatype_valid_ptr( recvtype_ptr, mpi_errno );                        MPID_Datatype_committed_ptr( recvtype_ptr, mpi_errno );                    }                    for (i=0; i<comm_size; i++) {                        if (recvcnts[i] > 0) {                            MPIR_ERRTEST_RECVBUF_INPLACE(recvbuf, recvcnts[i], mpi_errno);                            MPIR_ERRTEST_USERBUFFER(recvbuf,recvcnts[i],recvtype,mpi_errno);                             break;                        }                    }                }                else if (root != MPI_PROC_NULL) {                    MPIR_ERRTEST_COUNT(sendcnt, mpi_errno);                    MPIR_ERRTEST_DATATYPE(sendtype, "sendtype", mpi_errno);                    if (HANDLE_GET_KIND(sendtype) != HANDLE_KIND_BUILTIN) {                        MPID_Datatype_get_ptr(sendtype, sendtype_ptr);                        MPID_Datatype_valid_ptr( sendtype_ptr, mpi_errno );                        MPID_Datatype_committed_ptr( sendtype_ptr, mpi_errno );                    }                    MPIR_ERRTEST_SENDBUF_INPLACE(sendbuf, sendcnt, mpi_errno);                    MPIR_ERRTEST_USERBUFFER(sendbuf,sendcnt,sendtype,mpi_errno);                }	    }            if (mpi_errno != MPI_SUCCESS) goto fn_fail;        }        MPID_END_ERROR_CHECKS;    }#   endif /* HAVE_ERROR_CHECKING */    /* ... body of routine ...  */    if (comm_ptr->coll_fns != NULL && comm_ptr->coll_fns->Gatherv != NULL)    {	mpi_errno = comm_ptr->coll_fns->Gatherv(sendbuf, sendcnt,                                                sendtype, recvbuf, recvcnts,                                                displs, recvtype, root,                                                comm_ptr);      }    else    {        /* if (comm_ptr->comm_kind == MPID_INTERCOMM) {	    mpi_errno = MPIR_Err_create_code( MPI_SUCCESS, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_COMM, 					      "**intercommcoll",					      "**intercommcoll %s", FCNAME );	}	else         */                MPIR_Nest_incr();        mpi_errno = MPIR_Gatherv(sendbuf, sendcnt, sendtype,                                  recvbuf, recvcnts,                                 displs, recvtype, root, comm_ptr);         MPIR_Nest_decr();    }    if (mpi_errno != MPI_SUCCESS) goto fn_fail;    /* ... end of body of routine ... */      fn_exit:    MPID_MPI_COLL_FUNC_EXIT(MPID_STATE_MPI_GATHERV);    MPID_CS_EXIT();    return mpi_errno;  fn_fail:    /* --BEGIN ERROR HANDLING-- */#   ifdef HAVE_ERROR_CHECKING    {	mpi_errno = MPIR_Err_create_code(	    mpi_errno, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_OTHER, "**mpi_gatherv",	    "**mpi_gatherv %p %d %D %p %p %p %D %d %C", sendbuf, sendcnt, sendtype,	    recvbuf, recvcnts, displs, recvtype, root, comm);    }#   endif    mpi_errno = MPIR_Err_return_comm( comm_ptr, FCNAME, mpi_errno );    goto fn_exit;    /* --END ERROR HANDLING-- */}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -