[Matrix-SIG] Success with the other Fortran wrapping program.

Travis Oliphant Oliphant.Travis@mayo.edu
Mon, 19 Jul 1999 14:50:44 -0500 (CDT)


Recently on this list two Fortran wrapping programs have been presented.
I thought I would share a couple of observations of both codes and report
some results with Pearu's f2py code.

Paul Dubois has an excellent PyFort program which uses interface files to
generate relatively high-level wrappings of users Fortran code.  He has
done a nice job.

I have been involved with Pearu in development of another Fortran-wrapper
generator which has a somewhat different design philosophy which may be
more suitable for some uses.

Pearu's f2py program does not require interface definition files as it
gets the information directly from the Fortran code itself.  The resulting
wrapper is a direct translation to Python of the underlying Fortran, so
the same documentation applies.  The idea is to make available large
numbers of routines quickly to Python where higher-level calling sytnax
could be developed more quickly and generally.  The low-level code checks
the type and size of the arguments but little else.

We have implemented call_back functions in the same low-level manner so
that Fortran codes requiring call-back functions can be wrapped.  In this
case, however, the user must provide a Fortran delcaration statement of
the call-back function (similar to the python-interface definition file of
PyFort but without the extra features).  The advantage is that only one
declaration statement must be included for all functions to be wrapped
that use that statement.

As an example of the utility of the lower-level approach for some
problems, I have wrapped up all of lapack and quadpack and made those
routines available to Python using the following commands:

./f2py.py -m_quad -ffdefs ../multipack/quadpack/*.f
gcc -I/usr/include/python1.5 -c _quadmodule.c
g77 -shared -o _quadmodule.so _quadmodule.o -L../multipack/lib -lquadpack
-llinpack_lite -lblas -lmach

./f2py.py -m_lapack -ffdefs ../multipack/lapack/SRC/*.f
gcc -I/usr/include/python1.5 -c _lapackmodule.c
g77 -shared -o _lapackmodules.so _lapackmodule.o -L../multipack/lib
-llapack -lblas

The fdefs.py file looked like this:

func_defs={'lsoda':"""\
subroutine f(neq,t,y,ydot)
integer neq
real*8 t,y(neq),ydot(neq)
end
subroutine jac(neq,t,y,ml,mu,pd,nrowpd)
integer neq,nl,ml,mu,nrowpd
real*8 t,y(neq),pd(nrowpd,1)
end
""",
           'dq*':"""
real*8 function f(x)
real*8 x
end
real*8 function w(x)
real*8 x
end
""",
           'sg*':"""
logical function select(wr,wi)
real wr, wi
end
""",
           'dg*':"""
logical function select(wr,wi)
double precision wr, wi
end
""",
           'cg*':"""
logical function select(w)
complex w
end
""",
           'zg*':"""
logical function select(w)
complex*16 w
end
"""
}


I just thought some people would be interested in the two approaches to
wrapping Fortran code and how they differ and the benefits of the "no
interface file" for making lots of Fortran code available to Python
quickly.  With f2py it is not inconceivable to wrap up all of the Fortran
code at netlib.org for use with NumPy.

For practical use of these routines, perhaps a friendlier interface could
be written (in Python only) to make it easier to use some of these
functions.

Note that f2py could also be used to generate preliminary *.pyf files for
PyFort

Best,

Travis