---
# System prepended metadata

title: 'Research: Prototype Python into Fortran integration'
tags: [functional cycle 11]

---

# Research: Prototype Python into Fortran integration

###### tags: `functional cycle 11`

- Appetite: about half a cycle
- Developer: Magdalena, (Enrique + Hannes for discussion, Abishek for MPI consulting?)


## Goal

Prototype calling GT4Py stencils from Fortran via Python.

In the transition from an ICON driven by Fortran to a ICON driven by Python, we might want to plug in larger pieces of Python code, e.g. the full Python dynamical core. Such a code might contain dispatching logic (outside of the GT4Py DSL).

## Ideas

Steps:

1. hello_world example
    
2. Map fortran fields to python (buffer protocol?) and call a GT4Py stencil
```python
def normal_python_function(field):
    gt4py_program(field)
```
3. Measure the Python overhead by comparing performance of (2.) vs directly calling the generated C++ code from Fortan. To measure the python overhead there are several steps to look at:
    * pure python overhead by doing nothing
        ```python
        def fun(a:np.ndarray, b:np.ndarray):
                b = a
       ```
       
    * copying
     ```python 
        def fun(a:np.ndarray, b:np.ndarray):
                b[:] = a[:]
    ```
       
    * gt4py 
        1. call a gt4py stencil
        2. create Fortran bindings to the generated code from (1.)
        3. extract the python module generated by gt4py in (1.) (Rico can help)
        4. create cffi wrapper to this (generated) python module
        5. create Fortran bindings for the cffi code

5. Investigate MPI from Fortran -> mpi4py
6. Optional: prototype annotated Python function to generate Fortran interface




## Progress
Code and findings: https://github.com/halungge/python_from_fortran

- [x] step 1: hello world examples for different libraries
- [x] step 2: mapping fortran field to python
    - [x] POC using CFFI
    - [x] POC using call_fort_py
    - [x] compare overhead of the two versions
- [ ] step 3: Python overhead
    - [x] compare pure python overhead with "empty function"
    - [x] compare field copying
    - [ ] compare gt4py 
- [x] step 4: Investigate MPI: https://github.com/halungge/python_from_fortran/tree/master/src/parallel
- [ ] step 5: generate Fortran interface from Python 
