You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to figure out a way to pass numpy arrays directly to a cdll (wrapped with pyclibrary), without the user having to manually do the type conversion, and without having any memory duplication.
I was wondering if you had any thoughts on that and if your framework could handle it smoothly. I'm aware of the build_array function provided by c_library, but I believe it would be more transparent for the users if they can use numpy arrays directly. I believe it's fine to impose a dtype to the users though.
Test case
Here's my way of doing it so far, I have the following fortran dll calls, that adds vector x and y and returns a vector add, all of them of size n, with the following signature:
On the python side, I create number arrays, that I then have to convert to c_double arrays using a small function to_c, and then I pass it to the library (which is a CLibrary object):
Is there anyways we can make this call more "transparent" for the user, without the need for the explicit type conversion?
Can we avoid any memory duplication by asking the user to create numpy arrays of a given type?
I'm guessing one solution could be to handle the conversion within pyclibrary directly.
I tried with x_c = x.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), which seems to work for the input arguments, but then gives a MemoryError if I try to access res['x'] and it won't work for the returned value add. Since it's a fortran dll, everything is passed by reference, so there is not real notion of input and outputs though.
The text was updated successfully, but these errors were encountered:
In this case it perhaps does not matter but you should be careful to pass F ordered arrays since this likely what your library expect.
I know I have used the ctypes attributes in the past. I will check how. Do not hesitate to ping me.
I'm trying to figure out a way to pass numpy arrays directly to a cdll (wrapped with
pyclibrary
), without the user having to manually do the type conversion, and without having any memory duplication.I was wondering if you had any thoughts on that and if your framework could handle it smoothly. I'm aware of the
build_array
function provided byc_library
, but I believe it would be more transparent for the users if they can use numpy arrays directly. I believe it's fine to impose adtype
to the users though.Test case
Here's my way of doing it so far, I have the following fortran dll calls, that adds vector
x
andy
and returns a vectoradd
, all of them of sizen
, with the following signature:On the python side, I create number arrays, that I then have to convert to
c_double
arrays using a small functionto_c
, and then I pass it to the library (which is aCLibrary
object):Questions
Is there anyways we can make this call more "transparent" for the user, without the need for the explicit type conversion?
Can we avoid any memory duplication by asking the user to create numpy arrays of a given type?
I'm guessing one solution could be to handle the conversion within
pyclibrary
directly.I tried with
x_c = x.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
, which seems to work for the input arguments, but then gives aMemoryError
if I try to accessres['x']
and it won't work for the returned valueadd
. Since it's a fortran dll, everything is passed by reference, so there is not real notion of input and outputs though.The text was updated successfully, but these errors were encountered: