Hi, I’m looking for examples how to call to pybind defined c++ methods from cfunc or if not available perhaps someone can look at my specific example. I have defined method with the signature int func_int(); in module with:
Thank you for the reply. I have tried global variable, removing c++ namespaces but all without success. It is as if inside the cfunc the method is no longer visible.
I also tried simpler scenario of calling method directly instead of casting the method address.
A follow up question regarding this, how do we convert np.array’s raw pointer inside of the jitted function?
bind_int_func = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.POINTER(ctypes.c_int), ctypes.c_int)(custom_ops.int_func_address())
@njit
def f(arr: np.array):
# How to cast arr.ctypes.data to a raw pointer inside of the jitted function?
# arr.ctypes.data works but I can't do
# `ctypes.cast(x.ctypes.data, ctypes.POINTER(ctypes.c_int32))` here
If I just directly pass arr.ctypes.data to bind_int_func, I am getting this error:
No implementation of function ExternalFunctionPointer((int32*, int32) -> int32) found for signature:
>>> ExternalFunctionPointer(uint64, Literal[int](3))
There are 2 candidate implementations:
- Of which 2 did not match due to:
Type Restricted Function in function 'unknown': File: unknown: Line unknown.
With argument(s): '(uint64, int64)':
No match for registered cases:
* (int32*, int32) -> int32
During: resolving callee type: ExternalFunctionPointer((int32*, int32) -> int32)
During: typing of call at /usr/local/google/_blaze_jimlintw/2610b537ee3958b4624a3da396c47630/execroot/google3/blaze-out/k8-fastbuild/bin/learning/processing/experimental/tf2jax/symbolic_trace/run_custom_op_with_tracing.runfiles/google3/learning/processing/experimental/tf2jax/symbolic_trace/run_custom_op_with_tracing.py (51)
File "../../../../../../../../../../../../../../../usr/local/google/_blaze_jimlintw/2610b537ee3958b4624a3da396c47630/execroot/google3/blaze-out/k8-fastbuild/bin/learning/processing/experimental/tf2jax/symbolic_trace/run_custom_op_with_tracing.runfiles/google3/learning/processing/experimental/tf2jax/symbolic_trace/run_custom_op_with_tracing.py", line 51:
def f(arr):
<source elided>
# ptr = ptr_type(arr.ctypes.data)
return bind_func(arr.ctypes.data, 3)
^
During: Pass nopython_type_inference