【发布时间】:2013-01-29 16:02:52
【问题描述】:
我正在编写一个包含 Cython 扩展并使用 LAPACK(和 BLAS)的 Python 模块。如有必要,我愿意使用clapack 或lapacke,或某种f2c 或f2py 解决方案。重要的是我能够在没有 Python 调用开销的情况下从 Cython 调用 lapack 和 blas 例程。
我找到了一个例子here。但是,该示例取决于 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)
【问题讨论】:
标签: python numpy cython lapack blas