| 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_TRTRI_SERIAL_INTERNAL_HPP |
| 18 |
#define KOKKOSBATCHED_TRTRI_SERIAL_INTERNAL_HPP |
| 19 |
|
| 20 |
#include "KokkosBatched_Util.hpp" |
| 21 |
#include "KokkosBatched_Trmm_Serial_Internal.hpp" |
| 22 |
|
| 23 |
namespace KokkosBatched { |
| 24 |
|
| 25 |
template <typename AlgoType> |
| 26 |
struct SerialTrtriInternalLower { |
| 27 |
template <typename ValueType> |
| 28 |
KOKKOS_INLINE_FUNCTION static int invoke(const bool use_unit_diag, const int am, const int an, |
| 29 |
ValueType *KOKKOS_RESTRICT A, const int as0, const int as1); |
| 30 |
}; |
| 31 |
|
| 32 |
template <typename AlgoType> |
| 33 |
struct SerialTrtriInternalUpper { |
| 34 |
template <typename ValueType> |
| 35 |
KOKKOS_INLINE_FUNCTION static int invoke(const bool use_unit_diag, const int am, const int an, |
| 36 |
ValueType *KOKKOS_RESTRICT A, const int as0, const int as1); |
| 37 |
}; |
| 38 |
|
| 39 |
template <> |
| 40 |
template <typename ValueType> |
| 41 |
KOKKOS_INLINE_FUNCTION int SerialTrtriInternalLower<Algo::Trtri::Unblocked>::invoke(const bool use_unit_diag, |
| 42 |
const int am, const int /*an*/, |
| 43 |
ValueType *KOKKOS_RESTRICT A, |
| 44 |
const int as0, const int as1) { |
| 45 |
ValueType one(1.0), zero(0.0), A_ii; |
| 46 |
if (!use_unit_diag) { |
| 47 |
#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL) |
| 48 |
#pragma unroll |
| 49 |
#endif |
| 50 |
// Check for singularity |
| 51 |
for (int i = 0; i < am; ++i) |
| 52 |
if (A[i * as0 + i * as1] == zero) return i + 1; |
| 53 |
} |
| 54 |
|
| 55 |
#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL) |
| 56 |
#pragma unroll |
| 57 |
#endif |
| 58 |
for (int i = am - 1; i >= 0; --i) { |
| 59 |
A[i * as0 + i * as1] = one / A[i * as0 + i * as1]; |
| 60 |
|
| 61 |
if (i < am - 1) { |
| 62 |
if (use_unit_diag) |
| 63 |
A_ii = -one; |
| 64 |
else |
| 65 |
A_ii = -A[i * as0 + i * as1]; |
| 66 |
|
| 67 |
ValueType *KOKKOS_RESTRICT A_subblock = &A[(i + 1) * as0 + (i + 1) * as1]; |
| 68 |
int A_subblock_m = am - i - 1, A_subblock_n = am - i - 1; |
| 69 |
ValueType *KOKKOS_RESTRICT A_col_vec = &A[(i + 1) * as0 + i * as1]; |
| 70 |
int A_col_vec_m = am - i - 1, A_col_vec_n = 1; |
| 71 |
// TRMV/TRMM −− x=Ax |
| 72 |
// A((j+1):n,j) = A((j+1):n,(j+1):n) ∗ A((j+1):n,j) ; |
| 73 |
SerialTrmmInternalLeftLower<Algo::Trmm::Unblocked>::invoke(use_unit_diag, false, A_subblock_m, A_subblock_n, |
| 74 |
A_col_vec_m, A_col_vec_n, one, A_subblock, as0, as1, |
| 75 |
A_col_vec, as0, as1); |
| 76 |
|
| 77 |
// SCAL -- x=ax |
| 78 |
// A((j+1):n,j) = A_ii * A((j+1):n,j) |
| 79 |
KokkosBlas::Impl::SerialScaleInternal::invoke(A_col_vec_m, A_col_vec_n, A_ii, A_col_vec, as0, as1); |
| 80 |
} |
| 81 |
} |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
template <> |
| 86 |
template <typename ValueType> |
| 87 |
KOKKOS_INLINE_FUNCTION int SerialTrtriInternalUpper<Algo::Trtri::Unblocked>::invoke(const bool use_unit_diag, |
| 88 |
const int am, const int /*an*/, |
| 89 |
ValueType *KOKKOS_RESTRICT A, |
| 90 |
const int as0, const int as1) { |
| 91 |
ValueType one(1.0), zero(0.0), A_ii; |
| 92 |
|
| 93 |
if (!use_unit_diag) { |
| 94 |
#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL) |
| 95 |
#pragma unroll |
| 96 |
#endif |
| 97 |
// Check for singularity |
| 98 |
for (int i = 0; i < am; ++i) |
| 99 |
if (A[i * as0 + i * as1] == zero) return i + 1; |
| 100 |
} |
| 101 |
|
| 102 |
#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL) |
| 103 |
#pragma unroll |
| 104 |
#endif |
| 105 |
for (int i = 0; i < am; ++i) { |
| 106 |
A[i * as0 + i * as1] = one / A[i * as0 + i * as1]; |
| 107 |
|
| 108 |
if (i > 0) { |
| 109 |
if (use_unit_diag) |
| 110 |
A_ii = -one; |
| 111 |
else |
| 112 |
A_ii = -A[i * as0 + i * as1]; |
| 113 |
|
| 114 |
ValueType *KOKKOS_RESTRICT A_subblock = &A[0 * as0 + 0 * as1]; |
| 115 |
int A_subblock_m = i, A_subblock_n = i; |
| 116 |
ValueType *KOKKOS_RESTRICT A_col_vec = &A[0 * as0 + i * as1]; |
| 117 |
int A_col_vec_m = i, A_col_vec_n = 1; |
| 118 |
// TRMV/TRMM −− x=Ax |
| 119 |
// A(1:(j-1),j) = A(1:(j-1),1:(j-1)) ∗ A(1:(j-1),j) ; |
| 120 |
// SerialTrmm<Side::Left,Uplo::Lower,Trans::NoTranspose,Diag::NoUnit,Algo::Trmm::Unblocked> |
| 121 |
SerialTrmmInternalLeftUpper<Algo::Trmm::Unblocked>::invoke(use_unit_diag, false, A_subblock_m, A_subblock_n, |
| 122 |
A_col_vec_m, A_col_vec_n, one, A_subblock, as0, as1, |
| 123 |
A_col_vec, as0, as1); |
| 124 |
|
| 125 |
// SCAL -- x=ax |
| 126 |
// A((j+1):n,j) = A_ii * A((j+1):n,j) |
| 127 |
KokkosBlas::Impl::SerialScaleInternal::invoke(A_col_vec_m, A_col_vec_n, A_ii, A_col_vec, as0, as1); |
| 128 |
} |
| 129 |
} |
| 130 |
return 0; |
| 131 |
} |
| 132 |
} // namespace KokkosBatched |
| 133 |
#endif // KOKKOSBATCHED_TRTRI_SERIAL_INTERNAL_HPP |