我正在编写一个Python模块,其中包括Cython扩展,并使用LAPACK和BLAS。 我可以使用clapack或lapacke,或者如果必要,使用f2c或f2py解决方案。 重要的是我能够在紧密循环中从Cython调用lapack和blas例程而没有Python调用开销。
我发现了一个示例,链接在这里。 但是,该示例依赖于SAGE。 我希望我的模块可安装而无需安装SAGE,因为我的用户不太可能需要或想要安装SAGE以外的任何内容。 我的用户可能已经安装了像numpy、scipy、pandas和scikit-learn等软件包,因此它们将是合理的依赖项。 使用哪些界面的组合最佳,并且什么样的setup.py文件最小化可以获取(从numpy、scipy等)编译所需的信息?
编辑:以下是我最终采取的方法。 它在我的macbook上运行良好,但我不知道它有多少可移植性。 肯定有更好的方法。
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy
from Cython.Build import cythonize
from numpy.distutils.system_info import get_info
# TODO: This cannot be the right way
blas_include = get_info('blas_opt')['extra_compile_args'][1][2:]
includes = [blas_include,numpy.get_include()]
setup(
cmdclass = {'build_ext': build_ext},
ext_modules = cythonize([Extension("cylapack", ["cylapack.pyx"],
include_dirs = includes,
libraries=['blas','lapack'])
])
)
这是因为在我的 MacBook 上,clapack.h
头文件与 cblas.h
处于同一目录下。然后我可以在我的 pyx 文件中这样做:
ctypedef np.int32_t integer
cdef extern from "cblas.h":
double cblas_dnrm2(int N,double *X, int incX)
cdef extern from "clapack.h":
integer dgelsy_(integer *m, integer *n, integer *nrhs,
double *a, integer *lda, double *b, integer *ldb, integer *
jpvt, double *rcond, integer *rank, double *work, integer *
lwork, integer *info)
setup.py
文件中进行任何特殊操作。 - IanH