Abstract: We describe an implementation of the Farfel Fortran AD extensions. These extensions integrate forward and reverse AD directly into the programming model, with attendant benefits to flexibility, modularity, and ease of use. The implementation we describe is a "prepreprocessor" that generates input to existing Fortran-based AD tools. In essence, blocks of code which are targeted for AD by Farfel constructs are put into subprograms which capture their lexical variable context, and these are closure-converted into top-level subprograms and specialized to eliminate EXTERNAL arguments, rendering them amenable to existing AD preprocessors, which are then invoked, possibly repeatedly if the AD is nested.
Introduction
Automatic Differentiation (AD) is a cornerstone of scientific engineering, essential for computing gradients and Jacobians in optimization and sensitivity analysis. However, the most widely used Fortran AD tools, such as ADIFOR and TAPENADE, often struggle with modern programming paradigms. They typically lack support for nested AD—where one differentiates a function that itself contains an AD routine—and lexical scoping, which allows functions to access variables from their parent context.
To address these limitations, we introduce FARFALLEN, a "prepreprocessor" designed to bring modern conveniences to the Fortran ecosystem. By implementing the FARFEL language extensions, FARFALLEN allows researchers to write clean, modular code that is automatically transformed into a format compatible with legacy AD engines.
The FARFALLEN Approach
The core innovation of FARFALLEN lies in its ability to bridge the gap between high-level modular code and the flat, explicit structure required by legacy tools. It achieves this through several key transformations:
- FARFEL Language Extensions: We introduce
ADF(forward mode) andADR(reverse mode) blocks. These constructs allow developers to mark specific code segments for differentiation without manual "code plumbing." - Closure Conversion: To support nested functions (a feature of ALGOL 60 but absent in standard Fortran 77), FARFALLEN "lifts" nested subprograms to the top level. It automatically adds the necessary arguments to pass "free variables" from the parent context to the lifted function.
- Specialization: Legacy AD tools often cannot process
EXTERNALprocedure arguments. FARFALLEN resolves this by creating specialized versions of subprograms for every unique call site, effectively "baking in" function pointers to satisfy the requirements of tools like TAPENADE. - Orchestration: FARFALLEN acts as a coordinator, generating the required Fortran code and shell scripts to invoke underlying AD tools in the correct sequence, particularly for complex nested dependencies.
Benchmarking and Performance
To evaluate the system, we utilized a concrete benchmark: finding the equilibrium of a two-player game with continuous scalar strategies. This problem is mathematically dense and requires nested optimization and differentiation, providing a rigorous test for the tool's ability to handle AD nesting.
We compared the execution time of code generated by FARFALLEN (using both TAPENADE and ADIFOR backends) against STALINGRAD, a highly optimized AD compiler for the Scheme-like language VLAD.
Results
The performance results indicate that while FARFALLEN relies on source-to-source transformations and general-purpose legacy tools, it remains remarkably competitive with dedicated AD compilers. For a benchmark of N=1000 iterations, the execution times were as follows:
As shown in the table, FARFALLEN + TAPENADE achieved a runtime of 6.97 seconds, only roughly 20% slower than the STALINGRAD baseline of 5.83 seconds. The FARFALLEN + ADIFOR configuration followed at 8.92 seconds. This overhead is a modest price for the significant gains in developer productivity and the ability to remain within the high-performance Fortran ecosystem.
Conclusion and Future Work
FARFALLEN demonstrates that modern functional programming techniques, such as closure conversion and lambda lifting, are highly practical for scientific computing in imperative languages like Fortran. By acting as a "force multiplier" for existing tools, it automates the tedious manual maneuvers previously required for complex AD tasks.
While the current implementation is a prototype with limitations in scalability and full Fortran 77 specification support, it serves as a successful proof of concept. Future work will focus on developing a production-quality version capable of handling large-scale engineering projects and expanding the range of supported Fortran features.
Summary updated