在 cython 中使用 mpi4py

上一篇中我們介紹了 mpi4py 中的 run 模塊,下面我們將介紹在 cython 中使用 mpi4py。

cython 簡介

Cython 是 Python 編程語言的超集,旨在通過主要使用 Python 編寫的代碼提供類似 C 的性能。Cython 是一種生成 CPython 擴展模塊的編譯語言,用 Cython 編寫 Python 的擴展模塊與直接寫 Python 程序差不多一樣容易。然后,可以使用 import 語句通過常規(guī) Python 代碼加載和使用這些擴展模塊。幾乎所有 Python 代碼都是合法的 Cython 代碼。

使用 Cython 一般只需在對應(yīng)的 Python 代碼中可選地添加一些靜態(tài)類型聲明,然后就可以使用 Cython 編譯器將其轉(zhuǎn)換成優(yōu)化的 C/C++ 代碼,然后被 C 編譯器(如 gcc 等)編譯成 Python 的擴展模塊。這些擴展模塊可以在 Python 中直接導(dǎo)入使用。

讀者可以參考其文檔以了解更多關(guān)于 Cython 的內(nèi)容及其使用方法。

在 cython 中使用 mpi4py

在前面已經(jīng)提到過,mpi4py 的主體部分是使用 Cython 編寫的,因此在 Cython 中使用 mpi4py 也是非常自然和非常容易的。根據(jù)所編寫的代碼更接近 Python 還是更接近 C,在 Cython 中使用 mpi4py 可以在 3 個級別上進行。

Python 級別 import

因為 Python 代碼(在絕大多數(shù)情況下)也是合法的 cython 代碼,因此直接將一段使用 mpi4py 的 Python 代碼放入一個 *.pyx 文件中,就成了相應(yīng)的 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 代碼中直接調(diào)用 MPI 庫的 C 語言函數(shù)接口(這些函數(shù)接口在 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,我們經(jīng)常需要將 mpi4py 的 MPI 通信子傳遞到 cython 中的 C 級別代碼,直接傳遞一個 mpi4py.MPI.Comm 對象是不行的,不過 mpi4py.MPI.Comm 在內(nèi)部存儲了一個 MPI_Comm 類型的句柄 ob_mpi (實際上,mpi4py.MPI 中的大部分對象,如 Groupe,Status,F(xiàn)ile 等,都有一個對應(yīng)的 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 就可以了。

例程

下面給出相應(yīng)的例程。

首先是 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 中導(dǎo)入的擴展模塊。

# 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

執(zhí)行的結(jié)果如下:

$ 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 混合編程。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,789評論 25 709
  • 用兩張圖告訴你,為什么你的 App 會卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 13,916評論 2 59
  • 在上一篇中我們介紹了 mpi4py 中的 profiling,下面我們將介紹 mpi4py 中的 futures ...
    自可樂閱讀 1,481評論 0 0
  • 我對自己無能為力 不要管我是否言不由衷 別流連在歲月中 人生已經(jīng)太匆匆,我好害怕總是淚眼朦朧 《當(dāng)愛已成往事》
    不愛吃飯的飯桶桶閱讀 140評論 0 0
  • 本來是打算清理硬盤的時候發(fā)現(xiàn)的電影,已經(jīng)忘記了 它在我的電腦盤里存儲了多久。只是覺得反正下載下來了,那就把它看了吧...
    鄧小怪閱讀 472評論 0 0

友情鏈接更多精彩內(nèi)容