| 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_PTTRF_SERIAL_INTERNAL_HPP_ |
| 17 |
#define KOKKOSBATCHED_PTTRF_SERIAL_INTERNAL_HPP_ |
| 18 |
|
| 19 |
#include <KokkosBatched_Util.hpp> |
| 20 |
|
| 21 |
/// \author Yuuichi Asahi ([email protected]) |
| 22 |
|
| 23 |
namespace KokkosBatched { |
| 24 |
|
| 25 |
template <typename AlgoType> |
| 26 |
struct SerialPttrfInternal { |
| 27 |
template <typename ValueType> |
| 28 |
KOKKOS_INLINE_FUNCTION static int invoke(const int n, ValueType *KOKKOS_RESTRICT d, const int ds0, |
| 29 |
ValueType *KOKKOS_RESTRICT e, const int es0); |
| 30 |
|
| 31 |
template <typename ValueType> |
| 32 |
KOKKOS_INLINE_FUNCTION static int invoke(const int n, ValueType *KOKKOS_RESTRICT d, const int ds0, |
| 33 |
Kokkos::complex<ValueType> *KOKKOS_RESTRICT e, const int es0); |
| 34 |
}; |
| 35 |
|
| 36 |
/// |
| 37 |
/// Real matrix |
| 38 |
/// |
| 39 |
|
| 40 |
template <> |
| 41 |
template <typename ValueType> |
| 42 |
KOKKOS_INLINE_FUNCTION int SerialPttrfInternal<Algo::Pttrf::Unblocked>::invoke( |
| 43 |
const int n, ValueType *KOKKOS_RESTRICT d, const int ds0, ValueType *KOKKOS_RESTRICT e, const int es0) { |
| 44 |
int info = 0; |
| 45 |
|
| 46 |
auto update = [&](const int i) { |
| 47 |
auto ei_tmp = e[i * es0]; |
| 48 |
e[i * es0] = ei_tmp / d[i * ds0]; |
| 49 |
d[(i + 1) * ds0] -= e[i * es0] * ei_tmp; |
| 50 |
}; |
| 51 |
|
| 52 |
auto check_positive_definitiveness = [&](const int i) { return (d[i] <= 0.0) ? (i + 1) : 0; }; |
| 53 |
|
| 54 |
// Compute the L*D*L' (or U'*D*U) factorization of A. |
| 55 |
const int i4 = (n - 1) % 4; |
| 56 |
for (int i = 0; i < i4; i++) { |
| 57 |
#if (KOKKOSKERNELS_DEBUG_LEVEL > 0) |
| 58 |
info = check_positive_definitiveness(i); |
| 59 |
if (info) { |
| 60 |
return info; |
| 61 |
} |
| 62 |
#endif |
| 63 |
|
| 64 |
update(i); |
| 65 |
} // for (int i = 0; i < i4; i++) |
| 66 |
|
| 67 |
for (int i = i4; i < n - 4; i += 4) { |
| 68 |
#if (KOKKOSKERNELS_DEBUG_LEVEL > 0) |
| 69 |
info = check_positive_definitiveness(i); |
| 70 |
if (info) { |
| 71 |
return info; |
| 72 |
} |
| 73 |
#endif |
| 74 |
|
| 75 |
update(i); |
| 76 |
|
| 77 |
#if (KOKKOSKERNELS_DEBUG_LEVEL > 0) |
| 78 |
info = check_positive_definitiveness(i + 1); |
| 79 |
if (info) { |
| 80 |
return info; |
| 81 |
} |
| 82 |
#endif |
| 83 |
|
| 84 |
update(i + 1); |
| 85 |
|
| 86 |
#if (KOKKOSKERNELS_DEBUG_LEVEL > 0) |
| 87 |
info = check_positive_definitiveness(i + 2); |
| 88 |
if (info) { |
| 89 |
return info; |
| 90 |
} |
| 91 |
#endif |
| 92 |
|
| 93 |
update(i + 2); |
| 94 |
|
| 95 |
#if (KOKKOSKERNELS_DEBUG_LEVEL > 0) |
| 96 |
info = check_positive_definitiveness(i + 3); |
| 97 |
if (info) { |
| 98 |
return info; |
| 99 |
} |
| 100 |
#endif |
| 101 |
|
| 102 |
update(i + 3); |
| 103 |
|
| 104 |
} // for (int i = i4; i < n-4; 4) |
| 105 |
|
| 106 |
#if (KOKKOSKERNELS_DEBUG_LEVEL > 0) |
| 107 |
info = check_positive_definitiveness(n - 1); |
| 108 |
if (info) { |
| 109 |
return info; |
| 110 |
} |
| 111 |
#endif |
| 112 |
|
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
/// |
| 117 |
/// Complex matrix |
| 118 |
/// |
| 119 |
|
| 120 |
template <> |
| 121 |
template <typename ValueType> |
| 122 |
KOKKOS_INLINE_FUNCTION int SerialPttrfInternal<Algo::Pttrf::Unblocked>::invoke( |
| 123 |
const int n, ValueType *KOKKOS_RESTRICT d, const int ds0, Kokkos::complex<ValueType> *KOKKOS_RESTRICT e, |
| 124 |
const int es0) { |
| 125 |
int info = 0; |
| 126 |
|
| 127 |
auto update = [&](const int i) { |
| 128 |
auto eir_tmp = e[i * es0].real(); |
| 129 |
auto eii_tmp = e[i * es0].imag(); |
| 130 |
auto f_tmp = eir_tmp / d[i * ds0]; |
| 131 |
auto g_tmp = eii_tmp / d[i * ds0]; |
| 132 |
e[i * es0] = Kokkos::complex<ValueType>(f_tmp, g_tmp); |
| 133 |
d[(i + 1) * ds0] = d[(i + 1) * ds0] - f_tmp * eir_tmp - g_tmp * eii_tmp; |
| 134 |
}; |
| 135 |
|
| 136 |
auto check_positive_definitiveness = [&](const int i) { return (d[i] <= 0.0) ? (i + 1) : 0; }; |
| 137 |
|
| 138 |
// Compute the L*D*L' (or U'*D*U) factorization of A. |
| 139 |
const int i4 = (n - 1) % 4; |
| 140 |
for (int i = 0; i < i4; i++) { |
| 141 |
#if (KOKKOSKERNELS_DEBUG_LEVEL > 0) |
| 142 |
info = check_positive_definitiveness(i); |
| 143 |
if (info) { |
| 144 |
return info; |
| 145 |
} |
| 146 |
#endif |
| 147 |
|
| 148 |
update(i); |
| 149 |
} // for (int i = 0; i < i4; i++) |
| 150 |
|
| 151 |
for (int i = i4; i < n - 4; i += 4) { |
| 152 |
#if (KOKKOSKERNELS_DEBUG_LEVEL > 0) |
| 153 |
info = check_positive_definitiveness(i); |
| 154 |
if (info) { |
| 155 |
return info; |
| 156 |
} |
| 157 |
#endif |
| 158 |
|
| 159 |
update(i); |
| 160 |
|
| 161 |
#if (KOKKOSKERNELS_DEBUG_LEVEL > 0) |
| 162 |
info = check_positive_definitiveness(i + 1); |
| 163 |
if (info) { |
| 164 |
return info; |
| 165 |
} |
| 166 |
#endif |
| 167 |
|
| 168 |
update(i + 1); |
| 169 |
|
| 170 |
#if (KOKKOSKERNELS_DEBUG_LEVEL > 0) |
| 171 |
info = check_positive_definitiveness(i + 2); |
| 172 |
if (info) { |
| 173 |
return info; |
| 174 |
} |
| 175 |
#endif |
| 176 |
|
| 177 |
update(i + 2); |
| 178 |
|
| 179 |
#if (KOKKOSKERNELS_DEBUG_LEVEL > 0) |
| 180 |
info = check_positive_definitiveness(i + 3); |
| 181 |
if (info) { |
| 182 |
return info; |
| 183 |
} |
| 184 |
#endif |
| 185 |
|
| 186 |
update(i + 3); |
| 187 |
|
| 188 |
} // for (int i = i4; i < n-4; 4) |
| 189 |
|
| 190 |
#if (KOKKOSKERNELS_DEBUG_LEVEL > 0) |
| 191 |
info = check_positive_definitiveness(n - 1); |
| 192 |
if (info) { |
| 193 |
return info; |
| 194 |
} |
| 195 |
#endif |
| 196 |
|
| 197 |
return 0; |
| 198 |
} |
| 199 |
|
| 200 |
} // namespace KokkosBatched |
| 201 |
|
| 202 |
#endif // KOKKOSBATCHED_PTTRF_SERIAL_INTERNAL_HPP_ |