---
# System prepended metadata

title: Prototype integration of ICON stencils with the functional model
tags: [cycle 4]

---

# Prototype integration of ICON stencils with the functional model

###### tags: `cycle 4`

Appetite: full cycle (Anton + ?), ? = e.g. 2 weeks of Hannes after vacation

## Background

### Functional model 

In the previous cycle we designed a functional model which covers the unstructured use-cases ("iterator view"). From the design, a prototype for the iterator view was developed that covers a low-level Python frontend, embedded execution in Python, translating frontend code to an intermediate representation, generating the frontend Python code from the intermediate representation and executing the generated code (round trip).
Additionally, a first toy C++ backend and a code generator for this C++ backend from the intermediate representation was developed.

### Unstructured prototype in GT4Py repository

In the last year, a prototype for unstructured was developed which is currently part of the GT4Py repository. It contains a DSL frontend in the spirit of the current GT4Py for Cartesian and a CUDA code generator. In spring, MeteoSwiss and CSCS had a 3-day ICON Hackathon to port some _Dusk_ stencils to this prototype and integrate them into ICON dycore.

## Goal

In this project we will develop a C++ CUDA backend for the iterator view and test it on selected ICON stencils.

The project will address the following problems:
- Reduce uncertainty regarding practicability of the functional model by integrating ICON stencils into the Fortran driver and discover potential design limitations.
- Having a first compiled backend integrated in a Fortran application allows prototyping and testing of performance optimizations in a realistic test environment.
- Establish a (very) preliminary performance baseline with the CUDA backend.

### ICON stencils

The following ICON stencils, which were already covered in the ICON Hackathon, will be implemented and tested. As we are not sure if we can paste ICON code here, we will refer to them only by name as used by MCH in their dusk implementation. 

- `nh_diff_05`
- `nh_diff_01`

The stencils contain weighted neighbor reductions and sparse fields.

Optional goal: implement a simple stencil containing horizontal indirection (used in some ICON stencils) as an experimental extension of the builtins in the model.

### Rabbit holes and no-goals

- The Python frontend is lower level than current GT4Py and is not meant to be the frontend for domain science users. This project will not work on any aspects of the frontend.
- The result of this project cannot cover all possible motifs that are found in ICON (or other models). There is a risk of trying to integrate too many features from the beginning. Therefore we need to focus on the patterns of the selected stencils first.
- The driver for the selected examples will be Fortran. The generated code will be manually integrated into ICON. Executing generated code from Python is not a goal of this project.

## Project plan

- Advance the naive C++ [toy model](https://github.com/GridTools/gridtools/pull/1643) to a prototype that covers motifs required by the selected stencils and implement a CUDA backend. (Anton, full cycle)
- Adapt the existing ICON bindings to the new representation and integrate the selected stencils into ICON Fortran code with the help of MCH developers. (E.g. Hannes (~2 weeks at the end of the cycle)?)



