My Cython code interacts too much with Python

Viewed 211

I implemented a loop in Cython to speed up my code:

When I run:

$ cython loop_fun2.pyx -a

I get the following output:

enter image description here

It seems that my Cython code interacts with the Python interpreter almost at every line. I hope to remove this interaction in order to speed up my code. What do I need to change in my code so that it will run only with the C interpreter? I suspect that the issue is related to the way I defined the arrays...

Edited: here is some additional information on the issue: enter image description here

And here is the code:

import cython
from cython.view cimport array as cvarray
from cpython.array cimport array, clone
import numpy as np

from libc.math cimport acos, pi, sqrt


@cython.cdivision
@cython.boundscheck(False)
@cython.wraparound(False)

def fill_mat(double[:,:] K,double[:,:] K_ii,double[:,:] K_jj, int q, int 
P, int Q):
    return fill_mat1(K,K_ii,K_jj, q, P, Q)
cdef fill_mat1(double[:,:] K,double[:,:] K_ii,double[:,:] K_jj, int q, int 
P, int Q):

cdef double[:,:] sigma=cvarray(shape=(P,Q), itemsize=sizeof(double), format="d")
cdef double[:,:] sigma_ii=cvarray(shape=(P,Q), itemsize=sizeof(double), format="d")
cdef double[:,:] sigma_jj=cvarray(shape=(P,Q), itemsize=sizeof(double), format="d")
cdef double temp_K
cdef double temp_K_ii
cdef double temp_K_jj

for i1 in range(P):
    for i2 in range(Q):
        temp_K = 0.0
        temp_K_ii = 0.0
        temp_K_jj = 0.0
        for i in range(0,q):
            for j in range(0,q):
                temp_K = temp_K + K[i1+i,i2+j]
                temp_K_ii = temp_K_ii + K_ii[i1+i,i2+j]
                temp_K_jj = temp_K_jj + K_jj[i1+i,i2+j]

        sigma[i1,i2] = temp_K
        sigma_ii[i1,i2] = temp_K_ii
        sigma_jj[i1,i2] = temp_K_jj

return sigma, sigma_ii, sigma_jj
0 Answers
Related