Skip to content

Commit

Permalink
[py2fgen]: support for Fortran granules (#402)
Browse files Browse the repository at this point in the history
feat: Enable GT4Py backend support and integrate Python diffusion module into ICON-DSL

- Added GT4Py backend support for both CPU and GPU platforms.
- Automated binding generation for embedding Python functions and GT4Py in Fortran.

Co-authored-by: abishekg7 <[email protected]>
  • Loading branch information
samkellerhals and abishekg7 authored Apr 12, 2024
1 parent 7d61b04 commit 6f6a460
Show file tree
Hide file tree
Showing 149 changed files with 2,234 additions and 837 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/icon4py-qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ jobs:
**/base-requirements-dev.txt
**/requirements.txt
**/requirements-dev.txt
- name: Install icon4py-model packages
working-directory: model
- name: Install all icon4py namespace packages
run: |
python -m pip install --upgrade pip setuptools wheel
python -m pip install -r ./requirements-dev.txt
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# nvidia
*.ncu-rep


### Custom ####
_build
_local
Expand All @@ -7,6 +11,7 @@ tmp
testdata
simple_mesh*.nc
/prof
.profile

### GT4Py ####
.gt_cache/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from enum import Enum
from typing import Final, Optional

import numpy as np
from gt4py.next import as_field
from gt4py.next.common import Dimension
from gt4py.next.ffront.fbuiltins import Field, int32
Expand Down Expand Up @@ -73,8 +72,7 @@
mo_intp_rbf_rbf_vec_interpol_vertex,
)
from icon4py.model.common.states.prognostic_state import PrognosticState
from icon4py.model.common.model_backend import backend

from icon4py.model.common.settings import xp

"""
Diffusion module ported from ICON mo_nh_diffusion.f90.
Expand Down Expand Up @@ -257,9 +255,6 @@ def _validate(self):
self.apply_to_temperature = False
self.apply_to_horizontal_wind = False
self.apply_to_vertical_wind = False
else:
self.apply_to_temperature = True
self.apply_to_horizontal_wind = True

if self.shear_type not in (
TurbulenceShearForcingType.VERTICAL_OF_HORIZONTAL_WIND,
Expand Down Expand Up @@ -474,7 +469,7 @@ def _allocate(*dims: Dimension):

def _index_field(dim: Dimension, size=None):
size = size if size else self.grid.size[dim]
return as_field((dim,), np.arange(size, dtype=int32))
return as_field((dim,), xp.arange(size, dtype=int32))

self.diff_multfac_vn = _allocate(KDim)

Expand All @@ -492,7 +487,7 @@ def _index_field(dim: Dimension, size=None):
self.horizontal_cell_index = _index_field(CellDim)
self.horizontal_edge_index = _index_field(EdgeDim)
self.w_tmp = as_field(
(CellDim, KDim), np.zeros((self.grid.num_cells, self.grid.num_levels + 1), dtype=float)
(CellDim, KDim), xp.zeros((self.grid.num_cells, self.grid.num_levels + 1), dtype=float)
)

def initial_run(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ class DiffusionInterpolationState:

@functools.cached_property
def geofac_n2s_c(self) -> Field[[CellDim], float]:
return as_field((CellDim,), data=self.geofac_n2s.asnumpy()[:, 0])
return as_field((CellDim,), data=self.geofac_n2s.ndarray[:, 0])

@functools.cached_property
def geofac_n2s_nbh(self) -> Field[[CECDim], float]:
geofac_nbh_ar = self.geofac_n2s.asnumpy()[:, 1:]
geofac_nbh_ar = self.geofac_n2s.ndarray[:, 1:]
old_shape = geofac_nbh_ar.shape
return as_field(
(CECDim,),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from typing import Tuple

import numpy as np
from gt4py.next import as_field
from gt4py.next.common import Dimension, Field, GridType
from gt4py.next.ffront.decorator import field_operator, program
from gt4py.next.ffront.fbuiltins import broadcast, int32, minimum

from icon4py.model.common.dimension import CellDim, EdgeDim, KDim, VertexDim
from icon4py.model.common.math.smagorinsky import _en_smag_fac_for_zero_nshift
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend, xp


# TODO(Magdalena): fix duplication: duplicated from test testutils/utils.py
def zero_field(grid, *dims: Dimension, dtype=float):
shapex = tuple(map(lambda x: grid.size[x], dims))
return as_field(dims, np.zeros(shapex, dtype=dtype))
return as_field(dims, xp.zeros(shapex, dtype=dtype))


@field_operator
Expand Down Expand Up @@ -186,8 +185,8 @@ def init_nabla2_factor_in_upper_damping_zone(
physcial_heights: vector of physical heights [m] of the height levels
"""
# TODO(Magdalena): fix with as_offset in gt4py
heights = physical_heights.asnumpy()
buffer = np.zeros(k_size)
heights = physical_heights.ndarray
buffer = xp.zeros(k_size)
buffer[1 : nrdmax + 1] = (
1.0
/ 12.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
_update_theta_and_exner,
)
from icon4py.model.common.dimension import CECDim, CEDim, CellDim, EdgeDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
)
from icon4py.model.atmosphere.diffusion.stencils.calculate_nabla4 import _calculate_nabla4
from icon4py.model.common.dimension import ECVDim, EdgeDim, KDim, VertexDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
_calculate_nabla2_for_w,
)
from icon4py.model.common.dimension import C2E2CODim, CellDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.next.ffront.fbuiltins import Field, astype, int32

from icon4py.model.common.dimension import EdgeDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from gt4py.next.ffront.fbuiltins import Field, astype, broadcast, int32, maximum

from icon4py.model.common.dimension import EdgeDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from gt4py.next.ffront.fbuiltins import Field, int32

from icon4py.model.common.dimension import EdgeDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.next.ffront.fbuiltins import Field, astype, int32, neighbor_sum

from icon4py.model.common.dimension import C2E2CO, C2E2CODim, CellDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.next.ffront.fbuiltins import Field, astype, broadcast, int32

from icon4py.model.common.dimension import CellDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
_temporary_fields_for_turbulence_diagnostics,
)
from icon4py.model.common.dimension import CEDim, CellDim, EdgeDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.next.ffront.fbuiltins import Field, astype

from icon4py.model.common.dimension import CellDim, KDim, Koff
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
_temporary_field_for_grid_point_cold_pools_enhancement,
)
from icon4py.model.common.dimension import CellDim, EdgeDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.next.ffront.fbuiltins import Field, astype, int32, neighbor_sum

from icon4py.model.common.dimension import C2E2CO, C2E2CODim, CellDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.next.ffront.fbuiltins import Field, astype, int32, maximum, minimum, sqrt

from icon4py.model.common.dimension import E2C2V, E2ECV, ECVDim, EdgeDim, KDim, VertexDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
_calculate_nabla2_of_theta,
)
from icon4py.model.common.dimension import CEDim, CellDim, EdgeDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.next.ffront.fbuiltins import Field, astype, int32, neighbor_sum

from icon4py.model.common.dimension import C2E2CO, C2E2CODim, CellDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.next.ffront.fbuiltins import Field, astype, int32

from icon4py.model.common.dimension import E2C, CellDim, EdgeDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.next.ffront.fbuiltins import Field, astype, int32, neighbor_sum

from icon4py.model.common.dimension import C2CE, C2E, C2EDim, CEDim, CellDim, EdgeDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.next.ffront.fbuiltins import Field, astype, int32

from icon4py.model.common.dimension import E2C2V, E2ECV, ECVDim, EdgeDim, KDim, VertexDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.next.ffront.fbuiltins import Field, int32, max_over, maximum

from icon4py.model.common.dimension import E2C, CellDim, E2CDim, EdgeDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.next.ffront.fbuiltins import Field, astype, int32, neighbor_sum, where

from icon4py.model.common.dimension import C2E2C, C2E2CDim, CellDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.next.ffront.fbuiltins import Field, astype, int32, neighbor_sum

from icon4py.model.common.dimension import C2CE, C2E, C2EDim, CEDim, CellDim, EdgeDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from gt4py.next.ffront.fbuiltins import Field, astype, int32, where

from icon4py.model.common.dimension import C2CEC, C2E2C, CECDim, CellDim, KDim, Koff
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.next.ffront.fbuiltins import Field, astype, int32

from icon4py.model.common.dimension import CellDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from icon4py.model.atmosphere.diffusion.diffusion_utils import scale_k
from icon4py.model.common.grid.horizontal import CellParams, EdgeParams
from icon4py.model.common.grid.vertical import VerticalModelParams
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.test_utils.datatest_utils import GLOBAL_EXPERIMENT, REGIONAL_EXPERIMENT
from icon4py.model.common.test_utils.helpers import dallclose
from icon4py.model.common.test_utils.reference_funcs import enhanced_smagorinski_factor_numpy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)
from icon4py.model.common.dimension import KDim, VertexDim
from icon4py.model.common.grid.simple import SimpleGrid
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.test_utils.helpers import random_field, zero_field

from .utils import construct_config, diff_multfac_vn_numpy, smag_limit_numpy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.next.ffront.fbuiltins import Field, int32

from icon4py.model.common.dimension import EdgeDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.next.ffront.fbuiltins import Field, astype, int32

from icon4py.model.common.dimension import CellDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.next.ffront.fbuiltins import Field, astype, int32

from icon4py.model.common.dimension import EdgeDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)

from icon4py.model.common.dimension import C2E2CO, C2E2CODim, CellDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
Koff,
VertexDim,
)
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from gt4py.next.ffront.fbuiltins import Field, astype, int32, neighbor_sum

from icon4py.model.common.dimension import C2CE, C2E, C2EDim, CEDim, CellDim, EdgeDim, KDim
from icon4py.model.common.model_backend import backend
from icon4py.model.common.settings import backend
from icon4py.model.common.type_alias import vpfloat, wpfloat


Expand Down
Loading

0 comments on commit 6f6a460

Please sign in to comment.