cython memoryview不比ndarray快

时间:2015-10-05 16:33:09

标签: numpy cython memoryview

我有一个用常规numpy ndarray编写的函数和另一个带有typed memoryview的函数。但是,我无法使memoryview版本比常规版本更快地工作(与许多博客不同,例如memoryview benchmarks)。

任何提高memoryview代码速度的指针/建议都会非常感激! ...或者...如果有人能指出任何明显的原因,为什么memoryview版本不比常规的numpy版本快得多

在下面的代码中有两个函数,它们都包含两个向量bixi并返回一个矩阵。第一个函数shrink_correl是常规numpy版本,第二个函数shrink_correl2是memoryview替代(让文件为sh_cor.pyx)。

# cython: boundscheck=False
# cython: wraparound=False
# cython: cdivision=True

cimport cython
cimport numpy as np
import numpy as np
from numpy cimport ndarray as ar

# -- ***this is the Regular Cython version*** -
cpdef ar[double, ndim=2, mode='c'] shrink_correl(ar[double, ndim=1, mode='c'] bi, ar[double, ndim=1, mode='c'] xi):
    cdef:
        int n_ = xi.shape[0]
        int n__ = int(n_*(n_-1)/2)
        ar[double, ndim=2, mode='c'] f = np.zeros([n__, n_+1])
        int x__ = 0
        ar[double, ndim=2, mode='c'] f1 = np.zeros([n_, n_+1])
        ar[double, ndim=2, mode='c'] f2 = np.zeros([n__, n_+1])
        ar[double, ndim=1, mode='c'] g = np.zeros(n_+1)
        ar[double, ndim=1, mode='c'] s = np.zeros(n__)
        ar[double, ndim=2, mode='c'] cori_ = np.zeros([n_, n_])
        Py_ssize_t j, k

    with nogil:
        for j in range(0, n_-1):
            for k in range(j+1, n_):
                x__ += 1
                f[x__-1, j] = bi[k]*xi[k]*1000
                f[x__-1, k] = bi[j]*xi[j]*1000
    f1 = np.dot(np.transpose(f), f)      
    with nogil:
        for j in range(0, n_):
            f1[n_, j] = xi[j]*1000
            f1[j, n_] = f1[n_, j]
    f2 = np.dot(f, np.linalg.inv(f1))
    with nogil:
        for j in range(0, n_):
            g[j] = -bi[j]*xi[j]*1000

    s = np.dot(f2, g)

    with nogil:
        for j in range(0, n_):
            cori_[j, j] = 1.0
    x__ = 0

    with nogil:
        for j in range(0, n_-1):
            for k in range(j+1, n_):
                x__ += 1
                cori_[j, k] = s[x__-1]
                cori_[k, j] = cori_[j, k]
    return cori_

# -- ***this is the MemoryView Cython version*** -    
cpdef ar[double, ndim=2, mode='c'] shrink_correl2(double[:] bi, double[:] xi):
    cdef:
        int n_ = xi.shape[0]
        int n__ = int(n_*(n_-1)/2)
        double[:, ::1] f = np.zeros([n__, n_+1])
        int x__ = 0
        double[:, ::1] f1 = np.zeros([n_, n_+1])
        double[:, ::1] f2 = np.zeros([n__, n_+1])
        double[:] g = np.zeros(n_+1)
        double[:] s = np.zeros(n__)
        double[:, ::1] cori_ = np.zeros([n_, n_])
        ar[double, ndim=2, mode='c'] cori__ = np.zeros([n_, n_])
        Py_ssize_t j, k
    with nogil:
        for j in range(0, n_-1):
            for k in range(j+1, n_):
                x__ += 1
                f[x__-1, j] = bi[k]*xi[k]*1000
                f[x__-1, k] = bi[j]*xi[j]*1000
    f1 = np.dot(np.transpose(f), f)      
    with nogil:
        for j in range(0, n_):
            f1[n_, j] = xi[j]*1000
            f1[j, n_] = f1[n_, j]
    f2 = np.dot(f, np.linalg.inv(f1))
    with nogil:
        for j in range(0, n_):
            g[j] = -bi[j]*xi[j]*1000

    s = np.dot(f2, g)

    with nogil:
        for j in range(0, n_):
            cori_[j, j] = 1.0
    x__ = 0

    with nogil:
        for j in range(0, n_-1):
            for k in range(j+1, n_):
                x__ += 1
                cori_[j, k] = s[x__-1]
                cori_[k, j] = cori_[j, k]
    cori__[:, :] = cori_
    return cori__

这是使用以下setup.py代码

编译的
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy as np
import os

ext_modules = [Extension('sh_cor', ['sh_cor.pyx'], include_dirs=[np.get_include(),
                                                                 os.path.join(np.get_include(), 'numpy')],
                         define_macros=[('NPY_NO_DEPRECATED_API', None)],
                         extra_compile_args=['-O3', '-march=native', '-ffast-math', '-flto'],
                         libraries=['m']
                         )]

setup(
    name="Sh Cor",
    cmdclass={'build_ext': build_ext},
    ext_modules=ext_modules
)

用于测试速度的代码是

import numpy as np
import sh_cor  # this the library created by the setup.py file
import time

b = np.random.random(400)
b = b/np.sum(b)

x = np.random.random(400)-0.5

n = 10 

t0 = time.time()
for i in range(n):
    v1 = sh_cor.shrink_correl(b, x)
t1 = time.time()
print((t1-t0)/n)

t0 = time.time()
for i in range(n):
    v2 = sh_cor.shrink_correl2(b, x)
t1 = time.time()
print((t1-t0)/n)

我的电脑上的输出是:

0.7070999860763549   # regular numpy
0.6726999998092651   # memoryview

使用memoryview(在上面的代码中)只能提高5%的速度(与博客中的巨大速度提升不同)。

1 个答案:

答案 0 :(得分:0)

@uday给我一个星期的时间,因为我的电脑更少,但是这里可以加快速度,让你开始:1)而不是使用np.transpose附加gil创建一个memoryview与你想要在任何循环之前转置的内容相同(即你将变量f声明为不需要gil的内存视图,只需在f_t上创建一个视图,即cdef double[:, ::1] f_T = np.transpose(f)或仅=f.T

2)这个步骤有点棘手,因为你需要np.dot的C / C ++样式包装版本(所以在这种情况下,确保对dgemm函数的调用是{{1在它上面&缩进函数下一行释放gil,有4个空格缩进SO要求):https://gist.github.com/pv/5437087。该示例看起来有效(尽管您必须将include with nogil:文件保存并将其放在正在构建项目的位置;我还怀疑您应该添加f2pyptr.h);如果没有,它需要mods,你可以看到我已在另一篇文章中完成:Calling BLAS / LAPACK directly using the SciPy interface and Cython (pointer issue?)/- also how to add MKL然后你需要在顶部添加cimport numpy as np并将所有循环更改为{来自from cython.parallel cimport prange的{​​1}}并确保所有prange部分均为range,并且所有变量均为prange,然后才会进行操作。此外,您必须在编译器参数中的setup.py中添加nogil以及包含其libs的链接。如果您需要澄清,请提出更多问题。这并不像应该的那样容易,但是一点指导变得非常简单。基本上,一旦您的cdef被修改为包含它将继续工作的所有内容。

3)虽然可能最容易修复 - 摆脱那个List。如果需要文本和数据,请将其设置为numpy数组或pandas数据帧。每当我使用数据列表时,减速就会令人难以置信。