MPI_allreduce
在指定的通信器上执行全局的规约操作
如何使输入缓冲与输出缓冲相同,可以在调用的时候使用MPI_IN_PLACE,SENDBUF对应的参数调用时用MPI_IN_PLACE代替
若是C语言调用
直接用比如
MPI_Allreduce(MPI_IN_PLACE,arr,4,MPI_INT,MPI_SUM,MPI_COMM_WORLD)
参见http://www.open-mpi.org/doc/v1.5/man3/MPI_Allreduce.3.php
http://www.mpi-forum.org/docs/mpi-20-html/node145.htm
When the communicator is an intracommunicator, you can perform an all-reduce operation in-place (the output buffer is used as the input buffer). Use the variable MPI_IN_PLACE as the value of sendbuf at all processes.
Note that MPI_IN_PLACE is a special kind of value; it has the same restrictions on its use as MPI_BOTTOM.
Because the in-place option converts the receive buffer into a send-and-receive buffer, a Fortran binding that includes INTENT must mark these as INOUT, not OUT.
Synopsis
int MPI_Allreduce ( void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm )
Input Parameters
-
sendbuf
- starting address of send buffer (choice) count
- number of elements in send buffer (integer) datatype
- data type of elements of send buffer (handle) op
- operation (handle) comm
- communicator (handle)
Output Parameter
-
recvbuf
- starting address of receive buffer (choice)
program main
implicit none
include 'mpif.h'
integer ::ierr
integer ::rank,np
! real(KIND=8),dimension(4) ::buf
real,dimension(2,2) ::arr
real,dimension(2,2) ::res
call MPI_Init(ierr)
call MPI_Comm_size(MPI_COMM_WORLD,np,ierr)
call MPI_Comm_rank(MPI_COMM_WORLD,rank,ierr)
if(rank==0)then
arr(1,2)=5
elseif(rank==1)then
arr(1,2)=-123
endif
!call MPI_Allreduce(arr,res,4,MPI_REAL,MPI_SUM,MPI_COMM_WORLD,ierr)
!call MPI_Allreduce(arr,arr,4,MPI_REAL,MPI_SUM,MPI_COMM_WORLD,ierr)
call MPI_Allreduce(MPI_IN_PLACE,arr,4,MPI_REAL,MPI_SUM,MPI_COMM_WORLD,ierr)
!print*,res(1,2)
print*,arr(1,2)
call MPI_Finalize(ierr)
end