Building ICON with ICON4Py#
ICON4Py provides Python-based substitutions for the dynamical core and horizontal diffusion components of ICON. ICON4Py is mainly tested on NVIDIA GPUs; it is not recommended for CPU-only runs. AMD GPU support is experimental.
Features#
Dynamical core (solve_nh in mo_solve_nonhydro.f90)#
ICON4Py replaces the dynamical core of ICON with a Python/GT4Py implementation. The substitution covers the non-hydrostatic solver, including velocity advection.
The following table lists the supported namelist options:
Parameter |
Supported Value(s) |
Description |
|---|---|---|
|
|
Time stepping scheme |
|
|
Rho-theta advection |
|
|
Pressure gradient method |
|
|
Rayleigh damping type |
|
|
Divergence damping order |
|
|
Divergence damping type |
|
|
Vertical nesting |
|
|
Deep atmosphere |
|
|
Extra diffusion near CFL limit for vertical advection |
Diffusion (diffusion in mo_nh_diffusion.f90)#
ICON4Py replaces the horizontal diffusion module.
The following table lists the supported namelist options:
Parameter |
Supported Value(s) |
Description |
|---|---|---|
|
|
Diffusion type |
|
|
Smagorinsky stencil type |
|
|
Temperature discretization |
|
|
Smagorinsky diffusion for w |
|
|
3D Smagorinsky coefficient computation |
|
|
Truly horizontal temperature diffusion over steep slopes |
|
|
Shear type |
|
|
Output flag for horizontal shear |
|
|
Type of physics forcing |
Dependencies#
For communication (halo exchanges), ICON4Py uses GHex, which requires mpi4py.
Installing ICON4Py#
ICON4Py requires Python 3.12 or newer.
The following instructions describe a manual installation. We recommend installing the required Python packages in a virtual environment in three steps:
Install mpi4py, which must be compatible with the MPI implementation used for ICON.
Install GHex with the flags relevant for the target architecture.
Install icon4py.
Installing mpi4py#
python3 -m pip install mpi4py
See the mpi4py documentation and the ComIn documentation for mpi4py for details. Make sure mpi4py is working as expected, e.g. by inspecting:
ldd $(python -c "import mpi4py.MPI; print(mpi4py.MPI.__file__)")
Depending on your MPI setup, you might need to install from source:
MPI4PY_BUILD_MPICC=<path to mpi wrapper> python3 -m pip install --no-binary=mpi4py --no-cache-dir mpi4py
Installing GHex#
GHex requires build-time flags to select the variant (e.g. GPU support). For a GPU-enabled build, the proper compiler must be available, e.g. nvcc for CUDA compilation.
export GHEX_USE_GPU=ON
export GHEX_GPU_TYPE=NVIDIA
export GHEX_GPU_ARCH=90 # GPU architecture for packing/unpacking kernels
export GHEX_TRANSPORT_BACKEND=MPI
python3 -m pip install ghex
GHex’s Python package uses CMake-based scikit-build-core. Additional build configuration options can be passed via environment variables:
export CXX=<path to C++ compiler> # GCC or Clang recommended
export CC=<path to C compiler> # GCC, Clang or NVHPC
export CUDA_PATH=<path to CUDA installation>
export BOOST_ROOT=<path to boost installation> # header-only install is sufficient; see https://github.com/ghex-org/GHEX for the minimum required version
export MPI_HOME=<path to MPI installation>
Installing ICON4Py#
Install ICON4Py, picking the extra that matches your build. externals/icon4py/constraints.txt pins the ICON4Py and DaCe versions tested with the current ICON sources.
python3 -m pip install icon4py-bindings -c externals/icon4py/constraints.txt # CPU only
python3 -m pip install icon4py-bindings[cuda12] -c externals/icon4py/constraints.txt # GPU, CUDA 12.x
python3 -m pip install icon4py-bindings[cuda13] -c externals/icon4py/constraints.txt # GPU, CUDA 13.x
Configuring and Building ICON with ICON4Py#
Configure ICON as usual (see Building ICON) with the additional option --enable-icon4py:
./configure --enable-icon4py <other arguments>
At configure time point to the Python interpreter with ICON4Py in its environment.
By default the resulting binary runs with ICON4Py enabled; see icon4py_mode in &run_nml for other modes (off, substitute, verify).
Running ICON with ICON4Py#
ICON4Py will just-in-time (JIT) compile optimized programs for the selected ICON setup which will have significant performance impact on the first run. On subsequent runs, the programs will be used from cache. Programs are specialized for the local domains; changing the decomposition will trigger a new compilation.
Source run.env before launching ICON#
configure writes a run.env file under externals/icon4py/ in the build directory. Source it in the shell that launches ICON:
. <build-dir>/externals/icon4py/run.env
The file groups its exports in three categories. Override individual variables after sourcing, or edit run.env directly, if you need to deviate.
Compilers#
Sets CC, CXX, CUDACXX, and CUDAHOSTCXX to the compilers picked at configure time so GT4Py’s JIT toolchain stays aligned with the rest of ICON. If CMake fails to locate the CUDA toolkit from CUDACXX (rare), add export CUDA_PATH=<path to CUDA toolkit>.
Cache variables#
Control where JIT-compiled libraries are stored and how long they survive between runs:
export GT4PY_BUILD_CACHE_LIFETIME=persistent
# export GT4PY_BUILD_CACHE_DIR=<path> # defaults to cwd
persistent keeps the cache across runs so stencils don’t have to be recompiled every launch. GT4PY_BUILD_CACHE_DIR defaults to the current working directory; set it explicitly to share a cache across runs launched from different working directories.
Performance settings#
export PYTHONOPTIMIZE=2
Always apply for best performance, set PYTHONOPTIMIZE=0 for debugging.
Troubleshooting#
Selecting the ICON4Py backend at runtime#
The GT4Py backend used by the ICON4Py granules is selected by the icon4py_backend switch in &run_nml; the default (0) lets ICON4Py pick automatically. See icon4py_backend in &run_nml for details.