在 cython 中使用 mpi4py

在上一篇中我们介绍了 mpi4py 中的 run 模块,下面我们将介绍在 cython 中使用 mpi4py。

cython 简介

Cython 是 Python 编程语言的超集,旨在通过主要使用 Python 编写的代码提供类似 C 的性能。Cython 是一种生成 CPython 扩展模块的编译语言,用 Cython 编写 Python 的扩展模块与直接写 Python 程序差不多一样容易。然后,可以使用 import 语句通过常规 Python 代码加载和使用这些扩展模块。几乎所有 Python 代码都是合法的 Cython 代码。

使用 Cython 一般只需在对应的 Python 代码中可选地添加一些静态类型声明,然后就可以使用 Cython 编译器将其转换成优化的 C/C++ 代码,然后被 C 编译器(如 gcc 等)编译成 Python 的扩展模块。这些扩展模块可以在 Python 中直接导入使用。

读者可以参考其文档以了解更多关于 Cython 的内容及其使用方法。

在 cython 中使用 mpi4py

在前面已经提到过,mpi4py 的主体部分是使用 Cython 编写的,因此在 Cython 中使用 mpi4py 也是非常自然和非常容易的。根据所编写的代码更接近 Python 还是更接近 C,在 Cython 中使用 mpi4py 可以在 3 个级别上进行。

Python 级别 import

因为 Python 代码(在绝大多数情况下)也是合法的 cython 代码,因此直接将一段使用 mpi4py 的 Python 代码放入一个 *.pyx 文件中,就成了相应的 Cython 代码。在这个级别上,编译成的扩展模块链接使用的是 mpi4py/MPI.so。下面是简单的例子:

# Python-level module import
# (file: mpi4py/MPI.so)

from mpi4py import MPI

# Python-level objects and code

size  = MPI.COMM_WORLD.Get_size()
rank  = MPI.COMM_WORLD.Get_rank()
pname = MPI.Get_processor_name()

hwmess = "Hello, World! I am process %d of %d on %s."
print (hwmess % (rank, size, pname))

Cython 级别 cimport

在这个级别上必须从 mpi4py 包中 cimport MPI 模块,然后可以使用和添加一些 mpi4py 中的 Python 扩展类型(在 mpi4py/include/mpi4py/MPI.pxd 中定义),下面是简单的例子:

# Cython-level cimport
# this make available mpi4py's Python extension types
# (file:  mpi4py/include/mpi4py/MPI.pxd)

from mpi4py cimport MPI
from mpi4py.MPI cimport Intracomm as IntracommType

# C-level cdef, typed, Python objects

cdef MPI.Comm WORLD = MPI.COMM_WORLD
cdef IntracommType SELF = MPI.COMM_SELF

C 级别

通过从 mpi4py 包中 cimport libmpi,使我们可以在 Cython 代码中直接调用 MPI 库的 C 语言函数接口(这些函数接口在 mpi4py/include/mpi4py/libmpi.pxd 中定义)。下面是简单的例子:

# Cython-level cimport with PXD file
# this make available the native MPI C API
# with namespace-protection (stuff accessed as mpi.XXX)
# (file: mpi4py/include/mpi4py/libmpi.pxd)

from mpi4py cimport libmpi as mpi

cdef int ierr1=0

cdef int size1 = 0
ierr1 = mpi.MPI_Comm_size(mpi.MPI_COMM_WORLD, &size1)

cdef int rank1 = 0
ierr1 = mpi.MPI_Comm_rank(mpi.MPI_COMM_WORLD, &rank1)

cdef int rlen1=0
cdef char pname1[mpi.MPI_MAX_PROCESSOR_NAME]
ierr1 = mpi.MPI_Get_processor_name(pname1, &rlen1)
pname1[rlen1] = 0 # just in case ;-)

hwmess = "Hello, World! I am process %d of %d on %s."
print (hwmess % (rank1, size1, pname1))

传递 Python 通信子到 C

为了在 cython 中使用 mpi4py,我们经常需要将 mpi4py 的 MPI 通信子传递到 cython 中的 C 级别代码,直接传递一个 mpi4py.MPI.Comm 对象是不行的,不过 mpi4py.MPI.Comm 在内部存储了一个 MPI_Comm 类型的句柄 ob_mpi (实际上,mpi4py.MPI 中的大部分对象,如 Groupe,Status,File 等,都有一个对应的 C 级别 MPI 句柄,且名字都为 ob_mpi,可以像通信子一样由 Python 传递到 C),因此只需将 mpi4py 通信子的 ob_mpi 属性传递到 C 级别代码就行了。参见下面的例程。

传递 C 通信子到 Python

有时我们也需要将一个 C 类型的 MPI 通信子从 cython 代码中传回 Python 变成一个 mpi4py.MPI.Comm 对象,为此可以先初始化一个 mpi4py.MPI.Comm 对象,将此 C 类型的 MPI 通信子赋值给该 mpi4py.MPI.Comm 对象的 ob_mpi 属性,然后将此对象传回 Python 就可以了。

例程

下面给出相应的例程。

首先是 cython 代码。

# hello.pyx

# python level
from mpi4py import MPI

def say_hello(comm):
    rank = comm.rank
    print 'say_hello: Hello from rank = %d' % rank


# ----------------------------------------------------------------------------
# cython and C level
from mpi4py cimport MPI
from mpi4py cimport libmpi as mpi

def c_say_hello(MPI.Comm comm):
    # get the C handle of MPI.Comm
    cdef mpi.MPI_Comm c_comm = comm.ob_mpi
    cdef int ierr=0
    cdef int rank = 0
    # call MPI C API to get rank
    ierr = mpi.MPI_Comm_rank(c_comm, &rank)
    print 'c_say_hello: Hello from rank = %d' % rank

def return_comm(MPI.Comm comm):
    # get the C handle of MPI.Comm
    cdef mpi.MPI_Comm c_comm = comm.ob_mpi
    cdef mpi.MPI_Group c_group
    # call MPI C API to get the associated group of c_comm
    mpi.MPI_Comm_group(c_comm, &c_group)
    cdef int n = 1
    cdef int *ranks = [0]
    cdef mpi.MPI_Group c_new_group
    # call MPI C API to create a new group by excluding rank 0
    mpi.MPI_Group_excl(c_group, n, ranks, &c_new_group)
    cdef mpi.MPI_Comm c_new_comm
    # call MPI C API to create a new communicator by excluding rank 0
    mpi.MPI_Comm_create(c_comm, c_new_group, &c_new_comm)

    cdef MPI.Comm py_comm
    if comm.rank == 0:
        return None
    else:
        # initialize a MPI.Comm object
        py_comm = MPI.Comm()
        # assign the new communicator to py_comm.ob_mpi
        py_comm.ob_mpi = c_new_comm

        return py_comm

使用下面的脚本将其编译成可在 Python 中导入的扩展模块。

# setup.py

import os
# import mpi4py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

mpi_compile_args = os.popen("mpicc --showme:compile").read().strip().split(' ')
mpi_link_args    = os.popen("mpicc --showme:link").read().strip().split(' ')

ext_modules = [
    Extension(
        "hello",
        ["hello.pyx"],
        # include_dirs = [mpi4py.get_include()],
        extra_compile_args = mpi_compile_args,
        extra_link_args    = mpi_link_args,
    )
]

setup(
    name='hello-parallel-world',
    cmdclass = {"build_ext": build_ext},
    ext_modules = ext_modules
)

编译扩展模块的命令如下:

$ python setup.py build_ext --inplace

然后是 Python 测试程序。

# cython_mpi.py

"""
Demonstrates how to use mpi4py in cython.

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

from mpi4py import MPI
import hello


comm = MPI.COMM_WORLD


hello.say_hello(comm)
hello.c_say_hello(comm)

new_comm = hello.return_comm(comm)

if not new_comm is None:
    print 'new_comm.size = %d' % new_comm.size

执行的结果如下:

$ mpiexec -n 4 python cython_mpi.py
say_hello: Hello from rank = 0
c_say_hello: Hello from rank = 0
say_hello: Hello from rank = 1
c_say_hello: Hello from rank = 1
new_comm.size = 3
say_hello: Hello from rank = 2
c_say_hello: Hello from rank = 2
new_comm.size = 3
say_hello: Hello from rank = 3
c_say_hello: Hello from rank = 3
new_comm.size = 3

以上介绍了在 cython 中使用 mpi4py 的方法,在下一篇中我们将介绍 mpi4py 与 OpenMP 混合编程。

你可能感兴趣的:(在 cython 中使用 mpi4py)