Table of Contents
Fetching ...

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

Le Chen, Bin Lei, Dunzhi Zhou, Pei-Hung Lin, Chunhua Liao, Caiwen Ding, Ali Jannesari

TL;DR

This work tackles the challenge of translating legacy Fortran HPC code to C++ under data-scarce conditions by introducing Fortran2CPP, an LLM agent-based framework that leverages a dual-LLM Questioner-Solver to generate a rich multi-turn dialogue dataset. The dataset, comprising 11.7k prompt–response turns derived from 1.2k dialogues, enables iterative translation, unit testing, and error correction, which, when used to fine-tune open-weight LLMs, yields substantial gains in CodeBLEU and compilation success. The approach demonstrates that dialogue-informed training captures translation reasoning and repair workflows, outperforming code-pair-only baselines on multiple benchmarks and achieving competitive results with state-of-the-art models. The authors provide open-source datasets and models, highlighting the practical impact for HPC modernization and offering a scalable paradigm for translating code in low-resource languages.

Abstract

Translating legacy Fortran code into C++ is a crucial step in modernizing high-performance computing (HPC) applications. However, the scarcity of high-quality, parallel Fortran-to-C++ datasets and the limited domain-specific expertise in large language models (LLMs) present significant challenges for automated translation. In this paper, we introduce Fortran2CPP, a multi-turn dialogue dataset generated by a novel LLM agent-based approach that integrates a dual-LLM Questioner-Solver module to enhance translation accuracy. Our dataset comprises 11.7k dialogues capturing iterative feedback-decision workflows including code translation, compilation, execution, unit testing, and error-fixing. Using this dataset, we fine-tune several open-weight LLMs and achieve up to a 3.31x improvement in CodeBLEU scores and a 92\% increase in compilation success rate, demonstrating enhanced syntactic accuracy and functional reliability. Our findings highlight the value of dialogue-based LLM training for complex code translation tasks. The dataset and model have been open-sourced and are available on our public GitHub repository\footnote{\url{https://github.com/HPC-Fortran2CPP/Fortran2Cpp}}.

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

TL;DR

This work tackles the challenge of translating legacy Fortran HPC code to C++ under data-scarce conditions by introducing Fortran2CPP, an LLM agent-based framework that leverages a dual-LLM Questioner-Solver to generate a rich multi-turn dialogue dataset. The dataset, comprising 11.7k prompt–response turns derived from 1.2k dialogues, enables iterative translation, unit testing, and error correction, which, when used to fine-tune open-weight LLMs, yields substantial gains in CodeBLEU and compilation success. The approach demonstrates that dialogue-informed training captures translation reasoning and repair workflows, outperforming code-pair-only baselines on multiple benchmarks and achieving competitive results with state-of-the-art models. The authors provide open-source datasets and models, highlighting the practical impact for HPC modernization and offering a scalable paradigm for translating code in low-resource languages.

Abstract

Translating legacy Fortran code into C++ is a crucial step in modernizing high-performance computing (HPC) applications. However, the scarcity of high-quality, parallel Fortran-to-C++ datasets and the limited domain-specific expertise in large language models (LLMs) present significant challenges for automated translation. In this paper, we introduce Fortran2CPP, a multi-turn dialogue dataset generated by a novel LLM agent-based approach that integrates a dual-LLM Questioner-Solver module to enhance translation accuracy. Our dataset comprises 11.7k dialogues capturing iterative feedback-decision workflows including code translation, compilation, execution, unit testing, and error-fixing. Using this dataset, we fine-tune several open-weight LLMs and achieve up to a 3.31x improvement in CodeBLEU scores and a 92\% increase in compilation success rate, demonstrating enhanced syntactic accuracy and functional reliability. Our findings highlight the value of dialogue-based LLM training for complex code translation tasks. The dataset and model have been open-sourced and are available on our public GitHub repository\footnote{\url{https://github.com/HPC-Fortran2CPP/Fortran2Cpp}}.
Paper Structure (27 sections, 4 equations, 5 figures, 6 tables)

This paper contains 27 sections, 4 equations, 5 figures, 6 tables.

Figures (5)

  • Figure 1: The questioner-solver model serves as the core of LLM agents in our approach.
  • Figure 2: Overview of Dataset Generation Pipeline
  • Figure 3: Comparison of C++ and Fortran source file line count distribution.
  • Figure 4: Comparison of C++ and Fortran keyword histograms.
  • Figure 5: Comparison of C++ and Fortran keyword histograms.