mpi4py 中的数据类型创建方法

在上一篇中我们介绍了数据类型图,下面我们将介绍 mpi4py 中的数据类型创建方法,用以创建用户自定义数据类型。

连续类型

把已有数据类型进行若干次复制,形成新的数据类型。其方法(MPI.Datatype 类方法)接口为:

Create_contiguous(self, int count)

将原数据类型 oldtype 复制 count 份形成新数据类型 newtype 并返回。

向量类型

较上面的 Create_contiguous 更通用一些,可在特定位置复制数据类型,形成占用空间相等的数据块,块之间的距离为原数据类型的整数倍,允许复制的数据之间有空隙。其方法(MPI.Datatype 类方法)接口为:

Create_vector(self, int count, int blocklength, int stride)
Create_hvector(self, int count, int blocklength, Aint stride)

将原数据类型 oldtype 复制 count 块,每块包含原数据类型 blocklength 个,块与块之间的间距为 stride。Create_vector 与 Create_hvector 的功能类似,不同的是前者的 stride 以元素个数为单位,而后者的 stride 以字节为单位。返回新生成的数据类型。

索引类型

索引数据类型比上面的向量类型更加灵活,可以分别指定每个块中的数据量以及每个块的起始位置。将已有数据类型复制成块系列,已有数据类型连接后形成各块,每块可包含不同数量数据,以及不同的偏移。其方法(MPI.Datatype 类方法)接口为:

Create_indexed(self, blocklengths, displacements)
Create_hindexed(self, blocklengths, displacements)

其中的参数 blocklengthsdisplacements 都为长度为 count 的整数序列。将原数据类型 oldtype 复制 count 块,每块的元素个数由 blocklengths 指定,每块的偏移量由displacements 指定。Create_indexed 与 Create_hindexed 的功能类似,不同的是前者的 displacementsoldtype 的跨度为单位计算,而后者的 displacements 以字节为单位计算。返回新生成的数据类型。

Create_indexed_block(self, int blocklength, displacements)
Create_hindexed_block(self, int blocklength, displacements)

其中的参数 displacements 为长度为 count 的整数序列。这两个方法是与 Create_indexed 和 Create_hindexed 对应的,所不同的是它们使用恒定的块大小参数 blocklength,即每一块的元素个数都相同。返回新生成的数据类型。

结构类型

可以生成结构类型。其方法(MPI.Datatype 类方法)接口为:

Create_struct(type cls, blocklengths, displacements, datatypes)

其中的参数 blocklengthsdisplacements 都为长度为 count 的整数序列,而 datatypes 为长度为 count 的数据类型序列。count 指明数据块数,blocklengths 指明每块的元素个数,displacements 是每块的偏移量数组,以字节为单位计算,datatypes 是原有的数据类型数组。返回新生成的数据类型。

子数组类型

创建一个用来描述 n 维数组的一个 n 维子数组的数据类型,也可以用来创建文件类型以便通过一个单一文件来访问分块分布于不同进程的全局数组元素。其方法(MPI.Datatype 类方法)接口为:

Create_subarray(self, sizes, subsizes, starts, int order=ORDER_C)

参数 sizessubsizesstarts 都为长度为数组维数 ndims 的序列,sizes 指明原数组中各维上的元素数(也即原数组的 shape),subsizes 指明子数组各维的大小(也即子数组的 shape),starts 指明子数组在原数组各维的起始位置。order 指明数组的排列方式,默认是 MPI.ORDER_C,即 C 数组的排列方式,也可以设置成 MPI.ORDER_F,即 Fortran 中的数组排列方式。返回新生成的数据类型。此方法对发送和接收多维 numpy 数组的子数组非常有用。

分布式数组类型

创建一个描述 n 维数组分布在 n 维进程网格上的数据类型,用于创建分布式数据结构,共享分布式文件等。其方法(MPI.Datatype 类方法)接口为:

Create_darray(self, int size, int rank, gsizes, distribs, dargs, psizes, int order=ORDER_C)

参数 sizerank 为组内的进程数和进程号,gsizesdistribsdargspsizes 都是长度为数组维数 ndims 的序列,gsizes 指明全局数组各维上的元素个数(也即全局数组的 shape),distribs 指明各维的分布方式,dargs 指明各维的分布参数,psizes 指明进程网格在各维的大小,order 指明数组的排列方式,默认是 MPI.ORDER_C,即 C 数组的排列方式。返回新生成的数据类型。

数组的各维可按如下 3 种方式分布:

  • 块分布方式: MPI.DISTRIBUTE_BLOCK;
  • 循环分布方式: MPI.DISTRIBUTE_CYCLIC;
  • 不分布: MPI.DISTRIBUTE_NONE。

如果在某一维上不分布,则 psizes 中对应此不分布维的元素须设置成 1,并且应该确保 psizes 中各元素的乘积等于组内的进程数 size

dargs 是一个长度为 ndims 的整数序列,序列中的元素可以使用默认的分布参数 MPI.DISTRIBUTE_DFLT_DARG,而对应不分布维的元素会被忽略掉。

注册与取消

新创建的数据类型必须在注册(提交)之后才可用于通信。注册方法(MPI.Datatype 类方法)接口为:

Commit(self)

可以取消注册的数据类型,并将其设置为 MPI.DATATYPE_NULL。取消方法(MPI.Datatype 类方法)接口为:

Free(self)

例程

下面给出以上介绍的部分方法的使用例程。

# data_type.py

"""
Demonstrates the usage of Create_contiguous, Create_vector, Create_hvector,
Create_indexed, Create_hindexed, Create_struct, Create_subarray, Commit, Free.

Run this with 4 processes like:
$ mpiexec -n 4 python data_type.py
"""

import os
import shutil
import numpy as np
from mpi4py import MPI


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

# ------------------------------------------------------------------------------
# Create_contiguous
# create a contiguous data type by copying MPI.INT 4 times
INT4 = MPI.INT.Create_contiguous(4)
print 'INT4:', INT4.lb, INT4.ub, INT4.size, INT4.extent

# in the following, B: one byte empty space

# ------------------------------------------------------------------------------
# Create_vector, Create_hvector
# create a vector data type from MPI.FLOAT with count = 2, blocklength = 3, stride = 4
# FLOAT_vector: [FLOAT FLOAT FLOAT] B B B B [FLOAT FLOAT FLOAT]
# blocklength:           3                           3
# stride:        0                           4
FLOAT_vector = MPI.FLOAT.Create_vector(2, 3, 4)
print 'FLOAT_vector:', FLOAT_vector.lb, FLOAT_vector.ub, FLOAT_vector.size, FLOAT_vector.extent
# FLOAT_hvector = MPI.FLOAT.Create_hvector(2, 3, 4*4)
# print 'FLOAT_hvector:', FLOAT_hvector.lb, FLOAT_hvector.ub, FLOAT_hvector.size, FLOAT_hvector.extent

# ------------------------------------------------------------------------------
# Create_indexed, Create_hindexed
# create a indexed data type from MPI.INT with blocklengths = [2, 3], displacements = [0, 4]
# INT_indexed:   [INT INT] B B B B B B B B [INT INT INT]
# blocklengths:      2                           3
# displacements:  0                         4
INT_indexed = MPI.INT.Create_indexed([2, 3], [0, 4])
print 'INT_indexed:', INT_indexed.lb, INT_indexed.ub, INT_indexed.size, INT_indexed.extent
# INT_hindexed = MPI.INT.Create_hindexed([2, 3], [0, 4*4])
# print 'INT_hindexed:', INT_hindexed.lb, INT_hindexed.ub, INT_hindexed.size, INT_hindexed.extent

# ------------------------------------------------------------------------------
# Create_struct
# create a struct data type with blocklengths = [2, 3], displacements = [0, 6], datatypes = [MPI.CHAR, MPI.INT]
# TYPE_struct:   [CHAR CHAR] B B B B [INT INT INT]
# blocklengths:       2                    3
# displacements:  0                   6
# datatypes:        CHAR                  INT
TYPE_struct = MPI.Datatype.Create_struct([2, 3], [0, 6], [MPI.CHAR, MPI.INT])
print 'TYPE_struct:', TYPE_struct.lb, TYPE_struct.ub, TYPE_struct.size, TYPE_struct.extent, TYPE_struct.true_lb, TYPE_struct.true_ub, TYPE_struct.true_extent

# ------------------------------------------------------------------------------
# Create_subarray
sizes = (2, 8) # a 2-dimensional array with 2 rows and 8 columns
if rank == 0:
    # the first 3 columns
    subsizes = (2, 3)
    starts = (0, 0)
elif rank == 1:
    # the nest 2 columns
    subsizes = (2, 2)
    starts = (0, 3)
elif rank == 2:
    # the nest 1 column
    subsizes = (2, 1)
    starts = (0, 5)
elif rank == 3:
    # the last 2 columns
    subsizes = (2, 2)
    starts = (0, 6)
INT_subarray = MPI.INT.Create_subarray(sizes, subsizes, starts)
print 'INT_subarray:', INT_subarray.lb, INT_subarray.ub, INT_subarray.size, INT_subarray.extent

# ------------------------------------------------------------------------------
# gather subarrays from all processes by using the defined INT_subarray
if rank == 0:
    full_array = np.empty((2, 8), dtype='i')
    subarray = np.array([[0, 1, 2], [8, 9, 10]], dtype='i')
elif rank == 1:
    full_array = None
    subarray = np.array([[3, 4], [11, 12]], dtype='i')
elif rank == 2:
    full_array = None
    subarray = np.array([[5], [13]], dtype='i')
elif rank == 3:
    full_array = None
    subarray = np.array([[6, 7], [14, 15]], dtype='i')

subsizes = comm.gather(subsizes, root=0)
starts = comm.gather(starts, root=0)
# each process sends subarray to rank 0
print 'rank %d sends:' % rank, subarray
sreq = comm.Isend(subarray, dest=0, tag=0)
# rank 0 receives subarray from each process and put them into full_array
if rank == 0:
    # create new data type INT_subarray and commit them
    subtypes = [ MPI.INT.Create_subarray(sizes, subsize, start).Commit() for (subsize, start) in zip(subsizes, starts) ]
    for i in range(size):
        comm.Recv([full_array, subtypes[i]], source=i, tag=0)
    # free the new data type INT_subarray
    [ subtype.Free() for subtype in subtypes ]

# wait for complete
sreq.Wait()
if rank == 0:
    print 'rank 0 receives:', full_array

运行结果如下:

$ mpiexec -n 4 python data_type.py
INT4: 0 16 16 16
FLOAT_vector: 0 28 24 28
INT_indexed: 0 28 20 28
TYPE_struct: 0 20 14 20 0 18 18
INT_subarray: 0 64 16 64    
INT4: 0 16 16 16
FLOAT_vector: 0 28 24 28
INT_indexed: 0 28 20 28
TYPE_struct: 0 20 14 20 0 18 18
INT_subarray: 0 64 24 64
INT4: 0 16 16 16
FLOAT_vector: 0 28 24 28
INT_indexed: 0 28 20 28
TYPE_struct: 0 20 14 20 0 18 18
INT_subarray: 0 64 16 64
INT4: 0 16 16 16
FLOAT_vector: 0 28 24 28
INT_indexed: 0 28 20 28
TYPE_struct: 0 20 14 20 0 18 18
INT_subarray: 0 64 8 64
rank 3 sends: [[ 6  7]
[14 15]]
rank 0 sends: [[ 0  1  2]
[ 8  9 10]]
rank 1 sends: [[ 3  4]
[11 12]]
rank 2 sends: [[ 5]
[13]]
rank 0 receives: [[ 0  1  2  3  4  5  6  7]
[ 8  9 10 11 12 13 14 15]]

以上我们介绍了 mpi4py 中的数据类型创建方法,在下一篇中我们将介绍数据的打包/解包操作。

你可能感兴趣的:(mpi4py 中的数据类型创建方法)