-
Notifications
You must be signed in to change notification settings - Fork 21
fix error utils to work on architectures where std::is_normal is not … #637
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
Open
Yurlungur
wants to merge
6
commits into
main
Choose a base branch
from
jmm/fix_error_utils
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2766704
fix error utils to work on architectures where std::is_normal is not …
jonahm-LANL f9b0652
man this really should never trigger so maybe it shouldn't be hidden …
jonahm-LANL dea748d
changelog
jonahm-LANL d8fac7e
use ports of call method
jonahm-LANL 2c8144f
no need to explicitly test C++20 now
jonahm-LANL 9a48595
update kokkos, kokkos-kernels to 5.1.0
jonahm-LANL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| //------------------------------------------------------------------------------ | ||
| // © 2021-2025. Triad National Security, LLC. All rights reserved. This | ||
| // © 2021-2026. Triad National Security, LLC. All rights reserved. This | ||
| // program was produced under U.S. Government contract 89233218CNA000001 | ||
| // for Los Alamos National Laboratory (LANL), which is operated by Triad | ||
| // National Security, LLC for the U.S. Department of Energy/National | ||
|
|
@@ -12,27 +12,33 @@ | |
| // publicly and display publicly, and to permit others to do so. | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| // This file created in part with the assistance of generative AI | ||
|
|
||
| #ifndef SINGULARITY_EOS_BASE_ERROR_UTILS_HPP_ | ||
| #define SINGULARITY_EOS_BASE_ERROR_UTILS_HPP_ | ||
|
|
||
| #include <cmath> | ||
| #include <limits> | ||
| #include <type_traits> | ||
|
|
||
| #include <ports-of-call/portability.hpp> | ||
| #include <ports-of-call/robust_utils.hpp> | ||
|
|
||
| namespace singularity { | ||
| namespace error_utils { | ||
|
|
||
| using PortsOfCall::printf; | ||
|
|
||
| constexpr double _NORMAL_FACTOR = 1.0e10; | ||
| // Only accept values that are safely away from overflow by this factor. In | ||
| // other words, a nonzero value must be normal and have magnitude no larger | ||
| // than max()/NORMAL_FACTOR. | ||
| constexpr double NORMAL_FACTOR = 1.0e10; | ||
|
|
||
| struct is_normal_or_zero { | ||
| template <typename valT> | ||
| constexpr bool PORTABLE_FORCEINLINE_FUNCTION operator()(valT value) const { | ||
| static_assert(std::is_floating_point<valT>::value); | ||
| return (value == valT{0}) || | ||
| (std::isnormal(_NORMAL_FACTOR * value) && std::isnormal(value)); | ||
| return PortsOfCall::Robust::is_normal_or_zero(value, | ||
| static_cast<valT>(NORMAL_FACTOR)); | ||
| } | ||
| }; | ||
|
|
||
|
|
@@ -52,16 +58,18 @@ struct is_non_negative { | |
| } | ||
| }; | ||
|
|
||
| // Checks whether a value obeys some sort of provided condition. If not, returns true and | ||
| // prints the provided error message, variable name, and value (but does not abort!) | ||
| // Checks whether a value obeys some sort of provided condition. If not, returns true | ||
| // and prints the provided error message, variable name, and value (but does not abort!) | ||
| template <typename valT, typename condT, typename nameT> | ||
| PORTABLE_FORCEINLINE_FUNCTION bool violates_condition(valT &&value, condT &&condition, | ||
| nameT &&var_name) { | ||
| const bool good = condition(std::forward<valT>(value)); | ||
| #ifndef NDEBUG | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now added for performance and convenience |
||
| if (!good) { | ||
| printf("### ERROR: Bad singularity-eos value\n Var: %s\n Value: %.15e\n", | ||
| var_name, value); | ||
| } | ||
| #endif // NDEBUG | ||
| return !good; | ||
| } | ||
|
|
||
|
|
@@ -78,7 +86,7 @@ PORTABLE_FORCEINLINE_FUNCTION bool non_positive_value(valT &&value, nameT &&var_ | |
| } | ||
| template <typename valT, typename nameT> | ||
| PORTABLE_FORCEINLINE_FUNCTION bool negative_value(valT &&value, nameT &&var_name) { | ||
| return violates_condition(std::forward<valT>(value), is_strictly_positive{}, | ||
| return violates_condition(std::forward<valT>(value), is_non_negative{}, | ||
|
Comment on lines
-81
to
+89
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is just a typo. this condition was identical to the one above. |
||
| std::forward<nameT>(var_name)); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| //------------------------------------------------------------------------------ | ||
| // © 2021-2026. Triad National Security, LLC. All rights reserved. This | ||
| // program was produced under U.S. Government contract 89233218CNA000001 | ||
| // for Los Alamos National Laboratory (LANL), which is operated by Triad | ||
| // National Security, LLC for the U.S. Department of Energy/National | ||
| // Nuclear Security Administration. All rights in the program are | ||
| // reserved by Triad National Security, LLC, and the U.S. Department of | ||
| // Energy/National Nuclear Security Administration. The Government is | ||
| // granted for itself and others acting on its behalf a nonexclusive, | ||
| // paid-up, irrevocable worldwide license in this material to reproduce, | ||
| // prepare derivative works, distribute copies to the public, perform | ||
| // publicly and display publicly, and to permit others to do so. | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| // This file created with the assistance of generative AI | ||
|
|
||
| #include <cmath> | ||
| #include <limits> | ||
|
|
||
| #include <ports-of-call/portability.hpp> | ||
| #include <singularity-eos/base/error_utils.hpp> | ||
|
|
||
| #ifndef CATCH_CONFIG_FAST_COMPILE | ||
| #define CATCH_CONFIG_FAST_COMPILE | ||
| #include <catch2/catch_test_macros.hpp> | ||
| #endif | ||
|
|
||
| namespace error_utils = singularity::error_utils; | ||
|
|
||
| template <typename T> | ||
| void CheckNormalOrZeroClassification(const char *type_name) { | ||
| using limits = std::numeric_limits<T>; | ||
| const T factor = static_cast<T>(error_utils::NORMAL_FACTOR); | ||
| const auto condition = error_utils::is_normal_or_zero{}; | ||
|
|
||
| CAPTURE(type_name); | ||
|
|
||
| REQUIRE(condition(T{0})); | ||
| REQUIRE(condition(-T{0})); | ||
| REQUIRE(condition(static_cast<T>(1.382884838243760e+06))); | ||
| REQUIRE(condition(limits::min())); | ||
| REQUIRE(condition(-limits::min())); | ||
|
|
||
| if constexpr (limits::has_denorm != std::denorm_absent) { | ||
| REQUIRE_FALSE(condition(limits::denorm_min())); | ||
| REQUIRE_FALSE(condition(-limits::denorm_min())); | ||
| } | ||
|
|
||
| REQUIRE_FALSE(condition(limits::quiet_NaN())); | ||
| REQUIRE_FALSE(condition(limits::infinity())); | ||
| REQUIRE_FALSE(condition(-limits::infinity())); | ||
|
|
||
| const T safely_bounded = (limits::max() / factor) * T{0.5}; | ||
| const T too_large = (limits::max() / factor) * T{2.0}; | ||
| REQUIRE(condition(safely_bounded)); | ||
| REQUIRE_FALSE(condition(too_large)); | ||
| } | ||
|
|
||
| template <typename T> | ||
| void CheckNormalOrZeroClassificationOnDevice(const char *type_name) { | ||
| using limits = std::numeric_limits<T>; | ||
| const T factor = static_cast<T>(error_utils::NORMAL_FACTOR); | ||
| const auto condition = error_utils::is_normal_or_zero{}; | ||
|
|
||
| CAPTURE(type_name); | ||
|
|
||
| int nwrong = 0; | ||
| portableReduce( | ||
| "Check error_utils::is_normal_or_zero on device", 0, 1, | ||
| PORTABLE_LAMBDA(const int /*i*/, int &nw) { | ||
| nw += !condition(T{0}); | ||
| nw += !condition(-T{0}); | ||
| nw += !condition(static_cast<T>(1.382884838243760e+06)); | ||
| nw += !condition(limits::min()); | ||
| nw += !condition(-limits::min()); | ||
|
|
||
| if constexpr (limits::has_denorm != std::denorm_absent) { | ||
| nw += condition(limits::denorm_min()); | ||
| nw += condition(-limits::denorm_min()); | ||
| } | ||
|
|
||
| nw += condition(limits::quiet_NaN()); | ||
| nw += condition(limits::infinity()); | ||
| nw += condition(-limits::infinity()); | ||
| nw += !condition((limits::max() / factor) * T{0.5}); | ||
| nw += condition((limits::max() / factor) * T{2.0}); | ||
| }, | ||
| nwrong); | ||
|
|
||
| REQUIRE(nwrong == 0); | ||
| } | ||
|
|
||
| SCENARIO( | ||
| "Error utilities classify normal values, denormals, and overflow-adjacent values", | ||
| "[ErrorUtils]") { | ||
| CheckNormalOrZeroClassification<float>("float"); | ||
| CheckNormalOrZeroClassification<double>("double"); | ||
| } | ||
|
|
||
| SCENARIO("Error utilities classify values consistently in device reductions", | ||
| "[ErrorUtils][Device]") { | ||
| CheckNormalOrZeroClassificationOnDevice<float>("float"); | ||
| CheckNormalOrZeroClassificationOnDevice<double>("double"); | ||
| } |
Submodule kokkos
updated
1580 files
Submodule kokkos-kernels
updated
1651 files
Submodule ports-of-call
updated
8 files
| +1 −0 | .gitignore | |
| +3 −3 | CMakeLists.txt | |
| +9 −3 | doc/sphinx/src/using.rst | |
| +27 −3 | ports-of-call/robust_utils.hpp | |
| +4 −17 | ports-of-call/span.hpp | |
| +1 −0 | test/CMakeLists.txt | |
| +3 −3 | test/test_math_utils.cpp | |
| +130 −0 | test/test_robust_utils.cpp |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
no longer needed as we're now just on C++20