| 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_APPLY_GIVENS_SERIAL_INTERNAL_HPP |
| 17 |
#define KOKKOSBATCHED_APPLY_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 SerialApplyLeftGivensInternal { |
| 32 |
template <typename ValueType> |
| 33 |
KOKKOS_INLINE_FUNCTION static int invoke(const Kokkos::pair<ValueType, ValueType> G, const int n, |
| 34 |
/* */ ValueType *a1t, const int a1ts, |
| 35 |
/* */ ValueType *a2t, const int a2ts) { |
| 36 |
typedef ValueType value_type; |
| 37 |
if (n == 0) return 0; // quick return |
| 38 |
if (G.first == value_type(1) && G.second == value_type(0)) return 0; |
| 39 |
/// G = [ gamma -sigma; |
| 40 |
/// sigma gamma ]; |
| 41 |
/// A := G A |
| 42 |
/// where gamma is G.first and sigma is G.second |
| 43 |
|
| 44 |
const value_type gamma = G.first; |
| 45 |
const value_type sigma = G.second; |
| 46 |
#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL) |
| 47 |
#pragma unroll |
| 48 |
#endif |
| 49 |
for (int j = 0; j < n; ++j) { |
| 50 |
const value_type alpha1 = a1t[j * a1ts]; |
| 51 |
const value_type alpha2 = a2t[j * a2ts]; |
| 52 |
a1t[j * a1ts] = gamma * alpha1 - sigma * alpha2; |
| 53 |
a2t[j * a1ts] = sigma * alpha1 + gamma * alpha2; |
| 54 |
} |
| 55 |
return 0; |
| 56 |
} |
| 57 |
}; |
| 58 |
|
| 59 |
struct SerialApplyRightGivensInternal { |
| 60 |
template <typename ValueType> |
| 61 |
KOKKOS_INLINE_FUNCTION static int invoke(const Kokkos::pair<ValueType, ValueType> G, const int m, |
| 62 |
/* */ ValueType *a1, const int a1s, |
| 63 |
/* */ ValueType *a2, const int a2s) { |
| 64 |
typedef ValueType value_type; |
| 65 |
if (m == 0) return 0; // quick return |
| 66 |
if (G.first == value_type(1) && G.second == value_type(0)) return 0; |
| 67 |
/// G = [ gamma -sigma; |
| 68 |
/// sigma gamma ]; |
| 69 |
/// A := A G' |
| 70 |
/// where gamma is G.first and sigma is G.second |
| 71 |
|
| 72 |
const value_type gamma = G.first; |
| 73 |
const value_type sigma = G.second; |
| 74 |
#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL) |
| 75 |
#pragma unroll |
| 76 |
#endif |
| 77 |
for (int i = 0; i < m; ++i) { |
| 78 |
const value_type alpha1 = a1[i * a1s]; |
| 79 |
const value_type alpha2 = a2[i * a2s]; |
| 80 |
a1[i * a1s] = gamma * alpha1 - sigma * alpha2; |
| 81 |
a2[i * a1s] = sigma * alpha1 + gamma * alpha2; |
| 82 |
} |
| 83 |
return 0; |
| 84 |
} |
| 85 |
}; |
| 86 |
|
| 87 |
struct SerialApplyLeftRightGivensInternal { |
| 88 |
template <typename ValueType> |
| 89 |
KOKKOS_INLINE_FUNCTION static int invoke(const Kokkos::pair<ValueType, ValueType> &G12, const int &m, const int &n, |
| 90 |
/* */ ValueType *KOKKOS_RESTRICT a1t, |
| 91 |
/* */ ValueType *KOKKOS_RESTRICT a2t, |
| 92 |
/* */ ValueType *KOKKOS_RESTRICT a1, |
| 93 |
/* */ ValueType *KOKKOS_RESTRICT a2, const int &as0, const int &as1) { |
| 94 |
typedef ValueType value_type; |
| 95 |
if (G12.first == value_type(1) && G12.second == value_type(0)) return 0; |
| 96 |
if (m == 0 && n == 0) return 0; // quick return |
| 97 |
|
| 98 |
const value_type gamma12 = G12.first; |
| 99 |
const value_type sigma12 = G12.second; |
| 100 |
|
| 101 |
#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL) |
| 102 |
#pragma unroll |
| 103 |
#endif |
| 104 |
for (int j = 0; j < n; ++j) { |
| 105 |
const value_type alpha1 = a1t[j * as1]; |
| 106 |
const value_type alpha2 = a2t[j * as1]; |
| 107 |
a1t[j * as1] = (gamma12 * alpha1 - sigma12 * alpha2); |
| 108 |
a2t[j * as1] = (sigma12 * alpha1 + gamma12 * alpha2); |
| 109 |
} |
| 110 |
|
| 111 |
#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL) |
| 112 |
#pragma unroll |
| 113 |
#endif |
| 114 |
for (int i = 0; i < m; ++i) { |
| 115 |
const value_type alpha1 = a1[i * as0]; |
| 116 |
const value_type alpha2 = a2[i * as0]; |
| 117 |
a1[i * as0] = (gamma12 * alpha1 - sigma12 * alpha2); |
| 118 |
a2[i * as0] = (sigma12 * alpha1 + gamma12 * alpha2); |
| 119 |
} |
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
template <typename ValueType> |
| 124 |
KOKKOS_INLINE_FUNCTION static int invoke(const Kokkos::pair<ValueType, ValueType> &G12, |
| 125 |
const Kokkos::pair<ValueType, ValueType> &G13, const int &m, const int &n, |
| 126 |
/* */ ValueType *KOKKOS_RESTRICT a1t, |
| 127 |
/* */ ValueType *KOKKOS_RESTRICT a2t, |
| 128 |
/* */ ValueType *KOKKOS_RESTRICT a3t, |
| 129 |
/* */ ValueType *KOKKOS_RESTRICT a1, |
| 130 |
/* */ ValueType *KOKKOS_RESTRICT a2, |
| 131 |
/* */ ValueType *KOKKOS_RESTRICT a3, const int &as0, const int &as1) { |
| 132 |
typedef ValueType value_type; |
| 133 |
if (m == 0 && n == 0) return 0; // quick return |
| 134 |
|
| 135 |
const value_type gamma12 = G12.first; |
| 136 |
const value_type sigma12 = G12.second; |
| 137 |
const value_type gamma13 = G13.first; |
| 138 |
const value_type sigma13 = G13.second; |
| 139 |
|
| 140 |
#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL) |
| 141 |
#pragma unroll |
| 142 |
#endif |
| 143 |
for (int j = 0; j < n; ++j) { |
| 144 |
const value_type alpha2 = a2t[j * as1]; |
| 145 |
const value_type alpha3 = a3t[j * as1]; |
| 146 |
{ |
| 147 |
const value_type alpha1 = a1t[j * as1]; |
| 148 |
a1t[j * as1] = (gamma12 * alpha1 - sigma12 * alpha2); |
| 149 |
a2t[j * as1] = (sigma12 * alpha1 + gamma12 * alpha2); |
| 150 |
} |
| 151 |
{ |
| 152 |
const value_type alpha1 = a1t[j * as1]; |
| 153 |
a1t[j * as1] = (gamma13 * alpha1 - sigma13 * alpha3); |
| 154 |
a3t[j * as1] = (sigma13 * alpha1 + gamma13 * alpha3); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL) |
| 159 |
#pragma unroll |
| 160 |
#endif |
| 161 |
for (int i = 0; i < m; ++i) { |
| 162 |
const value_type alpha2 = a2[i * as0]; |
| 163 |
const value_type alpha3 = a3[i * as0]; |
| 164 |
{ |
| 165 |
const value_type alpha1 = a1[i * as0]; |
| 166 |
a1[i * as0] = (gamma12 * alpha1 - sigma12 * alpha2); |
| 167 |
a2[i * as0] = (sigma12 * alpha1 + gamma12 * alpha2); |
| 168 |
} |
| 169 |
{ |
| 170 |
const value_type alpha1 = a1[i * as0]; |
| 171 |
a1[i * as0] = (gamma13 * alpha1 - sigma13 * alpha3); |
| 172 |
a3[i * as0] = (sigma13 * alpha1 + gamma13 * alpha3); |
| 173 |
} |
| 174 |
} |
| 175 |
return 0; |
| 176 |
} |
| 177 |
}; |
| 178 |
|
| 179 |
} // end namespace KokkosBatched |
| 180 |
|
| 181 |
#endif |