Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
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
10 changes: 9 additions & 1 deletion ndsl/stencils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
from .corners import CopyCorners, CopyCornersXY, FillCornersBGrid
from .corners import (
CopyCorners,
CopyCornersX,
CopyCornersXY,
CopyCornersY,
FillCornersBGrid,
)


__all__ = [
"CopyCorners",
"CopyCornersX",
"CopyCornersY",
"CopyCornersXY",
"FillCornersBGrid",
]
168 changes: 167 additions & 1 deletion ndsl/stencils/corners.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import warnings
from typing import Optional, Sequence, Tuple

from gt4py.cartesian import gtscript
from gt4py.cartesian.gtscript import PARALLEL, computation, horizontal, interval, region

from ndsl import StencilFactory, orchestrate
from ndsl.constants import (
X_DIM,
X_INTERFACE_DIM,
Y_DIM,
Y_INTERFACE_DIM,
Z_INTERFACE_DIM,
)
from ndsl.dsl.stencil import GridIndexing, StencilFactory
from ndsl.dsl.stencil import GridIndexing
from ndsl.dsl.typing import FloatField


Expand All @@ -22,6 +24,13 @@ class CopyCorners:

def __init__(self, direction: str, stencil_factory: StencilFactory) -> None:
"""The grid for this stencil"""
warnings.warn(
"Usage of the GT4Py implementation of CopyCorners is discouraged and will"
"be removed in the next release. Use `CopyCornersX` or `CopyCornersY` directly"
"for a more future-proof implementation of the same code.",
DeprecationWarning,
2,
)
grid_indexing = stencil_factory.grid_indexing

n_halo = grid_indexing.n_halo
Expand Down Expand Up @@ -59,6 +68,163 @@ def __call__(self, field: FloatField):
self._copy_corners(field, field)


def corner_copy_x(field_to_copy):
"""Equivalent to the copy_corners_x functions in fortran.

This is written to operate on plain ndarrarys and not use the GT4Py framework.
This choice was made because we've seen a lot of performance left on the table using
orchestration without explicitly describing the operations but rather have full 3d-
sweeps with conditionals.
Since DaCe can handle (simple) operations on ndarrays directly this gives us a more
explicit entrypoint to the language and more optimization-potential.

Args:
field_to_copy (ndarray): field to apply the corner copy on.
This is explicitly not type-hinted for orchestration
"""
field_to_copy[0, 0] = field_to_copy[0, 5]
field_to_copy[0, 1] = field_to_copy[1, 5]
field_to_copy[0, 2] = field_to_copy[2, 5]

field_to_copy[1, 0] = field_to_copy[0, 4]
field_to_copy[1, 1] = field_to_copy[1, 4]
field_to_copy[1, 2] = field_to_copy[2, 4]

field_to_copy[2, 0] = field_to_copy[0, 3]
field_to_copy[2, 1] = field_to_copy[1, 3]
field_to_copy[2, 2] = field_to_copy[2, 3]

field_to_copy[0, -4] = field_to_copy[2, -7]
field_to_copy[0, -3] = field_to_copy[1, -7]
field_to_copy[0, -2] = field_to_copy[0, -7]

field_to_copy[1, -4] = field_to_copy[2, -6]
field_to_copy[1, -3] = field_to_copy[1, -6]
field_to_copy[1, -2] = field_to_copy[0, -6]

field_to_copy[2, -4] = field_to_copy[2, -5]
field_to_copy[2, -3] = field_to_copy[1, -5]
field_to_copy[2, -2] = field_to_copy[0, -5]

field_to_copy[-4, 0] = field_to_copy[-2, 3]
field_to_copy[-4, 1] = field_to_copy[-3, 3]
field_to_copy[-4, 2] = field_to_copy[-4, 3]

field_to_copy[-3, 0] = field_to_copy[-2, 4]
field_to_copy[-3, 1] = field_to_copy[-3, 4]
field_to_copy[-3, 2] = field_to_copy[-4, 4]

field_to_copy[-2, 0] = field_to_copy[-2, 5]
field_to_copy[-2, 1] = field_to_copy[-3, 5]
field_to_copy[-2, 2] = field_to_copy[-4, 5]

field_to_copy[-4, -2] = field_to_copy[-2, -5]
field_to_copy[-4, -3] = field_to_copy[-3, -5]
field_to_copy[-4, -4] = field_to_copy[-4, -5]

field_to_copy[-3, -2] = field_to_copy[-2, -6]
field_to_copy[-3, -3] = field_to_copy[-3, -6]
field_to_copy[-3, -4] = field_to_copy[-4, -6]

field_to_copy[-2, -2] = field_to_copy[-2, -7]
field_to_copy[-2, -3] = field_to_copy[-3, -7]
field_to_copy[-2, -4] = field_to_copy[-4, -7]


def corner_copy_y(field_to_copy):
"""Equivalent to the copy_corners_y functions in fortran.

This is written to operate on plain ndarrarys and not use the GT4Py framework.
This choice was made because we've seen a lot of performance left on the table using
orchestration without explicitly describing the operations but rather have full 3d-
sweeps with conditionals.
Since DaCe can handle (simple) operations on ndarrays directly this gives us a more
explicit entrypoint to the language and more optimization-potential.

Args:
field_to_copy (ndarray): field to apply the corner copy on.
This is explicitly not type-hinted for orchestration
"""
field_to_copy[0, 0] = field_to_copy[5, 0]
field_to_copy[1, 0] = field_to_copy[5, 1]
field_to_copy[2, 0] = field_to_copy[5, 2]

field_to_copy[0, 1] = field_to_copy[4, 0]
field_to_copy[1, 1] = field_to_copy[4, 1]
field_to_copy[2, 1] = field_to_copy[4, 2]

field_to_copy[0, 2] = field_to_copy[3, 0]
field_to_copy[1, 2] = field_to_copy[3, 1]
field_to_copy[2, 2] = field_to_copy[3, 2]

field_to_copy[-4, 0] = field_to_copy[-7, 2]
field_to_copy[-3, 0] = field_to_copy[-7, 1]
field_to_copy[-2, 0] = field_to_copy[-7, 0]

field_to_copy[-4, 1] = field_to_copy[-6, 2]
field_to_copy[-3, 1] = field_to_copy[-6, 1]
field_to_copy[-2, 1] = field_to_copy[-6, 0]

field_to_copy[-4, 2] = field_to_copy[-5, 2]
field_to_copy[-3, 2] = field_to_copy[-5, 1]
field_to_copy[-2, 2] = field_to_copy[-5, 0]

field_to_copy[0, -2] = field_to_copy[5, -2]
field_to_copy[0, -3] = field_to_copy[4, -2]
field_to_copy[0, -4] = field_to_copy[3, -2]

field_to_copy[1, -2] = field_to_copy[5, -3]
field_to_copy[1, -3] = field_to_copy[4, -3]
field_to_copy[1, -4] = field_to_copy[3, -3]

field_to_copy[2, -2] = field_to_copy[5, -4]
field_to_copy[2, -3] = field_to_copy[4, -4]
field_to_copy[2, -4] = field_to_copy[3, -4]

field_to_copy[-2, -4] = field_to_copy[-5, -2]
field_to_copy[-2, -3] = field_to_copy[-6, -2]
field_to_copy[-2, -2] = field_to_copy[-7, -2]

field_to_copy[-3, -4] = field_to_copy[-5, -3]
field_to_copy[-3, -3] = field_to_copy[-6, -3]
field_to_copy[-3, -2] = field_to_copy[-7, -3]

field_to_copy[-4, -4] = field_to_copy[-5, -4]
field_to_copy[-4, -3] = field_to_copy[-6, -4]
field_to_copy[-4, -2] = field_to_copy[-7, -4]


class CopyCornersX:
"""
Helper-class to copy corners corresponding to the fortran function copy_corners_x
"""

def __init__(self, stencil_factory: StencilFactory) -> None:
orchestrate(
obj=self,
config=stencil_factory.config.dace_config,
)

def __call__(self, field: FloatField):
corner_copy_x(field)


class CopyCornersY:
"""
Helper-class to copy corners corresponding to the fortran function
copy_corners_y
"""

def __init__(self, stencil_factory: StencilFactory) -> None:
orchestrate(
obj=self,
config=stencil_factory.config.dace_config,
)

def __call__(self, field: FloatField):
corner_copy_y(field)


class CopyCornersXY:
"""
Helper-class to copy corners corresponding to the Fortran functions
Expand Down
Loading