sum and mean function in cython with nogil

84 Views Asked by At

I have math_helper.pyx that is written in cython with nogil. I have another cython script named feature.pyx that uses m_mean function, however, its output (z_mean) is always zero. I don't know where the problem is.

this is math_helper.pyx

cimport cython
from libc.math cimport fabs, pow, log, sqrt
from cython cimport view
#from libcpp.algorithm cimport sort


@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
@cython.cdivision(True)
cdef double m_sum(double [:] a) nogil:
    cdef:
        int i
        double total = 0.0
    for i in range(a.shape[0]):
        total += a[i]
    return total

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
@cython.cdivision(True)
cdef inline double m_mean(double [:] a) nogil:
    cdef:
        int i
        double n = a.shape[0]
    return m_sum(a) / n

this is part of feature.pyx that calls m_mean function

cimport math_helper

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
@cython.initializedcheck(False)
cdef inline void compute_features_from_neighbors(
    double[:] center_point,
    double[::1, :] neighbor_points,
    double[::1, :] neighbor_points2d,
    double radius,
    float [:] out,
    int [:] feature_map_list,
) nogil:
    cdef:
        double z_mean

    z_mean = math_helper.m_mean(neighbor_points[2, :])

The problem is somewhere in m_sum function in math_helper.pyx because total is zero even after loop!

0

There are 0 best solutions below