# mpi4py 中的进程拓扑方法

xiaoxiao2021-02-28  2

# 创建方法

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

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

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)

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

# 笛卡尔拓扑通信子（Cartcomm）

## 方法

Get_topo(self)

Get_cart_rank(self, coords)

Get_coords(self, int rank)

Get_dim(self)

Shift(self, int direction, int disp)

Sub(self, remain_dims)

coords

dim

dims

ndim

periods

topo

# 图拓扑通信子（Graphcomm）

## 方法

Get_topo(self)

Get_dims(self)

Get_neighbors(self, int rank)

Get_neighbors_count(self, int rank)

dims

edges

index

nedges

neighbors

nneighbors

nnodes

topo

# 分布式图拓扑（Distgraphcomm）

## 方法

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])