Skip to content

Commit

Permalink
Generic testing of device functions to see if a usable sincos exists …
Browse files Browse the repository at this point in the history
…for the kernel.
  • Loading branch information
will-saunders-ukaea committed Jan 6, 2025
1 parent 873a798 commit ddedc7f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
25 changes: 24 additions & 1 deletion include/neso_particles/device_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,30 @@ template <typename T> inline auto rsqrt(const T x) { return sycl::rsqrt(x); }
template <typename T> inline auto sin(const T x) { return sycl::sin(x); }
template <typename T> inline auto cos(const T x) { return sycl::cos(x); }
template <typename T> inline auto tan(const T x) { return sycl::tan(x); }
template <typename T> inline auto sincos(const T x, T *cosval) {

namespace Private {
// ACPP does not seem to define a sycl::sincos(REAL, REAL*)
template <class, class = void> struct sincos_exists_for_t : std::false_type {};

template <class T>
struct sincos_exists_for_t<T, std::void_t<decltype(sycl::sincos(
std::declval<T>(), std::declval<T *>))>>
: std::true_type {};
} // namespace Private
template <typename T,
std::enable_if_t<
std::is_same<typename Private::sincos_exists_for_t<T>::type,
std::true_type>::value,
bool> = true>
inline auto sincos(const T x, T *cosval) {
return sycl::sincos(x, cosval);
}
template <typename T,
std::enable_if_t<
!std::is_same<typename Private::sincos_exists_for_t<T>::type,
std::true_type>::value,
bool> = true>
inline auto sincos(const T x, T *cosval) {
*cosval = sycl::cos(x);
return sycl::sin(x);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,14 @@ inline void triangle_barycentric_to_cartesian(const REAL x1, const REAL y1,
* @param[in] z Cartesian coordinate, z component.
* @param[in, out] r Torus cylindrical coordinate, radius in poloidal plane
* from centre (a).
* @param[in, out] theta Torus cylindrical coordinate, angle in poloidal plane
* from centre (a).
* @param[in, out] phi Torus cylindrical coordinate, toroidal angle relative to
* origin (Cartesian (0,0,0)).
* @param[in, out] theta Torus cylindrical coordinate, angle in poloidal plane
* from centre (a).
*/
inline void cartesian_to_torus_cylindrical(const REAL R, const REAL x,
const REAL y, const REAL z, REAL *r,
REAL *theta, REAL *phi) {
REAL *phi, REAL *theta) {
*phi = Kernel::atan2(y, x);

const REAL plane_radius2 = x * x + y * y;
Expand Down Expand Up @@ -381,17 +381,17 @@ inline void cartesian_to_torus_cylindrical(const REAL R, const REAL x,
* @param[in] R Major radius of torus.
* @param[in] r Torus cylindrical coordinate, radius in poloidal plane
* from centre (a).
* @param[in] theta Torus cylindrical coordinate, angle in poloidal plane
* from centre (a).
* @param[in] phi Torus cylindrical coordinate, toroidal angle relative to
* origin (Cartesian (0,0,0)).
* @param[in] theta Torus cylindrical coordinate, angle in poloidal plane
* from centre (a).
* @param[in, out] x Cartesian coordinate, x component.
* @param[in, out] y Cartesian coordinate, y component.
* @param[in, out] z Cartesian coordinate, z component.
*
*/
inline void torus_cylindrical_to_cartesian(const REAL R, const REAL r,
const REAL theta, const REAL phi,
const REAL phi, const REAL theta,
REAL *x, REAL *y, REAL *z) {

REAL costheta;
Expand Down
12 changes: 6 additions & 6 deletions test/test_external_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,8 +748,8 @@ TEST(ExternalCommon, cartesian_to_torus_cylindrical) {
auto lambda_test = [&](const REAL R, const REAL x, const REAL y, const REAL z,
const REAL cr, const REAL ctheta, const REAL cphi) {
REAL r, theta, phi;
ExternalCommon::cartesian_to_torus_cylindrical(R, x, y, z, &r, &theta,
&phi);
ExternalCommon::cartesian_to_torus_cylindrical(R, x, y, z, &r, &phi,
&theta);

const REAL two_pi = 2 * test_pi;
const REAL theta_err = std::fmod((theta - ctheta) + 2.0 * two_pi, two_pi);
Expand All @@ -759,7 +759,7 @@ TEST(ExternalCommon, cartesian_to_torus_cylindrical) {
ASSERT_NEAR(phi_err, 0.0, 1.0e-12);

REAL tx, ty, tz;
ExternalCommon::torus_cylindrical_to_cartesian(R, r, theta, phi, &tx, &ty,
ExternalCommon::torus_cylindrical_to_cartesian(R, r, phi, theta, &tx, &ty,
&tz);

ASSERT_NEAR(x, tx, 1.0e-12);
Expand Down Expand Up @@ -791,9 +791,9 @@ TEST(ExternalCommon, cartesian_to_torus_cylindrical) {
const REAL y = rng_y(rng);
const REAL z = rng_z(rng);
REAL r, theta, phi, tx, ty, tz;
ExternalCommon::cartesian_to_torus_cylindrical(R, x, y, z, &r, &theta,
&phi);
ExternalCommon::torus_cylindrical_to_cartesian(R, r, theta, phi, &tx, &ty,
ExternalCommon::cartesian_to_torus_cylindrical(R, x, y, z, &r, &phi,
&theta);
ExternalCommon::torus_cylindrical_to_cartesian(R, r, phi, theta, &tx, &ty,
&tz);
ASSERT_NEAR(x, tx, 1.0e-12);
ASSERT_NEAR(y, ty, 1.0e-12);
Expand Down

0 comments on commit ddedc7f

Please sign in to comment.