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

📄 alltoall.c

📁 mpi并行计算的c++代码 可用vc或gcc编译通过 可以用来搭建并行计算试验环境
💻 C
📖 第 1 页 / 共 3 页
字号:
	{	    mpi_errno = MPIR_Err_create_code(mpi_errno, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_OTHER, "**fail", 0);	    return mpi_errno;	}	/* --END ERROR HANDLING-- */        /* Is comm_size a power-of-two? */        i = 1;        while (i < comm_size)            i *= 2;        if (i == comm_size)            pof2 = 1;        else             pof2 = 0;        /* Do the pairwise exchanges */        for (i=1; i<comm_size; i++) {            if (pof2 == 1) {                /* use exclusive-or algorithm */                src = dst = rank ^ i;            }            else {                src = (rank - i + comm_size) % comm_size;                dst = (rank + i) % comm_size;            }            mpi_errno = MPIC_Sendrecv(((char *)sendbuf +                                       dst*sendcount*sendtype_extent),                                       sendcount, sendtype, dst,                                      MPIR_ALLTOALL_TAG,                                       ((char *)recvbuf +                                       src*recvcount*recvtype_extent),                                      recvcount, recvtype, src,                                      MPIR_ALLTOALL_TAG, comm, &status);	    /* --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-- */        }    }        /* check if multiple threads are calling this collective function */    MPIDU_ERR_CHECK_MULTIPLE_THREADS_EXIT( comm_ptr );        return (mpi_errno);}/* end:nested *//* begin:nested *//* not declared static because a machine-specific function may call this one in some cases */int MPIR_Alltoall_inter(     void *sendbuf,     int sendcount,     MPI_Datatype sendtype,     void *recvbuf,     int recvcount,     MPI_Datatype recvtype,     MPID_Comm *comm_ptr ){/* Intercommunicator alltoall. We use a pairwise exchange algorithm   similar to the one used in intracommunicator alltoall for long   messages. Since the local and remote groups can be of different   sizes, we first compute the max of local_group_size,   remote_group_size. At step i, 0 <= i < max_size, each process   receives from src = (rank - i + max_size) % max_size if src <   remote_size, and sends to dst = (rank + i) % max_size if dst <   remote_size. */    static const char FCNAME[] = "MPIR_Alltoall_inter";    int          local_size, remote_size, max_size, i;    MPI_Aint     sendtype_extent, recvtype_extent;    int          mpi_errno = MPI_SUCCESS;    MPI_Status status;    int src, dst, rank;    char *sendaddr, *recvaddr;    MPI_Comm comm;        local_size = comm_ptr->local_size;     remote_size = comm_ptr->remote_size;    rank = comm_ptr->rank;    comm = comm_ptr->handle;    /* Get extent of send and recv types */    MPID_Datatype_get_extent_macro(sendtype, sendtype_extent);    MPID_Datatype_get_extent_macro(recvtype, recvtype_extent);        /* check if multiple threads are calling this collective function */    MPIDU_ERR_CHECK_MULTIPLE_THREADS_ENTER( comm_ptr );        /* Do the pairwise exchanges */    max_size = MPIR_MAX(local_size, remote_size);    for (i=0; i<max_size; i++) {        src = (rank - i + max_size) % max_size;        dst = (rank + i) % max_size;        if (src >= remote_size) {            src = MPI_PROC_NULL;            recvaddr = NULL;        }        else {            recvaddr = (char *)recvbuf + src*recvcount*recvtype_extent;        }        if (dst >= remote_size) {            dst = MPI_PROC_NULL;            sendaddr = NULL;        }        else {            sendaddr = (char *)sendbuf + dst*sendcount*sendtype_extent;        }        mpi_errno = MPIC_Sendrecv(sendaddr, sendcount, sendtype, dst,                                   MPIR_ALLTOALL_TAG, recvaddr,                                  recvcount, recvtype, src,                                  MPIR_ALLTOALL_TAG, comm, &status);	/* --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-- */    }    /* check if multiple threads are calling this collective function */    MPIDU_ERR_CHECK_MULTIPLE_THREADS_EXIT( comm_ptr );        return (mpi_errno);}/* end:nested */#endif#undef FUNCNAME#define FUNCNAME MPI_Alltoall/*@MPI_Alltoall - Sends data from all to all processesInput Parameters:+ sendbuf - starting address of send buffer (choice) . sendcount - number of elements to send to each process (integer) . sendtype - data type of send buffer elements (handle) . recvcount - number of elements received from any process (integer) . recvtype - data type of receive buffer elements (handle) - comm - communicator (handle) Output Parameter:. recvbuf - address of receive buffer (choice) .N ThreadSafe.N Fortran.N Errors.N MPI_ERR_COMM.N MPI_ERR_COUNT.N MPI_ERR_TYPE.N MPI_ERR_BUFFER@*/int MPI_Alltoall(void *sendbuf, int sendcount, MPI_Datatype sendtype,                  void *recvbuf, int recvcount, MPI_Datatype recvtype,                  MPI_Comm comm){    static const char FCNAME[] = "MPI_Alltoall";    int mpi_errno = MPI_SUCCESS;    MPID_Comm *comm_ptr = NULL;    MPID_MPI_STATE_DECL(MPID_STATE_MPI_ALLTOALL);    MPIR_ERRTEST_INITIALIZED_ORDIE();        MPID_CS_ENTER();    MPID_MPI_COLL_FUNC_ENTER(MPID_STATE_MPI_ALLTOALL);    /* 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;	                MPID_Comm_valid_ptr( comm_ptr, mpi_errno );            if (mpi_errno != MPI_SUCCESS) goto fn_fail;	    MPIR_ERRTEST_COUNT(sendcount, mpi_errno);	    MPIR_ERRTEST_COUNT(recvcount, mpi_errno);	    MPIR_ERRTEST_DATATYPE(sendtype, "sendtype", mpi_errno);	    MPIR_ERRTEST_DATATYPE(recvtype, "recvtype", 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 );            }            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 );            }             MPIR_ERRTEST_SENDBUF_INPLACE(sendbuf, sendcount, mpi_errno);            MPIR_ERRTEST_RECVBUF_INPLACE(recvbuf, recvcount, mpi_errno);            MPIR_ERRTEST_USERBUFFER(sendbuf,sendcount,sendtype,mpi_errno);	    MPIR_ERRTEST_USERBUFFER(recvbuf,recvcount,recvtype,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->Alltoall != NULL)    {	mpi_errno = comm_ptr->coll_fns->Alltoall(sendbuf, sendcount,                                                 sendtype, recvbuf, recvcount,                                                 recvtype, comm_ptr);    }    else    {	MPIR_Nest_incr();        if (comm_ptr->comm_kind == MPID_INTRACOMM)             /* intracommunicator */            mpi_errno = MPIR_Alltoall(sendbuf, sendcount, sendtype,                                      recvbuf, recvcount, recvtype, comm_ptr);         else {            /* intercommunicator */	    /*	    mpi_errno = MPIR_Err_create_code( MPI_SUCCESS, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_COMM, 					      "**intercommcoll",					      "**intercommcoll %s", FCNAME );*/            mpi_errno = MPIR_Alltoall_inter(sendbuf, sendcount,                                            sendtype, recvbuf,                                            recvcount, recvtype,                                            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_ALLTOALL);    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_alltoall",	    "**mpi_alltoall %p %d %D %p %d %D %C", sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, 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 + -