dolfin team mailing list archive
-
dolfin team
-
Mailing list archive
-
Message #16625
Re: [HG DOLFIN] Get typemap(in) for std::vector working.
Johan Hake wrote:
On Monday 16 November 2009 22:03:13 DOLFIN wrote:
One or more new changesets pushed to the primary dolfin repository.
A short summary of the last three changesets is included below.
changeset: 7459:739b7539d6eb
tag: tip
user: "Garth N. Wells <gnw20@xxxxxxxxx>"
date: Mon Nov 16 21:02:54 2009 +0000
files: dolfin/swig/numpy_typemaps.i dolfin/swig/std_vector_typemaps.i
site-packages/dolfin/expression.py description:
Get typemap(in) for std::vector working.
Seems to work but I don't know if it's any good.
(Resending the email as I forgot to send it using the correct email adress...)
I haven't got the time to look at it, but yes the provided typemaps do make a
copy, which is not optimal. I have have actually been a bit worried ;) that
you might change the interface of eval, and possible other methods too, from
double* to std::vector. In most perspectives it is much nicer to pass a
std::vector, not least for safety. However std::vector is more difficult to
interact with NumPy.
double* is easier, but my impression is that it's not particularly safe.
SWIG do provide it's own implementation of std::vector which more or less wrap
the c++ interface of a vector seamless. We have used this to pass arguments of
std:vector previously. However the amount of code that is produced and not
used is large (~1MB for DOLFIN version < 9.2). NumPy arrays and lists are also
much more convenient structures to work with in Python.
Does the SWIG implementation of std::vector involve copying? I expect
that it does because I don't see how one can create a
std::vector<double> from a list since that would require creating a
std::vector that points to someone else's (the list's) data. It would
break the std::vector design.
I see how it could work if one creates a DoubleVector rather than a
list, but that's ugly.
I have therefore decided to remove the dependency of std_vector.i by adding
our own typemaps for std::vector arguments. Each one of those typemaps do make
a copy. For now we have typmaps for primitives and pointers so each copy
should not be that bad. But for operations that is performed alot of times, it
might be a difference. But I doubt it would for the callback functionality, as
I assume just calling the python function would take most of the time.
Agree.
We could consider creating a NumPy array from the pointer to the first element
of the vector, as I have seen you have discussed on the list previously, for
other cases. We then have to consider the life time of the vector.
We need a std::vector that stores a shared_ptr to its data ;).
For
director typemaps that would probably be ok. For other "out" typemaps it might
not be ok.
I would be inclined to make a copy. It's safe and we can document it so
if one sends a NumPy array into a function that takes a
std::vector<double>&, and that function stores a reference to the
object, then it 's not the original NumPy array (confusing???).
Do we presently copy data between NumPy arrays and the sub-classes of
GenericVector?
Would it be an option to add, in C++, a small template wrapper class for
arrays that are supposed to go back and forth to Python via NumPy arrays?
Maybe. I see that this is what Hans Petter does in a chapter of one of
his books. Still, there are issues with ownership.
I guess in the end that memory management + pointers is always going to
be tough in a mixed-language environment.
Garth
Including basic operations so it will be useful also in C++.
Johan
changeset: 7458:f9d6aeab7271
user: "Garth N. Wells <gnw20@xxxxxxxxx>"
date: Mon Nov 16 19:06:12 2009 +0000
files: demo/function/eval/python/demo.py
site-packages/dolfin/expression.py description:
Comment out some dim checks in expression.py.
changeset: 7457:6dc505f97225
user: "Garth N. Wells <gnw20@xxxxxxxxx>"
date: Mon Nov 16 15:13:08 2009 +0000
files: demo/function/eval/python/demo.py dolfin/swig/docstrings.i
dolfin/swig/function_post.i dolfin/swig/function_pre.i
dolfin/swig/std_vector_typemaps.i description:
Add empty typemap for NumPy array to std::vector<double>.
----------------------------------------------------------------------
For more details, visit http://www.fenics.org/hg/dolfin
_______________________________________________
DOLFIN-dev mailing list
DOLFIN-dev@xxxxxxxxxx
http://www.fenics.org/mailman/listinfo/dolfin-dev
_______________________________________________
DOLFIN-dev mailing list
DOLFIN-dev@xxxxxxxxxx
http://www.fenics.org/mailman/listinfo/dolfin-dev
Follow ups
References