Is it possible to pass Fortran derived data types to Python via C and SWIG?
![](https://secure.gravatar.com/avatar/725a50eff7b51f36402b946bf786393e.jpg?s=120&d=mm&r=g)
I have a Fortran 90 algorithm which uses a derived data type to return data, and I would like to make a python wrapper for this algorithm. I understand that f2py cannot wrap derived data types; is it possible to do so with a C interface for the Fortran algorithm and SWIG? I would have to pass the derived data type into a C struct and then to Python. Best Regard, John Salvatier
![](https://secure.gravatar.com/avatar/9820b5956634e5bbad7f4ed91a232822.jpg?s=120&d=mm&r=g)
John Salvatier wrote:
I have a Fortran 90 algorithm which uses a derived data type to return data, and I would like to make a python wrapper for this algorithm. I understand that f2py cannot wrap derived data types; is it possible to do so with a C interface for the Fortran algorithm and SWIG? I would have to pass the derived data type into a C struct and then to Python.
It is possible as long as you can pass the structure from fortran to C. I don't know anything about Fortran derived data types, but if it is a non trivial object (more than a set of fundamental types), I am afraid it will be difficult. Does F90 supports POD data ? Otherwise, you will need a scheme for marshalling your data from Fortran to C (to match exactly how the structure would look like in C at the binary level). David
![](https://secure.gravatar.com/avatar/60e03bd1fd9f2dbc750e0899b9e7e71d.jpg?s=120&d=mm&r=g)
2008/11/30 David Cournapeau <david@ar.media.kyoto-u.ac.jp>:
John Salvatier wrote:
I have a Fortran 90 algorithm which uses a derived data type to return data, and I would like to make a python wrapper for this algorithm. I understand that f2py cannot wrap derived data types; is it possible to do so with a C interface for the Fortran algorithm and SWIG? I would have to pass the derived data type into a C struct and then to Python.
It is possible as long as you can pass the structure from fortran to C. I don't know anything about Fortran derived data types, but if it is a non trivial object (more than a set of fundamental types), I am afraid it will be difficult. Does F90 supports POD data ? Otherwise, you will need a scheme for marshalling your data from Fortran to C (to match exactly how the structure would look like in C at the binary level).
David
I've read an article (I don't remember where though, possibly CiSE) that stated that it's really not an easy task, as each Fortran compiler can do as it pleases it. So depending on the compiler and the Fortran standard, it can be possible, or not. So as there are no guaranties, you should write a function that transforms the Fortran structure in several pieces that are then passed to the C function. Matthieu -- Information System Engineer, Ph.D. Website: http://matthieu-brucher.developpez.com/ Blogs: http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn: http://www.linkedin.com/in/matthieubrucher
![](https://secure.gravatar.com/avatar/937b51a36258062586d6a7afd25e0585.jpg?s=120&d=mm&r=g)
Matthieu Brucher <matthieu.brucher <at> gmail.com> writes:
2008/11/30 David Cournapeau <david <at> ar.media.kyoto-u.ac.jp>:
John Salvatier wrote:
I have a Fortran 90 algorithm which uses a derived data type to return data, and I would like to make a python wrapper for this algorithm. I understand that f2py cannot wrap derived data types; is it possible to do so with a C interface for the Fortran algorithm and SWIG? I would have to pass the derived data type into a C struct and then to Python.
It is possible as long as you can pass the structure from fortran to C. I don't know anything about Fortran derived data types, but if it is a non trivial object (more than a set of fundamental types), I am afraid it will be difficult. Does F90 supports POD data ? Otherwise, you will need a scheme for marshalling your data from Fortran to C (to match exactly how the structure would look like in C at the binary level).
David
I've read an article (I don't remember where though, possibly CiSE) that stated that it's really not an easy task, as each Fortran compiler can do as it pleases it. So depending on the compiler and the Fortran standard, it can be possible, or not. So as there are no guaranties, you should write a function that transforms the Fortran structure in several pieces that are then passed to the C function.
Matthieu
A feasible way to achieve this would be to write a Fortran wrapper around your routine(x) that decomposes your derived data type to standard types and exposes these in the interface. Than you can compose the derived data type again in the wrapper and pass it to the original routine. :: module geom type Point real :: x, y end type Point type Circle type (Point) :: Center real :: Radius end type Circle end module geom subroutine test(c) use geom type (Circle) :: c print*, c%Radius print*, c%Center%X print*, c%Center%Y end subroutine test subroutine w_test(x, y, r) use geom real :: x, y, z type (Circle) :: C c%Radius = r c%Center%X = x c%Center%Y = y call test(c) end subroutine w_test Wrapping w_test should be trivial using f2py Regards Berthold
![](https://secure.gravatar.com/avatar/67eadceb08f909ead3915fc9d9145a7d.jpg?s=120&d=mm&r=g)
John, this is something I've wanted to look at. Here is what I had planned to do, so there is no guarantee that it will actually work... The ISO_C_BINDING module is part of the 2003 standard and allows interoperability between C and Fortran (it is included in the latest gfortran compiler). It allows interoperability of Fortran derived types with C structures (with certain restrictions). For example, use iso_c_binding type, bind(c) :: mytype real(c_float) :: data integer(c_int) :: n end type is interoperable with typedef struct { float data; int n; } mytype Now, I am just guessing, but if such a module was built into a shared library, maybe it could be accessed from python using ctypes STRUCTURES. Regards, David On Sun, Nov 30, 2008 at 4:38 PM, Berthold Höllmann < berthold@xn--hllmanns-n4a.de> wrote:
Matthieu Brucher <matthieu.brucher <at> gmail.com> writes:
2008/11/30 David Cournapeau <david <at> ar.media.kyoto-u.ac.jp>:
John Salvatier wrote:
I have a Fortran 90 algorithm which uses a derived data type to return data, and I would like to make a python wrapper for this algorithm. I understand that f2py cannot wrap derived data types; is it possible to do so with a C interface for the Fortran algorithm and SWIG? I would have to pass the derived data type into a C struct and then to Python.
It is possible as long as you can pass the structure from fortran to C. I don't know anything about Fortran derived data types, but if it is a non trivial object (more than a set of fundamental types), I am afraid it will be difficult. Does F90 supports POD data ? Otherwise, you will need a scheme for marshalling your data from Fortran to C (to match exactly how the structure would look like in C at the binary level).
David
I've read an article (I don't remember where though, possibly CiSE) that stated that it's really not an easy task, as each Fortran compiler can do as it pleases it. So depending on the compiler and the Fortran standard, it can be possible, or not. So as there are no guaranties, you should write a function that transforms the Fortran structure in several pieces that are then passed to the C function.
Matthieu
A feasible way to achieve this would be to write a Fortran wrapper around your routine(x) that decomposes your derived data type to standard types and exposes these in the interface. Than you can compose the derived data type again in the wrapper and pass it to the original routine. ::
module geom type Point real :: x, y end type Point type Circle type (Point) :: Center real :: Radius end type Circle end module geom subroutine test(c) use geom type (Circle) :: c print*, c%Radius print*, c%Center%X print*, c%Center%Y end subroutine test subroutine w_test(x, y, r) use geom real :: x, y, z type (Circle) :: C c%Radius = r c%Center%X = x c%Center%Y = y call test(c) end subroutine w_test
Wrapping w_test should be trivial using f2py
Regards Berthold
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/ef51b6c49b0e2ee4c3bcced82868f4ec.jpg?s=120&d=mm&r=g)
Thank you all for your help. I think that writing a set of subroutines that creates derived data types from arguments passed to it and vice versa is probably my best bet, but I may try Huard's solution. On Dec 1, 9:58 am, "David Huard" <david.hu...@gmail.com> wrote:
John,
this is something I've wanted to look at. Here is what I had planned to do, so there is no guarantee that it will actually work...
The ISO_C_BINDING module is part of the 2003 standard and allows interoperability between C and Fortran (it is included in the latest gfortran compiler). It allows interoperability of Fortran derived types with C structures (with certain restrictions). For example,
use iso_c_binding type, bind(c) :: mytype real(c_float) :: data integer(c_int) :: n end type
is interoperable with
typedef struct { float data; int n;
} mytype
Now, I am just guessing, but if such a module was built into a shared library, maybe it could be accessed from python using ctypes STRUCTURES.
Regards,
David
On Sun, Nov 30, 2008 at 4:38 PM, Berthold Höllmann <
berth...@xn--hllmanns-n4a.de> wrote:
Matthieu Brucher <matthieu.brucher <at> gmail.com> writes:
2008/11/30 David Cournapeau <david <at> ar.media.kyoto-u.ac.jp>:
John Salvatier wrote:
I have a Fortran 90 algorithm which uses a derived data type to return data, and I would like to make a python wrapper for this algorithm. I understand that f2py cannot wrap derived data types; is it possible to do so with a C interface for the Fortran algorithm and SWIG? I would have to pass the derived data type into a C struct and then to Python.
It is possible as long as you can pass the structure from fortran to C. I don't know anything about Fortran derived data types, but if it is a non trivial object (more than a set of fundamental types), I am afraid it will be difficult. Does F90 supports POD data ? Otherwise, you will need a scheme for marshalling your data from Fortran to C (to match exactly how the structure would look like in C at the binary level).
David
I've read an article (I don't remember where though, possibly CiSE) that stated that it's really not an easy task, as each Fortran compiler can do as it pleases it. So depending on the compiler and the Fortran standard, it can be possible, or not. So as there are no guaranties, you should write a function that transforms the Fortran structure in several pieces that are then passed to the C function.
Matthieu
A feasible way to achieve this would be to write a Fortran wrapper around your routine(x) that decomposes your derived data type to standard types and exposes these in the interface. Than you can compose the derived data type again in the wrapper and pass it to the original routine. ::
module geom type Point real :: x, y end type Point type Circle type (Point) :: Center real :: Radius end type Circle end module geom subroutine test(c) use geom type (Circle) :: c print*, c%Radius print*, c%Center%X print*, c%Center%Y end subroutine test subroutine w_test(x, y, r) use geom real :: x, y, z type (Circle) :: C c%Radius = r c%Center%X = x c%Center%Y = y call test(c) end subroutine w_test
Wrapping w_test should be trivial using f2py
Regards Berthold
_______________________________________________ SciPy-user mailing list SciPy-u...@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
_______________________________________________ SciPy-user mailing list SciPy-u...@scipy.orghttp://projects.scipy.org/mailman/listinfo/scipy-user
participants (6)
-
Berthold Höllmann
-
David Cournapeau
-
David Huard
-
John Sal
-
John Salvatier
-
Matthieu Brucher