NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

CG-Kit: Code Generation Toolkit for performant and maintainable variants of source code applied to Flash-X hydrodynamics simulations

Johann Rudi, Youngjun Lee, Aidan H. Chadha, Mohamed Wahib, Klaus Weide, Jared P. O'Neal, Anshu Dubey

TL;DR

CG-Kit is a new Python-based toolkit designed to solve the "portability nightmare" in high-performance computing (HPC). By using high-level "recipes" to generate optimized C++ and Fortran code, it allows researchers to maintain a single, concise codebase that can automatically adapt to different hardware architectures (like CPUs and GPUs) and algorithmic variants, reducing source code volume by over 60%.

What Problem Does This Paper Address?

In the era of "Exascale" computing, scientific software faces a massive challenge: heterogeneity. To get maximum performance, developers must write code for a dizzying array of hardware, from standard CPUs to various GPUs (NVIDIA, AMD, Intel).

Traditionally, this leads to two bad options:

  1. Code Bloat: Manually writing and maintaining separate versions of every algorithm for every hardware target.
  2. Complex Abstractions: Using C++ template metaprogramming (like Kokkos or RAJA) which, while powerful, can be incredibly difficult to debug and doesn't always handle differences at the algorithmic level well.

The authors target Flash-X, a massive multiphysics simulation framework. Maintaining its complex hydrodynamics solvers across different parallelization models (OpenMP, CUDA) and grid managers (AMReX, Paramesh) had become a significant maintenance burden.

Methods

The authors introduce CG-Kit, which moves the abstraction layer up from data structures to the algorithmic flow. It consists of three main components:

  • Parametrized Source Trees (PSTs): Unlike standard Abstract Syntax Trees (ASTs) used by compilers, which represent every tiny detail of a language, PSTs are simplified trees defined by the user. Developers add lightweight annotations (_connector, _link, _param) directly into their source code to define how pieces of code can be plugged together.
  • Control Flow Graphs (CFGs): These represent the logical steps of an algorithm and their dependencies as a Directed Acyclic Graph (DAG).
  • Recipes: This is the user interface. Written in Python, recipes allow developers to define the "what" and "how" of an algorithm. The recipe directs the construction of the CFG, which then "walks" the PST to stitch together the final, compilable source code.

This approach is language-agnostic, meaning it can generate both C++ and Fortran from the same high-level logic.

Datasets

As this is a systems and software architecture paper rather than a machine learning study, "datasets" refer to the codebases used for evaluation:

  • AXPY (Scalar-Vector Addition): A fundamental linear algebra operation used as a "hello world" for the toolkit to demonstrate generating five different parallel variants (OpenMP vs. CUDA, different memory access patterns).
  • Spark Solver: A production-grade hydrodynamics solver within the Flash-X framework. This served as the primary real-world benchmark for testing complex multiphysics logic.

Key Insights and Ideas

  • Algorithmic-Level Unification: Most portability tools focus on making data structures work everywhere. CG-Kit focuses on making the logic work everywhere. If a GPU version of an algorithm needs a completely different step than a CPU version (e.g., a specific memory transfer), the "recipe" handles that divergence cleanly.
  • Human-Readable Output: A major "aha" moment is the focus on the generated code's quality. CG-Kit produces code that looks like it was written by a human, with consistent indentation and optional "trace tags" that tell a developer exactly which template file produced which line of code. This makes debugging significantly easier than debugging template-heavy C++.
  • Separation of Concerns: The domain scientist writes the physics logic in templates, while the performance engineer writes the Python recipes to optimize how that logic is deployed on specific hardware.

Key Results

  • Massive Code Reduction: For the AXPY experiments, using the full CG-Kit toolchain resulted in a 64.7% reduction in the amount of input code a human has to maintain compared to the generated output.
  • Real-World Impact on Flash-X: When applied to the Spark hydrodynamics solver, the toolkit achieved a 66.1% reduction in Fortran source code.
  • Zero Performance Overhead: Because CG-Kit is a source-to-source generator, the final code is compiled directly by standard compilers (like gfortran or nvcc). There is no "runtime" penalty; the generated code is as fast as manually written, "hand-tuned" code.

Limitations and Future Work

  • Manual Annotation: Currently, developers must manually add PST annotations to their existing legacy code. While lightweight, this still requires an initial investment of effort.
  • Integration with Runtimes: The authors mention that while CG-Kit handles generating the code, they are still working on tighter integration with Milhoja, a runtime data movement tool, to better handle the orchestration of tasks on heterogeneous systems.
  • Complexity of Recipes: For extremely complex multiphysics interactions, the Python recipes themselves could become complex, potentially requiring better high-level abstractions within the toolkit.

Summary updated

BibTeX
@article{rudi2025cg,
  author = {Johann Rudi and Youngjun Lee and Aidan H. Chadha and Mohamed Wahib and Klaus Weide and Jared P. O'Neal and Anshu Dubey},
  title = {{CG-Kit: Code Generation Toolkit for performant and maintainable variants of source code applied to Flash-X hydrodynamics simulations}},
  journal = {Future Generation Computer Systems},
  year = {2025},
  doi = {10.1016/j.future.2024.107511},
}
Figure