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

Support for 4d Data #71

Merged
merged 11 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
25 changes: 24 additions & 1 deletion ndsl/dsl/gt4py_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ def wrapper(*args, **kwargs) -> Any:
def _mask_to_dimensions(
mask: Tuple[bool, ...], shape: Sequence[int]
) -> List[Union[str, int]]:
assert len(mask) == 3
assert len(mask) >= 3
dimensions: List[Union[str, int]] = []
for i, axis in enumerate(("I", "J", "K")):
if mask[i]:
dimensions.append(axis)
if len(mask) > 3:
for i in range(3, len(mask)):
dimensions.append(str(shape[i]))
offset = int(sum(mask))
dimensions.extend(shape[offset:])
return dimensions
Expand Down Expand Up @@ -154,6 +157,9 @@ def make_storage_data(
data = _make_storage_data_2d(
data, shape, start, dummy, axis, read_only, backend=backend
)
elif n_dims == 4:

data = _make_storage_data_4d(data, shape, start, backend=backend)
FlorianDeconinck marked this conversation as resolved.
Show resolved Hide resolved
else:
data = _make_storage_data_3d(data, shape, start, backend=backend)

Expand Down Expand Up @@ -256,6 +262,23 @@ def _make_storage_data_3d(
] = asarray(data, type(buffer))
return buffer

def _make_storage_data_4d(
FlorianDeconinck marked this conversation as resolved.
Show resolved Hide resolved
data: Field,
shape: Tuple[int, ...],
start: Tuple[int, ...] = (0, 0, 0, 0),
*,
backend: str,
) -> Field:
istart, jstart, kstart, lstart = start
isize, jsize, ksize, lsize = data.shape
buffer = zeros(shape, backend=backend)
buffer[
istart : istart + isize,
jstart : jstart + jsize,
kstart : kstart + ksize,
lstart : lstart + lsize,
] = asarray(data, type(buffer))
return buffer

def make_storage_from_shape(
shape: Tuple[int, ...],
Expand Down
17 changes: 14 additions & 3 deletions ndsl/stencils/testing/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import ndsl.dsl.gt4py_utils as utils
from ndsl.dsl.stencil import StencilFactory
from ndsl.dsl.typing import Field, Float # noqa: F401
from ndsl.dsl.typing import Field, Float, Int # noqa: F401
from ndsl.quantity import Quantity
from ndsl.stencils.testing.grid import Grid # type: ignore

Expand Down Expand Up @@ -113,6 +113,12 @@ def make_storage_data(
elif not full_shape and len(array.shape) < 3 and axis == len(array.shape) - 1:
use_shape[1] = 1
start = (int(istart), int(jstart), int(kstart))
if 'float' in str(array.dtype):
dtype = Float
elif 'int' in str(array.dtype):
dtype = Int
else:
dtype = array.dtype
if names_4d:
return utils.make_storage_dict(
array,
Expand All @@ -123,8 +129,12 @@ def make_storage_data(
axis=axis,
names=names_4d,
backend=self.stencil_factory.backend,
dtype=dtype,
)
else:
if len(array.shape) == 4:
start = (int(istart), int(jstart), int(kstart), 0)
use_shape.append(array.shape[-1])
return utils.make_storage_data(
array,
tuple(use_shape),
Expand All @@ -134,6 +144,7 @@ def make_storage_data(
axis=axis,
read_only=read_only,
backend=self.stencil_factory.backend,
dtype=dtype,
)

def storage_vars(self):
Expand All @@ -159,7 +170,7 @@ def collect_start_indices(self, datashape, varinfo):
kstart = self.get_index_from_info(varinfo, "kstart", 0)
return istart, jstart, kstart

def make_storage_data_input_vars(self, inputs, storage_vars=None):
def make_storage_data_input_vars(self, inputs, storage_vars=None, dict_4d=True):
inputs_in = {**inputs}
inputs_out = {}
if storage_vars is None:
Expand All @@ -183,7 +194,7 @@ def make_storage_data_input_vars(self, inputs, storage_vars=None):
)

names_4d = None
if len(inputs_in[serialname].shape) == 4:
if (len(inputs_in[serialname].shape) == 4) and dict_4d:
names_4d = info.get("names_4d", utils.tracer_variables)

dummy_axes = info.get("dummy_axes", None)
Expand Down
Loading