基于C语言实现并行程序设计
实现MyBcast() 思路: (1)将MPI进程按所在节点划分子通讯域N; (2)可以将各子通讯域的首进程(编号为0)再组成一个子通讯域H; (3)由广播的root进程将消息发给原来最大通讯域中的0号进程h,再由h在H通讯域中广播(MPI_Bcast),各首进程然后在各自子通讯域N中再行广播(MPI_Bcast); (4)子通讯域H:将N的首进程通过MPI_Group_incl()函数建立一个组,再用MPI_Comm_create()建立子通讯域H。
//伪代码
MPI_Comm_split(MPI_COMM_WORLD, color, key, &split_comm_world);
建立MPI_COMM_WORLD的进程组World_Group;
通过World_Group建立h_Group;
PI_Comm_create(MPI_COMM_WORLD, h_Group, &h_comm_world);
oot进程发送消息:
MPI_Send(data, count, MPI_TYPE, 0, 1, MPI_COMM_WORLD);
原通讯域的0号进程接收:
MPI_Recv(data, count, MPI_TYPE, root, 1, MPI_COMM_WORLD,&status);
号进程在H中广播:
MPI_Bcast(data, count, MPI_TYPE, 0, h_comm_world);
在N中广播
MPI_Bcast(data, count, MPI_TYPE, 0, split_comm_world);
```http://www.biyezuopin.vip
```c
//主要代码:
MPI_Comm h_comm_world;
MPI_Comm_group(MPI_COMM_WORLD, &world_group);
int grpsize = num_procs / 2;
int zerolist[] = {0, 1, 2, 3};
int zerocnt = 0;
MPI_Group_incl(world_group, grpsize, zerolist, & new_group);
MPI_Comm_create(MPI_COMM_WORLD, new_group, & h_comm_world);
// message from root to 0 proc of MPI_COMM_WORLD
if (id_procs == root)
{
MPI_Send(&seq, 16, MPI_CHAR, 0, 1, MPI_COMM_WORLD);
}
else if (id_procs == 0)
{
MPI_Recv(&seq, 16, MPI_CHAR, root, 1, MPI_COMM_WORLD, &status);
}
MPI_Barrier(MPI_COMM_WORLD);
// Broadcast within the group H
if(h_comm_world != MPI_COMM_NULL)
MPI_Bcast(&seq, 16, MPI_CHAR, 0, h_comm_world);
MPI_Barrier(MPI_COMM_WORLD);
//Broadcasr within the group N
MPI_Bcast(&seq, 16, MPI_CHAR, 0, split_comm_world);
MPI_Barrier(MPI_COMM_WORLD);
实验结果: