mpi4py 中的动态进程管理

上一篇中我们介绍了 mpi4py 中的进程拓扑操作方法,下面我们将介绍动态进程管理。

从 MPI-2 开始,允许在 MPI 应用程序启动之后创建和取消进程,定义和取消进程之间的协作关系,并提供一种机制在已有进程和新增进程之间建立联系,甚至在两个彼此无关的 MPI 应用程序之间建立联系。

在 mpi4py 中,一个 MPI 进程可通过 MPI.Intracomm.Spawn 或 MPI.Intracomm.Spawn_multiple 来创建新的进程。创建新的进程时,可通过 MPI.Info 对象告诉 MPI 运行时环境如何启动这个进程,但这些信息对 MPI 本身透明。

相关方法

MPI.Intracomm.Spawn(self, command, args=None, int maxprocs=1, Info info=INFO_NULL, int root=0, errcodes=None)

启动 maxprocs 个相同的 command 进程,并建立与这些进程间的联系,返回新进程的组间通信子对象。新创建的进程作为当前调用进程的子进程,子进程组具有与其父进程不同的 MPI.COMM_WORLD。Spawn 在当前通信子参数上为集合通信,因此仅当所有子进程都调用完 MPI.Init() 之后才可返回(MPI.Init() 在从 mpi4py 包 import MPI 时调用),因此可以说,父进程的 Spawn 和子进程的 MPI.Init() 形成一个集合操作。所返回的组间通信子对象中,在其 local 组中包含了父进程,在其远程组中包含了所有新创建的子进程。在子进程里,可通过下面介绍的 MPI.Comm.Get_parent() 方法获取组间通信子对象。command 字符串指出要启动进程所用的命令/文件名,搜索这些可执行文件的步骤和方法由具体的运行时环境决定,通常通过 PATH 环境变量以及当前工作目录来搜索。args 数组(如果不是默认的 None)是运行 command 程序所需的参数,如果没有参数,可以设置为 None。info 对象(如果不是默认的 MPI.INFO_NULL)中保存若干个用户指定的 (key, value) 对。root 参数的含义是:参数列表中 root 之前的参数仅在编号为 root 的进程里使用,在其它进程均被忽略,root 编号取在当前通信子中的编号。errcodes 参数是一个长度为 maxprocs 的数组,每个元素对应于相应进程创建时的错误信息。执行这个方法时,将尝试最大创建 maxprocs 个进程,如果不能,则会产生 MPI.ERR_SPAWN 错误。可通过 info 对象来控制创建进程的具体行为,例如如果无法创建 maxprocs 个进程,则可创建尽可能多个进程,而不是返回错误。要求全部 maxprocs 个进程都创建成功的做法称作 hard 约束,反之称作 soft 约束, hard 和 soft 是 info 对象的一个关键字。实际创建的子进程数目可通过组间通信子的 size 得知。

注意:Spawn 只允许创建相同进程的多个版本,无法同时创建不同进程或相同命令使用不同的参数创建多个进程。多次调用 Spawn 也能创建多个进程,但其 MPI.COMM_WORLD 彼此不同。

MPI.Intracomm.Spawn_multiple(self, command, args=None, maxprocs=None, info=INFO_NULL, int root=0, errcodes=None)

功能同 Spawn 类似,但是参数 commandargsmaxprocs 都应该(或可以)设置成一个序列(数组),用多个不同的命令及各自不同的参数来分别启动 maxprocs 序列元素所指定数目个进程,并建立与这些进程间的联系,返回新进程的组间通信子对象。所有新创建的进程共享一个新的 MPI.COMM_WORLD,在 MPI.COMM_WORLD 中的 rank 值与其在 command 数组中指定的顺序一一对应。如果某些命令不需要参数则在 args 相应的位置使用 None,当所有命令都不需要参数时,也可直接设置成 None (默认值)。

MPI.Comm.Get_parent(type cls)

返回当前进程的父组间通信子对象,父组间通信子对象由 MPI.Init() 间接创建,与父进程调用完 Spawn,Spawn_multiple 返回的组间通信子对象相同。

MPI.Comm.Disconnect(self)

等待所有通信动作完成,然后释放通信子对象,并将其设置为 MPI.COMM_NULL。不可在 MPI.COMM_WORLD 和 MPI.COMM_SELF 上调用。其使用条件与 MPI.Finalize 相同,即不可在通信中间而需等所有通信动作都完成时才可使用。

例程

下面给出动态进程管理相关方法的使用例程。

# spawn_master.py

"""
Demonstrates the usage of Spawn, Scatter, Gather, Disconnect.

Run this with 1 process like:
$ mpiexec -n 1 python spawn_master.py
# or
$ python spawn_master.py
"""

import sys
import numpy as np
from mpi4py import MPI

# create two new processes to execute spawn_slave.py
comm = MPI.COMM_SELF.Spawn(sys.executable, ['spawn_slave.py'], maxprocs=2)
print 'master: rank %d of %d' % (comm.rank, comm.size)

# scatter [1, 2] to the two new processes
send_buf = np.array([1, 2], dtype='i')
comm.Scatter(send_buf, None, root=MPI.ROOT)
print 'master: rank %d sends %s' % (comm.rank, send_buf)

# gather data from the two new processes
recv_buf = np.array([0, 0], dtype='i')
comm.Gather(None, recv_buf, root=MPI.ROOT)
print 'master: rank %d receives %s' % (comm.rank, recv_buf)

# disconnect and free comm
comm.Disconnect()
# spawn_slave.py

"""
Demonstrates the usage of Get_parent, Scatter, Gather, Disconnect.
"""

import numpy as np
from mpi4py import MPI

# get the parent intercommunicator
comm = MPI.Comm.Get_parent()
print 'slave: rank %d of %d' % (comm.rank, comm.size)

# receive data from master process
recv_buf = np.array(0, dtype='i')
comm.Scatter(None, recv_buf, root=0)
print 'slave: rank %d receives %d' % (comm.rank, recv_buf)

# increment the received data
recv_buf += 1

# send the incremented data to master
comm.Gather(recv_buf, None, root=0)
print 'slave: rank %d sends %d' % (comm.rank, recv_buf)

# disconnect and free comm
comm.Disconnect()

运行结果如下:

$ python spawn_master.py
master: rank 0 of 1
master: rank 0 sends [1 2]
master: rank 0 receives [2 3]
slave: rank 0 of 2
slave: rank 0 receives 1
slave: rank 0 sends 2
slave: rank 1 of 2
slave: rank 1 receives 2
slave: rank 1 sends 3
# spawn_multiple_master.py

"""
Demonstrates the usage of Spawn_multiple, Disconnect.

Run this with 1 process like:
$ mpiexec -n 1 python spawn_multiple_master.py
# or
$ python spawn_multiple_master.py
"""

import sys
import numpy as np
from mpi4py import MPI

# create one new process to execute spawn_slave1.py, and two new processes to execute spawn_slave2.py
commands = [sys.executable] * 2
args    = [['spawn_slave1.py'], ['spawn_slave2.py']]
maxprocs = [1, 2]
comm = MPI.COMM_WORLD.Spawn_multiple(commands, args, maxprocs)

# disconnect and free comm
comm.Disconnect()
# spawn_slave1.py

"""
Demonstrates the usage of Get_parent, scatter, Disconnect.
"""

from mpi4py import MPI


comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()

print 'slave1: rank %d of %d' % (rank, size)

recv_obj = comm.scatter([0, 1, 2], root=0)
print 'slave1: rank %d receives %d' % (rank, recv_obj)

# disconnect and free comm
MPI.Comm.Get_parent().Disconnect()
# spawn_slave2.py

"""
Demonstrates the usage of Get_parent, scatter, Disconnect.
"""

from mpi4py import MPI


comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()

print 'slave2: rank %d of %d' % (rank, size)

recv_obj = comm.scatter([0, 1, 2], root=0)
print 'slave2: rank %d receives %d' % (rank, recv_obj)

# disconnect and free comm
MPI.Comm.Get_parent().Disconnect()

运行结果如下:

$ python spawn_multiple_master.py
slave1: rank 0 of 3
slave1: rank 0 receives 0
slave2: rank 1 of 3
slave2: rank 1 receives 1
slave2: rank 2 of 3
slave2: rank 2 receives 2

以上我们介绍了 mpi4py 中的动态进程管理,在下一篇中我们将介绍单边通信。

    原文作者:自可乐
    原文地址: https://www.jianshu.com/p/fcf31970f038
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞