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 iterative implementation for Utilities::pow to support Intel GPUs #15868

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 18 additions & 26 deletions include/deal.II/base/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,34 +477,26 @@ namespace Utilities
# endif
# endif
#endif
// The "exponentiation by squaring" algorithm used below has to be
// compressed to one statement due to C++11's restrictions on constexpr
// functions. A more descriptive version would be:
//
// <code>
// if (iexp <= 0)
// return 1;
//
// // avoid overflow of one additional recursion with pow(base * base, 0)
// if (iexp == 1)
// return base;
//
// // if the current exponent is not divisible by two,
// // we need to account for that.
// const unsigned int prefactor = (iexp % 2 == 1) ? base : 1;
//
// // a^b = (a*a)^(b/2) for b even
// // a^b = a*(a*a)^((b-1)/2 for b odd
// return prefactor * dealii::Utilities::pow(base*base, iexp/2);
// </code>

static_assert(std::is_integral_v<T>, "Only integral types supported");

return iexp <= 0 ?
1 :
(iexp == 1 ? base :
(((iexp % 2 == 1) ? base : 1) *
dealii::Utilities::pow(base * base, iexp / 2)));
// The "exponentiation by squaring" algorithm used below has to be expressed
// in an iterative version since SYCL doesn't allow recursive functions used
// in device code.
Comment on lines +482 to +484
Copy link
Member

Choose a reason for hiding this comment

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

I thought that restrictions on the use of recursion had been lifted sometime during the transition from Fortran 4 to C. How disappointing to see that this has been reintroduced in 2023 :-(


if (iexp <= 0)
return 1;

int exp = iexp;
T x = base;
T y = 1;
while (exp > 1)
{
if (exp % 2 == 1)
y *= x;
x *= x;
exp /= 2;
}
return x * y;
}

/**
Expand Down