---
# System prepended metadata

title: Field-based execution model
tags: [GTScript]

---

# Field-based execution model

###### tags: `GTScript`

## Unstructured functional executable frontend idea

- **Location** is a tag
- **NeighborHood\[FromLocation, ToLocation\]** describes **ToLocation** neighbors relative to **FromLocation** (there can be multiple non-identical Neighborhoods with the same FromLocation and ToLocation)
- **Field\[Location\]** is a function from index to **Value**
- **Stencil** takes **Connectivities** and **Fields** and returns **Fields** (Stencil has implicit **Location** property, defined by the Location of the returned Field, see example (1))
- **Connectivity\[NeighborHood\]** takes **Field\[NeighborHood.ToLocation\]** and returns **Accessor\[NeighborHood\]** (missing: generate higher rank accessors)
- **Accessor\[NeighborHood\]** takes **NeighborIdentifier** and returns **Field\[NeighborHood.FromLocation\]** (side note: **Accessor** is a **SparseField**?)
- **NeighborIdentifier** could be enum, offset, or index

#### (1) Why implicit Location for Stencil?
Take the following stencil *neighbor_sum*, it is generic for all locations

```python=
LocA = TypeVar('LocA')
LocB = TypeVar('LocB')

def neighbor_sum(neighbor_connectivity: Connectivity[LocA,LocB], inp: Field[LocB]) -> Field[LocA]:
    return sum_reduce(neighbor_connectivity(inp))
```

## For Cartesian

- **Field**
    - is a function from index to **Value**
    - is an **Accessor**
- **Stencil** takes **Fields** and returns **Fields**
- **Accessor** takes Offsets and returns (shifted) **Field**

## Notes

- Arithmetic operations for fields are per grid point

## Example
```python=
inp # is a field
inp+inp2 # is a field
inp+inp[I+2] # is a field...
```

## Another unstructured

- **Location** is a tag
- **Field\[T, Location\]** is a function from index to **Optional\<T\>**, where **T** is **Field** or **Value** (if we like we can distinguish Fields by their element type and call them **SparseField** and **ValueField** respectively)
- **ReducibleField** is a **Field** with additional property **MaxElements** (and for performance a property **HasOptionalElements**)
- **Stencil** takes **Stencils** and **Fields** and returns **Fields** of the same **Location** (TODO work on Locations)

### Notes
- **Connectivity** is a **Stencil** that takes a **Field** and returns a **SparseField**)
- **ReducibleFields** are needed if you want to implement a reductions over all neighbors.
- Every expression is a **Stencil** where all parameters are bound