MPI:获得给定通信器中所有处理器的等级

时间:2015-03-27 22:58:53

标签: c++ parallel-processing mpi

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

我所能找到的只是如何获得给定通信器中的处理器数量,但似乎没有一个功能来获得一组排名。

2 个答案:

答案 0 :(得分:3)

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

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

如果您正在寻找子通信器处理器的全局排名(在MPI_COMM_WORLD中分配)。您必须使用MPI_GatherMPI_Allgather,其流程排名为MPI_COMM_WORLD

// 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);

答案 1 :(得分:3)

加入帕特里克的回答:

要获得从comm_1到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);

您将在数组rank2中获得数组rank1对应的排名。