-
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
Changes from all commits
dc52a12
073f425
37c21b2
66c1bf1
6f91160
47e9c7a
b085cb3
6bfed5d
491344c
1a54450
bc8a73f
cd4b75c
fa8def5
2a23643
742336f
64d3e59
96a90bc
9d865bd
27106a3
c6a8ac9
f6ba342
2af2071
7bfbc6e
92b6460
8162aef
6ff540c
5992e38
ef3aa29
b34fe50
5382941
3d81bbd
404bf7d
de1c4aa
378234f
dff89b7
0057ef2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -15,7 +15,7 @@ | |||||
| from icon4py.model.common.utils import data_allocation as data_alloc | ||||||
|
|
||||||
|
|
||||||
| def compute_zdiff_gradp_dsl( # noqa: PLR0912 [too-many-branches] | ||||||
| def compute_zdiff_gradp( # noqa: PLR0912 [too-many-branches] | ||||||
| e2c, | ||||||
| z_mc: data_alloc.NDArray, | ||||||
| c_lin_e: data_alloc.NDArray, | ||||||
|
|
@@ -129,6 +129,9 @@ def compute_zdiff_gradp_dsl( # noqa: PLR0912 [too-many-branches] | |||||
| break | ||||||
|
|
||||||
| vertoffset_gradp = vertidx_gradp - vertoffset_gradp | ||||||
| vertoffset_gradp[:horizontal_start_1, :, :] = 0.0 | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see why this line was added...
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could this be related to the test that verifies this over the entire domain instead of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed, I couldn't find this line in Fortran. My best guess is that is made the test work with the previous serialized data, but not sure... |
||||||
|
|
||||||
| # TODO(havogt): NumpyDataProvider needs to be extended to support implict exchange. | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
:(
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW, we're tracking this for the next cycle in #1136. |
||||||
| exchange(zdiff_gradp[:, 0, :]) | ||||||
| exchange(zdiff_gradp[:, 1, :]) | ||||||
|
|
||||||
| return zdiff_gradp, vertoffset_gradp | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # 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 use out-of-bounds offsets in intermediate computations. | ||
|
|
||
| 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. | ||
|
|
||
| Example: | ||
| >>> import numpy as np | ||
| >>> from gt4py import next as gtx | ||
| >>> from icon4py.model.common import dimension as dims | ||
| >>> index_field = gtx.as_field( | ||
| ... {dims.CellDim: range(2, 6)}, np.array([5, 3, 4, 2], dtype=gtx.int32) | ||
| ... ) | ||
| >>> result = index2offset(index_field, dims.CellDim, allocator=np) | ||
| >>> result.domain | ||
| Domain(dims=(Dimension(value='Cell', kind=<DimensionKind.HORIZONTAL: 'horizontal'>),), ranges=(UnitRange(2, 6),)) | ||
| >>> result.ndarray | ||
| array([ 3, 0, 0, -3], dtype=int32) | ||
| """ | ||
| # 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) | ||
Uh oh!
There was an error while loading. Please reload this page.