Generic programming in Fortran

Martin Erwig, Zhe Fu, Ben PflaumPepm 2006 · DOI

TL;DR

This paper introduces Parametric Fortran, an extension to the Fortran programming language that enables generic programming by allowing arbitrary Fortran constructs to be parameterized. This significantly reduces code duplication and improves maintainability in scientific computing, particularly for complex simulation systems like ocean models.

What Problem Does This Paper Address?

Scientific computing, especially in fields like ocean modeling, often involves developing complex simulation programs that need to be adapted for various underlying models. These models can differ significantly in their data structures and representations. Traditionally, adapting a simulation program to a new model meant rewriting large portions of the code, a tedious, error-prone, and time-consuming process. Fortran, while dominant in scientific computing due to its performance, lacks robust support for generic programming, making this adaptation problem particularly acute. Existing solutions like defining well-defined interfaces require significant refactoring, and ad-hoc polymorphism in Fortran 90 is insufficient.

Methods

Parametric Fortran addresses this by introducing parameterization constructs that allow developers to write generic programs. These programs can then be translated into regular Fortran code tailored to specific models by providing values for the parameters. The paper highlights two key new features:

  • Accessors: These allow parameters to be structured as records, enabling access to individual fields within a parameter (e.g., p.f). This improves readability and reduces the number of explicit parameters needed.
  • List Parameters: This feature allows a parameter to represent a list of other parameters. This is crucial for handling situations where multiple similar entities (like state variables in a simulation) need to be processed, effectively eliminating code duplication through "copy-and-paste" methods.

The Parametric Fortran compiler is implemented in Haskell and uses a type-directed approach to transform parameterized Fortran code into concrete Fortran programs based on provided parameter values.

Datasets

Not discussed in the paper. The paper focuses on language design and implementation, using illustrative examples rather than specific datasets for evaluation.

Key Insights and Ideas

  • Fortran's need for generic programming: The paper strongly argues that Fortran, despite its performance advantages, is hindered by a lack of generic programming support, leading to significant maintenance burdens in scientific applications.
  • Parameterization beyond types: Parametric Fortran's genericity extends beyond just data types, allowing parameterization of arbitrary Fortran constructs, including declarations, statements, and expressions. This is essential for handling model-specific differences in scientific computing.
  • Accessors for cleaner parameter management: Structured parameters with accessors simplify the representation and use of complex parameter information, improving code clarity.
  • List parameters as a powerful tool against code duplication: This feature directly tackles the pervasive "copy-and-paste" problem in scientific code, enabling a single code fragment to be adapted for multiple entities.
  • Haskell as a metaprogramming engine: The use of Haskell for implementing the Parametric Fortran compiler leverages its strong type system and metaprogramming capabilities to manage the complex transformations required for program generation.

Key Results

The paper demonstrates the effectiveness of Parametric Fortran through examples like generic array summation and slicing. It highlights that the IOM system, developed using Parametric Fortran, has seen significant productivity gains, with developers maintaining a single codebase for multiple ocean models. The introduction of accessors and list parameters is shown to further reduce code size and improve maintainability, with estimates suggesting potential code size reductions of up to 30% in projects like WRF and 50% in the IOM project after applying list parameters.

Limitations and Future Work

The authors acknowledge that the current system does not guarantee type correctness for generated programs when parameter values are provided. They propose developing a dependent type system for Parametric Fortran to enforce constraints on parameter values, ensuring that generated Fortran programs are free of type errors. Future work also includes further exploration of how accessors and list parameters can be combined to enhance expressiveness and address complex scientific computing challenges.

Summary updated

BibTeX
@inproceedings{erwig2006generic,
  author = {Martin Erwig and Zhe Fu and Ben Pflaum},
  title = {{Generic programming in Fortran}},
  booktitle = {Pepm},
  year = {2006},
  doi = {10.1145/1111542.1111564},
}
Figure