上下文:
整个问题可以概括为我正在尝试复制调用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
子.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=1
、root=0
和 MPI_COMM_SELF
作为父通信器,我告诉每个父进程生成 1 个仅了解其父级的子级,因为它是 MPI_COMM_SELF
范围的 root=0
(也是唯一的进程)。然后我要求它等待来自子进程的消息。子进程获取父进程的 (SELF
) 通信器,并将其消息发送到只能是父进程的 root=0
。所以这一切都很好。
问题:
我希望每个进程都能在自己的节点上生成其子进程。我运行的 mpi 进程数等于节点数,当我调用 mpirun
时,我使用标志 --map-by node
来确保一个进程每个节点。我希望子进程能够以某种方式继承它,否则不知道任何其他节点的存在。但我看到的行为是非常不可预测的,一些进程分布在节点上,而其他节点(特别是主 mpi 进程的 root=0
)上有很多堆积。
是否有某种方法可以确保将进程绑定(bind)到父进程的节点?也许通过我可以传递给 MPI_COMM_SPAWN
的 MPI_Info
选项?
最佳答案
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.local
、node2.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 进行测试)。
关于控制 MPI_COMM_SPAWN 的节点映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47743425/