【发布时间】:2021-11-19 17:58:41
【问题描述】:
here 部分解决了这个问题,但我对解决方案不太满意,我想知道:
- 有没有办法编写结构并将其映射到扩展类
- 为什么会出现错误?函数是
cdef和nogil,在GIL环境中调用就可以了
第一点对我来说非常重要,因为我将从utils 库中调用很多函数并以here 描述的方式编写所有内容将非常耗时。
代码(iPython 笔记本):
%%cython
import numpy as np
cimport numpy as np
mingw_setup_args={'script_args': ["--compiler=mingw32", "--cython-cplus"], 'include_dirs': np.get_include()}
import pyximport; pyximport.install(setup_args=mingw_setup_args, reload_support=True, language_level=3)
from utils import blas_multiply
D_TYPE = np.dtype(np.float64)
cdef double[:,:] A = np.random.randn(100, 10, dtype=D_TYPE)
cdef double[:] b = np.random.randn(10, dtype=D_TYPE)
cdef double[:] out = np.zeros((A.shape[0], ), dtype=D_TYPE)
# blas_multiply(A, b, out) # works fine
with nogil:
blas_multiply(A, b, out)
utils.pyx 文件
#!python
# distutils: language=c++
# cython: language_level=3
cimport cython
import numpy as np
cimport numpy as np
cimport scipy.linalg.cython_blas as blas
@cython.cdivision(True)
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
cpdef void blas_multiply(double[:,:] A, double[:] b, double[:] c) nogil:
"""
calls dgemv from BLAS which computes y = alpha * trans(A) + beta * y
A: m rows by k columns
b: k rows by 1 columns
c: k rows by 1 columns
"""
cdef int m = A.shape[0]
cdef int k = A.shape[1]
cdef int n = 1
cdef double alpha = 1.0
cdef double beta = 0.0
# for F contiguous arrays
# blas.dgemv("N", &m, &k, &alpha, &A[0, 0], &m, &b[0], &n, &beta, &c[0], &n)
# for C contiguous arrays
blas.dgemv("T", &k, &m, &alpha, &A[0, 0], &k, &b[0], &n, &beta, &c[0], &n)
回溯:
Error compiling Cython file:
------------------------------------------------------------
...
# blas_multiply(A, b, out)
# print(out)
with nogil:
blas_multiply(A, b, out)
^
------------------------------------------------------------
C:\Users\...\.ipython\cython\_cython_magic_6b53bffa20f1e5bcc572f847b4b26a3f.pyx:22:17: Discarding owned Python object not allowed without gil
Error compiling Cython file:
------------------------------------------------------------
...
# blas_multiply(A, b, out)
# print(out)
with nogil:
blas_multiply(A, b, out)
^
------------------------------------------------------------
C:\Users\...\.ipython\cython\_cython_magic_6b53bffa20f1e5bcc572f847b4b26a3f.pyx:22:17: Calling gil-requiring function not allowed without gil
Error compiling Cython file:
------------------------------------------------------------
...
# blas_multiply(A, b, out)
# print(out)
with nogil:
blas_multiply(A, b, out)
^
------------------------------------------------------------
C:\Users\...\.ipython\cython\_cython_magic_6b53bffa20f1e5bcc572f847b4b26a3f.pyx:22:4: Accessing Python global or builtin not allowed without gil
Error compiling Cython file:
------------------------------------------------------------
...
# blas_multiply(A, b, out)
# print(out)
with nogil:
blas_multiply(A, b, out)
^
------------------------------------------------------------
C:\Users\...\.ipython\cython\_cython_magic_6b53bffa20f1e5bcc572f847b4b26a3f.pyx:22:17: Constructing Python tuple not allowed without gil
Error compiling Cython file:
------------------------------------------------------------
...
# blas_multiply(A, b, out)
# print(out)
with nogil:
blas_multiply(A, b, out)
^
------------------------------------------------------------
C:\Users\...\.ipython\cython\_cython_magic_6b53bffa20f1e5bcc572f847b4b26a3f.pyx:22:18: Converting to Python object not allowed without gil
Error compiling Cython file:
------------------------------------------------------------
...
# blas_multiply(A, b, out)
# print(out)
with nogil:
blas_multiply(A, b, out)
^
------------------------------------------------------------
C:\Users\...\.ipython\cython\_cython_magic_6b53bffa20f1e5bcc572f847b4b26a3f.pyx:22:21: Converting to Python object not allowed without gil
Error compiling Cython file:
------------------------------------------------------------
...
# blas_multiply(A, b, out)
# print(out)
with nogil:
blas_multiply(A, b, out)
^
------------------------------------------------------------
C:\Users\...\.ipython\cython\_cython_magic_6b53bffa20f1e5bcc572f847b4b26a3f.pyx:22:24: Converting to Python object not allowed without gil
【问题讨论】:
标签: python c++ jupyter-notebook cython cpython