0
%%cython
cimport cython
import numpy as np
cimport openmp
from numpy.linalg import inv
from cython.parallel cimport prange
@cython.boundscheck(False)
@cython.wraparound(False)
def inv_unit_diagon_blocks(double[::1,:] A,double[::1,:] U,int m):
    cdef n=int(len(A)/m)
    cdef double[::1,:] Lambda=np.empty((A.shape[0],A.shape[1]))
    cdef int i
    A=A.reshape(A.shape[0]//n, n, A.shape[1]//n, n).swapaxes(1, 2).reshape(-1, n, n)
    with nogil:
        for i in prange(m*m, schedule='static'):
            Lambda[i]=inv(U[i])@A[i]@U[i]
    return Lambda
Error compiling Cython file:
------------------------------------------------------------
...
    cdef double[::1,:] Lambda=np.empty((A.shape[0],A.shape[1]))
    cdef int i
    A=A.reshape(A.shape[0]//n, n, A.shape[1]//n, n).swapaxes(1, 2).reshape(-1, n, n)
    with nogil:
        for i in prange(m*m, schedule='static'):
            Lambda[i]=inv(U[i])@A[i]@U[i]
                                   ^
------------------------------------------------------------

.ipython\cython\_cython_magic_f2335949c00c765c6ffad6f007548be3.pyx:15:36: Coercion from Python not allowed without the GIL

It gives this error probably cause I try to multiply the nxn matrices. Can somebody tell me how can I do that? How to multiply parallel m*m times the 2 matrices? I know that it can be done using prange but it needs gil to operate matrix mul and prange can only be used with nogil. Is there anyway to make it work?

ABZANMASTER
  • 105
  • 6
  • Does this answer your question? "[Cython error: Coercion from Python not allowed without the GIL](//stackoverflow.com/q/42251064/90527)", "[Coercion from Python not allowed without the GIL, using arrays in parallel (multithreading in Cython)](//stackoverflow.com/q/65462161/90527)" – outis May 08 '22 at 18:11
  • Welcome to SO. Please take a look at the [help], especially "[ask]". – outis May 08 '22 at 18:13
  • @outis I don't think either really answer the question. They similar errors but they don't really explain why this particular operation is failing. – DavidW May 08 '22 at 20:15
  • @DavidW: note the `import numpy …`, `from numpy.linalg import …` and `cdef n` lines in the sample. – outis May 08 '22 at 21:23
  • 1
    Related: "[calling dot products and linear algebra operations in Cython?](//stackoverflow.com/q/16114100/90527)", "[Missing numpy attribute when using Cython](//stackoverflow.com/q/53026820/90527)" – outis May 08 '22 at 21:24
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 09 '22 at 06:29

0 Answers0