Reputation: 45
I define a C function with memoryview input to work with a NumPy array, but a pure C-defined temporary float array can not work with 'base_func'. Error:
Operation not allowed without gil
How can I modify C function base_func to work with both the numpy.array and cdef C array?
cdef void base_func(float[:] vec1) noexcept nogil:
return
def python_entry(vec: np.ndarray):
cdef float[:] vec_view = vec
base_func(vec_view)
cdef void cfunc(float[:] vec2) noexcept nogil:
cdef float[10] tmp_vec
base_func(tmp_vec)
cdef void cfunc(float[:] vec2) noexcept nogil:
cdef float[10] tmp_vec
base_func(tmp_vec) ^
------------------------------------------------------------
c_test.pyx:21:14: Operation not allowed without gil
I want to cythonize the GROUP BY operation on 1D or 2D np.ndarray. The python interface will be like group_mean(data, group), and group_mean = FuncWrapper(c_group_mean). So I can write other c-functions like c_group_std to implement another python interface group_std
Problems and Resolutions:
cdef void c_mean (float[:] data, int[:] group, float[:] result, const int length, const int group_number):
float[group_num] sum_up
int[group_num] count
So my final code should be,
group_mean = FuncWrapper(CFuncWrapper.bind_cfunc_group_num(c_group_mean))
1. Python Interface: group_mean
2. FuncWrapper: pass in different base function, like c_group_mean
3. CFuncWrapper: make sure python object FuncWrapper can accept cdef functions
4. TODO: convert MemoryviewSlice to float*?
5. base c functions: c_group_mean, c_group_std
Upvotes: 1
Views: 111