| 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_SHIFTED_TRSV_SERIAL_INTERNAL_HPP |
| 17 |
#define KOKKOSBATCHED_SHIFTED_TRSV_SERIAL_INTERNAL_HPP |
| 18 |
|
| 19 |
/// \author Kyungjoo Kim ([email protected]) |
| 20 |
|
| 21 |
#include "KokkosBatched_Util.hpp" |
| 22 |
|
| 23 |
#include "KokkosBlas1_set_impl.hpp" |
| 24 |
#include "KokkosBlas1_team_scal_impl.hpp" |
| 25 |
#include "KokkosBatched_InnerTrsm_Serial_Impl.hpp" |
| 26 |
|
| 27 |
namespace KokkosBatched { |
| 28 |
|
| 29 |
/// |
| 30 |
/// Serial Internal Impl |
| 31 |
/// ==================== |
| 32 |
|
| 33 |
/// |
| 34 |
/// Lower |
| 35 |
/// |
| 36 |
|
| 37 |
struct SerialShiftedTrsvInternalLower { |
| 38 |
template <typename ScalarType, typename ValueTypeA, typename ValueTypeB> |
| 39 |
KOKKOS_INLINE_FUNCTION static int invoke(const int m, const ScalarType lambda, const ValueTypeA *KOKKOS_RESTRICT A, |
| 40 |
const int as0, const int as1, |
| 41 |
/* */ ValueTypeB *KOKKOS_RESTRICT b, const int bs0, |
| 42 |
const int *KOKKOS_RESTRICT blks) { |
| 43 |
const int as = as0 + as1; |
| 44 |
|
| 45 |
int p = 0; |
| 46 |
for (; p < m;) { |
| 47 |
const int blk = blks[p], iend = m - p - blk; |
| 48 |
assert(((blk == 1) || (blk == 2)) && "ShiftedTrsvLower: blocks are not 1x1 or 2x2"); |
| 49 |
if (blk == 1) { |
| 50 |
const auto alpha11 = A[p * as] - lambda; |
| 51 |
ValueTypeB *KOKKOS_RESTRICT beta1 = b + p * bs0; |
| 52 |
|
| 53 |
// with KOKKOS_RESTRICT a compiler assumes that the pointer is not |
| 54 |
// accessed by others op(/=) uses this pointer and changes the |
| 55 |
// associated values, which brings a compiler problem |
| 56 |
*beta1 = *beta1 / alpha11; |
| 57 |
|
| 58 |
if (iend) { |
| 59 |
const ValueTypeA *KOKKOS_RESTRICT a21 = A + p * as + as0; |
| 60 |
ValueTypeB *KOKKOS_RESTRICT b2 = beta1 + bs0; |
| 61 |
for (int i = 0; i < iend; ++i) b2[i * bs0] -= a21[i * as0] * (*beta1); |
| 62 |
} |
| 63 |
} else { |
| 64 |
const int p_plus_one = p + 1; |
| 65 |
const auto alpha11 = A[p * as] - lambda; |
| 66 |
const auto alpha12 = A[p * as + as1]; |
| 67 |
const auto alpha21 = A[p * as + as0]; |
| 68 |
const auto alpha22 = A[p_plus_one * as] - lambda; |
| 69 |
const auto det = alpha11 * alpha22 - alpha12 * alpha21; |
| 70 |
|
| 71 |
ValueTypeB *KOKKOS_RESTRICT beta1 = b + p * bs0; |
| 72 |
ValueTypeB *KOKKOS_RESTRICT beta2 = b + p_plus_one * bs0; |
| 73 |
|
| 74 |
const ValueTypeB beta_one = *beta1; |
| 75 |
const ValueTypeB beta_two = *beta2; |
| 76 |
|
| 77 |
*beta1 = (alpha22 * (beta_one)-alpha12 * (beta_two)) / det; |
| 78 |
*beta2 = (-alpha21 * (beta_one) + alpha11 * (beta_two)) / det; |
| 79 |
|
| 80 |
if (iend) { |
| 81 |
const ValueTypeA *KOKKOS_RESTRICT A21 = A + p * as + 2 * as0; |
| 82 |
ValueTypeB *KOKKOS_RESTRICT b2 = beta1 + 2 * bs0; |
| 83 |
|
| 84 |
for (int i = 0; i < iend; ++i) b2[i * bs0] -= (A21[i * as0] * (*beta1) + A21[i * as0 + as1] * (*beta2)); |
| 85 |
} |
| 86 |
} |
| 87 |
p += blk; |
| 88 |
} |
| 89 |
return 0; |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
/// |
| 94 |
/// Upper |
| 95 |
/// |
| 96 |
|
| 97 |
struct SerialShiftedTrsvInternalUpper { |
| 98 |
template <typename ScalarType, typename ValueTypeA, typename ValueTypeB> |
| 99 |
KOKKOS_INLINE_FUNCTION static int invoke(const int m, const ScalarType lambda, const ValueTypeA *KOKKOS_RESTRICT A, |
| 100 |
const int as0, const int as1, |
| 101 |
/**/ ValueTypeB *KOKKOS_RESTRICT b, const int bs0, |
| 102 |
const int *KOKKOS_RESTRICT blks) { |
| 103 |
const int as = as0 + as1; |
| 104 |
|
| 105 |
ValueTypeB *KOKKOS_RESTRICT b0 = b; |
| 106 |
|
| 107 |
int p = m - 1; |
| 108 |
for (; p >= 0;) { |
| 109 |
const int blk = blks[p], iend = p + 1 - blk; |
| 110 |
assert(((blk == 1) || (blk == 2)) && "ShiftedTrsvUpper: blocks are not 1x1 or 2x2"); |
| 111 |
if (blk == 1) { |
| 112 |
const auto alpha11 = A[p * as] - lambda; |
| 113 |
/**/ ValueTypeB *KOKKOS_RESTRICT beta1 = b + p * bs0; |
| 114 |
|
| 115 |
// with KOKKOS_RESTRICT a compiler assumes that the pointer is not |
| 116 |
// accessed by others op(/=) uses this pointer and changes the |
| 117 |
// associated values, which brings a compiler problem |
| 118 |
*beta1 = *beta1 / alpha11; |
| 119 |
|
| 120 |
if (iend) { |
| 121 |
const ValueTypeA *KOKKOS_RESTRICT a01 = A + p * as1; |
| 122 |
for (int i = 0; i < iend; ++i) b0[i * bs0] -= a01[i * as0] * (*beta1); |
| 123 |
} |
| 124 |
} else { |
| 125 |
const int p_minus_one = p - 1; |
| 126 |
const auto alpha11 = A[p_minus_one * as] - lambda; |
| 127 |
const auto alpha12 = A[p_minus_one * as + as1]; |
| 128 |
const auto alpha21 = A[p_minus_one * as + as0]; |
| 129 |
const auto alpha22 = A[p * as] - lambda; |
| 130 |
const auto det = alpha11 * alpha22 - alpha12 * alpha21; |
| 131 |
|
| 132 |
ValueTypeB *KOKKOS_RESTRICT beta1 = b + p_minus_one * bs0; |
| 133 |
ValueTypeB *KOKKOS_RESTRICT beta2 = b + p * bs0; |
| 134 |
|
| 135 |
const ValueTypeB beta_one = *beta1; |
| 136 |
const ValueTypeB beta_two = *beta2; |
| 137 |
|
| 138 |
*beta1 = (alpha22 * (beta_one)-alpha12 * (beta_two)) / det; |
| 139 |
*beta2 = (-alpha21 * (beta_one) + alpha11 * (beta_two)) / det; |
| 140 |
|
| 141 |
if (iend) { |
| 142 |
const ValueTypeA *KOKKOS_RESTRICT A01 = A + p_minus_one * as1; |
| 143 |
for (int i = 0; i < iend; ++i) b0[i * bs0] -= (A01[i * as0] * (*beta1) + A01[i * as0 + as1] * (*beta2)); |
| 144 |
} |
| 145 |
} |
| 146 |
p -= blk; |
| 147 |
} |
| 148 |
return 0; |
| 149 |
} |
| 150 |
}; |
| 151 |
|
| 152 |
} // namespace KokkosBatched |
| 153 |
|
| 154 |
#endif |