| 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 |
|
| 17 |
#ifndef KOKKOSBATCHED_PBTRF_SERIAL_INTERNAL_HPP_ |
| 18 |
#define KOKKOSBATCHED_PBTRF_SERIAL_INTERNAL_HPP_ |
| 19 |
|
| 20 |
#include "KokkosBatched_Util.hpp" |
| 21 |
#include "KokkosBlas1_serial_scal_impl.hpp" |
| 22 |
|
| 23 |
namespace KokkosBatched { |
| 24 |
|
| 25 |
/// |
| 26 |
/// Serial Internal Impl |
| 27 |
/// ==================== |
| 28 |
|
| 29 |
/// |
| 30 |
/// Lower |
| 31 |
/// |
| 32 |
|
| 33 |
template <typename AlgoType> |
| 34 |
struct SerialPbtrfInternalLower { |
| 35 |
template <typename ValueType> |
| 36 |
KOKKOS_INLINE_FUNCTION static int invoke(const int an, |
| 37 |
/**/ ValueType *KOKKOS_RESTRICT AB, const int as0, const int as1, |
| 38 |
const int kd); |
| 39 |
|
| 40 |
template <typename ValueType> |
| 41 |
KOKKOS_INLINE_FUNCTION static int invoke(const int an, |
| 42 |
/**/ Kokkos::complex<ValueType> *KOKKOS_RESTRICT AB, const int as0, |
| 43 |
const int as1, const int kd); |
| 44 |
}; |
| 45 |
|
| 46 |
/// |
| 47 |
/// Real matrix |
| 48 |
/// |
| 49 |
|
| 50 |
template <> |
| 51 |
template <typename ValueType> |
| 52 |
KOKKOS_INLINE_FUNCTION int SerialPbtrfInternalLower<Algo::Pbtrf::Unblocked>::invoke(const int an, |
| 53 |
/**/ ValueType *KOKKOS_RESTRICT AB, |
| 54 |
const int as0, const int as1, |
| 55 |
const int kd) { |
| 56 |
// Compute the Cholesky factorization A = L*L'. |
| 57 |
for (int j = 0; j < an; ++j) { |
| 58 |
auto a_jj = AB[0 * as0 + j * as1]; |
| 59 |
|
| 60 |
// Check if L (j, j) is positive definite |
| 61 |
#if (KOKKOSKERNELS_DEBUG_LEVEL > 0) |
| 62 |
if (a_jj <= 0) { |
| 63 |
return j + 1; |
| 64 |
} |
| 65 |
#endif |
| 66 |
|
| 67 |
a_jj = Kokkos::sqrt(a_jj); |
| 68 |
AB[0 * as0 + j * as1] = a_jj; |
| 69 |
|
| 70 |
// Compute elements J+1:J+KN of column J and update the |
| 71 |
// trailing submatrix within the band. |
| 72 |
int kn = Kokkos::min(an - j - 1, kd); |
| 73 |
if (kn > 0) { |
| 74 |
// scale to diagonal elements |
| 75 |
const ValueType alpha = 1.0 / a_jj; |
| 76 |
KokkosBlas::Impl::SerialScaleInternal::invoke(kn, alpha, &(AB[1 * as0 + j * as1]), 1); |
| 77 |
|
| 78 |
// syr (lower) with alpha = -1.0 to diagonal elements |
| 79 |
for (int k = 0; k < kn; ++k) { |
| 80 |
auto x_k = AB[(k + 1) * as0 + j * as1]; |
| 81 |
if (x_k != 0) { |
| 82 |
auto temp = -1.0 * x_k; |
| 83 |
for (int i = k; i < kn; ++i) { |
| 84 |
auto x_i = AB[(i + 1) * as0 + j * as1]; |
| 85 |
AB[i * as0 + (j + 1 + k - i) * as1] += x_i * temp; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
/// |
| 96 |
/// Complex matrix |
| 97 |
/// |
| 98 |
template <> |
| 99 |
template <typename ValueType> |
| 100 |
KOKKOS_INLINE_FUNCTION int SerialPbtrfInternalLower<Algo::Pbtrf::Unblocked>::invoke( |
| 101 |
const int an, |
| 102 |
/**/ Kokkos::complex<ValueType> *KOKKOS_RESTRICT AB, const int as0, const int as1, const int kd) { |
| 103 |
// Compute the Cholesky factorization A = L*L**H |
| 104 |
for (int j = 0; j < an; ++j) { |
| 105 |
auto a_jj = AB[0 * as0 + j * as1].real(); |
| 106 |
|
| 107 |
// Check if L (j, j) is positive definite |
| 108 |
#if (KOKKOSKERNELS_DEBUG_LEVEL > 0) |
| 109 |
if (a_jj <= 0) { |
| 110 |
AB[0 * as0 + j * as1] = a_jj; |
| 111 |
return j + 1; |
| 112 |
} |
| 113 |
#endif |
| 114 |
|
| 115 |
a_jj = Kokkos::sqrt(a_jj); |
| 116 |
AB[0 * as0 + j * as1] = a_jj; |
| 117 |
|
| 118 |
// Compute elements J+1:J+KN of column J and update the |
| 119 |
// trailing submatrix within the band. |
| 120 |
int kn = Kokkos::min(kd, an - j - 1); |
| 121 |
if (kn > 0) { |
| 122 |
// scale to diagonal elements |
| 123 |
const ValueType alpha = 1.0 / a_jj; |
| 124 |
KokkosBlas::Impl::SerialScaleInternal::invoke(kn, alpha, &(AB[1 * as0 + j * as1]), 1); |
| 125 |
|
| 126 |
// zher (lower) with alpha = -1.0 to diagonal elements |
| 127 |
for (int k = 0; k < kn; ++k) { |
| 128 |
auto x_k = AB[(k + 1) * as0 + j * as1]; |
| 129 |
if (x_k != 0) { |
| 130 |
auto temp = -1.0 * Kokkos::conj(x_k); |
| 131 |
AB[k * as0 + (j + 1) * as1] = AB[k * as0 + (j + 1) * as1].real() + (temp * x_k).real(); |
| 132 |
for (int i = k + 1; i < kn; ++i) { |
| 133 |
auto x_i = AB[(i + 1) * as0 + j * as1]; |
| 134 |
AB[i * as0 + (j + 1 + k - i) * as1] += x_i * temp; |
| 135 |
} |
| 136 |
} else { |
| 137 |
AB[k * as0 + (j + 1) * as1] = AB[k * as0 + (j + 1) * as1].real(); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
/// |
| 147 |
/// Upper |
| 148 |
/// |
| 149 |
|
| 150 |
template <typename AlgoType> |
| 151 |
struct SerialPbtrfInternalUpper { |
| 152 |
template <typename ValueType> |
| 153 |
KOKKOS_INLINE_FUNCTION static int invoke(const int an, |
| 154 |
/**/ ValueType *KOKKOS_RESTRICT AB, const int as0, const int as1, |
| 155 |
const int kd); |
| 156 |
|
| 157 |
template <typename ValueType> |
| 158 |
KOKKOS_INLINE_FUNCTION static int invoke(const int an, |
| 159 |
/**/ Kokkos::complex<ValueType> *KOKKOS_RESTRICT AB, const int as0, |
| 160 |
const int as1, const int kd); |
| 161 |
}; |
| 162 |
|
| 163 |
/// |
| 164 |
/// Real matrix |
| 165 |
/// |
| 166 |
template <> |
| 167 |
template <typename ValueType> |
| 168 |
KOKKOS_INLINE_FUNCTION int SerialPbtrfInternalUpper<Algo::Pbtrf::Unblocked>::invoke(const int an, |
| 169 |
/**/ ValueType *KOKKOS_RESTRICT AB, |
| 170 |
const int as0, const int as1, |
| 171 |
const int kd) { |
| 172 |
// Compute the Cholesky factorization A = U'*U. |
| 173 |
for (int j = 0; j < an; ++j) { |
| 174 |
auto a_jj = AB[kd * as0 + j * as1]; |
| 175 |
|
| 176 |
// Check if U (j,j) is positive definite |
| 177 |
#if (KOKKOSKERNELS_DEBUG_LEVEL > 0) |
| 178 |
if (a_jj <= 0) { |
| 179 |
return j + 1; |
| 180 |
} |
| 181 |
#endif |
| 182 |
a_jj = Kokkos::sqrt(a_jj); |
| 183 |
AB[kd * as0 + j * as1] = a_jj; |
| 184 |
|
| 185 |
// Compute elements J+1:J+KN of row J and update the |
| 186 |
// trailing submatrix within the band. |
| 187 |
int kn = Kokkos::min(kd, an - j - 1); |
| 188 |
int kld = Kokkos::max(1, as0 - 1); |
| 189 |
if (kn > 0) { |
| 190 |
// scale to diagonal elements |
| 191 |
const ValueType alpha = 1.0 / a_jj; |
| 192 |
KokkosBlas::Impl::SerialScaleInternal::invoke(kn, alpha, &(AB[(kd - 1) * as0 + (j + 1) * as1]), kld); |
| 193 |
|
| 194 |
// syr (upper) with alpha = -1.0 to diagonal elements |
| 195 |
for (int k = 0; k < kn; ++k) { |
| 196 |
auto x_k = AB[(k + kd - 1) * as0 + (j + 1 - k) * as1]; |
| 197 |
if (x_k != 0) { |
| 198 |
auto temp = -1.0 * x_k; |
| 199 |
for (int i = 0; i < k + 1; ++i) { |
| 200 |
auto x_i = AB[(i + kd - 1) * as0 + (j + 1 - i) * as1]; |
| 201 |
AB[(kd + i) * as0 + (j + 1 + k - i) * as1] += x_i * temp; |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
return 0; |
| 209 |
} |
| 210 |
|
| 211 |
/// |
| 212 |
/// Complex matrix |
| 213 |
/// |
| 214 |
template <> |
| 215 |
template <typename ValueType> |
| 216 |
KOKKOS_INLINE_FUNCTION int SerialPbtrfInternalUpper<Algo::Pbtrf::Unblocked>::invoke( |
| 217 |
const int an, |
| 218 |
/**/ Kokkos::complex<ValueType> *KOKKOS_RESTRICT AB, const int as0, const int as1, const int kd) { |
| 219 |
// Compute the Cholesky factorization A = U**H * U. |
| 220 |
for (int j = 0; j < an; ++j) { |
| 221 |
auto a_jj = AB[kd * as0 + j * as1].real(); |
| 222 |
|
| 223 |
// Check if U (j,j) is positive definite |
| 224 |
#if (KOKKOSKERNELS_DEBUG_LEVEL > 0) |
| 225 |
if (a_jj <= 0) { |
| 226 |
AB[kd * as0 + j * as1] = a_jj; |
| 227 |
return j + 1; |
| 228 |
} |
| 229 |
#endif |
| 230 |
|
| 231 |
a_jj = Kokkos::sqrt(a_jj); |
| 232 |
AB[kd * as0 + j * as1] = a_jj; |
| 233 |
|
| 234 |
// Compute elements J+1:J+KN of row J and update the |
| 235 |
// trailing submatrix within the band. |
| 236 |
int kn = Kokkos::min(kd, an - j - 1); |
| 237 |
int kld = Kokkos::max(1, as0 - 1); |
| 238 |
if (kn > 0) { |
| 239 |
// scale to diagonal elements |
| 240 |
const ValueType alpha = 1.0 / a_jj; |
| 241 |
KokkosBlas::Impl::SerialScaleInternal::invoke(kn, alpha, &(AB[(kd - 1) * as0 + (j + 1) * as1]), kld); |
| 242 |
|
| 243 |
// zlacgv to diagonal elements |
| 244 |
for (int i = 0; i < kn; ++i) { |
| 245 |
AB[(i + kd - 1) * as0 + (j + 1 - i) * as1] = Kokkos::conj(AB[(i + kd - 1) * as0 + (j + 1 - i) * as1]); |
| 246 |
} |
| 247 |
|
| 248 |
// zher (upper) with alpha = -1.0 to diagonal elements |
| 249 |
for (int k = 0; k < kn; ++k) { |
| 250 |
auto x_k = AB[(k + kd - 1) * as0 + (j + 1 - k) * as1]; |
| 251 |
if (x_k != 0) { |
| 252 |
auto temp = -1.0 * Kokkos::conj(x_k); |
| 253 |
for (int i = 0; i < k + 1; ++i) { |
| 254 |
auto x_i = AB[(i + kd - 1) * as0 + (j + 1 - i) * as1]; |
| 255 |
AB[(kd + i) * as0 + (j + 1 + k - i) * as1] += x_i * temp; |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
// zlacgv to diagonal elements |
| 261 |
for (int i = 0; i < kn; ++i) { |
| 262 |
AB[(i + kd - 1) * as0 + (j + 1 - i) * as1] = Kokkos::conj(AB[(i + kd - 1) * as0 + (j + 1 - i) * as1]); |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
return 0; |
| 268 |
} |
| 269 |
|
| 270 |
} // namespace KokkosBatched |
| 271 |
|
| 272 |
#endif // KOKKOSBATCHED_PBTRF_SERIAL_INTERNAL_HPP_ |