J'utilise prange
afin de modifier un tableau. J'essaie d'avoir aussi peu de lignes jaunes possible dans la page HTML généré par le compilateur cython avec:Cython Prange - traitant avec __Pyx_ErrFetchWithState/__Pyx_ErrRestoreWithState
cython function_prange.pyx -a
Mais lors de l'extraction d'une partie d'un tableau pour le modifier, je l'ai toujours ce genre de code qui est généré :
{
#ifdef WITH_THREAD
PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();
#endif
#ifdef _OPENMP
#pragma omp flush(__pyx_parallel_exc_type)
#endif /* _OPENMP */
if (!__pyx_parallel_exc_type) {
__Pyx_ErrFetchWithState(&__pyx_parallel_exc_type, &__pyx_parallel_exc_value, &__pyx_parallel_exc_tb);
__pyx_parallel_filename = __pyx_filename; __pyx_parallel_lineno = __pyx_lineno; __pyx_parallel_clineno = __pyx_clineno;
__Pyx_GOTREF(__pyx_parallel_exc_type);
}
#ifdef WITH_THREAD
PyGILState_Release(__pyx_gilstate_save);
#endif
}
Est-il possible d'éviter ces __Pyx_ErrFetchWithState
/__Pyx_ErrRestoreWithState
? Est-ce que c'est vraiment important?
Voici le code que j'utilise:
Une première fonction en ajoutant un double à la diagonale d'une matrice contenue dans une matrice de taille dans une function_nogil.pyx
import cython
@cython.boundscheck(False) # turn off bounds-checking for entire function
@cython.wraparound(False) # turn off negative index wrapping for entire function
cdef void add_diag(double [:,:,:] a, int a_len, int a_wid, double coeff) nogil:
cdef int x_max = a_len
cdef int x
for x in xrange(x_max):
a[0,x,x] = a[0,x,x] + coeff
Son tête dans function_nogil.pxd
cdef void add_diag(double [:,:,:] a, int a_len, int a_wid, double coeff) nogil
la fonction en utilisant prange
dans function_prange.pyx
@cython.boundscheck(False) # turn off bounds-checking for entire function
@cython.wraparound(False) # turn off negative index wrapping for entire function
def prange_loop_idx(double [:,:,:] a, int a_dim1, int a_dim2, int a_dim3, double coeff,int num_threads):
cdef int i = 0
with nogil,parallel(num_threads=num_threads):
for i in prange(a_dim1):
add_diag(a[i:i+1:,],a_dim2,a_dim3,coeff)