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

feat[next]: concat_where for boundary conditions #1468

Merged
merged 67 commits into from
Mar 14, 2024

Conversation

havogt
Copy link
Contributor

@havogt havogt commented Feb 25, 2024

Introduces concat_where to be used for boundary conditions.

where will intersect all 3 fields. Therefore where(klevel == 0, boundary_layer, interior) will not work if boundary_layer is only defined on klevel == 0.

concat_where will concatenate the fields that are selected by the mask regions. The mask is currently required to be 1 dimensional.

mask: core_defs.NDArrayObject,
) -> list[tuple[bool, common.UnitRange]]:
"""Take a 1-dimensional mask and return a sequence of mappings from boolean values to ranges."""
# TODO: does it make sense to upgrade this naive algorithm to numpy?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if used in k, probably not relevant, if used in the horizontal, probably this is a performance problem

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe some timeit microbenchmarks with typical (both small and large) domain sizes would help to figure out if it'd pay off.

@havogt havogt marked this pull request as ready for review March 7, 2024 10:13
@havogt havogt requested a review from egparedes March 7, 2024 10:18
Copy link
Contributor

@egparedes egparedes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started doing a more exhaustive review but then switched to more general questions and comments, so there's a mix of high-level and low-level implementation details (and didn't review the tests at all).

src/gt4py/next/common.py Outdated Show resolved Hide resolved
src/gt4py/next/embedded/common.py Outdated Show resolved Hide resolved
@@ -97,14 +97,57 @@ def _absolute_sub_domain(
return common.Domain(*named_ranges)


def intersect_domains(*domains: common.Domain) -> common.Domain:
def domain_intersection(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion, I think this could be transformed into the .intersection() method in the Domain class to follow the same API as set (https://devdocs.io/python~3.12/library/stdtypes#set)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe adding that makes sense, however I like more that this function works also when domains is empty

return functools.reduce(
operator.and_,
domains,
common.Domain(dims=tuple(), ranges=tuple()),
)


def intersect_domains(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this function confusing specially given the previous one. I think the name could contain the name restrict instead of intersection (e.g. restrict_to_lower_bound, restrict_to_intersection) and the ignore_dims argument shouldn't be optional, because otherwise is just a simple intersection of domains.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Changed the name.
  • In generic context allowing ignore_dims to be optional makes sense to me. Maybe I could change the API to only accept tuples and default to empty tuple. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't like that much that ignore_dims is optional but it's ok. ignore_dims accepting only tuples makes sense to me, but it's not strictly needed and it's orthogonal to my original complaint because it'll still have a default value.

src/gt4py/next/embedded/common.py Show resolved Hide resolved
src/gt4py/next/embedded/nd_array_field.py Outdated Show resolved Hide resolved
src/gt4py/next/embedded/nd_array_field.py Outdated Show resolved Hide resolved
src/gt4py/next/embedded/nd_array_field.py Show resolved Hide resolved
src/gt4py/next/embedded/nd_array_field.py Show resolved Hide resolved
mask: core_defs.NDArrayObject,
) -> list[tuple[bool, common.UnitRange]]:
"""Take a 1-dimensional mask and return a sequence of mappings from boolean values to ranges."""
# TODO: does it make sense to upgrade this naive algorithm to numpy?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe some timeit microbenchmarks with typical (both small and large) domain sizes would help to figure out if it'd pay off.

Copy link
Contributor

@egparedes egparedes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good, just have a few comments, most of them optional, and it should be ready to merge.

src/gt4py/next/common.py Outdated Show resolved Hide resolved
src/gt4py/next/embedded/exceptions.py Outdated Show resolved Hide resolved
src/gt4py/next/ffront/experimental.py Outdated Show resolved Hide resolved
Copy link
Contributor

@egparedes egparedes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ILGTM

@havogt havogt merged commit 1abfaea into GridTools:main Mar 14, 2024
31 checks passed
@havogt havogt deleted the concat_where branch March 14, 2024 18:36
edopao pushed a commit to edopao/gt4py that referenced this pull request Mar 18, 2024
Introduces `concat_where` to be used for boundary conditions.

`where` will intersect all 3 fields. Therefore `where(klevel == 0,
boundary_layer, interior)` will not work if `boundary_layer` is only
defined on `klevel == 0`.

`concat_where` will concatenate the fields that are selected by the mask
regions. The `mask` is currently required to be 1 dimensional.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants