MPI_allreduce如何指定输入缓冲与输出缓冲相同

本文介绍MPI_Allreduce函数的使用方法,特别关注如何通过使用MPI_IN_PLACE实现输入缓冲区与输出缓冲区相同的情况。同时提供了C语言及Fortran的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值