4

有没有办法在 Cython 中为 cimport 提供相当于 Python try 语句的方法?

像这样的东西:

try:
    cimport something
except ImportError:
    pass

我需要这个来编写一个可以使用或不使用 mpi4py 编译的 Cython 扩展。这在编译语言中是非常标准的,其中 mpi 命令可以放在 #ifdef 和 #endif 预处理器指令之间。我们如何在 Cython 中获得相同的结果?

我试过这个但它不起作用:

try:
    from mpi4py import MPI
    from mpi4py cimport MPI
    from mpi4py.mpi_c cimport *
except ImportError:
    rank = 0
    nb_proc = 1

# solve a incompatibility between openmpi and mpi4py versions
cdef extern from 'mpi-compat.h': pass

does_it_work = 'Not yet'

实际上,如果 mpi4py 安装正确,但如果 import mpi4py引发 ImportError,Cython 文件不会编译,我得到错误:

Error compiling Cython file:
------------------------------------------------------------
...

try:
    from mpi4py import MPI
    from mpi4py cimport MPI
   ^
------------------------------------------------------------

mod.pyx:4:4: 'mpi4py.pxd' not found

文件setup.py

from setuptools import setup, Extension
from Cython.Distutils import build_ext

import os
here = os.path.abspath(os.path.dirname(__file__))

include_dirs = [here]

try:
    import mpi4py
except ImportError:
    pass
else:
    INCLUDE_MPI = '/usr/lib/openmpi/include'
    include_dirs.extend([
        INCLUDE_MPI,
        mpi4py.get_include()])

name = 'mod'
ext = Extension(
    name,
    include_dirs=include_dirs,
    sources=['mod.pyx'])

setup(name=name,
      cmdclass={"build_ext": build_ext},
      ext_modules=[ext])
4

2 回答 2

4

以这种方式使用 try-catch 块是您无法做到的。您正在制作的扩展模块必须静态编译并链接到它用于cimport在 C 级别加载的内容。try-catch 块是在导入模块时执行的,而不是在编译时执行的。

另一方面,理论上,您应该能够使用 Cython对条件编译的支持获得您正在寻找的效果。在您的setup.py文件中,您可以检查是否定义了所需的模块,然后定义要传递给 Cython 编译器的环境变量,这又取决于所需的模块是否存在。

在Cython 的一项测试中有一个如何执行此操作的示例。在那里,他们将包含所需环境变量的字典Extension作为关键字参数传递给 Cython 类的构造函数pyrex_compile_time_env,该参数已重命名cython_compile_time_env,并且Cython.Build.Dependencies.cythonize称为compile_time_env)。

于 2014-10-07T00:19:12.613 回答
2

感谢您非常有用的回答@IanH。我举了一个例子来说明它给出了什么。

文件setup.py

from setuptools import setup
from Cython.Distutils.extension import Extension
from Cython.Distutils import build_ext

import os
here = os.path.abspath(os.path.dirname(__file__))

import numpy as np
include_dirs = [here, np.get_include()]

try:
    import mpi4py
except ImportError:
    MPI4PY = False
else:
    MPI4PY = True
    INCLUDE_MPI = '/usr/lib/openmpi/include'
    include_dirs.extend([
        INCLUDE_MPI,
        mpi4py.get_include()])

name = 'mod'
ext = Extension(
    name,
    include_dirs=include_dirs,
    cython_compile_time_env={'MPI4PY': MPI4PY},
    sources=['mod.pyx'])

setup(name=name,
      cmdclass={"build_ext": build_ext},
      ext_modules=[ext])

if not MPI4PY:
    print('Warning: since importing mpi4py raises an ImportError,\n'
          '         the extensions are compiled without mpi and \n'
          '         will work only in sequencial.')

还有文件mod.pyx,带有一些真实的mpi命令:

import numpy as np
cimport numpy as np

try:
    from mpi4py import MPI
except ImportError:
    nb_proc = 1
    rank = 0
else:
    comm = MPI.COMM_WORLD
    nb_proc = comm.size
    rank = comm.Get_rank()

IF MPI4PY:
    from mpi4py cimport MPI
    from mpi4py.mpi_c cimport *

    # solve an incompatibility between openmpi and mpi4py versions
    cdef extern from 'mpi-compat.h': pass

    print('mpi4py ok')
ELSE:
    print('no mpi4py')

n = 8
if n % nb_proc != 0:
    raise ValueError('The number of processes is incorrect.')

if rank == 0:
    data_seq = np.ones([n], dtype=np.int32)
    s_seq = data_seq.sum()
else:
    data_seq = np.zeros([n], dtype=np.int32)

if nb_proc > 1:
    data_local = np.zeros([n/nb_proc], dtype=np.int32)
    comm.Scatter(data_seq, data_local, root=0)
else:
    data_local = data_seq

s = data_local.sum()
if nb_proc > 1:
    s = comm.allreduce(s, op=MPI.SUM)

if rank == 0:
    print('s: {}; s_seq: {}'.format(s, s_seq))
    assert s == s_seq

使用and构建python setup.py build_ext --inplace和测试。如果没有安装,仍然可以构建模块并按顺序使用。python -c "import mod"mpirun -np 4 python -c "import mod"mpi4py

于 2014-10-07T11:16:40.380 回答