Forthon

David Grote DPGrote@lbl.gov

Python interface generator for Fortran based codes (a code development aid)

What is Forthon?

Forthon generates links between Fortran95 and Python. Python is a high level, object oriented, interactive and scripting language that allows a flexible and versatile interface to computational tools. The Forthon package generates the necessary wrapping code which allows access to the Fortran database and to the Fortran subroutines and functions. This provides a development package where the computationally intensive parts of a code can be written in efficient Fortran, and the high level controlling code can be written in the much more versatile Python language.

The developer creates an interface file that describes what part of the fortran is to be accessible from Python. Variables defined in Fortran modules can be made accessible, including scalars, arrays, and variables of derived type. A subset of the Fortran subroutines, as specified in the interface file, can be called from Python, with argument lists including scalars, arrays, and variables of derived type. Arrays can be statically dimensioned or dynamically dimensioned, in which case Fortran95 style pointers are used. Variables of derived type can be pointers, and derived types can have elements which are themselves derived types or pointers to derived types. A tool is included which will automatically compile the user's source code and the generated wrapping code into a Python module.

Forthon is licensed under the BSD license, enabling reuse with few restrictions.

Where to get it?

Forthon-0.8.17.tgz

Forthon is also available with git:

git clone https://github.com/dpgrote/Forthon.git

Installation

Forthon requires Numpy.

Of course, a Fortran95 compiler is required. The recommended compiler is gfortran. The following are also supported:

To install Forthon, run "python setup.py install".

Note that the python used to install Forthon must be the same python that will be used with Forthon. Also, note that you will need to have write access to the lib directory where the python modules are installed, and to the bin directory where the python executable is installed.

Method of Solution

Forthon will generate the wrapping code needed to access Fortran subroutines and data in Python, from a user created interface file that describes the Fortran95 modules and the subroutines to be accessed from Python. The listing of variables in the interface file includes variable type information, array dimensions, attributes, and documentation. Additionally, Fortran derived types can be described, and variables of derived type defined, any of which will be accessible from Python. Built into the wrapper are mechanisms for dealing with dynamically allocated arrays and pointers to derived types.

Using the interface file, Forthon generates both C and Fortran95 code which is compiled along with the user's source code to form the Python module. The C code contains wrapper routines which are called directly from Python and in turn call the appropriate Fortran routines. The wrapper processes the input arguments so that they can be passed into the Fortran routines. For multi-dimensional arrays, transposes are done if necessary to put the data in the correct ordering for Fortran. (Any changes made in the data are copied back into the original so they are seen at the Python level.) Other differences between Fortran and C, such as subroutine name mangling and passing of character string lengths, are handled automatically. The C code also contains a database of the variables in the Fortran modules. For each variable, the memory location of the data is stored. This data is updated as appropriate for dynamic arrays and pointers to derived types. The automatically generated Fortran code contains the definitions of the modules as defined in the interface file (this can be skipped if the modules are already defined), as well as other routines that carry out actions such as passing variable memory locations to the C and doing proper Fortran95 pointer assignments to data passed in from C.

At the Python level, for each interface file, a Python object is created that is used to access the information from Fortran; it uses Python's standard interface to get and set object attributes. Dynamic arrays and derived types can be assigned to, and new instances of variables of derived type can be created.

Tools are included for doing dynamic array allocation, allowing all of the dynamic arrays in a Fortran module to be allocated in one call. The sizes of dynamic arrays can then later be changed, preserving any existing data.

Tools are also available to write all (or a selection) of the data out to disk in a self-describing file format, allowing data to be retrieved for later use. Python modules, written by others, that wrap libraries for self-describing data such as HDF, are used.

The layout of the interface file was taken originally from the Basis package and extended.

Forthon attempts to be very portable

Forthon runs with Python versions 2 and 3.

Forthon runs on the major OS flavors: Unix, MacOSX, Windows

Forthon will run on most computers, from laptops, to workstations, to massively parallel supercomputers.

Limitations

Forthon does not support all features of Fortran95. Some of the major features not supported include the use of IntentIn and IntentOut attributes on subroutine arguments and functions which returns arrays. Also, Forthon can only process integer and real variables of kind that match the length of integers and reals used by Python, i.e. the same size in bytes as longs and doubles in C. These limitations may be removed in future versions.

Unique Features

Forthon makes use of interface files, which allows more information about variables to be given than by obtaining the information from the original Fortran. The information includes type, documentation, units, and attributes.

Forthon can wrap variables of Fortran derived type so they are accessible from Python.

Forthon also has database management tools to do such things as allocate groups of dynamic arrays and change there sizes while preserving any existing data.

Future directions

Implement arrays of derived type quantities.

Make the derived types picklable. (Already done!)

Parse the fortran source to gather additional information, or to replace parts or all of variable description file.

References

  1. P. F. Dubois et. al., ``The Basis System,'' LLNL Document M-225 (1988).
  2. D. P. Grote, A. Friedman, I. Haber, ``Methods used in WARP3d, a Three-Dimensional PIC/Accelerator Code'', Proceedings of the 1996 Computational Accelerator Physics Conference, AIP Conference Proceedings 391, p. 51.

David Grote DPGrote@lbl.gov

Forthon was developed at the Lawrence Livermore National Laboratory

UCRL-CODE-155927

Legal notice Last updated October 4, 2013