一、MPI的四个基本函数
1、MPI_Init
MPI_Init:初始化MPI执行环境。
在MPI_Init函数中,并不一定都需要设置argc_p和argv_p这两个参数的,不需要的时候,将它们设置为NULL即可
MPI_Init(
int* argc_p,
char*** argv_p
);
他们的第一个参数都传入通信子作为参数,第二参数都用传出参数分别把正在调用通信子的进程号和通信的个数
2、MPI_Finalize
MPI_Finalize:结束MPI执行环境。
3、MPI_Comm_rank
用于标识各个进程的编号
4、MPI_Comm_size
用于标识有多少个进程
二、点对点通信
MPI_SEND:
int MPI_Send (void *buf, int count, MPI_Datatype datatype,int dest, int tag,MPI_Comm comm)
MPI_RECV :
int MPI_RECV(buf,count,datatype,source,tag,comm,status)
三、消息参数
(1) 发送或者接收缓冲区buf;
(2) 数据数量count;
(3) 数据类型datatype;
注意类型匹配,主要是发送和接收的数据类型的匹配。
(4) 目标进程或者源进程destination/source;
(5) 消息标签tag;
(6) 通信域comm;.
(7) 消息状态status,只在接收的函数中出现
四、聚合通信
1、同步
int MPI_Barrier(MPI_Comm comm)
2、广播
一对多
int MPI_Bcast( void *buffer, int count, MPI_Datatype datatype, int root, MPI_Comm comm )
3、聚集
多对一
int MPI_Gather(void *sendbuf, int sendcnt, MPI_Datatype sendtype,void *recvbuf, int recvcnt, MPI_Datatype recvtype,int root, MPI_Comm comm)
4、扩散
int MPI_Scatter(void *sendbuf, int sendcnt, MPI_Datatype sendtype,void *recvbuf, int recvcnt, MPI_Datatype recvtype, int root,MPI_Comm comm)
5、allgather
MPI_Allgather的作用是每一个进程都收集到其他所有进程的消息,它相当于每一个进程都执行了MPI_Gather执行完了MPI_Gather之后,所有的进程的接收缓冲区的内容都是相同的,也就是说每个进程给所有进程都发送了一个相同的消息,所以名为allgather。
int MPI_Allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype,MPI_Comm comm)
五、规约和扫描reduce and scan
1、reduce
这里每个进程的待处理数据存放在sendbuf中,可以是标量也可以是向量。所有进程将这些值通过输入的操作子op计算为最终结果并将它存入root进程的recvbuf。数据项的数据类型在Datatype域中定义。
int MPI_Reduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype,
MPI_Op op, int root, MPI_Comm comm)
2、scan
MPI_Scan常用于对分布于组中的数据作前置归约操作。此操作将序列号为0,···,i(包括i)的进程发送缓冲区的归约结果存入序列号为i 的进程接收消息缓冲区中。这种操作支持的数据类型、操作以及对发送及接收缓冲区的限制和规约相同。与规约相比,扫描Scan操作省去了Root域,因为扫描是将部分值组合成n个最终值,并存放在n个进程的recvbuf中。具体的扫描操作由Op域定义。
MPI的归约和扫描操作允许每个进程贡献向量值,而不只是标量值。向量的长度由Count定义。MPI也支持用户自定义的归约操作。
int MPI_Scan(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype,
MPI_Op op, MPI_Comm comm)