---
# System prepended metadata

title: Regions with modified temporaries
tags: [gt4py.cartesian]

---

# Regions with modified temporaries

## Related issues

Main issue: want to be able to read temporaries with offsets in regions that are not the offsets used over the whole domain. Specifically, we want "locational extents", which are not supported by the GridTools interface.

There are other patterns that can be expressed in gt4py, e.g. reading and writing from API fields back and forth, that cannot be expressed and solving this would solve that as well.

## Potential Solutions and Workarounds

### Functions that take offsets (a)

Could not sove the `copy_corners` issues, BUT that might not be as important once they are combined.

```
def function(x, ...):
    return x[0, 1, 0]
 
tmp = function(a[1, 0, 0], ...)
```

This would shift all inputs, but could also shift each individually.

What this solves:
- Makes "parallelizing" computation easier to avoid reading temporaries with offsets, but this comes at the price of extra computation/reads.

Follow-up questions:
- How much progress would we be able to make with this?

### Stencils inlined into other stencils (b)

```
def another_stencil(x, y):
    ...
    
def stencil(a, b, c):
    another_stencil(a, a)
    with compuation(PARALLEL):
        stmts...
```

Potential problem: Want to be able to call `another_stencil(a, a)` (aliasing arguments), but creating temporaries for the arguments can change the behavior.

Potential solution: All temporaries can be treated as if they were API fields, but that greatly limits what we could do with these, since creating temporaries and reading with offsets is a primary computation pattern.

### Use cooperative groups to synchronize

Prerequisite: do an exploration of the computational cost.

Problem: GridTools backends would require potentially significant, but plain CUDA backend could be implemented.

This is helped out by using smaller domains at edges and corners.

Can we agressively inline to the point of getting back to all API fields.
- May not always be possible: There are cases where there are cycles that cannot be broken.

### Split stencils into multiple gridtools stencils

Need to write all temporaries to API fields, and write out before end of stencil, then read in again.

Potential problem: dependency on frontend from backend: backends would require adding new statements

Prerequisites
- True memory pool in C++. Perhaps use Umpire?
- Write all API fields to temporaries. Does this get around the read with offset after write issues?

## Follow-ups

- Solve functions that take offsets. This seems to work in certain instances already?
- Explore converting all read-with-offset-after-writes to temporaries, then write back at end of computation.
- Make issue to allow stencils inlined into other stencils
- Answer question: Can we always inline to get all API fields?
- Can we share the gridtools memory pool across stencils
- Can we put multiple stencils into the same shared object?