[Numpy-discussion] _import_array()

Mads Ipsen madsipsen@gmail....
Tue Feb 14 03:20:19 CST 2012


Hi,

The C++ module here is a class that's used by an OpenGL window, to 
extract data from numpy arrays and basically draw molecules whose 
coordinates are stored in numpy arrays.

The C++ module is accessed from Python using wrappers generated by swig. 
Our application may contain many active OpenGL windows, where each one 
of them contains an instance of the swig wrapped C++ module.  So the 
question is then:

* Should the constructor of each instance of the C++ class call 
import_array()?
* Should import_array() be called every time a method in the C++ class 
handles a numpy structure?
* Should import_array() only be called one time, namely when the main 
application is started?

Best regards,

Mads

On 14/02/2012 10:09, Travis Oliphant wrote:
> Technically, when you write an extension module you really should use 
> import_array(); in the init method of the extensions module.   This 
> ensures that the C-API is loaded so that the API -table is available 
> if your C++ code uses the C-API at all.
>
> In this case you are just using some #defines that access the NumPy 
> array structure, so it works without the import_array().   However, 
> this could change in future releases (i.e. PyArray_DIMS and 
> PyArray_DATA could become functions that are looked up in an API-table 
> that must be loaded by import_array() ).
>
> Best regards,
>
> -Travis
>
>
>
>
>
>
>
> On Feb 14, 2012, at 3:03 AM, Mads Ipsen wrote:
>
>> Hi,
>>
>> I have C++ module (OpenGL) that extracts data from numpy arrays. The 
>> interface is pure read-only: It never returns any Python objects but 
>> only extracts data from numpy arrays. Eg:
>>
>> #include "numpy/arrayobject.h"
>>
>> void PrimitiveManager::deleteAtoms(PyObject * numpy_indices)
>> {
>>     // Extract number of indices
>>     int const n = static_cast<int>(PyArray_DIMS(numpy_indices)[0]);
>>     long * const indices = (long *) PyArray_DATA(numpy_indices);
>>
>>     // Delete atoms in buffer
>>     for (int i = 0; i < n; ++i)
>>     {
>>         // Do stuff
>>     }
>> }
>>
>> Now, when I compile the code with g++, I get the following warning:
>>
>>   numpy/core/include/numpy/__multiarray_api.h:1532: warning: ‘int 
>> _import_array()’ defined but not used
>>
>> Do I need to call '_import_array()' somewhere? Am I doing something 
>> potentially nasty?
>>
>> Best regards,
>>
>> Mads
>>
>>
>>
>>
>>
>> -- 
>> +-----------------------------------------------------+
>> | Mads Ipsen                                          |
>> +----------------------+------------------------------+
>> | Gåsebæksvej 7, 4. tv |                              |
>> | DK-2500 Valby        | phone:          +45-29716388 |
>> | Denmark              | email:mads.ipsen@gmail.com  |
>> +----------------------+------------------------------+
>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org <mailto: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


-- 
+-----------------------------------------------------+
| Mads Ipsen                                          |
+----------------------+------------------------------+
| Gåsebæksvej 7, 4. tv |                              |
| DK-2500 Valby        | phone:          +45-29716388 |
| Denmark              | email:  mads.ipsen@gmail.com |
+----------------------+------------------------------+


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120214/4a89058b/attachment.html 


More information about the NumPy-Discussion mailing list