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

distributed/tria.cc: avoid FPE due to undefined behavior #16820

Merged
merged 1 commit into from Mar 30, 2024

Conversation

tamiko
Copy link
Member

@tamiko tamiko commented Mar 30, 2024

The following construct

static_cast<types::subdomain_id>(this_sc_point[dim])

triggers a floating point exception when the stored double is -1.0 (at least with gcc-11 and avx512 instructions enabled).

Judging from cppreference [1] this is undefined behavior:
"""
A prvalue of floating-point type can be converted to a prvalue of any integer type. The fractional part is truncated, that is, the fractional part is discarded.

If the truncated value cannot fit into the destination type, the behavior is undefined (even when the destination type is unsigned, modulo arithmetic does not apply).
"""

Thus, work around the issue by explicitly setting negative values to numbers::invalid_subdomain_id.

Preparatory step to get the avx256 / avx512 regression tester variants online.

In reference to #16796

[1] https://en.cppreference.com/w/cpp/language/implicit_conversion

The following construct
```
static_cast<types::subdomain_id>(this_sc_point[dim])
```
triggers a floating point exception when the stored double is -1.0 (at
least with gcc-11 and avx512 instructions enabled).

Judging from cppreference [1] this is undefined behavior:
"""
A prvalue of floating-point type can be converted to a prvalue of any
integer type. The fractional part is truncated, that is, the fractional
part is discarded.

If the truncated value cannot fit into the destination type, the
behavior is undefined (even when the destination type is unsigned,
modulo arithmetic does not apply).
"""

Thus, work around the issue by explicitly setting negative values to
`numbers::invalid_subdomain_id` if we encounter -1.

[1] https://en.cppreference.com/w/cpp/language/implicit_conversion
@tamiko
Copy link
Member Author

tamiko commented Mar 30, 2024

/rebuild

@tamiko
Copy link
Member Author

tamiko commented Mar 30, 2024

All, this fixes the last pre-alignment avx test-failure on my side. I have marked it as Draft for a moment because I have to verify that I actually understood the code in question. (aka Matthias runs a full ctest while going out for dinner).

@tamiko tamiko marked this pull request as ready for review March 30, 2024 04:08
Copy link
Member

@bangerth bangerth left a comment

Choose a reason for hiding this comment

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

Yes, this is easier to understand anyway!

@bangerth bangerth merged commit ba5fde5 into dealii:master Mar 30, 2024
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants