MPI Note[3]: 基本通信续

补充一些来自于MPITutorial的内容

MPI_Status

Send、Recv示例中的消息数量是固定的预先知道的,如果有动态的消息传输则需要使用MPI_Status处理

MPI_Status包含了如下一些信息:

  • MPI_SOURCE: 发送端的Rank.
  • MPI_Tag: 消息的Tag
  • Count: 使用MPI_Get_count获取消息的长度。
 

MPI_Probe

相对于前述示例,可以使用MPI_Probe在Recv前获取接收状态,并分配接收缓冲区:

// Author: Wes Kendall
// Copyright 2011 www.mpitutorial.com
// This code is provided freely with the tutorials on mpitutorial.com. Feel
// free to modify it for your own use. Any distribution of the code must
// either provide a link to www.mpitutorial.com or keep this header intact.
//
// Example of using MPI_Probe to dynamically allocated received messages
//

节点分组

很多分布式任务需要将节点分组,MPI里默认会创建一个MPI_COMM_WORLDMPI_Group并将所有的节点加入其中。

例如我们可以采用如下方式把节点分配到不同的组中

 

执行结果

zartbot@mpi1:~/mpi/study/group$ mpicc subgroup.c -o subgroup
zartbot@mpi1:~/mpi/study/group$ mpirun -np 8 subgroup
WORLD RANK:4 Belongs Sub Group 2,Local Rank:0
WORLD RANK:5 Belongs Sub Group 2,Local Rank:1
WORLD RANK:6 Belongs Sub Group 2,Local Rank:2
WORLD RANK:7 Belongs Sub Group 2,Local Rank:3
WORLD RANK:0 Belongs Sub Group 1,Local Rank:0
WORLD RANK:1 Belongs Sub Group 1,Local Rank:1
WORLD RANK:2 Belongs Sub Group 1,Local Rank:2
WORLD RANK:3 Belongs Sub Group 1,Local Rank:3

MPI_Comm_Split

一种简单的分组方式

 
zartbot@mpi1:~/mpi/study/group$ mpirun  -np 8 ./split
WORLD RANK/SIZE: 0/8 --- ROW RANK/SIZE: 0/4
WORLD RANK/SIZE: 1/8 --- ROW RANK/SIZE: 1/4
WORLD RANK/SIZE: 2/8 --- ROW RANK/SIZE: 2/4
WORLD RANK/SIZE: 4/8 --- ROW RANK/SIZE: 0/4
WORLD RANK/SIZE: 5/8 --- ROW RANK/SIZE: 1/4
WORLD RANK/SIZE: 6/8 --- ROW RANK/SIZE: 2/4
WORLD RANK/SIZE: 3/8 --- ROW RANK/SIZE: 3/4
WORLD RANK/SIZE: 7/8 --- ROW RANK/SIZE: 3/4

同步屏障

有些时候需要进行一些粗颗粒度的并行计算,例如每个local节点进行大批量的模型训练,需要在某一个地方等待一下,大家步调一致再执行下一步。 或者有些需要Benchmark测量的地方,需要停下来统计一下时间:

 
zartbot@mpi1:~/mpi/study/group$ mpicc barrier.c -o barrier
zartbot@mpi1:~/mpi/study/group$ mpirun -np 8 barrier
time 0.002982

BSP

Bulk Synchrononous Parallel,BSP模型计算步骤如下:

  1. concurrent computation step: processes locally and asynchronously compute, and those local computation can overlap with communications,
  2. communication step: processes exchange data between themselves,
  3. synchronization barrier step: when a process reaches a synchronization barrier,it waits for all the other processes to reach this barrier before proceeding another super-step.

并行矩阵乘法

可以用最简单的阻塞通信实现一个并行矩阵乘法

 

执行结果:

zartbot@mpi1:~/mpi/study/matrix$ mpicc vector.c -o vector
zartbot@mpi1:~/mpi/study/matrix$ mpirun -np 4 vector
a[0][0]=0 a[0][1]=1 a[0][2]=2 a[0][3]=3 b[0]=0
a[1][0]=1 a[1][1]=2 a[1][2]=3 a[1][3]=4 b[1]=1
a[2][0]=2 a[2][1]=3 a[2][2]=4 a[2][3]=5 b[2]=2
a[3][0]=3 a[3][1]=4 a[3][2]=5 a[3][3]=6 b[3]=3
c[0]=14
c[1]=20
c[2]=26
c[3]=32

Reduce Example

Reduce函数如下:

int MPI_Reduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)

我们以一个简单的阶乘为例, sendbuf为number变量,recvbuf为globalFact,count=1,MPI_Op为MPI_Prod,根节点为0.

中间我们加了一些屏障用于输出中间过程。

 

执行结果:

zartbot@mpi1:~/mpi/study/matrix$ mpirun -np 8 reduce
[before]rank: 0 local number:1 GlobalFact: -1
[before]rank: 1 local number:2 GlobalFact: -1
[before]rank: 2 local number:3 GlobalFact: -1
[before]rank: 3 local number:4 GlobalFact: -1
[before]rank: 4 local number:5 GlobalFact: -1
[before]rank: 6 local number:7 GlobalFact: -1
[before]rank: 7 local number:8 GlobalFact: -1
[before]rank: 5 local number:6 GlobalFact: -1
[after]rank: 0 local number:1 GlobalFact: 40320
[after]rank: 2 local number:3 GlobalFact: -1
[after]rank: 3 local number:4 GlobalFact: -1
[after]rank: 7 local number:8 GlobalFact: -1
[after]rank: 4 local number:5 GlobalFact: -1
[after]rank: 6 local number:7 GlobalFact: -1
[after]rank: 5 local number:6 GlobalFact: -1
[after]rank: 1 local number:2 GlobalFact: -1
Computing the factorial in MPI: 8 processus = 40320
Versus local factorial: 40320

MPI Note[3]: 基本通信续》来自互联网,仅为收藏学习,如侵权请联系删除。本文URL:http://www.bookhoes.com/743.html