Fortran2CPP: Automating Fortran-to-C++ Migration using LLMs via Multi-Turn Dialogue and Dual-Agent Integration

Le Chen, Bin Lei, Dunzhi Zhou, Pei-Hung Lin, Chunhua Liao, Caiwen Ding, Ali JannesariarXiv 2024 · DOI

Abstract

High-Performance Computing (HPC) relies heavily on legacy Fortran code, which faces increasing maintenance challenges due to a shrinking expert pool and the lack of modern abstractions. We introduce Fortran2CPP, a novel framework that automates the migration of Fortran to C++ using a dual-agent system and a multi-turn dialogue dataset. By training models on the iterative process of translation, error-fixing, and unit testing, our approach achieves up to a 3.31x improvement in CodeBLEU scores and a 92% increase in compilation success.

Introduction

Fortran has powered scientific simulations for decades, but maintaining this legacy code is increasingly difficult. Large Language Models (LLMs) often struggle with Fortran due to two "data deserts": the scarcity of Fortran code in public repositories (comprising only 0.04% of GitHub) and a lack of high-quality parallel datasets pairing Fortran with C++. Fortran2CPP addresses these gaps by moving beyond simple one-shot translation toward an iterative, collaborative process.

Methodology: The Questioner-Solver Framework

The core of Fortran2CPP is a dual-agent system that simulates a collaborative programming environment, preventing the "lost in the middle" problem common in complex single-agent tasks.

Dual-Agent Architecture

The system employs two specialized roles:

  • The Questioner: Acts as an architect, assessing code state, planning next steps (e.g., unit test generation), and relaying environment feedback.
  • The Solver: Acts as the programmer, generating code, creating tests, and performing bug fixes based on the Questioner's prompts.
The dual-agent architecture featuring the Questioner and Solver roles.

Iterative Refinement Loop

The migration process follows a structured pipeline designed to ensure functional equivalence through feedback:

  1. Initial Translation: Direct conversion from Fortran to C++.
  2. Unit Test Generation: Creation of tests for both source and target code to ensure identical behavior.
  3. Compilation and Execution: Automated testing within a real environment using compilers and CLI tools.
  4. Error Fixing: Iterative refinement where agents analyze logs to resolve failures.
  5. Final Verification: A concluding check for functional equivalence before saving results.
The iterative refinement workflow of the Fortran2CPP framework.

Datasets and Evaluation

The researchers contributed two primary resources to the community:

  • Fortran2CPP Dataset: A collection of 11.7k multi-turn dialogues and 2,529 verified code pairs. This dataset captures the "back-and-forth" of fixing code, which is critical for training models to reason through translation errors.
  • HumanEval-Fortran: An extension of the HumanEval benchmark to Fortran, providing a new standard for evaluating translation accuracy.

Key Results

Fine-tuning open-weight models like DeepSeek-Coder (6.7B), CodeLlama (13B), and StarCoder (15.5B) on the dialogue dataset yielded dramatic performance gains. Syntactic accuracy (CodeBLEU) improved by up to 3.31x for StarCoder. Notably, a fine-tuned CodeLlama-13B outperformed GPT-4 Turbo in structural accuracy and compilation success on the HPC-Fortran-Cpp dataset, proving that specialized, high-quality data can outperform significantly larger general-purpose models.

Conclusion and Limitations

Fortran2CPP demonstrates that training LLMs on the process of translation and debugging is more effective than training on static code snippets. While the system significantly boosts migration success, limitations remain, such as the potential for LLM-generated unit tests to be flawed. Future work will focus on integrating more rigorous validation processes and expanding the dataset to include a wider variety of Fortran standards.

Summary updated

BibTeX
@misc{chen2024fortran,
  author = {Le Chen and Bin Lei and Dunzhi Zhou and Pei-Hung Lin and Chunhua Liao and Caiwen Ding and Ali Jannesari},
  title = {{Fortran2CPP: Automating Fortran-to-C++ Migration using LLMs via Multi-Turn Dialogue and Dual-Agent Integration}},
  year = {2024},
  eprint = {2412.19770},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2412.19770},
}
Figure