[SciPy-Dev] Degraded performance via Cython LAPACK wrappers

Ilhan Polat ilhanpolat at gmail.com
Sun Mar 15 08:46:21 EDT 2020


Dear all,

I am trying to increase the performance of a few functions in linalg module
by cythonizing trivial but time-consuming tasks. After not getting too much
performance, I decided to consult the scipy folks about it.

I have the following cython MVE specifically working on fortran contiguous
arrays for simplicity.

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cpdef void zzz(double[::1, :] a, double[::1, :]b) nogil:
    cdef int n, nrhs
    n =  a.shape[0]
    nrhs = b.shape[1]
    lda = n

    cdef int *ipiv = <int *>malloc(<int>n * sizeof(int))
    cdef int info = 0

    if not ipiv:
        raise MemoryError()
    # dgesv(int *n, int *nrhs, d *a, int *lda, int *ipiv, d *b, int *ldb,
int *info)
    dgesv(&n, &nrhs, &a[0,0], &lda, &ipiv[0], &b[0,0], &lda, &info)
    free(ipiv)


This is supposed to be much faster than the regular sp.linalg.solve(a, b)
call since it bypasses a lot of checks and the relevant lapack flavor
detection etc.

However using the following data

n = 150
a = np.empty([n, n], dtype=float, order='F')
a[:, :] = np.random.rand(n, n) + np.eye(n)*5
b = np.asfortranarray(np.random.rand(n, 1))

I see that for about 800 > n > 50 the results are almost identical which is
pretty surprising as cython code should be spending a lot of time
somewhere.

I haven't been successful enabling the line trace in cython code hence can
anyone please tell me where I am losing performance and how I can remedy it?

Best,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scipy-dev/attachments/20200315/212673f6/attachment.html>


More information about the SciPy-Dev mailing list