MPI:获取给定通讯器中所有处理器的等级

MPI: get ranks of all processors in a given communicator

本文关键字:处理器 获取 通讯器 MPI      更新时间:2023-10-16

我有一个通信器,我如何获得该通信器中所有处理器的等级?

我所能找到的就是如何获得给定通信器中的处理器数量,但似乎没有获得秩集合的函数。

添加到Patrick的答案中:

要获得从comm_1到comm_2或从comm_2到comm_2的进程等级,您可以首先提取底层MPI_Group,然后使用MPI_Group_translate_ranks

MPI_Comm comm = MPI_COMM_WORLD;
MPI_Comm my_comm;
int n;
MPI_Comm_size(comm, &n);
int rank1[n] = {0,1,2,3,...}
int rank2[n];
// Some Code
MPI_Group world_group;
MPI_Group my_comm_group;
MPI_Comm_group(comm, &world_group);
MPI_Comm_group(my_comm, &my_comm_group);
MPI_Group_translate_ranks(world_group, n, rank1, my_comm_group, rank2);

您将获得数组rank1,数组rank2中对应的等级。

秩总是线性分配的。如果您的通信器的大小为p,那么所有处理器的列将为0, 1, 2, ..., p-1

如果您的通信器是MPI_COMM_WORLD的子通信器,则处理器将重新标记为从0到子通信器大小的等级。

如果您正在查找子通信器的处理器的全局排列(如MPI_COMM_WORLD中分配的)。您必须使用进程排名为MPI_COMM_WORLD:的MPI_GatherMPI_Allgather

// get global rank
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// getting size of your communicator
int size;
MPI_Comm_size(your_comm, &size);
// all-gather global ranks
int * ranks = malloc(sizeof(int)*size);
MPI_Allgather(&rank, 1, MPI_INT, ranks, 1, MPI_INT, your_comm);