Chapter 13. Interfacing with Fortran

Table of Contents
13.1. Overview
13.2. Name Binding
13.3. Datatype Mapping
13.4. Parameter Passing
13.5. Portability Issues

Providing a type-safe Sather interface to Fortran 77 is desirable for several reasons. There is a large body of well debugged and well tested high performance Fortran source code for various kinds of numerical computations. Many vendors provide versions of low level numerical Fortran libraries tuned for particular hardware platforms. Fortran 77 BLAS have become a de facto standard for the elementary vector and matrix operations. The external Fortran interface provides a standard mechanism for Fortran procedures and data to be accessed from Sather and vice versa. It enables a Sather programmer to exploit the wealth of available numerical software in a type safe and portable manner.

Several important issues need to be resolved to provide interoperability between Sather and Fortran. The issues are:

unnamedlink introduces the Sather/Fortran interface and provides a few illustrative examples. unnamedlink talks about binding Sather entities to corresponding Fortran entities. Section unnamedlink provides a mapping of "basic" Sather types to Fortran types. Section unnamedlink explains how arguments in a Sather call are passed to a Fortran procedure or function that implements the feature. Finally, section unnamedlink talks about various portability issues.

13.1. Overview

Sather 1.1 provides an interface to a superset of Fortran 77 (ANSI X3.9-1978). The interoperability with Fortran code is achieved with the help of external Fortran classes. External Fortran classes are used to implement a strongly typed bidirectional Sather/Fortran interface. The extended library provides a set of built-in classes corresponding to all Fortran 77 types. Signatures of all inter-language calls must contain only these built-in classes as argument or return types.

13.1.1. External Fortran Call Example

The keywords 'external' and 'FORTRAN' preceding 'class' indicate that some class features may be implemented externally in Fortran and some other features are compiled in a way that makes it possible to call them from Fortran. An example of a simple call to a Fortran function is given below

external FORTRAN class FOO is
   -- a feature with a missing body is implemented externally
   -- in Fortran.
   -- Fortran definition:
   -- ...

-- a call to an externally defined Fortran function
i:F_INTEGER := FOO::foo(#F_INTEGER(1), #F_INTEGER(2));
-- #F_INTEGER(1) creates a variable of Fortran type F_INTEGER and
--  initializes it to 1,
-- #F_INTEGER(2) does a similar job,but initializes a new variable to 2

F_INTEGER is a built-in type representing Fortran integers. A full list of builtin-in Fortran types will be given in unnamedlink. Standard libraries provide a set of constructors and conversion routines for conversion from Sather to Fortran types and vice versa. The definition of feature 'foo' in external class FOO looks similar to abstract signatures in abstract classes. The implementation of external classes methods without bodies is assumed to be given in a corresponding language (Fortran in the case of 'foo'.) Such abstract signatures specify the interface from Sather code to Fortran code.

13.1.2. Overall Organization

External Fortran classes are used to provide both Sather/Fortran and Fortran/Sather interfaces. External Fortran classes can contain methods of two kinds: bodyless routines indicating externally implemented features and methods with code bodies some of which could be called from Fortran code. External Fortran classes cannot be instantiated and exist only to provide a bidirectional interface from Sather to Fortran.

Only routines may have no body in the external Fortran classes (not iterators). Bodyess routines specify the interface for Sather code (both in external and "regular" Sather classes) to call Fortran code. They have Sather signatures corresponding to the Fortran functions and subroutines implementing these features. Calls to such routines are compiled using the Fortran style name binding and parameter passing convention. The full correspondence between Fortran 77 types and Sather built-in Fortran classes is given in unnamedlink.

Methods with bodies in external Fortran classes serve a dual purpose. Methods whose arguments and return types are a combination of Sather and external Fortran types are merely helper routines and iterators whose semantics is the same as that of regular routines and iterators. They could be called from any Sather or external classes and such calls support the Sather name binding and parameter passing convention. Code for such methods can contain all sorts of calls without restrictions.

If all argument types and a return type, if any, in a routine with a body are built-in Fortran types (e.g. F_INTEGER, F_REAL, etc.) , such routines are meant to be callable from Fortran. They are compiled using the Fortran name binding and parameter passing convention. In fact, they could be freely substituted for Fortran 77 subroutines and functions that perform the same functions. Such routines could be also called from Sather code, but these calls will also support the Fortran parameter passing convention which is often less efficient relative to regular Sather calls. There are no restrictions on the implementation of these function: they can freely use internally any methods implemented either in Sather or Fortran. Routines which are meant to be called from Fortran cannot be overloaded.

In the diagram, arrows indicate the direction of calls. For example, an arrow connecting Fortran classes with bodyless routines in External classes indicate calls in the regular Sather code to abstract routines in the external Fortran classes. The type of the arrow demonstrates that such calls are compiled using the Fortran style call name binding and parameter passing convention. On the other hand, calls from routines with bodies in the external Fortran classes into regular Sather classes are represented by a solid arrow which denotes the Sather call name binding and parameter passing convention.

-- This is a Fortran definition for FOO

external FORTRAN class FOO is
   -- this routine is implemented externally in Fortran and could
   -- be called in Sather like this: tmp::=FOO::foo(i,a,c)

   -- this routine could be called from both Sather and Fortran
   -- all calls to bar (either from Sather or Fortran) use the
   -- Fortran 77 parameter passing convention
   bar(i:F_INTEGER,a:F_REAL) is

   -- this routine can only be called in Sather since
   -- argument size has a Sather type
   helper(arr:F_ARRAY{F_INTEGER}, size:INT) is
      t::=foo(i,a,c);  --call uses Fortran parameter passing convention
      bar(t,a);        --Fortran convention, but implemented in Sather

In this example, a Fortran function implementing 'foo' is called in Sather code as if it were a regular Sather routine: FOO::foo(i,a,c). However, the call is generated using the Fortran name binding and parameter passing convention. Calls to 'bar' are compiled in a similar fashion; however, it could be called from both Sather or external Fortran code. Finally, 'helper' has both Sather and external Fortran types as arguments and therefore could be called from Sather code only.

Points to note