-
Notifications
You must be signed in to change notification settings - Fork 58
feat[cartesian]: 2D temporaries [Experimental] #2314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
FlorianDeconinck
merged 30 commits into
GridTools:main
from
FlorianDeconinck:cartesian/feature/2d_temporaries
Oct 24, 2025
Merged
Changes from 19 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
01bf96a
Frontend: allow FieldDescriptor to be used when defining temporaries …
FlorianDeconinck 5615911
Make `debug` backend respect temporaries cartesian sizes
FlorianDeconinck 79a0102
Update common `empty` method to pass down dims for temporaries
FlorianDeconinck 3fb1a02
Make `npir` backend respect temporaries cartesian sizes
FlorianDeconinck c62d5c6
Unit test
FlorianDeconinck 0f3b8dc
Solve type annotation against `gtscript`
FlorianDeconinck 684d8fa
Flag as experimental feature (lack of `gt:X` support)
FlorianDeconinck 5c3f84b
Fix `scalars to temps` pass in `numpy`
FlorianDeconinck 430de2c
Fix to `npir` dimensions resolution in slicing
FlorianDeconinck 1e0ca8e
Fix NPIR test to add `dimensions`
FlorianDeconinck a53ee1e
Fix proper calculation of `dimensions` when data dimensions are involved
FlorianDeconinck 5fc038f
Inject `dimensions` in `gtcpp.Temporary` with a default to 3D for now…
FlorianDeconinck 15e2278
Merge branch 'main' into cartesian/feature/2d_temporaries
FlorianDeconinck b1c595f
Cleaner appending dimensions
FlorianDeconinck f8bad86
Fix bad read on `from_gtscript`, better docs
FlorianDeconinck 991e85e
Merge remote-tracking branch 'fdeconinck/cartesian/feature/2d_tempora…
FlorianDeconinck a2f1e7e
Swap `Self` for `Domain` + __future__
FlorianDeconinck 90ed8c9
Fix `Domain.from_gtscript` proper translation of parallel axes
FlorianDeconinck ae53356
ADR
FlorianDeconinck 3b04477
Correcting ADR
FlorianDeconinck bc6e274
Merge branch 'main' into cartesian/feature/2d_temporaries
FlorianDeconinck 9dbf30e
Use `warn_experimental_feature`
FlorianDeconinck 43803f4
Move the Domain convert from gtscript.Axis into the frontend processor
FlorianDeconinck cbe5862
Add fv3 dynamics example for 3D temps that behave like 2D on read
FlorianDeconinck 411b591
Lint
FlorianDeconinck a92af2f
Typo
FlorianDeconinck 5fe3303
Typo on ADR
FlorianDeconinck 2d9772f
Merge branch 'main' into cartesian/feature/2d_temporaries
FlorianDeconinck 2c265c6
Merge `main` into `cartesian/feature/2d_temporaries`
FlorianDeconinck 752530c
Merge branch 'main' into cartesian/feature/2d_temporaries
FlorianDeconinck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
docs/development/ADRs/cartesian/experimental/2d-temporaries.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| # ⚠️ 2 Dimenions Temporaries | ||
|
|
||
| In the context of porting physics parametrizations, we have encoutered multiple examples of computation requiring a temporary 2D storage within a stencil. | ||
|
FlorianDeconinck marked this conversation as resolved.
Outdated
|
||
|
|
||
| We thus decided to expand the DLS be able to port these stencils. We accept the increase in DSL surface and test it as an [experimental feature](../experimental-features.md). | ||
|
|
||
| ## Context | ||
|
|
||
| Porting physics parametrizations, we found temporary 2D field used, mostly when computing particular level (surface, phase change, etc.). Previous workaround was to define the temporaries as an argument. The goal is to undo this workaround that complexify calling code. | ||
|
FlorianDeconinck marked this conversation as resolved.
Outdated
FlorianDeconinck marked this conversation as resolved.
Outdated
|
||
|
|
||
| Use case in GEOS's shallow convection parametrization, `total_pwo_solid_phase` will be used later in the stencil but not out of it. | ||
|
FlorianDeconinck marked this conversation as resolved.
Outdated
|
||
|
|
||
| ```python | ||
| with computation(FORWARD), interval(...): | ||
| if MELT_GLAC == 1 and cumulus == 1: | ||
| if K <= ktf-1: | ||
| if ierr == 0: | ||
| dp = 100.*(po_cup-po_cup[0,0,1])\ | ||
| pwo_eff = 0.5*(pwo+pwo[0,0,1]) | ||
| pwo_solid_phase = (1.-p_liq_ice)*pwo_eff | ||
| total_pwo_solid_phase = total_pwo_solid_phase+pwo_solid_phase*dp/constants.MAPL_GRAV | ||
| ``` | ||
|
|
||
| Use case in GEOS's microphysics. Vertical interpolation to a given level requires to compound pressure around it. Here `pb`, `pt` and `boolean_2d_mask` are all temporary 2D fields. | ||
|
|
||
| ```python | ||
| def vertical_interpolation( | ||
| field: FloatField, | ||
| interpolated_field: FloatFieldIJ, | ||
| p_interface_mb: FloatField, | ||
| target_pressure: Float, | ||
| pb: FloatFieldIJ, | ||
| pt: FloatFieldIJ, | ||
| boolean_2d_mask: BoolFieldIJ, | ||
| ): | ||
| """ | ||
| Interpolate to a specific vertical level. | ||
|
|
||
| Only works for non-interface fields. Must be constructed using Z_DIM. | ||
|
|
||
| Arguments: | ||
| field (in): three dimensional field to be interpolated to a specific pressure | ||
| interpolated_field (out): output two dimension field of interpolated values | ||
| p_interface_mb (in): interface pressure in mb | ||
| target_pressure (in): target pressure for interpolation in Pascals | ||
| pb (in): placeholder 2d quantity, can be removed onces 2d temporaries are available | ||
| pt (in): placeholder 2d quantity, can be removed onces 2d temporaries are available | ||
| boolean_2d_mask (in): boolean mask to track when each cell is modified | ||
| """ | ||
| # mask tracks which points have been touched. check later on ensures that every point has been touched | ||
| with computation(FORWARD), interval(0, 1): | ||
| boolean_2d_mask = False | ||
|
|
||
| with computation(FORWARD), interval(-1, None): | ||
| pb = 0.5 * (log(p_interface_mb * 100) + log(p_interface_mb[0, 0, 1] * 100)) | ||
|
|
||
| with computation(BACKWARD), interval(1, None): | ||
| pt = 0.5 * (log(p_interface_mb[0, 0, -1] * 100) + log(p_interface_mb * 100)) | ||
| if log(target_pressure) > pt and log(target_pressure) <= pb and not boolean_2d_mask: | ||
| al = (pb - log(target_pressure)) / (pb - pt) | ||
| interpolated_field = field[0, 0, -1] * al + field * (1.0 - al) | ||
| boolean_2d_mask = True | ||
| pb = pt | ||
|
|
||
| with computation(FORWARD), interval(-1, None): | ||
| pt = 0.5 * (log(p_interface_mb * 100) + log(p_interface_mb[0, 0, -1] * 100)) | ||
| pb = 0.5 * (log(p_interface_mb * 100) + log(p_interface_mb[0, 0, 1] * 100)) | ||
| if ( | ||
| log(target_pressure) > pb | ||
| and log(target_pressure) <= log(p_interface_mb[0, 0, 1] * 100) | ||
| and not boolean_2d_mask | ||
| ): | ||
| interpolated_field = field | ||
| boolean_2d_mask = True | ||
|
|
||
| # ensure every point was actually touched | ||
| if boolean_2d_mask == False: # noqa | ||
| interpolated_field = field | ||
|
|
||
| # reset masks and temporaries for later use | ||
| with computation(FORWARD), interval(0, 1): | ||
| boolean_2d_mask = False | ||
| pb = 0 | ||
| pt = 0 | ||
| ``` | ||
|
|
||
| 2D temporaries would clean those code and allow for futher optimization (scalarization) in all performance backend. | ||
|
FlorianDeconinck marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Decision | ||
|
|
||
| All the guardraisls for 2D temporaries are already in place: | ||
|
FlorianDeconinck marked this conversation as resolved.
Outdated
|
||
|
|
||
| - allocation can only be done under `interval == 1` and `computation` in `FORWARD` or `BACKWARD`, | ||
| - use of 2D fields are fully defined, using the above rules again. | ||
|
|
||
| The main change is on the frontend of stencils and proper forwarding of dimensions through to code genreation, except for `gt:X` backend (see [Consequences](#consequences)). | ||
|
FlorianDeconinck marked this conversation as resolved.
Outdated
|
||
|
|
||
| Type hint for temporaries have been introduced before for mixed precision e.g.: | ||
|
FlorianDeconinck marked this conversation as resolved.
Outdated
|
||
|
|
||
| ```python | ||
| with computation(PARALLEL), inteval(...): | ||
|
FlorianDeconinck marked this conversation as resolved.
Outdated
|
||
| tmp_3D_as_f32: float32 = 0 | ||
| ``` | ||
|
|
||
| We propose to extend this type hint to allow specification of the dimensions re-using the `FieldDescriptor`, e.g. | ||
|
|
||
| ```python | ||
| with computation(FORWARD), inteval(...): | ||
|
FlorianDeconinck marked this conversation as resolved.
Outdated
|
||
| tmp_2D_as_f32: Field[IJ, np.float64] = 0 | ||
| ``` | ||
|
|
||
| We also guard against any definition of other type of temporaries (e.g. 1D temporary) because they are ill-defined within `gtscript` which pre-suppose that the horizontal dimensions are always computed upon. | ||
|
FlorianDeconinck marked this conversation as resolved.
Outdated
|
||
|
|
||
| The type hint can be a little verbose, so we offer to expand upon the `dtypes` dictionnary present in stencil configuration to give a `str: type` pair that can be swapped at parsing time, as long as the type is a derivative of `FieldDescriptor` so the relevant informations can be retrieved. | ||
|
FlorianDeconinck marked this conversation as resolved.
Outdated
|
||
|
|
||
| ```python | ||
| @stencil(backend=..., dtype={"My2DType": Field[IJ, np.float64]}) | ||
| def the_stencil(...): | ||
| with computation(FORWARD), inteval(...): | ||
|
FlorianDeconinck marked this conversation as resolved.
Outdated
|
||
| tmp_2D: My2DType = 0 | ||
| ... | ||
| ``` | ||
|
|
||
| ## Consequences | ||
|
|
||
| Users of GT4Py can now defined 2D temporaries in-stencil. | ||
|
FlorianDeconinck marked this conversation as resolved.
Outdated
|
||
|
|
||
| There's one remaining hiccups: GridTools C++ does not natively offer 2D temporaries. GridTools pre-suppose that all computation of temporaries are done on the 3D grid at least - and won't allocate below that dimensionality. | ||
|
FlorianDeconinck marked this conversation as resolved.
Outdated
|
||
| This is a fair limitation since GridTools doesn't provide the guardrails against race condition that GT4Py does. We can circumvent this by creating a pool of buffers passed as arguments for `gt:X` backends - see [issue](https://github.com/GridTools/gt4py/issues/2322). | ||
|
|
||
| ## References | ||
|
|
||
| - [PR](https://github.com/GridTools/gt4py/pull/2314) PR where the proposal and the discussion occured. | ||
|
FlorianDeconinck marked this conversation as resolved.
Outdated
|
||
| - [Issue](https://github.com/GridTools/gt4py/issues/2322) to extend support to the `gt:X` backends | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.