【问题标题】:Controlling node mapping of MPI_COMM_SPAWN控制 MPI_COMM_SPAWN 的节点映射
【发布时间】:2018-05-24 09:57:55
【问题描述】:

上下文:

整个问题可以概括为我正在尝试复制对system(或fork)的调用行为,但在mpi 环境中。 (事实证明你不能并行调用system。)这意味着我有一个程序在许多节点上运行,每个节点上有一个进程,然后我希望每个进程调用一个外部程序(所以对于n 节点我会运行外部程序的n 副本),等待所有这些副本完成,然后继续运行原始程序。

为了以一种在并行环境中安全的方式实现这一点,我一直在使用MPI_COMM_SPAWN 和阻塞发送的组合。以下是我实现的一些示例父子程序(代码在 Fortran 90 中,但 C 程序的语法类似):

父.f90:

program parent

    include 'mpif.h'

    !usual mpi variables                                                                                                
    integer                        :: size, rank, ierr
    integer                        :: status(MPI_STATUS_SIZE)

    integer MPI_COMM_CHILD, ri
    integer tag
    character *128 message

    call MPI_Init(ierr)
    call MPI_Comm_size(MPI_COMM_WORLD, size, ierr)
    call MPI_Comm_rank(MPI_COMM_WORLD, rank, ierr)

    write(*, *) "I am parent on rank", rank, "of", size                                                 

    call MPI_COMM_SPAWN('./child', MPI_ARGV_NULL, 1, MPI_INFO_NULL, 0, &
        MPI_COMM_SELF, MPI_COMM_CHILD, MPI_ERRCODES_IGNORE, ierr)

    write(*, *) "Parent", MPI_COMM_SELF, "child comm", MPI_COMM_CHILD

    tag = 1
    call MPI_RECV(message, 128, MPI_CHARACTER, 0, tag, MPI_COMM_CHILD,&
                  status, ierr)
    write(*, *) "Parent", MPI_COMM_SELF, "child comm", MPI_COMM_CHILD,&
                "!!!"//trim(message)//"!!!"

    call mpi_barrier(mpi_comm_world, ierr)
    call MPI_Finalize(ierr)

end program parent

child.f90:

program child

  include 'mpif.h'

  !usual mpi variables                                                                                                
  integer                        :: size, rank, ierr, parent
  integer                        :: status(MPI_STATUS_SIZE)

  integer MPI_COMM_PARENT, psize, prank
  integer tag
  character *128 message

  call MPI_init(ierr)
  call MPI_Comm_size(MPI_COMM_WORLD, size, ierr)
  call MPI_Comm_rank(MPI_COMM_WORLD, rank, ierr)

  call MPI_Comm_get_parent(MPI_COMM_PARENT)
  call MPI_Comm_size(MPI_COMM_PARENT, psize, ierr)
  call MPI_Comm_rank(MPI_COMM_PARENT, prank, ierr)

  write(*, *) "I am child on rank", rank, "of", size, "with comm",&
              MPI_COMM_WORLD, "and parent", MPI_COMM_PARENT,&
              psize, prank

  tag = 1
  message = 'Hello Mom and/or Dad!'
  call MPI_SEND(message, 128, MPI_CHARACTER, 0, tag, MPI_COMM_PARENT, ierr)

  call mpi_barrier(MPI_COMM_WORLD, ierr)
  call MPI_Finalize(ierr)

end program child

使用 ifort 16.0.3 和 intel openmpi 1.10.3 编译并使用(例如)mpirun -np 4 ./parent 运行后,我得到以下输出:

 I am parent on rank           0 of           4
 I am parent on rank           1 of           4
 I am parent on rank           2 of           4
 I am parent on rank           3 of           4
 Parent           1 child comm           3
 I am child on rank           0 of           1 with comm           0 and parent
           3           1           0
 Parent           1 child comm           3 !!!Hello Mom and/or Dad!!!!
 Parent           1 child comm           3
 I am child on rank           0 of           1 with comm           0 and parent
           3           1           0
 Parent           1 child comm           3
 I am child on rank           0 of           1 with comm           0 and parent
           3           1           0
 Parent           1 child comm           3 !!!Hello Mom and/or Dad!!!!
 Parent           1 child comm           3 !!!Hello Mom and/or Dad!!!!
 Parent           1 child comm           3
 I am child on rank           0 of           1 with comm           0 and parent
           3           1           0
 Parent           1 child comm           3 !!!Hello Mom and/or Dad!!!!

这本质上是我想要的行为。据我了解,通过使用maxprocs=1root=0MPI_COMM_SELF 作为父通信器,我告诉每个父进程生成一个只知道其父进程的子进程,因为它是root=0MPI_COMM_SELF 范围的唯一进程)。然后我要求它等待来自其子进程的消息。孩子获得父母的(SELF)通信器并将其消息发送到只能是父母的root=0。所以这一切都很好。

问题:

我希望每个进程都会在自己的节点上生成子进程。我运行的 mpi 进程数等于节点数,当我调用 mpirun 时,我使用标志 --map-by node 来确保每个节点一个进程。我希望子进程会以某种方式继承它,否则不知道是否存在任何其他节点。但是我看到的行为是非常不可预测的,一些进程跨节点传播,而其他节点(尤其是主 mpi 进程的root=0)会堆积很多。

有没有办法确保进程绑定到父进程的节点?也许通过我可以传递给MPI_COMM_SPAWNMPI_Info 选项?

【问题讨论】:

  • 对于所有 Fortran 问题,请使用标签 fortran。您的问题并非针对旧的和过时的 Fortran 90。请注意,使用 use mpi 而不是 include "mpif.h" 可以帮助您诊断某些类型的错误。

标签: c parallel-processing fortran mpi openmpi


【解决方案1】:

Open MPI 中的每个 MPI 作业都从分布在一个或多个主机上的一组插槽开始。这些槽由初始 MPI 进程和作为子 MPI 作业的一部分生成的任何进程使用。在您的情况下,可以在类似于以下的主机文件中提供主机:

host1 slots=2 max_slots=2
host2 slots=2 max_slots=2
host3 slots=2 max_slots=2
...

slots=2 max_slots=2 将 Open MPI 限制为每台主机仅运行两个进程。

初始作业启动应为每个主机指定一个进程,否则 MPI 将用来自父作业的进程填充所有槽。 --map-by ppr:1:node 成功了:

mpiexec --hostfile hosts --map-by ppr:1:node ./parent

现在的问题是,随着新的子作业的产生,Open MPI 将继续按照先到先得的原则填充插槽,因此无法保证子进程将在与其父进程相同的主机上启动。要强制执行此操作,请按照 Gilles Gouaillardet 的建议将 info 参数的 host 键设置为 MPI_Get_processor_name 返回的主机名:

character(len=MPI_MAX_PROCESSOR_NAME) :: procn
integer :: procl
integer :: info

call MPI_Get_processor_name(procn, procl, ierr)

call MPI_Info_create(info, ierr)
call MPI_Info_set(info, 'host', trim(procn), ierr)

call MPI_Comm_spawn('./child', MPI_ARGV_NULL, 1, info, 0, &
...

您的 MPI 作业可能会中止并显示以下消息:

--------------------------------------------------------------------------
All nodes which are allocated for this job are already filled.
--------------------------------------------------------------------------

这基本上意味着请求的主机要么已满(所有插槽都已填满)主机不在原始主机列表中,因此没有分配插槽它。前者显然不是这种情况,因为主机文件列出了每个主机的两个插槽,而父作业只使用一个。 host 键值对中提供的主机名必须与初始主机列表中的条目完全匹配。通常情况下,主机文件只包含不合格的主机名,如第一段中的示例主机文件,而MPI_Get_processor_name 如果设置了域部分,则返回 FQDN,例如,node1.some.domain.localnode2.some.domain.local 等。解决方案是在主机文件中使用 FQDN:

host1.example.local slots=2 max_slots=2
host2.example.local slots=2 max_slots=2
host3.example.local slots=2 max_slots=2
...

如果分配是由 SLURM 等资源管理器提供的,则解决方案是将结果从 MPI_Get_processor_name 转换为匹配 RM 提供的内容。

请注意,MPI_Comm_spawn 的手册页列出了 add-host 键,它应该将值中的主机名添加到作业的主机列表中:

add-host               char *   Add the specified host to the list of
                                hosts known to this job and use it for
                                the associated process. This will be
                                used similarly to the -host option.

根据我的经验,这从未奏效(使用 Open MPI 最高 1.10.4 进行测试)。

【讨论】:

    【解决方案2】:

    来自Open MPI MPI_Comm_spawn 手册页

       The following keys for info are recognized in Open MPI. (The reserved values mentioned in Section 5.3.4 of the MPI-2 standard are not implemented.)
    
       Key                    Type     Description
       ---                    ----     -----------
    
       host                   char *   Host on which the process should be
                                       spawned.  See the orte_host man
                                       page for an explanation of how this
                                       will be used.
    

    您可以使用MPI_Get_processor_name() 来获取正在运行 MPI 任务的主机名。

    【讨论】:

      猜你喜欢
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-02
      相关资源
      最近更新 更多