Skip to content
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

Enable optional stencils and modules in Liskov #358

Merged
merged 83 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
46ce0e2
[optional modules] Add CLI option and liskov directives
Jan 22, 2024
7454a48
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
Jan 22, 2024
8ba115e
Update mo_advection_btraj_compute_o1_dsl
Jan 22, 2024
8c240d1
Pre-commit run
Jan 22, 2024
51f18fa
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
Jan 23, 2024
ebad3c6
Remove START OPTIONAL STENCIL directives; only optional arg.
Jan 24, 2024
e40db7f
Pre-commit fix
Jan 24, 2024
f3afd0c
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
Jan 24, 2024
7da3b7c
Merge remote-tracking branch 'origin' into optional_modules_liskov
Jan 25, 2024
07b6fd9
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
Jan 29, 2024
4bd6faa
Add advection unit tests in tox
Jan 30, 2024
59ea47c
Fix optional_module attribute in liskov
Jan 30, 2024
c6e9f68
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
Jan 30, 2024
d85eaf0
Change field name in set_zero_c_k
Jan 30, 2024
bd4c1b7
Adapt optional feature in liskov
Jan 30, 2024
9fb954d
Pre-commit fixes
Jan 30, 2024
bf03135
Use list comprehension again
samkellerhals Feb 7, 2024
c13d301
Special case string cleaning for DSL INSERT
samkellerhals Feb 7, 2024
1d416f9
improvements
huppd Feb 7, 2024
5f1d2b0
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
huppd Feb 7, 2024
9f169bf
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
huppd Feb 8, 2024
df96eeb
WIP
huppd Feb 15, 2024
93df4cd
clean up
huppd Feb 15, 2024
b5b7cc2
fix output name
huppd Feb 15, 2024
6703bdc
fix + unittest
huppd Feb 16, 2024
3733fb9
fix style
huppd Feb 16, 2024
15a4e8a
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
huppd Feb 16, 2024
ebb4211
Merge branch 'optional_modules_liskov' of github.com:C2SM/icon4py int…
huppd Feb 16, 2024
395ebcd
polish liskov
huppd Feb 19, 2024
9f20290
fix style
huppd Feb 19, 2024
7791db7
add to Readme
huppd Feb 19, 2024
b7760a2
fix test
huppd Feb 19, 2024
d989c66
fix test
huppd Feb 19, 2024
c69afa4
fix style
huppd Feb 19, 2024
e273bbe
fix style
huppd Feb 19, 2024
2b08d29
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
huppd Feb 20, 2024
c85bd8a
fix test
huppd Feb 20, 2024
967c38d
fix test
huppd Feb 20, 2024
eae09f1
fix style
huppd Feb 20, 2024
45ff5a3
addresse comments
huppd Feb 20, 2024
9b63734
fix style
huppd Feb 20, 2024
4125888
False doesnot work :(
huppd Feb 20, 2024
fb37d59
fix ty[es
huppd Feb 20, 2024
b9515e7
fix style
huppd Feb 20, 2024
5540690
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
huppd Feb 20, 2024
5173126
Add comma separated argument list for optional modules
samkellerhals Feb 20, 2024
dcab426
Add comma separated argument list for optional modules
samkellerhals Feb 20, 2024
6990c70
fix conflicts
samkellerhals Feb 20, 2024
435ca1d
Test that optional module is not part of serialised savepoints
samkellerhals Feb 20, 2024
9da0eb7
Refactor OptionalModulesTransformer
samkellerhals Feb 20, 2024
7d2321d
Skip slow stencils (move markers to correct place)
samkellerhals Feb 20, 2024
1bd9850
Pull marker fix
samkellerhals Feb 20, 2024
cef9bb7
Merge branch 'main' into optional_modules_liskov
samkellerhals Feb 20, 2024
a0724ae
rm stencil
huppd Feb 21, 2024
eecd6a6
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
huppd Feb 21, 2024
3dfcf52
Merge branch 'main' of github.com:C2SM/icon4py into optional_modules_…
samkellerhals Feb 22, 2024
07c6243
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
huppd Feb 26, 2024
cc72a57
fix style
huppd Feb 26, 2024
777db5f
Merge branch 'main' of github.com:C2SM/icon4py into optional_modules_…
samkellerhals Feb 27, 2024
7b0895e
Merge branch 'optional_modules_liskov' of github.com:C2SM/icon4py int…
samkellerhals Feb 27, 2024
f2ae727
update stencil
huppd Feb 28, 2024
fd96ec4
fix style
huppd Feb 28, 2024
5081390
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
huppd Mar 1, 2024
d63fdf8
add commentns
huppd Mar 1, 2024
f694769
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
huppd Mar 4, 2024
9ca874c
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
huppd Mar 11, 2024
6e30b3a
fix style
huppd Mar 11, 2024
9ecb726
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
huppd Apr 8, 2024
f3b72b0
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
huppd Apr 9, 2024
73672f6
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
huppd Apr 10, 2024
d9322d2
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
huppd Apr 15, 2024
a32f015
Merge remote-tracking branch 'origin/main' into optional_modules_liskov
huppd May 7, 2024
888936b
rm stencil
huppd May 7, 2024
8e95d61
fix division with zero
huppd May 8, 2024
55091fe
fix style
huppd May 8, 2024
00f1e3d
Merge branch 'main' of https://github.com/C2SM/icon4py into optional_…
nfarabullini May 13, 2024
27e2247
Merge branch 'optional_modules_liskov' of https://github.com/C2SM/ico…
nfarabullini May 13, 2024
bccc0e8
fix stencil
huppd May 14, 2024
ded02ac
fix 2
huppd May 14, 2024
6cd3ea1
dbg test
huppd May 14, 2024
5e0e069
change default
huppd May 15, 2024
33b884e
seperate liskov and utests
huppd May 15, 2024
b216479
rm utest
huppd May 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ def _hor_adv_stencil_01(
geofac_div: Field[[CEDim], float],
p_dtime: float,
) -> Field[[CellDim, KDim], float]:
tracer_new = (
tracer_new_hor = (
tracer_now * rhodz_now
- p_dtime
* deepatmo_divh
* neighbor_sum(p_mflx_tracer_h(C2E) * geofac_div(C2CE), axis=C2EDim)
) / rhodz_new

return tracer_new
return tracer_new_hor


@program
Expand All @@ -45,7 +45,7 @@ def hor_adv_stencil_01(
rhodz_now: Field[[CellDim, KDim], float],
rhodz_new: Field[[CellDim, KDim], float],
geofac_div: Field[[CEDim], float],
tracer_new: Field[[CellDim, KDim], float],
tracer_new_hor: Field[[CellDim, KDim], float],
p_dtime: float,
):
_hor_adv_stencil_01(
Expand All @@ -56,5 +56,5 @@ def hor_adv_stencil_01(
rhodz_new,
geofac_div,
p_dtime,
out=tracer_new,
out=tracer_new_hor,
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def _mo_advection_traj_btraj_compute_o1_dsl(
dual_normal_cell_2: Field[[ECDim], wpfloat],
p_dthalf: wpfloat,
) -> tuple[
Field[[EdgeDim, KDim], int32],
Field[[EdgeDim, KDim], int32],
Field[[EdgeDim, KDim], int32],
Field[[EdgeDim, KDim], vpfloat],
Expand All @@ -41,6 +42,7 @@ def _mo_advection_traj_btraj_compute_o1_dsl(
lvn_pos = where(p_vn > 0.0, True, False)

p_cell_idx = where(lvn_pos, cell_idx(E2EC[0]), cell_idx(E2EC[1]))
p_cell_rel_idx_dsl = where(lvn_pos, int32(0), int32(1))
p_cell_blk = where(lvn_pos, cell_blk(E2EC[0]), cell_blk(E2EC[1]))

z_ntdistv_bary_1 = -(
Expand All @@ -67,7 +69,13 @@ def _mo_advection_traj_btraj_compute_o1_dsl(
+ z_ntdistv_bary_2 * dual_normal_cell_2(E2EC[1]),
)

return p_cell_idx, p_cell_blk, astype(p_distv_bary_1, vpfloat), astype(p_distv_bary_2, vpfloat)
return (
p_cell_idx,
p_cell_rel_idx_dsl,
p_cell_blk,
astype(p_distv_bary_1, vpfloat),
astype(p_distv_bary_2, vpfloat),
)


@program(grid_type=GridType.UNSTRUCTURED)
Expand All @@ -83,6 +91,7 @@ def mo_advection_traj_btraj_compute_o1_dsl(
primal_normal_cell_2: Field[[ECDim], wpfloat],
dual_normal_cell_2: Field[[ECDim], wpfloat],
p_cell_idx: Field[[EdgeDim, KDim], int32],
p_cell_rel_idx_dsl: Field[[EdgeDim, KDim], int32],
p_cell_blk: Field[[EdgeDim, KDim], int32],
p_distv_bary_1: Field[[EdgeDim, KDim], vpfloat],
p_distv_bary_2: Field[[EdgeDim, KDim], vpfloat],
Expand All @@ -104,7 +113,13 @@ def mo_advection_traj_btraj_compute_o1_dsl(
primal_normal_cell_2,
dual_normal_cell_2,
p_dthalf,
out=(p_cell_idx, p_cell_blk, p_distv_bary_1, p_distv_bary_2),
out=(
p_cell_idx,
p_cell_rel_idx_dsl,
p_cell_blk,
p_distv_bary_1,
p_distv_bary_2,
),
domain={
EdgeDim: (horizontal_start, horizontal_end),
KDim: (vertical_start, vertical_end),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ def _set_zero_c_k() -> Field[[CellDim, KDim], float]:


@program(grid_type=GridType.UNSTRUCTURED)
def set_zero_c_k(field: Field[[CellDim, KDim], float]):
_set_zero_c_k(out=field)
def set_zero_c_k(z_cfl: Field[[CellDim, KDim], float]):
_set_zero_c_k(out=z_cfl)
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from gt4py.next import GridType
from gt4py.next.ffront.decorator import field_operator, program
from gt4py.next.ffront.fbuiltins import Field
from gt4py.next.ffront.fbuiltins import Field, broadcast, int32, where

from icon4py.model.common.dimension import CellDim, KDim, Koff


@field_operator
def _vert_adv_stencil_01(
def _vert_adv_stencil_01a(
tracer_now: Field[[CellDim, KDim], float],
rhodz_now: Field[[CellDim, KDim], float],
p_mflx_tracer_v: Field[[CellDim, KDim], float],
Expand All @@ -35,6 +35,42 @@ def _vert_adv_stencil_01(
return tracer_new


@field_operator
def _vert_adv_stencil_01(
tracer_now: Field[[CellDim, KDim], float],
rhodz_now: Field[[CellDim, KDim], float],
p_mflx_tracer_v: Field[[CellDim, KDim], float],
deepatmo_divzl: Field[[KDim], float],
deepatmo_divzu: Field[[KDim], float],
rhodz_new: Field[[CellDim, KDim], float],
k: Field[[KDim], int32],
p_dtime: float,
ivadv_tracer: int32,
iadv_slev_jt: int32,
) -> Field[[CellDim, KDim], float]:
k = broadcast(k, (CellDim, KDim))

tracer_new = (
where(
(iadv_slev_jt <= k),
_vert_adv_stencil_01a(
tracer_now,
rhodz_now,
p_mflx_tracer_v,
deepatmo_divzl,
deepatmo_divzu,
rhodz_new,
p_dtime,
),
tracer_now,
)
if (ivadv_tracer != int32(0))
else tracer_now
)

return tracer_new


@program(grid_type=GridType.UNSTRUCTURED)
def vert_adv_stencil_01(
tracer_now: Field[[CellDim, KDim], float],
Expand All @@ -43,8 +79,11 @@ def vert_adv_stencil_01(
deepatmo_divzl: Field[[KDim], float],
deepatmo_divzu: Field[[KDim], float],
rhodz_new: Field[[CellDim, KDim], float],
tracer_new: Field[[CellDim, KDim], float],
k: Field[[KDim], int32],
p_dtime: float,
ivadv_tracer: int32,
iadv_slev_jt: int32,
tracer_new: Field[[CellDim, KDim], float],
):
_vert_adv_stencil_01(
tracer_now,
Expand All @@ -53,6 +92,9 @@ def vert_adv_stencil_01(
deepatmo_divzl,
deepatmo_divzu,
rhodz_new,
k,
p_dtime,
ivadv_tracer,
iadv_slev_jt,
out=tracer_new,
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

import numpy as np
import pytest
from gt4py.next import as_field
from gt4py.next.ffront.fbuiltins import int32

from icon4py.model.atmosphere.advection.vert_adv_stencil_01 import vert_adv_stencil_01
from icon4py.model.common.dimension import CellDim, KDim
from icon4py.model.common.test_utils.helpers import StencilTest, random_field, zero_field
from icon4py.model.common.test_utils.helpers import StencilTest, _shape, random_field, zero_field


class TestVertAdvStencil01(StencilTest):
Expand All @@ -32,14 +34,29 @@ def reference(
deepatmo_divzl: np.array,
deepatmo_divzu: np.array,
rhodz_new: np.array,
p_dtime,
k: np.array,
ivadv_tracer: int32,
iadv_slev_jt: int32,
p_dtime: np.float64,
**kwargs,
) -> np.array:
tracer_new = (
tracer_now * rhodz_now
+ p_dtime
* (p_mflx_tracer_v[:, 1:] * deepatmo_divzl - p_mflx_tracer_v[:, :-1] * deepatmo_divzu)
) / rhodz_new
if ivadv_tracer != int32(0):
tracer_new = np.where(
(iadv_slev_jt <= k),
(
tracer_now * rhodz_now
+ p_dtime
* (
p_mflx_tracer_v[:, 1:] * deepatmo_divzl
- p_mflx_tracer_v[:, :-1] * deepatmo_divzu
)
)
/ rhodz_new,
tracer_now,
)
else:
tracer_new = tracer_now

return dict(tracer_new=tracer_new)

@pytest.fixture
Expand All @@ -50,7 +67,10 @@ def input_data(self, grid):
deepatmo_divzl = random_field(grid, KDim)
deepatmo_divzu = random_field(grid, KDim)
rhodz_new = random_field(grid, CellDim, KDim)
k = as_field((KDim,), np.arange(0, _shape(grid, KDim)[0], dtype=int32))
p_dtime = np.float64(5.0)
ivadv_tracer = int32(1)
iadv_slev_jt = int32(4)
tracer_new = zero_field(grid, CellDim, KDim)
return dict(
tracer_now=tracer_now,
Expand All @@ -59,6 +79,9 @@ def input_data(self, grid):
deepatmo_divzl=deepatmo_divzl,
deepatmo_divzu=deepatmo_divzu,
rhodz_new=rhodz_new,
k=k,
p_dtime=p_dtime,
ivadv_tracer=ivadv_tracer,
iadv_slev_jt=iadv_slev_jt,
tracer_new=tracer_new,
)
2 changes: 2 additions & 0 deletions model/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ allowlist_externals =
commands =
pytest -v -s -m "not slow_tests" --cov --cov-append atmosphere/diffusion/tests/diffusion_stencil_tests --benchmark-skip -n auto {posargs}
pytest -v -s -m "not slow_tests" --cov --cov-append atmosphere/dycore/tests/dycore_stencil_tests --benchmark-skip -n auto {posargs}
pytest -v -s -m "not slow_tests" --cov --cov-append atmosphere/advection/tests/stencil_tests --benchmark-skip -n auto {posargs}

[testenv:run_benchmarks]
commands =
pytest -s -m "not slow_tests" atmosphere/diffusion/tests/diffusion_stencil_tests --benchmark-only {posargs}
pytest -s -m "not slow_tests" atmosphere/dycore/tests/dycore_stencil_tests --benchmark-only {posargs}
pytest -s -m "not slow_tests" atmosphere/advection/tests/stencil_tests --benchmark-only {posargs}


[testenv:dev]
Expand Down
2 changes: 2 additions & 0 deletions tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ In addition, other optional keyword arguments are the following:

- `copies`: Takes a boolean string input, and controls whether before field copies should be made or not. If set to False only the `#ifdef __DSL_VERIFY` directive is generated. Defaults to true.<br><br>

- `optional_module`: Takes a boolean string input, and controls whether stencils is part of an optional module. Defaults to "None".<br><br>

#### `!$DSL END STENCIL()`

This directive denotes the end of a stencil. The required argument is `name`, which must match the name of the preceding `START STENCIL` directive.
Expand Down
9 changes: 8 additions & 1 deletion tools/src/icon4pytools/liskov/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ def main(ctx: click.Context) -> None:
default=True,
help="Adds fused or unfused stencils.",
)
@click.option(
"--enable-dsl-optional",
type=click.Choice(["advection", "graupel", "no"]),
samkellerhals marked this conversation as resolved.
Show resolved Hide resolved
default="no",
help="Specify the DSL optional module to enable.",
)
@click.argument(
"input_path",
type=click.Path(exists=True, dir_okay=False, resolve_path=True, path_type=pathlib.Path),
Expand All @@ -70,10 +76,11 @@ def integrate(
fused: bool,
profile: bool,
metadatagen: bool,
enable_dsl_optional: str,
) -> None:
mode = "integration"
iface = parse_fortran_file(input_path, output_path, mode)
iface_gt4py = process_stencils(iface, fused)
iface_gt4py = process_stencils(iface, fused, optional_modules_to_enable=enable_dsl_optional)
run_code_generation(
input_path,
output_path,
Expand Down
12 changes: 9 additions & 3 deletions tools/src/icon4pytools/liskov/codegen/integration/deserialise.py
huppd marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ def create_stencil_data(
bounds = self._make_bounds(named_args)
fields = self._make_fields(named_args, field_dimensions)
fields_w_tolerance = self._update_tolerances(named_args, fields)

deserialised.append(
dtype(
name=stencil_name,
Expand All @@ -305,14 +304,21 @@ def create_stencil_data(
return deserialised

def _pop_additional_attributes(
self, dtype: Type[StartStencilData | StartFusedStencilData], named_args: dict[str, Any]
self,
dtype: Type[StartStencilData | StartFusedStencilData],
named_args: dict[str, Any],
) -> dict:
"""Pop and return additional attributes specific to StartStencilData."""
additional_attrs = {}
if dtype == StartStencilData:
mergecopy = string_to_bool(pop_item_from_dict(named_args, "mergecopy", "false"))
copies = string_to_bool(pop_item_from_dict(named_args, "copies", "true"))
additional_attrs = {"mergecopy": mergecopy, "copies": copies}
optional_module = pop_item_from_dict(named_args, "optional_module", "None")
samkellerhals marked this conversation as resolved.
Show resolved Hide resolved
additional_attrs = {
"mergecopy": mergecopy,
"copies": copies,
"optional_module": optional_module,
}
return additional_attrs

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def _generate_start_stencil(self) -> None:
acc_present=stencil.acc_present,
mergecopy=stencil.mergecopy,
copies=stencil.copies,
optional_module=stencil.optional_module,
)
i += 2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class BaseStartStencilData(CodeGenInput):
class StartStencilData(BaseStartStencilData):
mergecopy: Optional[bool]
copies: Optional[bool]
optional_module: Optional[str]


@dataclass
Expand Down
Loading
Loading