<div dir="ltr">Phil, <div><br></div><div>Great direction. I think that I have working example that uses NumPy C-API alongside SIP. This requires that numpy/arrayobject.h be included. Below is the method declaration and method code. I am seeing this get data allocated to the GPU and other methods return roughly meaningful results (I have validation to do, etc.).</div><div><br></div><div><div><font face="monospace, monospace"> void Allocate(int width, int height, int pitch, bool withHost, float *devMem /In/ = NULL, SIP_PYOBJECT *hostMem /In/ = NULL) /KeywordArgs="All"/;</font></div><div><font face="monospace, monospace"> %MethodCode</font></div><div><font face="monospace, monospace"> import_array();</font></div><div><font face="monospace, monospace"> //Interpret arg5 as a numpy array</font></div><div><font face="monospace, monospace"> PyObject *hostmem = PyArray_FROM_OTF(a5, NPY_CFLOAT, NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED | NPY_ARRAY_ENSUREARRAY);</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> //Check that the above worked and did not return a NULL pointer</font></div><div><font face="monospace, monospace"> if(hostmem == NULL){</font></div><div><font face="monospace, monospace"> Py_XDECREF(hostmem);</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> //Get a pointer to the data as C-types</font></div><div><font face="monospace, monospace"> float *x = (float*)PyArray_DATA(hostmem);</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> //Call the function</font></div><div><font face="monospace, monospace"> sipCpp->Allocate(a0, a1, a2, a3, NULL, x);</font></div><div><font face="monospace, monospace"> %End</font></div></div><div><font face="monospace, monospace"><br></font></div><div><font face="arial, helvetica, sans-serif">I have to dig a bit to see about getting the type dynamically from the PyObject, but otherwise I think that this is work.</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">Thanks!</font></div><div><font face="arial, helvetica, sans-serif">J</font></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Oct 4, 2016 at 1:38 AM, Phil Thompson <span dir="ltr"><<a href="mailto:phil@riverbankcomputing.com" target="_blank">phil@riverbankcomputing.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 4 Oct 2016, at 5:47 am, Jay L. <<a href="mailto:jlaura@asu.edu">jlaura@asu.edu</a>> wrote:<br>
><br>
> I have made some progress on this. My .sip file now includes:<br>
><br>
> 13 void Allocate(int width, int height, int pitch, bool withHost, float *devMem /In/ = NULL, SIP_PYOBJECT hostMem /In/ = NULL);<br>
> 14 %MethodCode<br>
> 15 Py_BEGIN_ALLOW_THREADS<br>
> 16 long length = a0 * a1;<br>
> 17 a5 = PyFloat_AsDouble(<wbr>sipConvertToArray(a5, "f", length, 0));<br>
> 18 sipCpp->Allocate(a0, a1, a2, a3, NULL, a5);<br>
> 19 Py_END_ALLOW_THREADS<br>
> 20 %End<br>
><br>
> This is, I believe the method that I should be using to grab the arguments, operate on the buffer and then pass the arguments on to the C++ method.<br>
><br>
> As Phil suggested, sipConvertToArray looks like way to go here. I pass in a memoryview (buffer) using ndarray.data. Using just sipConvertToArray results in an error saying that no known conversion from Py_OBJECT* to float* is known.<br>
><br>
> Above, I am trying to use PyFloat_AsDouble to make the conversion (that I believe needs to be made). This though, results in the same error. I have checked PyQt, PyKDE, PyQwt, and the QGIS project to see if those projects have examples of passing a numpy array in, but must be missing any examples.<br>
><br>
> Any additional suggestions much appreciated for getting numpy array data passed over to the C++ side.<br>
<br>
</span>No, you would use sipConvertToArray() to create a Python object that wrapped an array of floats. Here you want to get at the array of floats that is wrapped by whatever object numpy is giving you.<br>
<br>
You need to understand exactly what ndarray.data is. Don't get confused between memoryview objects and objects that support the buffer protocol. They are not the same. The former is a wrapper around the latter that gives access to the latter from Python.<br>
<br>
I would hope (but I don't know) that ndarray implements the buffer protocol and that the 'data' attribute is simply a memoryview of the ndarray. In which case you would specify the hostMem argument as SIP_PYBUFFER and pass in ndarray (and not ndarray.data). In your %MethodCode you would then use the standard Python C calls to check that the buffer provided has the correct layout and get it's address.<br>
<br>
If it's appropriate you should also calculate other arguments (like width and height) from the information provided by the buffer protocol rather than require the use to pass them in.<br>
<span class="HOEnZb"><font color="#888888"><br>
Phil</font></span></blockquote></div><br></div>