MPI集体通讯(一)

集体通讯(collective communications) 能够在MPI指定的通讯域中所有的进程之间传递数据。MPI提供以下几种集体通讯例子:
1) 同步所有进程
2)全局通讯函数:包括Broadcase,Gather,Scatter,以及全局的规约操作(含求和,最大值,最小值等等),全局通讯函数基本来说有三个通讯模式:
a) 根进程给所有进程(包括自己)发送数据,如broadcase,scatter
b) 根进程从所有进程(包括自己)接收数据,如gather
c) 每个进程和每个进程通讯(包括自己),如allgather和alltoall.
MPI集体通讯的语法设计和点对点通信基本一致。然而,为了使用方便,MPI集体通讯函数比点对点通讯有更多的限制。如,和点对点通讯不同,集体通讯中数据发送的数量必须和接收函数指定的数量精确匹配。另外一个简化是集体通讯函数采用阻塞通讯模式。集体通讯函数不需要用tag,因此,在通讯域内通讯时,集体通讯函数的调用是根据执行的顺序而严格匹配的。
最后的简化是集体通讯函数仅有一种模式,这个模式可以类比为点对点通讯中的标准模式。更具体的说,一个集体通讯函数能够在它完成参与整个通讯域之后立刻返回。通常,完成意味着函数调用者能够修改通讯缓冲器的内容,并不意味着其他进程也完成了相应操作,甚至开始了相应操作。
1. 阻塞同步函数MPI_BARRIER
MPI_BARRIER能够阻塞调用函数,直到所有进程都调用了此函数。简单来说,任何进程只能够在其他所有进程都调用了此函数之后才能够返回。
2. 广播函数MPI_BCAST
MPI_BCAST能够将根进程的消息广播给其他所有进程。此函数返回的时候,意味着根进程缓存中的内容已经拷贝给了所有进程。导出的数据类型也可以在此函数中使用。例子:

MPI_Bcast(
void *buffer //starting address of buffer
int count //number of entries in buffer
MPI_Datatype datatype //data type of buffer
int root //rank of broadcast root
MPI_Comm comm) //group communicator

这里写图片描述

MPI_Comm comm;
int array[100];
int root = 0;
......
MPI_Bcast(array,100,MPI_INT,root,comm);

For example, say 0 is the root process. To process 0, MPI_Bcast functions as a send with buffer being the sending buffer. For everybody else, MPI_Bcast functions as a receive with buffer being the receiving buffer. So, the code to use MPI_Bcast would be something like this:
.
.
.
if (process 0)
{
some_value=something;
}

    /* at this point, only process 0 has some_value */

    MPI_Bcast(&some_value,...0,...)

    /* at this point everybody has some_value */
    .
    .
    .

3.收集函数Gather
每个进程(包括根进程)发送它发送缓存中的内容到根进程。根进程收到并且根据进程序号存储接收的内容。所以其结果好像是每一个进程都执行了一次MPI_Send(sendbuf,sendcount,sendtype,root,tag,comm) 并且根进程执行了n次MPI_Recv(recvbuff+i*recvcount*extent(recvtype),recvcount,recvtype,i,tag,comm).
通常,导出数据类型也能够在Gather函数中

MPI_Gather(
void *sendbuf //starting address of send buffer
int sendcount //number of elements in send buffer
MPI_Datatype sendtype //data type of send buffer elements
void *recvbuf //starting address of receive buffer
int *recvcounts //number of elements for any single receive
MPI_Datatype recvtype //data type of recv buffer elements
int root //rank of receiving process
MPI_Comm comm) //group communicator

See the blow image for the detailed information:
这里写图片描述

Gather 100 ints from every process in group to root:
MPI_Comm comm;
int gsize,sendarray[100];
int root, * rbuf;
.....
MPI_Comm_size(comm,&gsize);
rbuf = (int*)malloc(gsize*100*sizeof(int));
MPI_Gather(sendarry,100,MPI_INT,rbuf,100,MPI_INT,root,comm);

或者,可以只在根进程分配接收缓存:

MPI_Comm comm;
int gsize,sendarray[100];
int root, * rbuf,myrank,root;
.....
MPI_Co
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值