Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buffer unpacking not optimized away , However only using 1d Numpy array

Tags:

cython

I got a problem with an inline function in cython

cdef inline int binary_search(np.ndarray[np.float_t,ndim=1] cumsum, double rdn):
  cdef int mi=0,ma=len(cumsum)-1,pos 
  while True:
    if ( mi > ma):
      break
    pos = (mi+ma) / 2
    if (rdn <= cumsum[pos] ):
      ma=pos-1
    else:
      mi=pos+1
  return mi-1  

However, I get the following problem: Buffer unpacking not optimized away.

I heard that this is an issue when dealing with multidimensional arrays, but this is not the case for me.

Thanks for any help

like image 318
bios Avatar asked Apr 18 '26 03:04

bios


1 Answers

I am getting the same warning using Cython 3.0.0a10.
What jcrudy says in the comment is true, namely, removing the inline would removed the warning.
Nevertheless, the warning is likely suggesting that directly passing an array, rather then a memory view would cause an overhead in the function call.
This is described in the Cython documentation.

In my case using a memory view, rather then a an array as argument, solved the problem without the need to remove the inline. The above function would then look like this (if using float32):

cdef inline int binary_search(np.float32_t[:] cumsum, double rdn):

Keep in mind that creating the memory-view adds a little overhead, but since in your function you are not modifying the cumsum it would make perfectly sense to pass a memory view as argument to the function.

like image 133
Salvatore Cosentino Avatar answered Apr 21 '26 22:04

Salvatore Cosentino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!