1

无论如何,我可以通过它了解作为通信器一部分的所有进程吗?假设总共有 16 个 MPI 进程,MPI_Comm comm 有 4 个进程作为一个组。仅给定通信器 comm 我们可以知道属于通信器的所有进程的等级吗?

谢谢你

4

1 回答 1

5

每个通信器都有一个关联的进程组,可通过调用MPI_COMM_GROUPMPI_Comm_group在 C 绑定中)获得。获得进程组comm后,可以使用MPI_GROUP_TRANSLATE_RANKS将组中的等级列表转换为组中comm的相应等级MPI_COMM_WORLD。必须经过翻译过程,因为在 组内comm,参与过程的等级从0MPI_COMM_SIZE(comm)-1

这是一个示例实现:

void print_comm_ranks(MPI_Comm comm)
{
   MPI_Group grp, world_grp;

   MPI_Comm_group(MPI_COMM_WORLD, &world_grp);
   MPI_Comm_group(comm, &grp);

   int grp_size;

   MPI_Group_size(grp, &grp_size);

   int *ranks = malloc(grp_size * sizeof(int));
   int *world_ranks = malloc(grp_size * sizeof(int));

   for (int i = 0; i < grp_size; i++)
      ranks[i] = i;

   MPI_Group_translate_ranks(grp, grp_size, ranks, world_grp, world_ranks);

   for (int i = 0; i < grp_size; i++)
      printf("comm[%d] has world rank %d\n", i, world_ranks[i]);

   free(ranks); free(world_ranks);

   MPI_Group_free(&grp);
   MPI_Group_free(&world_grp);
}

这是一个示例用法:

int rank;
MPI_Comm comm;

MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_split(MPI_COMM_WORLD, rank % 2, rank, &comm);

if (rank == 0)
{
   printf("Rank 0 view:\n");
   print_comm_ranks(comm);
}
else if (rank == 1)
{
   printf("Rank 1 view:\n");
   print_comm_ranks(comm);
}

和对应的输出有 7 个进程:

Rank 0 view:
comm[0] has world rank 0
comm[1] has world rank 2
comm[2] has world rank 4
comm[3] has world rank 6
Rank 1 view:
comm[0] has world rank 1
comm[1] has world rank 3
comm[2] has world rank 5

(分裂后排名01最终在不同的传播者中)

Note that you can only enumerate the contents of a communicator that the current process knows about, because communicators are referred by their handles and those are local values to each process.

于 2013-02-05T16:53:00.157 回答