在上一篇中我們介紹了 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 混合編程。