好问题!
这就是MPI_Comm_split 命令的用处。
定义
int MPI_Comm_split(MPI_Comm comm, int color, int key, MPI_Comm *newcomm)
参数
更长的解释
在整个过程中请记住,您有许多进程在执行看似相同的代码。因此,newcomm 的值可能因进程而异。
color 是一个整数值,用于确定当前进程将落在哪个新的子通信器中。 comm 的所有颜色具有相同数值的进程都将成为同一个新子通信器newcomm 的一部分。
例如,如果您定义了color = rank%4(参见下面的示例 4),那么您将(全局)创建四个新的通信器。请记住,每个进程只会看到它所属的这些新通信器之一。换句话说,颜色决定了您将创建哪些“球队”,例如足球队的球衣颜色。
key 将确定流程在它们所属的新通信器中的排名方式。如果设置key = rank,那么每个新通讯器newcomm中的排名顺序(而不是排名本身)将遵循原始通讯器comm中的排名顺序。如果 key 的两个或多个值具有相同的值,则 comm 中排名较低的进程在 newcomm 中排名较低。 (参见下面的示例 2。)
示例
以下是我在下面的代码中复制的一些图片示例。示例 #5 回答了您的具体问题。
示例代码
//Compile with mpic++ main.cpp
#include <mpi.h>
int main(int argc, char **argv){
int world_size, world_rank;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &world_size); //Get the number of processes
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); //Get the rank of the process
MPI_Comm comm = MPI_COMM_WORLD;
MPI_Comm newcomm1, newcomm2, newcomm3, newcomm4, newcomm5;
//Example 1: Duplicate the existing communicator. The command `MPI_Comm_dup()`
// does exactly this.
MPI_Comm_split(comm, 0, world_rank, &newcomm1);
//Example 2: Duplicate the existing communicator, but reverse the
// rankings
MPI_Comm_split(comm, 0, world_size-world_rank, &newcomm2);
int rank2; //Get the rank of the process
MPI_Comm_rank(newcomm2, &rank2); //in the new communicator
//Example 3: Split each process into its own communicator. This is the
// equivalent of using `MPI_COMM_SELF` for each process.
MPI_Comm_split(comm, world_rank, world_rank, &newcomm3);
//Example 4: Split processes into communicators based on their colouring. Use
// their rank in the existing communicator to determine their
// relative rank order in the new communicator.
int color = world_rank / 4;
MPI_Comm_split(comm, color, world_rank, &newcomm4);
int rank4; //Get the rank of the process
MPI_Comm_rank(newcomm2, &rank4); //in the new communicator
//Example 5: Group only some of the processes into a new communicator based on
//a flag.
int flag = world_rank%2==0; //An example flag
MPI_Comm_split(comm, flag?0:MPI_UNDEFINED, world_rank, &newcomm5);
MPI_Finalize();
}
更多信息