Skip to content

Conversation

whannah1
Copy link
Contributor

@whannah1 whannah1 commented Sep 11, 2025

This adds a check to print_field_hyperslab() to aid in debugging an edge case where a postcondition check tries to throw an error but the underlying grid of the current process is incorrectly defined. Hopefully the check is general enough to help debug other edge cases as well.

[BFB]

@whannah1 whannah1 requested a review from bartgol September 11, 2025 21:30
@whannah1 whannah1 added Atmosphere EAMxx Issues related to EAMxx BFB PR leaves answers BFB labels Sep 11, 2025
" - requested indices: (" + ekat::join(indices,",") + ")\n");

const int num_indices = indices.size();
for (int i=0; i<num_indices; ++i) {
Copy link
Contributor

Choose a reason for hiding this comment

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

For future reference, the range-based for loops are prob more expressive in this case:

for (auto idx : indices) {
    EKAT_REQUIRE_MSG ( idx>=0 && idx<fl.dim(0),
      "Error! Requested index is invalid.\n"
      "  - field name: " + f.name() + "\n"
      "  - field layout: " + fl.to_string() + "\n"
      "  - requested indices: (" + ekat::join(indices,",") + ")\n");
}

They kind of resemble the python way of looping over iterables.

@tcclevenger tcclevenger added the CI: approved Allow gh actions PR testing on ghci-snl-* machines label Sep 15, 2025
Copy link
Contributor

@tcclevenger tcclevenger left a comment

Choose a reason for hiding this comment

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

Added CI approval tag, will merge after testing completes.

@bartgol bartgol marked this pull request as draft September 15, 2025 21:58
@bartgol bartgol marked this pull request as ready for review September 15, 2025 21:58
@tcclevenger
Copy link
Contributor

tcclevenger commented Sep 18, 2025

Looks like this got caught up in the eamxx/src/share reorg. Now the code you're changing is in the field_utils.cpp file

void print_field_hyperslab (const Field& f,

@whannah1
Copy link
Contributor Author

Looks like this got caught up in the eamxx/src/share reorg. Now the code you're changing is in the field_utils.cpp file

ah, darn. I'll do a rebase and deal with the conflict.

@whannah1 whannah1 force-pushed the whannah/eamxx/update-field_utils branch from f5299dc to 6cb7958 Compare September 18, 2025 17:55

const int num_indices = indices.size();
for (int i=0; i<num_indices; ++i) {
EKAT_REQUIRE_MSG ( indices[i]>=0 && indices[i]<fl.dim(i),
Copy link
Contributor

@bartgol bartgol Sep 22, 2025

Choose a reason for hiding this comment

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

Upon further consideration, I think this check is incorrect. We should check that the provided index is in bounds along the dim of the corresponding slice. E.g., if I provide 1 index, I may be slicing away the 2nd or 3rd dim, while the current code checks that the index is in bounds along the 1st dim.

Since the fcn is implemented to use recursion, I think we could simply check that the 1st index is in bounds, by adding a check down below, near these lines

    auto it = ekat::find(layout.tags(),tag);
    EKAT_REQUIRE_MSG (it!=layout.tags().end(),
        "Error! Something went wrong while slicing field.\n"
        "  - field name  : " + f.name() + "\n"
        "  - field layout: " + layout.to_string() + "\n"
        "  - curr tag    : " + e2str(tag) + "\n");
    auto idim = std::distance(layout.tags().begin(),it);

    EKAT_REQUIRE_MSG (idim==0 || idim==1,
        "Error! Cannot subview field for printing.\n"
        "  - field name  : " + f.name() + "\n"
        "  - field layout: " + layout.to_string() + "\n"
        "  - loc tags    : <" + ekat::join(tags,",") + ">\n"
        "  - loc indices : (" + ekat::join(indices,",") + ")\n");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not really following. Are you saying that there's no way to know if fl.dim(i) corresponds to indices[i]?

In that case I'm not sure how to obtain the size of the correct dimension...

Copy link
Contributor

Choose a reason for hiding this comment

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

Say the layout of the field f is {COL,CMP,LEV} and the user is calling print_field_hyperslab(f,{CMP},{3}). Then, in your current impl, you are comparing the first index (3) with the extent of the 0-th component of f, which is not the right one.

Copy link
Contributor

Choose a reason for hiding this comment

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

Instead, you should find the first occurrence of tags[i] in the field layout. Something like:

auto it = ekat::find(fl.tags(),tags[i]);
int idx = std::distance(fl.tags().begin(),it);
EKAT_REQUIRE_MSG ( indices[i]>=0 && indices[i]<fl.dim(idx),
  ...

I recon that this is a bit verbose and not so clear. Unfortunately, FieldLayout::dim(FieldTag t), which returns the extent along tag t, requires that the tag appears only once, which may not be the case (e.g., for tensor quantities, or for dyn layouts, where GP appears twice). Perhaps I could change that method so that it returns the extent of the first occurrence of the tag.

Would you like me to do that? Maybe push a commit to this branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about you handle this update. I'm afraid I don't understand well enough to get it right without your guidance anyway.

@bartgol bartgol force-pushed the whannah/eamxx/update-field_utils branch from 11fe7b3 to 6583cd8 Compare September 24, 2025 20:20
@bartgol bartgol force-pushed the whannah/eamxx/update-field_utils branch from 6583cd8 to a5452e0 Compare September 24, 2025 20:23
@bartgol
Copy link
Contributor

bartgol commented Sep 24, 2025

@whannah1 I had to rebase on master, due to conflicts related to the share folder reorg ongoing. If you want to add more stuff, you will have to reset --hard on the current origin version first.

@bartgol bartgol requested a review from mahf708 September 24, 2025 21:18
@bartgol
Copy link
Contributor

bartgol commented Sep 24, 2025

@mahf708 Since I contributed, it's best if another set of eyes takes a look. Conrad is on vacation until next week, so I pinged you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Atmosphere BFB PR leaves answers BFB CI: approved Allow gh actions PR testing on ghci-snl-* machines EAMxx Issues related to EAMxx
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants