[Matrix-SIG] Pyfort Version 2 now available

Paul F. Dubois dubois1@llnl.gov
Fri, 10 Sep 1999 08:33:17 -0700


Version 2 of Pyfort, a tool that lets you extend Python with Fortran, is now
available. See http://xfiles.llnl.gov/python.htm.

To use Pyfort, you construct an input file in a Fortran-90 like syntax. This
input file contains interfaces for the functions in Fortran that are to be
called. Pyfort then constructs a C-language extension module that contains
the "glue" to turn Python calls into the calls to your Fortran.

Example:
     Fortran:
          function sum (n, x)   ! return sum of elements of x
          integer n
          real x(n)
          real sum
          ....implementation...
          end function sum

    Pyfort input:
         module modsum
               function sum (n, x) ! return sum of elements of x
               integer n = size(x)
               real x(n)
               real sum
               end function sum
         end module modsum

    Calling sum in Python:
         from modsum import sum
         x = [1., 2., 3.]    # or a Numeric array
         print sum(x)
         print sum.__doc__  #prints "return sum of elements of x"

pyfort -c compiler modsum.pyf produces modsummodule.c and modsum.txt; the
former is used to construct the Python extension and the latter documents
the Python calling sequence. If no comment is given, the doc string is the
calling sequence.

Users can declare output quantities by using the intent attribute:
         real, intent(out):: y(n)
Such an argument becomes an output of the function. If there are multiple
return values, a tuple is returned.

Intent "temporary" requests that an array be constructed and passed to the
Fortran as a work array.

Persons wanting a "raw" interface that is exactly like the Fortran routine
and which does as little as possible can achieve this by declaring any array
arguments or scalar outputs as intent(inout).

New for version 2:
      Module attributes NONE, TRANSPOSE (default), MIRROR control behavior
of multiple dimension intent(in) and intent(output) arrays.
           NONE -- pass argument as is; return array has transposed strides
to "look right" in Python.
           TRANSPOSE -- transpose the data area on input; return array has
transposed strides. A returned array subsequently used in another call as
input will not in fact ever be transposed.
           MIRROR -- the Fortran has been written with all the dimensions
reversed; check shapes backward and create output arrays with a backward
shape. No data movement ever required.

      Method set_pyfort_option available to set the default option.

      Each generated wrapper can take an optional extra argument of one of
these three attributes to override the default.

      Scalar input arguments can be given an initial value as an expression
involving constants, the Fortran 90 size operator,
other scalar input arguments that were not given an initial value, and
scalar input arguments that occur earlier in the argument list. See argument
"n" above, for example. The size operator can be size(a) or size(a,
dimensionnumber) (Fortran style, 1-based).

       Example: subroutine doit (n, m, x, k, y, z)
                         integer n = size(x, 1), m = size(x, 2) , k = n * m
                         real x(n, m)
                         real, intent(out):: y(k)
                         complex, intent(temporary):: z(n, m)

Python call: y = doit(x)

The purpose of this facility is to enable the construction of "nice"
interfaces without needing an additional Python layer in many cases.

Pyfort has been tested under Linux using the Portland Group (-c pgf77) and
g77 (-c g77) compilers, under Solaris (choose -c solaris) and SGI (-c
solaris works). Potential users should note that both a Fortran and C
compiler are needed. Pyfort has not yet been heavily used and bugs are
possible.

Documentation is in the file pyfort.pdf that comes with the package.

Pyfort has been released for free redistribution. See Legal.htm.
Questions to support@pcmdi.llnl.gov

Paul F. Dubois
Program for Climate Model Diagnosis and Intercomparison
Lawrence Livermore National Laboratory

<P><A HREF="http://xfiles.llnl.gov/python.htm">Pyfort 2</A> -
Python / Fortran connection tool.  (9 - Sep - 99)