| 1 |
//@HEADER |
| 2 |
// ************************************************************************ |
| 3 |
// |
| 4 |
// Kokkos v. 4.0 |
| 5 |
// Copyright (2022) National Technology & Engineering |
| 6 |
// Solutions of Sandia, LLC (NTESS). |
| 7 |
// |
| 8 |
// Under the terms of Contract DE-NA0003525 with NTESS, |
| 9 |
// the U.S. Government retains certain rights in this software. |
| 10 |
// |
| 11 |
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. |
| 12 |
// See https://kokkos.org/LICENSE for license information. |
| 13 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 14 |
// |
| 15 |
//@HEADER |
| 16 |
#ifndef KOKKOSBATCHED_GIVENS_SERIAL_INTERNAL_HPP |
| 17 |
#define KOKKOSBATCHED_GIVENS_SERIAL_INTERNAL_HPP |
| 18 |
|
| 19 |
/// \author Kyungjoo Kim ([email protected]) |
| 20 |
|
| 21 |
#include "KokkosBatched_Util.hpp" |
| 22 |
|
| 23 |
namespace KokkosBatched { |
| 24 |
|
| 25 |
/// |
| 26 |
/// Serial Internal Impl |
| 27 |
/// ==================== |
| 28 |
/// |
| 29 |
/// this impl follows the flame interface of householder transformation |
| 30 |
/// |
| 31 |
struct SerialGivensInternal { |
| 32 |
template <typename ValueType> |
| 33 |
KOKKOS_INLINE_FUNCTION static int invoke(const ValueType chi1, const ValueType chi2, |
| 34 |
/* */ Kokkos::pair<ValueType, ValueType>* G, |
| 35 |
/* */ ValueType* chi1_new) { |
| 36 |
typedef ValueType value_type; |
| 37 |
const value_type zero(0), one(1); |
| 38 |
/// compute G = [ gamma -sigma; |
| 39 |
/// sigma gamma ]; |
| 40 |
/// G.first = gamma and G.second = sigma |
| 41 |
/// this rotation satisfy the following |
| 42 |
/// G' [chi1; = [ alpha; |
| 43 |
/// chi2] zero ]; |
| 44 |
value_type cs, sn, r; |
| 45 |
if (chi2 == zero) { |
| 46 |
r = chi1; |
| 47 |
cs = one; |
| 48 |
sn = zero; |
| 49 |
} else if (chi1 == zero) { |
| 50 |
r = chi2; |
| 51 |
cs = zero; |
| 52 |
sn = one; |
| 53 |
} else { |
| 54 |
// here we do not care overflow caused by the division although it is |
| 55 |
// probable.... |
| 56 |
r = Kokkos::ArithTraits<value_type>::sqrt(chi1 * chi1 + chi2 * chi2); |
| 57 |
cs = chi1 / r; |
| 58 |
sn = chi2 / r; |
| 59 |
|
| 60 |
if (Kokkos::ArithTraits<value_type>::abs(chi1) > Kokkos::ArithTraits<value_type>::abs(chi2) && cs < zero) { |
| 61 |
cs = -cs; |
| 62 |
sn = -sn; |
| 63 |
r = -r; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
G->first = cs; |
| 68 |
G->second = sn; |
| 69 |
*chi1_new = r; |
| 70 |
|
| 71 |
return 0; |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
} // end namespace KokkosBatched |
| 76 |
|
| 77 |
#endif |