-
Notifications
You must be signed in to change notification settings - Fork 10
Move transformations (index2offset, transpose) from Fortran to Python #1119
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
Merged
Changes from 29 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
dc52a12
bindings: move swap from Fortran to Python
havogt 073f425
Apply suggestions from code review
havogt 37c21b2
update references
havogt 66c1bf1
Move vertoffset_gradp to Python
havogt 6f91160
cleanup flip
havogt 47e9c7a
fix references
havogt b085cb3
add tests and fix bugs
havogt 6bfed5d
fixes
havogt 491344c
fix test
havogt 1a54450
Merge branch 'main' into blueline_swap_rbf_vec_coeff_e_and_v
havogt bc8a73f
fix test
havogt cd4b75c
fix tests
havogt fa8def5
Merge branch 'blueline_swap_rbf_vec_coeff_e_and_v' into tmp_vertoffse…
havogt 2a23643
switch to serialized data v3
havogt 742336f
Update base.yml
havogt 64d3e59
resolve module name conflict
havogt 96a90bc
Merge branch 'main' into tmp_vertoffset_gradp_and_dim_swap3
havogt 9d865bd
limit test
havogt 27106a3
Merge branch 'tmp_vertoffset_gradp_and_dim_swap3' of github.com:C2SM/…
havogt c6a8ac9
Merge remote-tracking branch 'upstream/main' into tmp_vertoffset_grad…
havogt f6ba342
switch on/off download
havogt 2af2071
Merge remote-tracking branch 'upstream/main' into tmp_vertoffset_grad…
havogt 7bfbc6e
add comments and disable data download
havogt 92b6460
fix verification range
havogt 8162aef
fix accidental tuple
havogt 6ff540c
add missing exchange
havogt 5992e38
hack: index_field conversion
havogt ef3aa29
cleanup and document index2offset
havogt b34fe50
Merge branch 'main' into tmp_vertoffset_gradp_and_dim_swap3
havogt 5382941
cleanup exchange in zdiff_gradp
havogt 3d81bbd
renames
havogt 404bf7d
Apply suggestions from code review
havogt de1c4aa
address review comments
havogt 378234f
slightly improve dummy_exchange
havogt dff89b7
add doctest to index2offset
havogt 0057ef2
Merge branch 'tmp_vertoffset_gradp_and_dim_swap3' of github.com:C2SM/…
havogt 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
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
61 changes: 61 additions & 0 deletions
61
model/common/src/icon4py/model/common/utils/field_utils.py
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,61 @@ | ||
| # ICON4Py - ICON inspired code in Python and GT4Py | ||
| # | ||
| # Copyright (c) 2022-2024, ETH Zurich and MeteoSwiss | ||
| # All rights reserved. | ||
| # | ||
| # Please, refer to the LICENSE file in the root directory. | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| import array_api_compat | ||
| from gt4py import next as gtx | ||
| from gt4py.next import typing as gtx_typing | ||
|
|
||
|
|
||
| def flip(field: gtx.Field, dim: gtx.Dimension, allocator: gtx_typing.Allocator) -> gtx.Field: | ||
| """Flip a field along a given dimension. | ||
|
|
||
| Args: | ||
| field: The field to flip. | ||
| dim: The dimension along which to flip the field. | ||
| allocator: Allocator to use for the output field. | ||
jcanton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| # Note: `allocator` needs to be passed explicitly since GT4Py fields currently don't persist how they were allocated. | ||
| xp = array_api_compat.array_namespace(field.ndarray) | ||
| flipped_array = xp.flip(field.ndarray, axis=field.domain.dims.index(dim)) | ||
| return gtx.as_field(field.domain, flipped_array, allocator=allocator) | ||
|
|
||
|
|
||
| def index2offset( | ||
| index_field: gtx.Field, dim: gtx.Dimension, allocator: gtx_typing.Allocator | ||
| ) -> gtx.Field: | ||
| """Convert an index field to an offset field. | ||
havogt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Note: Additionally clips negative indices to become zero offset as Fortran initializes some indices with `0` (which corresponds to `-1` in Python) to indicate that they are not used. | ||
| As GT4Py's unstructured domain inference is incomplete and runs over the whole domain we might useout-of-bounds offsets in intermediate computations. | ||
havogt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Args: | ||
| index_field: Index field in Python indexing (0-based). | ||
| dim: The dimension along which to convert indices to offsets. | ||
| allocator: Allocator to use for the output field. | ||
| """ | ||
| # Note: `allocator` needs to be passed explicitly since GT4Py fields currently don't persist how they were allocated. | ||
| xp = array_api_compat.array_namespace(index_field.ndarray) | ||
|
|
||
| current_index = gtx.as_field( | ||
| gtx.Domain(index_field.domain[dim]), | ||
| xp.arange( | ||
| index_field.domain[dim].unit_range.start, | ||
| index_field.domain[dim].unit_range.stop, | ||
| dtype=index_field.ndarray.dtype, | ||
| ), | ||
| allocator=allocator, | ||
| ) | ||
| # use GT4Py's broadcasting and field arithmetic (includes clipping) | ||
| offset_field = gtx.where( # type: ignore[attr-defined] | ||
| index_field >= 0, # type: ignore[operator] | ||
| index_field - current_index, | ||
| 0, | ||
| ) | ||
|
|
||
| # if GT4Py embedded would propagate the allocator, we could avoid this extra conversion. | ||
| return gtx.as_field(offset_field.domain, offset_field.ndarray, allocator=allocator) | ||
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.
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.