Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. from cffi import FFI
  2.  
  3. import numpy as np
  4.  
  5. ffi = FFI()
  6. ffi.cdef("""
  7.    void test_numpy_array(unsigned long size, float *data, float* (*resize)(int args...);
  8. """)
  9.  
  10. C = ffi.dlopen("rust-lib/target/debug/libdata_reader.so")
  11.  
  12. commData = C.init()
  13.  
  14. def resize(array, *args):
  15.     shape = tuple(args)
  16.     array = np.resize(array, shape)
  17.     return ffi.cast("float *", ffi.from_buffer(array))
  18.  
  19. def curry_resize(array):
  20.     def inner(*shape):
  21.         resize(array, *shape)
  22.     return inner
  23.  
  24. arr = np.empty((2,3,4), dtype=np.float32)
  25.  
  26. cptr = ffi.cast("float *", ffi.from_buffer(arr))
  27.  
  28. callback = curry_resize(arr)
  29. ffi.callback("float *(int...)", callback)
  30.  
  31. C.test_numpy_array(2*3*4, cptr, callback)
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement