【问题标题】:How to know the all the ranks of the processor that are part of a communicator in MPI?如何知道 MPI 中作为通信器一部分的处理器的所有等级?
【发布时间】:2013-02-06 00:58:25
【问题描述】:

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

谢谢

【问题讨论】:

  • 如果您的意思是您想知道comm 中的进程在默认通信器mpi_comm_world 中的排名,那么您问题的答案是这取决于您如何创建@987654323 @。那么,您是如何创建 comm 的?
  • 假设如果通讯器是使用 MPI_Comm_Create 创建的。我们怎么知道?

标签: mpi


【解决方案1】:

每个通信器都有一个关联的进程组,可通过调用MPI_COMM_GROUP(C 绑定中的MPI_Comm_group)获得。一旦获得comm 的进程组,就可以使用MPI_GROUP_TRANSLATE_RANKScomm 组中的rank 列表转换为MPI_COMM_WORLD 组中的相应rank。必须经过翻译过程,因为在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在拆分后最终在不同的传播者中)

请注意,您只能枚举当前进程知道的通信器的内容,因为通信器由它们的句柄引用,并且这些是每个进程的本地值。

【讨论】:

    猜你喜欢
    • 2015-06-01
    • 1970-01-01
    • 2021-09-18
    • 2014-05-18
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    相关资源
    最近更新 更多