Greetings I'm extending our existing C/C++ software with Python/Numpy in order to do extra number crunching. It already works like a charm calling python with the C API <Python.h>. But what is the proper way of passing double arrays returned from Python/Numpy routines back to C? I came across PyArray but I can see in the compiler warnings, it is deprecated and I don't wanna start from scratch on legacy facilities. Going forward, what is the intended way of doing this with neat code on both sides and with a minimum of mem copy gymnastics overhead? thanks in advance Søren
On Wed, Mar 20, 2013 at 1:59 PM, Søren <sd@syntonetic.com> wrote:
Greetings
I'm extending our existing C/C++ software with Python/Numpy in order to do extra number crunching. It already works like a charm calling python with the C API <Python.h>.
But what is the proper way of passing double arrays returned from Python/Numpy routines back to C?
I came across PyArray but I can see in the compiler warnings, it is deprecated and I don't wanna start from scratch on legacy facilities.
What is this `PyArray` that you are referring to? There is nothing named just `PyArray` to my knowledge. Do you mean direct access to the `data` member of the PyArrayObject struct? Yes, that is deprecated. Use the PyArray_DATA() macro to get a `void*` pointer to the start of the data. http://docs.scipy.org/doc/numpy/reference/c-api.array.html#PyArray_DATA -- Robert Kern
On Wed, Mar 20, 2013 at 9:03 AM, Robert Kern <robert.kern@gmail.com> wrote: I highly recommend using an existing tool to write this interface, to take care of the reference counting, etc for you. Cython is particularly nice. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
Thanks Robert, for making that clear. I got a deprecated warning the second I added #include <numpy/arrayobject.h> and I got scared off too fast in my exploring phase. Cheers Søren On 20/03/2013 17:03, Robert Kern wrote:
Greetings
I'm extending our existing C/C++ software with Python/Numpy in order to do extra number crunching. It already works like a charm calling python with the C API <Python.h>.
But what is the proper way of passing double arrays returned from Python/Numpy routines back to C?
I came across PyArray but I can see in the compiler warnings, it is deprecated and I don't wanna start from scratch on legacy facilities. What is this `PyArray` that you are referring to? There is nothing named just `PyArray` to my knowledge. Do you mean direct access to the `data` member of the PyArrayObject struct? Yes, that is deprecated. Use the PyArray_DATA() macro to get a `void*` pointer to the start of
On Wed, Mar 20, 2013 at 1:59 PM, Søren <sd@syntonetic.com> wrote: the data.
http://docs.scipy.org/doc/numpy/reference/c-api.array.html#PyArray_DATA
-- Robert Kern _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Dear Søren, if you are new to interfacing python/numpy with C/C++, you may want to check out: http://scipy-lectures.github.com/advanced/interfacing_with_c/interfacing_wit... Disclaimer: I am the author of this chapter, so this response is a bit of a shameless plug :D Hope it helps none the less. V- * Søren <sd@syntonetic.com> [2013-03-21]:
Thanks Robert, for making that clear.
I got a deprecated warning the second I added #include <numpy/arrayobject.h> and I got scared off too fast in my exploring phase.
Cheers Søren
On 20/03/2013 17:03, Robert Kern wrote:
Greetings
I'm extending our existing C/C++ software with Python/Numpy in order to do extra number crunching. It already works like a charm calling python with the C API <Python.h>.
But what is the proper way of passing double arrays returned from Python/Numpy routines back to C?
I came across PyArray but I can see in the compiler warnings, it is deprecated and I don't wanna start from scratch on legacy facilities. What is this `PyArray` that you are referring to? There is nothing named just `PyArray` to my knowledge. Do you mean direct access to the `data` member of the PyArrayObject struct? Yes, that is deprecated. Use the PyArray_DATA() macro to get a `void*` pointer to the start of
On Wed, Mar 20, 2013 at 1:59 PM, Søren <sd@syntonetic.com> wrote: the data.
http://docs.scipy.org/doc/numpy/reference/c-api.array.html#PyArray_DATA
-- Robert Kern _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
On 21/03/2013 09:45, Valentin Haenel wrote:
if you are new to interfacing python/numpy with C/C++, you may want to check out:
http://scipy-lectures.github.com/advanced/interfacing_with_c/interfacing_wit...
Disclaimer: I am the author of this chapter, so this response is a bit of a shameless plug :D
Hello Valentin, I had a quick look at the chapter. It looks good! Thanks for sharing it. However I have a small comment on the way you implement the Cython-Numpy solution. I would have written the loop over the array element in Cython itself rather than in a separately compiled C function. This would have the advantage of presenting more capabilities of Cython and would slightly decrease the complexity of the solution (one source file instead of two). Cheers, Daniele
Dear Daniele * Daniele Nicolodi <daniele@grinta.net> [2013-03-21]:
On 21/03/2013 09:45, Valentin Haenel wrote:
if you are new to interfacing python/numpy with C/C++, you may want to check out:
http://scipy-lectures.github.com/advanced/interfacing_with_c/interfacing_wit...
Disclaimer: I am the author of this chapter, so this response is a bit of a shameless plug :D
Hello Valentin,
I had a quick look at the chapter. It looks good! Thanks for sharing it.
However I have a small comment on the way you implement the Cython-Numpy solution. I would have written the loop over the array element in Cython itself rather than in a separately compiled C function. This would have the advantage of presenting more capabilities of Cython and would slightly decrease the complexity of the solution (one source file instead of two).
Thanks very much for your feedback! Since the chapter in under a CC licence, you are welcome to submit your proposal as a Pull-Request. :D The reason why I wrote the loop in C is so that the cython example synergieses with the others. The ideas is, that you have an already existing code-base that has a function which has such a signature. Ideally, your proposal would be an improvement, where the original example stays in place and you develop the improvement including reasons as to why it is better. ;) best V-
On 21/03/2013 10:16, Valentin Haenel wrote:
Dear Daniele
* Daniele Nicolodi <daniele@grinta.net> [2013-03-21]:
On 21/03/2013 09:45, Valentin Haenel wrote:
if you are new to interfacing python/numpy with C/C++, you may want to check out:
http://scipy-lectures.github.com/advanced/interfacing_with_c/interfacing_wit...
Disclaimer: I am the author of this chapter, so this response is a bit of a shameless plug :D
Hello Valentin,
I had a quick look at the chapter. It looks good! Thanks for sharing it.
However I have a small comment on the way you implement the Cython-Numpy solution. I would have written the loop over the array element in Cython itself rather than in a separately compiled C function. This would have the advantage of presenting more capabilities of Cython and would slightly decrease the complexity of the solution (one source file instead of two).
Thanks very much for your feedback! Since the chapter in under a CC licence, you are welcome to submit your proposal as a Pull-Request. :D
The reason why I wrote the loop in C is so that the cython example synergieses with the others. The ideas is, that you have an already existing code-base that has a function which has such a signature. Ideally, your proposal would be an improvement, where the original example stays in place and you develop the improvement including reasons as to why it is better. ;)
I understand the reasoning behind your choice. I'm adding sending you a patch with this addition to my todo list, but I don't really know when I will have time to work on it... Cheers, Daniele
* Daniele Nicolodi <daniele@grinta.net> [2013-03-21]:
On 21/03/2013 10:16, Valentin Haenel wrote:
Dear Daniele
* Daniele Nicolodi <daniele@grinta.net> [2013-03-21]:
On 21/03/2013 09:45, Valentin Haenel wrote:
if you are new to interfacing python/numpy with C/C++, you may want to check out:
http://scipy-lectures.github.com/advanced/interfacing_with_c/interfacing_wit...
Disclaimer: I am the author of this chapter, so this response is a bit of a shameless plug :D
Hello Valentin,
I had a quick look at the chapter. It looks good! Thanks for sharing it.
However I have a small comment on the way you implement the Cython-Numpy solution. I would have written the loop over the array element in Cython itself rather than in a separately compiled C function. This would have the advantage of presenting more capabilities of Cython and would slightly decrease the complexity of the solution (one source file instead of two).
Thanks very much for your feedback! Since the chapter in under a CC licence, you are welcome to submit your proposal as a Pull-Request. :D
The reason why I wrote the loop in C is so that the cython example synergieses with the others. The ideas is, that you have an already existing code-base that has a function which has such a signature. Ideally, your proposal would be an improvement, where the original example stays in place and you develop the improvement including reasons as to why it is better. ;)
I understand the reasoning behind your choice. I'm adding sending you a patch with this addition to my todo list, but I don't really know when I will have time to work on it...
Aye, that would be great! No need to rush -- you can also throw a feature request into the project issue tracker, <wishful_thinking> maybe someone else will grab it. </wishful_thinking> V-
participants (5)
-
Chris Barker - NOAA Federal
-
Daniele Nicolodi
-
Robert Kern
-
Søren
-
Valentin Haenel