Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into improve_liskov_styleg…
Browse files Browse the repository at this point in the history
…uide
  • Loading branch information
Mikael Stellio committed Apr 10, 2024
2 parents daf9550 + db65aee commit 89bf611
Show file tree
Hide file tree
Showing 14 changed files with 307 additions and 134 deletions.
3 changes: 2 additions & 1 deletion ci/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ benchmark_model_stencils:
extends: .test_template
stage: benchmark
script:
- tox -r -e run_benchmarks -c model/ -- --backend=$BACKEND --grid=$GRID --verbose
# force execution of tests where validation is expected to fail, because the reason for failure is wrong numpy reference
- tox -r -e run_benchmarks -c model/ -- --backend=$BACKEND --grid=$GRID --runxfail --verbose
parallel:
matrix:
- BACKEND: [gtfn_cpu, gtfn_gpu]
Expand Down
3 changes: 2 additions & 1 deletion ci/dace.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ benchmark_model_stencils:
# TODO(edopao): wait for next dace release to fix the inline_sdfgs simplify step
# - pip install dace==$DACE_VERSION
- pip install git+https://github.com/spcl/dace.git@$DACE_VERSION
- tox -r -e run_benchmarks -c model/ -- --backend=$BACKEND --grid=$GRID --verbose
# force execution of tests where validation is expected to fail, because the reason for failure is wrong numpy reference
- tox -r -e run_benchmarks -c model/ -- --backend=$BACKEND --grid=$GRID --runxfail --verbose
parallel:
matrix:
- BACKEND: [dace_cpu, dace_gpu]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def reference(

@pytest.fixture
def input_data(self, grid):
pytest.skip(
pytest.xfail(
"Verification of z_v_grad_w currently not working, because numpy version incorrect."
)
if isinstance(grid, IconGrid) and grid.limited_area:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def reference(

@pytest.fixture
def input_data(self, grid):
pytest.skip(
pytest.xfail(
"Verification of w_concorr_c currently not working, because numpy version is incorrect."
)
z_kin_hor_e = random_field(grid, EdgeDim, KDim)
Expand Down
17 changes: 11 additions & 6 deletions model/common/src/icon4py/model/common/grid/grid_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ def __init__(
self._grid: Optional[IconGrid] = None
self._file_name = grid_file

def __call__(self, on_gpu: bool = False):
def __call__(self, on_gpu: bool = False, limited_area=True):
dataset = self._read_gridfile(self._file_name)
_, grid = self._constuct_grid(dataset, on_gpu=on_gpu)
_, grid = self._constuct_grid(dataset, on_gpu=on_gpu, limited_area=limited_area)
self._grid = grid

def _read_gridfile(self, fname: str) -> Dataset:
Expand Down Expand Up @@ -326,9 +326,11 @@ def _get_index(self, dim: Dimension, start_marker: int, index_dict):
self._log.error(msg)
raise IconGridError(msg) from err

def _constuct_grid(self, dataset: Dataset, on_gpu: bool) -> tuple[UUID, IconGrid]:
def _constuct_grid(
self, dataset: Dataset, on_gpu: bool, limited_area: bool
) -> tuple[UUID, IconGrid]:
grid_id = UUID(dataset.getncattr(GridFile.PropertyName.GRID_ID))
return grid_id, self._from_grid_dataset(dataset, on_gpu=on_gpu)
return grid_id, self._from_grid_dataset(dataset, on_gpu=on_gpu, limited_area=limited_area)

def get_size(self, dim: Dimension):
if dim == VertexDim:
Expand All @@ -354,7 +356,7 @@ def _get_index_field(
field = field + self._transformation.get_offset_for_index_field(field)
return field

def _from_grid_dataset(self, dataset: Dataset, on_gpu: bool) -> IconGrid:
def _from_grid_dataset(self, dataset: Dataset, on_gpu: bool, limited_area=True) -> IconGrid:
reader = GridFile(dataset)
num_cells = reader.dimension(GridFile.DimensionName.CELL_NAME)
num_edges = reader.dimension(GridFile.DimensionName.EDGE_NAME)
Expand Down Expand Up @@ -386,7 +388,10 @@ def _from_grid_dataset(self, dataset: Dataset, on_gpu: bool) -> IconGrid:
) = self._read_grid_refinement_information(dataset)

config = GridConfig(
horizontal_config=grid_size, vertical_config=self._config, on_gpu=on_gpu
horizontal_config=grid_size,
vertical_config=self._config,
on_gpu=on_gpu,
limited_area=limited_area,
)
icon_grid = (
IconGrid()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,23 @@ def get_icon_grid_from_gridfile(experiment: str, on_gpu: bool = False) -> IconGr
"icon_grid_0013_R02B04_R.nc",
num_levels=GLOBAL_NUM_LEVELS,
on_gpu=on_gpu,
limited_area=False,
)
elif experiment == REGIONAL_EXPERIMENT:
return _load_from_gridfile(
REGIONAL_EXPERIMENT,
"grid.nc",
num_levels=MCH_CH_R04B09_LEVELS,
on_gpu=on_gpu,
limited_area=True,
)
else:
raise ValueError(f"Unknown experiment: {experiment}")


def _load_from_gridfile(file_path: str, filename: str, num_levels: int, on_gpu: bool) -> IconGrid:
def _load_from_gridfile(
file_path: str, filename: str, num_levels: int, on_gpu: bool, limited_area: bool
) -> IconGrid:
grid_file = GRIDS_PATH.joinpath(file_path, filename)
if not grid_file.exists():
from icon4py.model.common.test_utils.data_handling import download_and_extract
Expand All @@ -66,7 +70,7 @@ def _load_from_gridfile(file_path: str, filename: str, num_levels: int, on_gpu:
str(grid_file),
VerticalGridSize(num_levels),
)
gm(on_gpu=on_gpu)
gm(on_gpu=on_gpu, limited_area=limited_area)
return gm.get_grid()


Expand Down
4 changes: 2 additions & 2 deletions spack/gt4py-stable/spack.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
spack:
specs:
- py-icon4py@main%[email protected] ^[email protected].5%[email protected]
- py-icon4py@main%[email protected] ^[email protected].6%[email protected]
view: false
concretizer:
unify: true
develop:
py-icon4py:
spec: py-icon4py@main%[email protected] ^[email protected].5%[email protected]
spec: py-icon4py@main%[email protected] ^[email protected].6%[email protected]
path: ../../
158 changes: 135 additions & 23 deletions tools/src/icon4pytools/icon4pygen/bindings/codegen/f90.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class F90Generator(TemplatedGenerator):
contains
{{wrap_run_fun}}
{{wrap_run_and_verify_fun}}
{{wrap_setup_fun}}
end module
"""
Expand Down Expand Up @@ -95,10 +96,10 @@ class F90Generator(TemplatedGenerator):
"""
)

F90WrapRunFun = as_jinja(
F90WrapRunAndVerifyFun = as_jinja(
"""\
subroutine &
wrap_run_{{stencil_name}}( &
wrap_run_and_verify_{{stencil_name}}( &
{{params}}
)
use, intrinsic :: iso_c_binding
Expand All @@ -117,19 +118,43 @@ class F90Generator(TemplatedGenerator):
{{k_sizes_assignments}}
{{conditionals}}
!$ACC host_data use_device( &
{{openacc}}
{{host_data_run_and_verify}}
!$ACC )
#ifdef __DSL_VERIFY
call run_and_verify_{{stencil_name}} &
( &
{{run_ver_params}}
)
#else
call run_{{stencil_name}} &
( &
{{run_params}}
)
#endif
call run_and_verify_{{stencil_name}} &
( &
{{run_ver_params}}
)
!$ACC end host_data
end subroutine
"""
)

F90WrapRunFun = as_jinja(
"""\
subroutine &
wrap_run_{{stencil_name}}( &
{{params}}
)
use, intrinsic :: iso_c_binding
use openacc
{{binds}}
integer(c_int) :: vertical_start
integer(c_int) :: vertical_end
integer(c_int) :: horizontal_start
integer(c_int) :: horizontal_end
{{k_sizes}}
vertical_start = vertical_lower-1
vertical_end = vertical_upper
horizontal_start = horizontal_lower-1
horizontal_end = horizontal_upper
{{k_sizes_assignments}}
!$ACC host_data use_device( &
{{host_data_run}}
!$ACC )
call run_{{stencil_name}} &
( &
{{run_params}}
)
!$ACC end host_data
end subroutine
"""
Expand Down Expand Up @@ -163,7 +188,7 @@ class F90Generator(TemplatedGenerator):

F90Field = as_jinja("{{ name }}{% if suffix %}_{{ suffix }}{% endif %}")

F90OpenACCField = as_jinja("!$ACC {{ name }}{% if suffix %}_{{ suffix }}{% endif %}")
F90HostDataField = as_jinja("!$ACC {{ name }}{% if suffix %}_{{ suffix }}{% endif %}")

F90TypedField = as_jinja(
"{{ dtype }}, {% if dims %}{{ dims }},{% endif %} target {% if _this_node.optional %} , optional {% endif %}:: {{ name }}{% if suffix %}_{{ suffix }}{% endif %} "
Expand All @@ -185,7 +210,7 @@ class F90Field(eve.Node):
suffix: str = ""


class F90OpenACCField(F90Field):
class F90HostDataField(F90Field):
...


Expand Down Expand Up @@ -347,7 +372,7 @@ def __post_init__(self, *args: Any, **kwargs: Any) -> None:
self.binds = F90EntityList(fields=bind_fields)


class F90WrapRunFun(Node):
class F90WrapRunAndVerifyFun(Node):
stencil_name: str
all_fields: Sequence[Field]
out_fields: Sequence[Field]
Expand All @@ -358,7 +383,7 @@ class F90WrapRunFun(Node):
conditionals: F90EntityList = eve.datamodels.field(init=False)
k_sizes: F90EntityList = eve.datamodels.field(init=False)
k_sizes_assignments: F90EntityList = eve.datamodels.field(init=False)
openacc: F90EntityList = eve.datamodels.field(init=False)
host_data_run_and_verify: F90EntityList = eve.datamodels.field(init=False)
tol_decls: F90EntityList = eve.datamodels.field(init=False)
run_ver_params: F90EntityList = eve.datamodels.field(init=False)
run_params: F90EntityList = eve.datamodels.field(init=False)
Expand Down Expand Up @@ -418,10 +443,10 @@ def __post_init__(self, *args: Any, **kwargs: Any) -> None:
for short, long in [("rel", "RELATIVE"), ("abs", "ABSOLUTE")]
for field in self.tol_fields
]
open_acc_fields = [
F90OpenACCField(name=field.name) for field in self.all_fields if field.rank() != 0
host_data_run_and_verify_fields = [
F90HostDataField(name=field.name) for field in self.all_fields if field.rank() != 0
] + [
F90OpenACCField(name=field.name, suffix="before")
F90HostDataField(name=field.name, suffix="before")
for field in self.out_fields
if field.rank() != 0
]
Expand Down Expand Up @@ -475,13 +500,93 @@ def __post_init__(self, *args: Any, **kwargs: Any) -> None:
self.conditionals = F90EntityList(fields=cond_fields)
self.k_sizes = F90EntityList(fields=k_sizes_fields)
self.k_sizes_assignments = F90EntityList(fields=k_sizes_assignment_fields)
self.openacc = F90EntityList(fields=open_acc_fields, line_end=", &", line_end_last=" &")
self.host_data_run_and_verify = F90EntityList(
fields=host_data_run_and_verify_fields, line_end=", &", line_end_last=" &"
)
self.run_ver_params = F90EntityList(
fields=run_ver_param_fields, line_end=", &", line_end_last=" &"
)
self.run_params = F90EntityList(fields=run_param_fields, line_end=", &", line_end_last=" &")


class F90WrapRunFun(Node):
stencil_name: str
all_fields: Sequence[Field]
out_fields: Sequence[Field]

params: F90EntityList = eve.datamodels.field(init=False)
binds: F90EntityList = eve.datamodels.field(init=False)
k_sizes: F90EntityList = eve.datamodels.field(init=False)
k_sizes_assignments: F90EntityList = eve.datamodels.field(init=False)
host_data_run: F90EntityList = eve.datamodels.field(init=False)
run_params: F90EntityList = eve.datamodels.field(init=False)

def __post_init__(self, *args: Any, **kwargs: Any) -> None:
param_fields = [F90Field(name=field.name) for field in self.all_fields] + [
F90Field(name=name) for name in _DOMAIN_ARGS
]
bind_fields = (
[
F90TypedField(
name=field.name,
dtype=field.renderer.render_ctype("f90"),
dims=field.renderer.render_ranked_dim_string(),
)
for field in self.all_fields
]
+ [
F90TypedField(
name=field.name,
suffix="before",
dtype=field.renderer.render_ctype("f90"),
dims=field.renderer.render_ranked_dim_string(),
)
for field in self.out_fields
]
+ [
F90TypedField(name=name, dtype="integer(c_int)", dims="value")
for name in _DOMAIN_ARGS
]
)
k_sizes_fields = [
F90TypedField(name=field.name, suffix=s, dtype="integer")
for s in ["k_size"]
for field in self.out_fields
]
k_sizes_assignment_fields = [
F90Assignment(
left_side=f"{field.name}_k_size",
right_side=f"SIZE({field.name}, 2)",
)
for field in self.out_fields
]
host_data_run_fields = [
F90HostDataField(name=field.name) for field in self.all_fields if field.rank() != 0
]
run_param_fields = (
[F90Field(name=field.name) for field in self.all_fields]
+ [F90Field(name=field.name, suffix="k_size") for field in self.out_fields]
+ [
F90Field(name=name)
for name in [
"vertical_start",
"vertical_end",
"horizontal_start",
"horizontal_end",
]
]
)

self.params = F90EntityList(fields=param_fields, line_end=", &", line_end_last=" &")
self.binds = F90EntityList(fields=bind_fields)
self.k_sizes = F90EntityList(fields=k_sizes_fields)
self.k_sizes_assignments = F90EntityList(fields=k_sizes_assignment_fields)
self.host_data_run = F90EntityList(
fields=host_data_run_fields, line_end=", &", line_end_last=" &"
)
self.run_params = F90EntityList(fields=run_param_fields, line_end=", &", line_end_last=" &")


class F90WrapSetupFun(Node):
stencil_name: str
all_fields: Sequence[Field]
Expand Down Expand Up @@ -539,6 +644,7 @@ class F90File(Node):
run_fun: F90RunFun = eve.datamodels.field(init=False)
run_and_verify_fun: F90RunAndVerifyFun = eve.datamodels.field(init=False)
setup_fun: F90SetupFun = eve.datamodels.field(init=False)
wrap_run_and_verify_fun: F90WrapRunAndVerifyFun = eve.datamodels.field(init=False)
wrap_run_fun: F90WrapRunFun = eve.datamodels.field(init=False)
wrap_setup_fun: F90WrapSetupFun = eve.datamodels.field(init=False)

Expand All @@ -565,13 +671,19 @@ def __post_init__(self, *args: Any, **kwargs: Any) -> None:
out_fields=out_fields,
)

self.wrap_run_fun = F90WrapRunFun(
self.wrap_run_and_verify_fun = F90WrapRunAndVerifyFun(
stencil_name=self.stencil_name,
all_fields=all_fields,
out_fields=out_fields,
tol_fields=tol_fields,
)

self.wrap_run_fun = F90WrapRunFun(
stencil_name=self.stencil_name,
all_fields=all_fields,
out_fields=out_fields,
)

self.wrap_setup_fun = F90WrapSetupFun(
stencil_name=self.stencil_name,
all_fields=all_fields,
Expand Down
Loading

0 comments on commit 89bf611

Please sign in to comment.