-
Notifications
You must be signed in to change notification settings - Fork 446
EAMxx: hide field utils impl using a type-agnostic scalar wrapper #7857
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
Conversation
Useful to pass around scalars without having to template every function on the passed scalar
Start from utils that are not templated
Use ScalarWrapper to hide implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors the field utility APIs to simplify template-based interfaces and improve code organization. The key change is introducing a ScalarWrapper type to eliminate the need for templated scalar types in function signatures, allowing implementations to be moved from header files to compilation units.
- Introduces
ScalarWrapperclass to wrap scalar values with type safety - Replaces templated scalar parameters with
ScalarWrapperin field utility functions (randomize,perturb,field_min/max/sum,frobenius_norm,compute_mask, etc.) - Refactors field utility implementations into separate
.cppfiles for better compile-time performance - Updates
randomizeAPI to use seed-based interface instead of engine+PDF - Simplifies test code by using simpler randomization and utility function calls
Reviewed Changes
Copilot reviewed 66 out of 66 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
eamxx_scalar_wrapper.hpp |
New wrapper class for type-safe scalar value passing without templates |
eamxx_data_type.hpp |
Made functions device-compatible with KOKKOS_INLINE_FUNCTION |
field_utils.hpp |
Updated API signatures to use ScalarWrapper instead of templates |
field_utils_impl.hpp |
Deleted - implementations moved to separate .cpp files |
field/utils/*.cpp |
New files containing implementations split by functionality |
field.hpp/cpp |
Added optional allocate parameter to Field constructor |
| Test files | Updated to use simplified randomization and utility APIs |
Comments suppressed due to low confidence (1)
components/eamxx/src/share/field/utils/randomize.cpp:1
- In the
randomize_discretefunction at line 151 incoarsening_remapper_tests.cpp,seed++is used without storing the result. This increments the seed but doesn't pass the incremented value to the function. The pattern should useseedand then increment separately, or use pre-increment. Same issue appears in multiple test files.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| EKAT_KERNEL_ERROR_MSG ("Error! Unsupported data type.\n"); | ||
| return DataType::Invalid; |
Copilot
AI
Nov 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return statement on line 38 is unreachable since EKAT_KERNEL_ERROR_MSG likely terminates execution. This return is only present to satisfy the compiler. Consider adding a comment to clarify this is unreachable code required for compilation.
| impl::horiz_contraction<double>(f_out,f_in,weight,AVG,comm,f_tmp); | ||
| break; | ||
| default: | ||
| EKAT_ERROR_MSG ("[vert_contraction] Error! Unsupported data type.\n"); |
Copilot
AI
Nov 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error message says '[vert_contraction]' but this is in the horiz_contraction.cpp file. The error message should say '[horiz_contraction]' instead.
mahf708
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks fine to me from a quick scan, I think it helps.
Quick question re:
If this is merged, I would like to
were you expected push back? Is there something specific you want us to look very closely at?
|
Well, I don't want to assume my PRs will be for sure merged. I thought the design helps, but testing may reveal some underlying insormountable issue, or other ppl may have different opinions... E.g., someone may find the |
Use ScalarWrapper to hide the template implementation. The randomize util will ALWAYS use a uniform distribution. Use ranodmize_normal for a normal distribution
32b8642 to
b08d7ec
Compare
|
Sounds good. To me, it appears helpful. I will take a more thorough look soon, but I am approving based on a quick look. Btw sorry, didn't mean to edit the reviewer section when I added copilot. It was a mistake. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 66 out of 66 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
jgfouca
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving based on the PR description. I ain't looking through 3000 lines, lol
|
The diffs are expected, as the new impl of perturb is non-BFB w.r.t. to the previous impl, and we cannot make it BFB. The reason is that the previous impl used the same @tcclevenger You introduced the IC perturbation feature; are you ok with this specific mod? |
The constructor of ScalarWrapper already takes care of type conversion
|
Failures are expected, waiting for final review by @tcclevenger |
This design pattern uses a type-agnostic wrapper for scalars, so that we don't need to expose the templated method implementation to the customers. [non-BFB] For runs using the IC perturb feature
This design pattern uses a type-agnostic wrapper for scalars, so that we don't need to expose the templated method implementation to the customers.
[non-BFB] For runs using the IC perturb feature
A more detailed summary of changes
ScalarWrapper: tiny struct that stores a scalar as a double, but allows to retrieve it with any scalar type. This is almost as using double everywhere, but it adds some type safety, as it ensures that no narrowing happens when getting the scalar back. E.g., storing anintin aScalarWrapperallows to later get it back with any type (no narrowing for int->float/double), but storing afloatwould cause an error if later used to retrieve the scalar withinttype.compute_mask, make the comparison type a runtime param. We don't use this utility often, and probably the branch predictor can already help to pick the right case anywaysScalarWrapper, and the engine is created internally, so that the API is non-templated.For utils accepting
ScalarWrapper, we also offer a templated version with the template arg being the scalar. Wait, isn't this undoing the whole idea of not having to template the utils? Well, the templated version calls the version that acceptsScalarWrapper, which is NOT templated, so that the true (long) impl of the util can still be hidden in the cpp file.If this is merged, I would like to take a similar approach also for the field update methods (maybe even turning them into another set of field utils, rather than field methods).