steampunk heart
Uncategorized

Standard Fortran and C Interoperability

Fortran is a general-purpose, compiled imperative programming language that is especially suited to numeric computation and scientific computing.

Originally developed by IBM in the 1950s for scientific and engineering applications, FORTRAN came to dominate this area of programming early on and has been in continuous use for over six decades in computationally intensive areas such as numerical weather prediction, finite element analysis, computational fluid dynamics, geophysics, computational physics, crystallography and computational chemistry. It is a popular language for high-performance computing and is used for programs that benchmark and rank the world’s fastest supercomputers.

Fortran encompasses a lineage of versions, each of which evolved to add extensions to the language while usually retaining compatibility with prior versions. Successive versions have added support for structured programming and processing of character-based data (FORTRAN 77), array programming, modular programming and generic programming (Fortran 90), high performance Fortran (Fortran 95), object-oriented programming (Fortran 2003), concurrent programming (Fortran 2008), and native parallel computing capabilities (Coarray Fortran 2008/2018).

Although a 1968 journal article by the authors of BASIC already described FORTRAN as “old-fashioned”, Fortran has now been in use for several decades and there is a vast body of Fortran software in daily use throughout the scientific and engineering communities. Jay Pasachoff wrote in 1984 that “physics and astronomy students simply have to learn FORTRAN. So much exists in FORTRAN that it seems unlikely that scientists will change to Pascal, Modula-2, or whatever.” In 1993, Cecil E. Leith called FORTRAN the “mother tongue of scientific computing”, adding that its replacement by any other possible language “may remain a forlorn hope”.

Software for NASA probes Voyager 1 and Voyager 2 was originally written in FORTRAN 5, and later ported to FORTRAN 77. As of 25 September 2013, some of the software is still written in Fortran and some has been ported to C.

Portability was a problem in the early days because there was no agreed upon standard—not even IBM’s reference manual—and computer companies vied to differentiate their offerings from others by providing incompatible features. Standards have improved portability. The 1966 standard provided a reference syntax and semantics, but vendors continued to provide incompatible extensions. Although careful programmers were coming to realize that use of incompatible extensions caused expensive portability problems, and were therefore using programs such as The PFORT Verifier, it was not until after the 1977 standard, when the National Bureau of Standards (now NIST) published FIPS PUB 69, that processors purchased by the U.S. Government were required to diagnose extensions of the standard. Rather than offer two processors, essentially every compiler eventually had at least an option to diagnose extensions.

Incompatible extensions were not the only portability problem. For numerical calculations, it is important to take account of the characteristics of the arithmetic. This was addressed by Fox et al. in the context of the 1966 standard by the PORT library.

Standard Fortran and C Interoperability

The Intel® Fortran Compiler supports the Fortran standardized mechanism for allowing Fortran code to reliably communicate (or interoperate) with C code.

This mechanism includes a group of features for C interoperability, enabling mixed-language programming in a more portable manner.

The Fortran standard discusses interoperability in terms of a “companion C processor.” Each Fortran implementation is free to choose which C is its companion. Although the standard explicitly specifies a companion C (not C++) processor, you can use C++, as long as you use features that are compatible with C when interoperating with Fortran.

For Intel® Fortran, the supported C companion is the Intel® C++ Compiler or the Microsoft* Visual C++* Compiler on Windows*, and the Intel® C++ Compiler or gcc* on Linux* and macOS*.

The core principle of interoperability is that something should work the same way in Fortran as it does in C. In terms of interoperability, the following applies:

  • Fortran provides a way to reference procedures that are defined by the C programming language or procedures that are described by C prototypes, even if they are not actually defined by means of C.
  • Conversely, a procedure defined by a Fortran subprogram can be referenced by a function defined by means of C.
  • In addition, you can define global variables that are associated with C variables whose names have external linkage.
  • You can also declare Fortran variables, data structures and enumerations that correspond to similar declarations in C.

Note: The Fortran Automatic Coding System for the IBM 704 (15 October 1956) was the first programmer’s reference manual for Fortran

Please read these post to understand the following 4 articles.