Re: Numeric, %pythoncode, multiple inheritance and typemaps

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Numeric, %pythoncode, multiple inheritance and typemaps

Robert Lupton
I just went back to last months's thread on this topic, and decided that
I didn't know what the final decision was.  It ends with Marcelo saying
        "Oh, sorry, I miss the last part"
in one message followed by a C++ suggestion and
        "I forgot, another solution"
followed by a SWIG solution in another.  Both appear to supercede his
summary in a still earlier message.

Can someone provide a complete example of this "double porting" approach
to numPy and C/C++ data types?  It seems as if it'd be generally useful.

Ideal would be a small, complete example, rather than just the SWIG  
snippets.
Bill Spotz kindly pointed me at his Epetra code that started this, but
I don't think that the released version has implemented the discussion
on this list (and it's a biggish piece of code that I'd have to digest).

Yes, I could do this myself with some agony, and I probably shall if  
no-one
on the list has it near the tips of their fingers.

                                Robert



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: Re: Numeric, %pythoncode, multiple inheritance and typemaps

Marcelo Matus
I don't know, Bill never got back on that,

Bill, did you get something working?

Marcelo



Robert Lupton wrote:

> I just went back to last months's thread on this topic, and decided that
> I didn't know what the final decision was.  It ends with Marcelo saying
>     "Oh, sorry, I miss the last part"
> in one message followed by a C++ suggestion and
>     "I forgot, another solution"
> followed by a SWIG solution in another.  Both appear to supercede his
> summary in a still earlier message.
>
> Can someone provide a complete example of this "double porting" approach
> to numPy and C/C++ data types?  It seems as if it'd be generally useful.
>
> Ideal would be a small, complete example, rather than just the SWIG  
> snippets.
> Bill Spotz kindly pointed me at his Epetra code that started this, but
> I don't think that the released version has implemented the discussion
> on this list (and it's a biggish piece of code that I'd have to digest).
>
> Yes, I could do this myself with some agony, and I probably shall if  
> no-one
> on the list has it near the tips of their fingers.
>
>                 Robert
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc. Do you grep through log
> files
> for problems?  Stop!  Download the new AJAX search engine that makes
> searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
> http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
> _______________________________________________
> Swig-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/swig-user




-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: Re: Numeric, %pythoncode, multiple inheritance and typemaps

Bill Spotz
Yes, I did.  Details will follow, but I'm swamped right now ;-) and I  
'll have to go back and review what I did.

On Jan 5, 2006, at 7:46 PM, Marcelo Matus wrote:

> I don't know, Bill never got back on that,
>
> Bill, did you get something working?
>
> Marcelo
>
>
>
> Robert Lupton wrote:
>
>> I just went back to last months's thread on this topic, and  
>> decided that
>> I didn't know what the final decision was.  It ends with Marcelo  
>> saying
>>     "Oh, sorry, I miss the last part"
>> in one message followed by a C++ suggestion and
>>     "I forgot, another solution"
>> followed by a SWIG solution in another.  Both appear to supercede his
>> summary in a still earlier message.
>>
>> Can someone provide a complete example of this "double porting"  
>> approach
>> to numPy and C/C++ data types?  It seems as if it'd be generally  
>> useful.
>>
>> Ideal would be a small, complete example, rather than just the  
>> SWIG  snippets.
>> Bill Spotz kindly pointed me at his Epetra code that started this,  
>> but
>> I don't think that the released version has implemented the  
>> discussion
>> on this list (and it's a biggish piece of code that I'd have to  
>> digest).
>>
>> Yes, I could do this myself with some agony, and I probably shall  
>> if  no-one
>> on the list has it near the tips of their fingers.
>>
>>                 Robert
>>
>>
>>
>> -------------------------------------------------------
>> This SF.net email is sponsored by: Splunk Inc. Do you grep through  
>> log files
>> for problems?  Stop!  Download the new AJAX search engine that makes
>> searching your log files as easy as surfing the  web.  DOWNLOAD  
>> SPLUNK!
>> http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
>> _______________________________________________
>> Swig-user mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/swig-user

** Bill Spotz                                              **
** Sandia National Laboratories  Voice: (505)845-0170      **
** P.O. Box 5800                 Fax:   (505)284-5451      **
** Albuquerque, NM 87185-0316    Email: [hidden email] **





-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: Re: Numeric, %pythoncode, multiple inheritance and typemaps

Bill Spotz
Here is a summary of my problem and ultimate solution for wrapping  
vector-like objects and making them also inherit from Numeric arrays:

I have a C++ package called Epetra with a class for vectors in a
parallel, scientific computing context.  It looks some thing like
this:

----------
class Epetra_Map;  // Forward declaration

class Epetra_Vector {
public:
   Epetra_Vector(const Epetra_Map &);
   Epetra_Vector(const Epetra_Map &, double *);
   Epetra_Vector(const Epetra_Vector &);
   ~Epetra_Vector();
   // Some other methods here...

private:
   double * values;
   // Other private members...
};
----------

(Note that Epetra was written before namespaces were reliable across
all platforms and compilers we needed it to run on, hence the Epetra_
prefix.)

Here, Epetra_Map is supposed to be a parallel domain decomposition
object, that knows how many processors are being used and maps global
index values to local on-processor index values.  Epetra_Vector also has
a bunch of methods and operators that give us functionality we would
expect of scientific computing vectors.

I wish to wrap this class for python.  However, it does not have all
the bells and whistles of Numeric arrays . . . and Numeric arrays do
not have the parallelism support.  So what I really want ultimately is
a python class that wraps Epetra_Vector, but is also, via python
inheritance, a Numeric array.

My solution was to write a new class:

----------
class Epetra_NumPyVector : public Epetra_Vector {
public:
   Epetra_NumPyVector(const Epetra_Map &);
   Epetra_NumPyVector(const Epetra_Map &, PyObject *);
   Epetra_NumPyVector(PyObject *);
   Epetra_NumPyVector(const Epetra_Vector &);
   ~Epetra_NumPyVector();
   PyObject * ExtractView();
   // Possibly other methods here...
private:
   PyArrayObject * array;
};
----------

In the implementation, I write the constructors such that the base
Epetra_Vector is constructed and the member PyArrayObject is also
constructed (for the constructors with PyObject* arguments, I use
Numeric's PyArray_ContiguousFromObject() function) and make sure both
of the data buffer pointers (Epetra_Vector::values and array->data)
point to the same address.  My interface file looks like this:

----------
%module Epetra

%{
#include "Epetra_Map.h"
#include "Epetra_Vector.h"
#include "Epetra_NumPyVector.h"
%}

// For the python version, I remove the prefix and use the python
// namespace
%rename(Map        ) Epetra_Map;
%rename(NumPyVector) Epetra_NumPyVector;

%include "Epetra_Map.h"
%include "Epetra_Vector.h"
%include "Epetra_NumPyVector.h"

%pythoncode %{
from UserArray import *

class Vector(UserArray,NumPyVector):
     def __init__(self, *args):
         NumPyVector.__init__(self, *args)
         UserArray.__init__(self,self.ExtractView
(),'d',copy=False,savespace=False)
     def __setattr__(self, key, value):
         if key == "array":
             if key in self.__dict__:
                 raise AttributeError, "Cannot change Epetra.Vector  
array attribute"
         UserArray.__setattr__(self, key, value)
%}
----------

To make things a little clearer, here is how the classes get mapped
from C++ to python:

Epetra_Vector      -> Epetra.Epetra_Vector
Epetra_NumPyVector -> Epetra.NumPyVector
<None>             -> Epetra.Vector

Python users are supposed to use Epetra.Vector and ignore the first
two classes.

This worked pretty well for me until I came across a function that
returned a reference to Epetra_Vector.  SWIG, of course, wrapped the
return argument as an Epetra.Epetra_Vector, but what I really want is
for it to return an Epetra.Vector.

What I did, based on advice from Marcelo Matus, was modify the
interface file as follows:

----------
%{
...
struct Vector { };
%}

%rename(_AuxVector) Vector;
struct Vector { };
...
%pythoncode %{
...
_Epetra._AuxVector_swigregister(Vector)
%}
----------

Then I could write a typemap such as the following:

----------
%typemap(argout) Epetra_Vector*& OutVector {
   Epetra_NumPyVector * npmv;
   static swig_type_info *ty = SWIG_TypeQuery("Epetra_NumPyVector *");
   npmv = new Epetra_NumPyVector(**$1);
   $result = SWIG_NewPointerObj(npmv, ty, 1);
}
----------

This behaves the way I want it to: a function or method that returns
an Epetra_Vector*& in C++ returns an equivalent Epetra.Vector in
python.

** Bill Spotz                                              **
** Sandia National Laboratories  Voice: (505)845-0170      **
** P.O. Box 5800                 Fax:   (505)284-5451      **
** Albuquerque, NM 87185-0316    Email: [hidden email] **





-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user