mpi4py 中的进程拓扑方法

xiaoxiao2021-02-28  14

本文从本人简书博客同步过来

在上一篇中我们简要介绍了进程拓扑的基本概念,下面我们将介绍与进程拓扑相关的一些方法。

创建方法

注意:只能在组内通信子(Intracomm)或 Intracomm 类子类通信子上创建拓扑通信子。创建拓扑通信子的相关方法(MPI.Intracomm 类的方法)接口如下:

Create_cart(self, dims, periods=None, bool reorder=False)

创建并返回一个新的通信子,在这个新通信子上附加与笛卡尔拓扑管理有关的数据结构及相应的操作信息。dims 是长度为维数 ndims 的整型数组,指出各维的进程数,periods 可取值 None,True,False 或长度为 ndims 的布尔数组,指出各维是否周期性循环,默认值 None 表示各维都不循环,True/False 表示各维都循环/都不循环。布尔型的 reorder 指出进程在新创建的通信子组内是否进行重排序,默认值为 False,此时进程在新创建的通信子组内的顺序与在原通信子组内的顺序相同。在创建过程中,允许新通信子中包含的进程数少于原通信子中的进程数,此时多余的进程将返回 MPI.COMM_NULL,但不允许多于原通信子中的进程数。

Create_graph(self, index, edges, bool reorder=False)

创建并返回一个新的通信子,在这个新通信子上附加与图结构的拓扑管理有关的数据结构及相应的操作信息。通过 index 和 edges 两个整数序列参数来描述整个图的结构,假设生成的图拓扑的节点个数为 nnodes,各节点按照从 0 到 nnodes - 1 编号,其邻居节点都顺序、连续地放在 edges 序列中,通过 index 序列分别指定各编号节点的邻居节点列表在 edges 序列中的起始位置,index 的第 i 个元素保存图中前 i 个节点的邻居总数,而各节点的邻居顺序地排成一个一维序列存放在 edges 中。因此,index[0] 为节点 0 的邻居个数,index[i] - index[i - 1] 为节点 i > 0 的邻居个数,而节点 0 的邻居保存在 edges[0:(index[0] - 1)] 范围内,节点 i > 0 的邻居保存在 edges[index[i-1]:(index[i] - 1)] 范围内。布尔型的 reorder 指出进程在新创建的通信子组内是否进行重排序,默认值为 False,此时进程在新创建的通信子组内的顺序与在原通信子组内的顺序相同。在创建过程中,允许新通信子中包含的进程数少于原通信子中的进程数,此时多余的进程将返回 MPI.COMM_NULL,但不允许多于原通信子中的进程数。

下面举个简单的例子来说明如何设置以上参数,比如创建一个含有 4 个节点的图拓扑,各个节点的邻居节点如下表所示:

NodeNeighbors01, 3102330, 2

则相应的参数可以如下设置:

ArgumentInputnnodes4index2, 3, 4, 6edges1, 3, 0, 3, 0, 2 Create_dist_graph_adjacent(self, sources, destinations, sourceweights=None, destweights=None, Info info=INFO_NULL, bool reorder=False)

创建一个分布式的图拓扑通信子。MPI-3 中引进的新方法,暂不作介绍。

Create_dist_graph(self, sources, degrees, destinations, weights=None, Info info=INFO_NULL, bool reorder=False)

创建一个分布式的图拓扑通信子。MPI-3 中引进的新方法,暂不作介绍。

笛卡尔拓扑通信子(Cartcomm)

方法

继承自拓扑通信子(Topocomm),其特有的一些方法有:

Get_topo(self)

获取当前拓扑通信子的详细信息。也可以通过属性 topo 获取。

Get_cart_rank(self, coords)

返回根据笛卡尔拓扑的坐标 coords 得到的进程编号。

Get_coords(self, int rank)

返回根据进程编号 rank 得到的笛卡尔拓扑坐标。

Get_dim(self)

返回笛卡尔拓扑的维数。也可以通过属性 dim 或 ndim 获取。

Shift(self, int direction, int disp)

给定一个笛卡尔坐标平移的维度 direction 和步长 disp(大于 0 表示正方向,小于 0 表示负方向),返回平移的源进程号和目的进程号。对无周期的维,平移到末端后再平移将得到 MPI.PROC_NULL。

Sub(self, remain_dims)

将当前的笛卡尔拓扑通信子组划分成若干子组,每个子组对应原笛卡尔拓扑网格的子网格,返回由这些子组创建成的子笛卡尔拓扑通信子。由参数 remain_dims 决定如何进行划分:如果 remain_dims[i] 为 True 表示在新坐标中保留第 i 维,否则去掉第 i 维。该方法与 MPI.Comm.Split 方法的功能类似。

例如,如果当前笛卡尔拓扑定义了一个 2 × 3 × 4 的网格,当 remain_dims = [True, False, True] 时会创建 3 个子笛卡尔拓扑通信子,每一个包含 8 个进程构成一个 2 × 4 网格。当 remain_dims = [False, False, True] 时会创建 6 个子笛卡尔拓扑通信子,每一个包含 4 个进程构成一个一维网格。

属性

coords

笛卡尔拓扑坐标。

dim

笛卡尔拓扑维数。

dims

笛卡尔拓扑各维的进程数。

ndim

笛卡尔拓扑维数。

periods

笛卡尔拓扑各维的周期性。

topo

拓扑信息。

图拓扑通信子(Graphcomm)

方法

继承自拓扑通信子(Topocomm),其特有的一些方法有:

Get_topo(self)

获取当前拓扑通信子的详细信息。也可以通过属性 topo 获取。

Get_dims(self)

返回当前图拓扑的节点数和边数。也可以通过属性 dims 获取。

Get_neighbors(self, int rank)

返回进程 rank 的所有邻居节点。

Get_neighbors_count(self, int rank)

返回进程 rank 的邻居节点的个数。

属性

dims

图的节点数和边数。

edges

图的边。

index

图的 index。

nedges

图的边数。

neighbors

所有邻居节点。

nneighbors

邻居节点的数目。

nnodes

节点数。

topo

拓扑信息。

分布式图拓扑(Distgraphcomm)

继承自拓扑通信子(Topocomm),其特有的一些方法有:

方法

Get_dist_neighbors(self)

获取分布式图拓扑的邻居节点。

Get_dist_neighbors_count(self)

获取分布式图拓扑的邻居节点数目。

例程

下面给出部分进程拓扑操作相关方法的使用例程。

# topo.py """ Demonstrates the usage of Create_cart, Get_coords, Get_cart_rank, Shift, Sub. Run this with 6 processes like: $ mpiexec -n 6 python topo.py """ import numpy as np from mpi4py import MPI comm = MPI.COMM_WORLD rank = comm.Get_rank() # create a 3 x 2 Cartesian topocomm # period = True period = True # | (4) | (5) | # ------+-----------+-----------+-------- # (-2) | 0,0 (0) | 0,1 (1) | (-2) period = False # ------+-----------+-----------+-------- # (-2) | 1,0 (2) | 1,1 (3) | (-2) period = False # ------+-----------+-----------+-------- # (-2) | 2,0 (4) | 2,1 (5) | (-2) period = False # ------+-----------+-----------+-------- # | (0) | (1) | dims = [3, 2] periods = [True, False] cart_comm = comm.Create_cart(dims, periods) print 'rank %d has topo:' % rank, cart_comm.topo print 'rank %d has coords:' % rank, cart_comm.coords print 'rank %d has dims:' % rank, cart_comm.dims print 'rank %d has periods:' % rank, cart_comm.periods print 'rank 3 has coords:', cart_comm.Get_coords(3) print 'coords [1, 1] is rank:', cart_comm.Get_cart_rank([1, 1]) # shift sd = cart_comm.Shift(0, 1) print 'shift 1 for row: rank %d has (source, dest) = (%d, %d)' % (rank, sd[0], sd[1]) sd = cart_comm.Shift(1, 1) print 'shift 1 for column: rank %d has (source, dest) = (%d, %d)' % (rank, sd[0], sd[1]) print 'MPI.PROC_NULL =', MPI.PROC_NULL # sub remain_dims = [True, False] sub_comm = cart_comm.Sub(remain_dims) # sub_comm1 sub_comm2 # 0 <-> 0 | 1 <-> 0 # 2 <-> 1 | 3 <-> 1 # 4 <-> 2 | 5 <-> 2 print 'rank %d has topo (sub_comm):' % rank, sub_comm.topo

运行结果如下:

$ mpiexec -n 6 python topo.py rank 0 has topo: ([3, 2], [1, 0], [0, 0]) rank 0 has coords: [0, 0] rank 0 has dims: [3, 2] rank 0 has periods: [1, 0] rank 3 has coords: array('i', [1, 1]) coords [1, 1] is rank: 3 shift 1 for row: rank 0 has (source, dest) = (4, 2) shift 1 for column: rank 0 has (source, dest) = (-2, 1) MPI.PROC_NULL = -2 rank 0 has topo (sub_comm): ([3], [1], [0]) rank 1 has topo: ([3, 2], [1, 0], [0, 1]) rank 1 has coords: [0, 1] rank 1 has dims: [3, 2] rank 1 has periods: [1, 0] rank 3 has coords: array('i', [1, 1]) coords [1, 1] is rank: 3 shift 1 for row: rank 1 has (source, dest) = (5, 3) shift 1 for column: rank 1 has (source, dest) = (0, -2) MPI.PROC_NULL = -2 rank 1 has topo (sub_comm): ([3], [1], [0]) rank 2 has topo: ([3, 2], [1, 0], [1, 0]) rank 2 has coords: [1, 0] rank 2 has dims: [3, 2] rank 2 has periods: [1, 0] rank 3 has coords: array('i', [1, 1]) coords [1, 1] is rank: 3 shift 1 for row: rank 2 has (source, dest) = (0, 4) shift 1 for column: rank 2 has (source, dest) = (-2, 3) MPI.PROC_NULL = -2 rank 2 has topo (sub_comm): ([3], [1], [1]) rank 3 has topo: ([3, 2], [1, 0], [1, 1]) rank 3 has coords: [1, 1] rank 3 has dims: [3, 2] rank 3 has periods: [1, 0] rank 3 has coords: array('i', [1, 1]) coords [1, 1] is rank: 3 shift 1 for row: rank 3 has (source, dest) = (1, 5) shift 1 for column: rank 3 has (source, dest) = (2, -2) MPI.PROC_NULL = -2 rank 3 has topo (sub_comm): ([3], [1], [1]) rank 4 has topo: ([3, 2], [1, 0], [2, 0]) rank 4 has coords: [2, 0] rank 4 has dims: [3, 2] rank 4 has periods: [1, 0] rank 3 has coords: array('i', [1, 1]) coords [1, 1] is rank: 3 shift 1 for row: rank 4 has (source, dest) = (2, 0) shift 1 for column: rank 4 has (source, dest) = (-2, 5) MPI.PROC_NULL = -2 rank 4 has topo (sub_comm): ([3], [1], [2]) rank 5 has topo: ([3, 2], [1, 0], [2, 1]) rank 5 has coords: [2, 1] rank 5 has dims: [3, 2] rank 5 has periods: [1, 0] rank 3 has coords: array('i', [1, 1]) coords [1, 1] is rank: 3 shift 1 for row: rank 5 has (source, dest) = (3, 1) shift 1 for column: rank 5 has (source, dest) = (4, -2) MPI.PROC_NULL = -2 rank 5 has topo (sub_comm): ([3], [1], [2])

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

转载请注明原文地址: https://www.6miu.com/read-2649956.html

最新回复(0)