//@HEADER // ************************************************************************ // // Kokkos v. 4.0 // Copyright (2022) National Technology & Engineering // Solutions of Sandia, LLC (NTESS). // // Under the terms of Contract DE-NA0003525 with NTESS, // the U.S. Government retains certain rights in this software. // // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. // See https://kokkos.org/LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //@HEADER #ifndef KOKKOSBATCHED_VECTOR_SIMD_MISC_HPP #define KOKKOSBATCHED_VECTOR_SIMD_MISC_HPP /// \author Kyungjoo Kim (kyukim@sandia.gov) #include "Kokkos_Complex.hpp" namespace KokkosBatched { #define KOKKOSKERNELS_SIMD_MISC_RETURN_TYPE(T, l) Vector, l> #define KOKKOSKERNELS_SIMD_MISC_CONVERTIBLE_RETURN_VOID_TYPE(T0, T1, T2, l) void // typename std::enable_if::value && // std::is_convertible< T2 , T0 >::value, void >::type // scalar, scalar template KOKKOS_INLINE_FUNCTION static T conditional_assign(const bool cond, const T &if_true_val, const T &if_false_val) { return cond ? if_true_val : if_false_val; } template KOKKOS_INLINE_FUNCTION static KOKKOSKERNELS_SIMD_MISC_CONVERTIBLE_RETURN_VOID_TYPE(T0, T1, T2, l) conditional_assign(/* */ T0 &r_val, const bool cond, const T1 &if_true_val, const T2 &if_false_val) { r_val = cond ? if_true_val : if_false_val; } // vector, scalar template KOKKOS_INLINE_FUNCTION static KOKKOSKERNELS_SIMD_MISC_RETURN_TYPE(T, l) conditional_assign(const Vector, l> &cond, const Vector, l> &if_true_val, const T &if_false_val) { Vector, l> r_val; for (int i = 0; i < l; ++i) r_val[i] = cond[i] ? if_true_val[i] : if_false_val; return r_val; } template KOKKOS_INLINE_FUNCTION static KOKKOSKERNELS_SIMD_MISC_CONVERTIBLE_RETURN_VOID_TYPE(T0, T1, T2, l) conditional_assign(/* */ Vector, l> &r_val, const Vector, l> &cond, const Vector, l> &if_true_val, const T2 &if_false_val) { for (int i = 0; i < l; ++i) r_val[i] = cond[i] ? if_true_val[i] : if_false_val; } // scalar, vector template KOKKOS_INLINE_FUNCTION static KOKKOSKERNELS_SIMD_MISC_RETURN_TYPE(T, l) conditional_assign(const Vector, l> &cond, const T &if_true_val, const Vector, l> &if_false_val) { Vector, l> r_val; for (int i = 0; i < l; ++i) r_val[i] = cond[i] ? if_true_val : if_false_val[i]; return r_val; } template KOKKOS_INLINE_FUNCTION static KOKKOSKERNELS_SIMD_MISC_CONVERTIBLE_RETURN_VOID_TYPE(T0, T1, T2, l) conditional_assign(/* */ Vector, l> &r_val, const Vector, l> &cond, const T1 &if_true_val, const Vector, l> &if_false_val) { for (int i = 0; i < l; ++i) r_val[i] = cond[i] ? if_true_val : if_false_val[i]; } // vector, vector template KOKKOS_INLINE_FUNCTION static KOKKOSKERNELS_SIMD_MISC_RETURN_TYPE(T, l) conditional_assign(const Vector, l> &cond, const Vector, l> &if_true_val, const Vector, l> &if_false_val) { Vector, l> r_val; for (int i = 0; i < l; ++i) r_val[i] = cond[i] ? if_true_val[i] : if_false_val[i]; return r_val; } template KOKKOS_INLINE_FUNCTION static KOKKOSKERNELS_SIMD_MISC_CONVERTIBLE_RETURN_VOID_TYPE(T0, T1, T2, l) conditional_assign(/* */ Vector, l> &r_val, const Vector, l> &cond, const Vector, l> &if_true_val, const Vector, l> &if_false_val) { for (int i = 0; i < l; ++i) r_val[i] = cond[i] ? if_true_val[i] : if_false_val[i]; } template KOKKOS_INLINE_FUNCTION static T reduce(const Vector, l> &val, const BinaryOp &func) { T r_val = val[0]; for (int i = 1; i < l; ++i) r_val = func(r_val, val[i]); return r_val; } template KOKKOS_INLINE_FUNCTION static T reduce(const Vector, l> &val, const BinaryOp &func, const T init) { T r_val = init; for (int i = 0; i < l; ++i) r_val = func(r_val, val[i]); return r_val; } template KOKKOS_INLINE_FUNCTION static bool is_all_true(const Vector, l> &cond) { return reduce(cond, [](const bool left, const bool right) -> bool { return (left && right); }); } template KOKKOS_INLINE_FUNCTION static bool is_any_true(const Vector, l> &cond) { return reduce(cond, [](const bool left, const bool right) -> bool { return left || right; }); } template KOKKOS_INLINE_FUNCTION static T min(const Vector, l> &val) { return reduce(val, [](const T left, const T right) -> T { const auto tmp = left < right; return tmp * left + !tmp * right; }); } template KOKKOS_INLINE_FUNCTION static T max(const Vector, l> &val) { return reduce(val, [](const T left, const T right) -> T { const auto tmp = left > right; return tmp * left + !tmp * right; }); } template KOKKOS_INLINE_FUNCTION static T sum(const Vector, l> &val) { return reduce( val, [](const T left, const T right) -> T { return left + right; }, T(0)); } template KOKKOS_INLINE_FUNCTION static T prod(const Vector, l> &val) { return reduce( val, [](const T left, const T right) -> T { return left * right; }, T(1)); } #undef KOKKOSKERNELS_SIMD_MISC_RETURN_TYPE #undef KOKKOSKERNELS_SIMD_MISC_CONVERTIBLE_RETURN_VOID_TYPE } // namespace KokkosBatched #endif