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

Use if-constexpr more in Tensor. #16468

Merged
merged 2 commits into from Jan 15, 2024

Conversation

bangerth
Copy link
Member

In pursuit of #16465. I think this patch is best viewed side-by-side because the diffs show up poorly aligned.

Comment on lines +1416 to +1420
Assert(dim != 0,
ExcMessage("Cannot access an object of type Tensor<rank_,0,Number>"));
AssertIndexRange(i, dim);

return values[i];
Copy link
Member

Choose a reason for hiding this comment

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

I see no constexpr in this function; given that the previous code appears to have been a workaround (introduced by #1573, see there for the message it tries to fix) to silence compiler warnings, what prevents these warnings now? Or don't we support the old compilers any more that would trigger the warnings?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good question. I didn't investigate the history when I wrote this patch. I'd say that if the CI checks come back clean, we should assume that compilers have learned not to warn.

(No constexpr -- I accidentally used the commit message I had used for a bigger patch that had test problems also for the case I here that I decided to break out into its own patch. I'll fix the SUBJECT line.)

Copy link
Member Author

Choose a reason for hiding this comment

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

And just to be clear, we put the workaround in #1573 in more than 8 years ago. I don't think we still support many of the compilers we used at the time.

Comment on lines 1705 to 1709
typename numbers::NumberTraits<Number>::real_type s =
internal::NumberType<
typename numbers::NumberTraits<Number>::real_type>::value(0.0);
for (unsigned int i = 0; i < dim; ++i)
s += numbers::NumberTraits<Number>::abs_square(values[i]);
Copy link
Member

Choose a reason for hiding this comment

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

I'm usually preferring to peel off the first loop index because it avoids adding into zero (and compilers would need -ffast-math to avoid the instruction); in this case, it wouldn't even introduce more lines of code because we would write

Suggested change
typename numbers::NumberTraits<Number>::real_type s =
internal::NumberType<
typename numbers::NumberTraits<Number>::real_type>::value(0.0);
for (unsigned int i = 0; i < dim; ++i)
s += numbers::NumberTraits<Number>::abs_square(values[i]);
typename numbers::NumberTraits<Number>::real_type s =
numbers::NumberTraits<Number>::abs_square(values[0]);
for (unsigned int i = 1; i < dim; ++i)
s += numbers::NumberTraits<Number>::abs_square(values[i]);

The question that remains for me is if would trigger a warning for dim = 0. I do not feel particularly strongly about it, so either one is fine for me.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a loop with compile-time bounds. It's almost certainly going to be unrolled. Don't you think the compiler can make the optimization itself? I think it's easier to read this way, but I also don't feel strongly about it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I changed my mind and did as you suggested. I handled the dim==0 case explicitly via an other if constexpr at the top.

constexpr DEAL_II_HOST_DEVICE inline DEAL_II_ALWAYS_INLINE void
division_operator(Tensor<rank, dim, Number> (&t)[dim],
const OtherNumber &factor)
if constexpr (std::is_integral<
Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, I do have a constexpr here -- the SUBJECT line wasn't totally wrong ;-)

@kronbichler kronbichler merged commit 0ebd5ac into dealii:master Jan 15, 2024
15 checks passed
@bangerth bangerth deleted the tensor-specialization branch January 15, 2024 15:51
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

2 participants