I have a numpy array that I need to get to a dll
function wrapped in ctypes
.
myFunc.argtypes = (...POINTER(ctypes.c_double),POINTER(ctypes.c_double),...)
myFunc(...Data1_arr,Data2_arr...)
I'm defining arrays like this and it works great. its just slow. 1.5ms for a small set of data
#these are c_double_array types
Data1_arr = (ctypes.c_double * len(Data1))(*Data1)
Data2_arr = (ctypes.c_double * len(Data2))(*Data2)
I found several ways to operate on the numpy arrays more directly, which are much faster. average 6.899999999809836e-06 seconds
#these are .LP_c_double types
Data1_ptr = Data1.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
Data2_ptr = Data2.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
The Data1_ptr._arr values match the Data1_arr values obviously.
MyFunc()
even happily takes the Data1_ptr
and Data2_ptr
values. However, the output of the function is all messed up. MyFunc is obviously interpreting the c_double_array correctly, but not the LP_c_double.
I'll admit I'm punching over my weight here, but I am trying to learn.
Is there a way to get the c_double_array from the LP_c_double pointer without wasting extra time?
edit to add this: here is the C header
Proc_WriteData
(
int32 DataLength,
const double Data1[],
const double Data2[]
);
Python calling it:
def Write_New_Data(Data1,Data2):
dll.Proc_WriteData.argtypes = (int32,POINTER(ctypes.c_double),POINTER(ctypes.c_double))
Data_Length = len(Data1)
Data1_ptr = Data1.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
Data2_ptr = Data2.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
dll.Proc_WriteData(Data_Length, Data1_ptr, Data2_ptr)
I did see something like this. I tried it, but the output of the function was identical to the above.
return np.ctypeslib.ndpointer(dtype=element_type, shape=(dim,), flags="F_CONTIGUOUS")
Im now wondering about const double
im wondering if thats where im getting stuck?
Please let me know if you need any more information. thanks