[Numpy-discussion] Handle type convertion in C API
Benoit Gschwind
gschwind at gnu-log.net
Tue Mar 10 12:25:17 EDT 2020
Hello,
I writing a python binding of one of our library. The binding intend to
vectorize the function call. for exemple:
double foo(double, double) will be bound to a python:
<numpy.array of double> module.foo(<numpy.array>, <numpy.array>)
and the function foo will be called like :
for (int i = 0; i < size; ++i)
outarr[i] = foo(inarr0[i], inarr[1]);
My question is about how can I handle type conversion of input array,
preferably in efficient manner, given that each input array may require
different input type.
Currently I basically enforce the type and no type conversion is
performed. But I would like relax it. I thought of several possibility
starting from the obvious solution consisting on recasting in the inner
loop that would give:
for (int i = 0; i < size; ++i)
if (inarr[i] need recast)
in0 =
recast(inarr[i])
else
in0 = inarr[i]
[... same for all
inputs parameter ...]
outarr[i] = foo(in0, in1, ...);
This solution is memory efficient, but not actually computationally
efficient.
The second solution is to copy&recast the entire inputs arrays, but in
that case it's not memory efficient. And my final thought is to mix the
first and the second by chunking the second method, i.e. converting N
input in a raw, then applying the function to them en so on until all
the array is processed.
Thus my questions are:
- there is another way to do what I want?
- there is an existing or recommended way to do it?
And a side question, I use the PyArray_FROM_OTF, but I do not
understand well it's semantic. If I pass a python list, it is converted
to the desired type and requirement; when I pass a non-continuous array
it is converted to the continuous one; but when I pass a numpy array of
another type than the one specified I do not get the conversion. There
is a function that do the conversion unconditionally? Did I missed
something ?
Thank you by advance for your help
Best regards
More information about the NumPy-Discussion
mailing list