3

I want to release the GIL inside a for loop on a 3-dimensional numpy array

cdef np.ndarray[DTYPE_t,ndim=3] array=np.ones((10000000,4,2))
cdef np.ndarray[DTYPE_t,ndim=2] sliced_array
cdef int i
cdef int N=array.shape[0]
for i in range(N):
  sliced_array=array[i]
  #perform computations on slice

When I look at the html produced by Cython it looks like it is calling Python when it is doing sliced_array=array[i] I guess it is because it infers the size of the two other dimensions but even when using typed ranges for the second and third axis this line is still yellow !

sliced_array=array[i,typed_slice_x,typed_slice_y]
jeandut
  • 2,471
  • 4
  • 29
  • 56

1 Answers1

5

One of the advantages of the newer memoryview syntax over declaring things as numpy arrays is that you can do indexing operations without the GIL:

cdef double[:,:,:] array=np.ones((10000000,4,2))
cdef double[:,:] sliced_array
cdef int i
cdef int N=array.shape[0]
for i in range(N):
  with nogil: # just to prove the point
    sliced_array=array[i,:,:]

If you declare them as cdef np.ndarray then you can't easily avoid needing the GIL for indexing.

DavidW
  • 29,336
  • 6
  • 55
  • 86
  • 1
    Ok thanks again @DavidW I need to buy a book on Python it seems I really need to learn some more about how Python is working internally not to be stuck every damn time. – jeandut Aug 21 '17 at 18:16