| 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_LU_SERIAL_INTERNAL_HPP |
| 17 |
#define KOKKOSBATCHED_LU_SERIAL_INTERNAL_HPP |
| 18 |
|
| 19 |
/// \author Kyungjoo Kim ([email protected]) |
| 20 |
|
| 21 |
#include "KokkosBatched_Util.hpp" |
| 22 |
#include "KokkosBatched_Vector.hpp" |
| 23 |
#include "KokkosBatched_InnerLU_Serial_Impl.hpp" |
| 24 |
#include "KokkosBatched_InnerTrsm_Serial_Impl.hpp" |
| 25 |
#include "KokkosBatched_Gemm_Serial_Internal.hpp" |
| 26 |
|
| 27 |
namespace KokkosBatched { |
| 28 |
|
| 29 |
/// |
| 30 |
/// Serial Internal Impl |
| 31 |
/// ==================== |
| 32 |
|
| 33 |
template <typename AlgoType> |
| 34 |
struct SerialLU_Internal { |
| 35 |
template <typename ValueType> |
| 36 |
KOKKOS_INLINE_FUNCTION static int invoke(const int m, const int n, ValueType *KOKKOS_RESTRICT A, const int as0, |
| 37 |
const int as1, const typename MagnitudeScalarType<ValueType>::type tiny); |
| 38 |
}; |
| 39 |
|
| 40 |
template <> |
| 41 |
template <typename ValueType> |
| 42 |
KOKKOS_INLINE_FUNCTION int SerialLU_Internal<Algo::LU::Unblocked>::invoke( |
| 43 |
const int m, const int n, ValueType *KOKKOS_RESTRICT A, const int as0, const int as1, |
| 44 |
const typename MagnitudeScalarType<ValueType>::type tiny) { |
| 45 |
const int k = (m < n ? m : n); |
| 46 |
if (k <= 0) return 0; |
| 47 |
|
| 48 |
using mst = typename MagnitudeScalarType<ValueType>::type; |
| 49 |
const auto abs_tiny = tiny > 0 ? tiny : mst(-tiny); |
| 50 |
const auto minus_abs_tiny = -abs_tiny; |
| 51 |
|
| 52 |
for (int p = 0; p < k; ++p) { |
| 53 |
const int iend = m - p - 1, jend = n - p - 1; |
| 54 |
|
| 55 |
const ValueType *KOKKOS_RESTRICT a12t = A + (p)*as0 + (p + 1) * as1; |
| 56 |
|
| 57 |
ValueType *KOKKOS_RESTRICT a21 = A + (p + 1) * as0 + (p)*as1, |
| 58 |
*KOKKOS_RESTRICT A22 = A + (p + 1) * as0 + (p + 1) * as1; |
| 59 |
|
| 60 |
if (tiny != 0) { |
| 61 |
ValueType &alpha11_reference = A[p * as0 + p * as1]; |
| 62 |
const auto alpha11_real = Kokkos::ArithTraits<ValueType>::real(alpha11_reference); |
| 63 |
alpha11_reference += minus_abs_tiny * ValueType(alpha11_real < 0); |
| 64 |
alpha11_reference += abs_tiny * ValueType(alpha11_real >= 0); |
| 65 |
} |
| 66 |
|
| 67 |
const ValueType alpha11 = A[p * as0 + p * as1]; |
| 68 |
|
| 69 |
for (int i = 0; i < iend; ++i) { |
| 70 |
// a21[i*as0] *= inv_alpha11; |
| 71 |
a21[i * as0] /= alpha11; |
| 72 |
|
| 73 |
#if defined(KOKKOS_ENABLE_PRAGMA_UNROLL) |
| 74 |
#pragma unroll |
| 75 |
#endif |
| 76 |
for (int j = 0; j < jend; ++j) A22[i * as0 + j * as1] -= a21[i * as0] * a12t[j * as1]; |
| 77 |
} |
| 78 |
} |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
template <> |
| 83 |
template <typename ValueType> |
| 84 |
KOKKOS_INLINE_FUNCTION int SerialLU_Internal<Algo::LU::Blocked>::invoke( |
| 85 |
const int m, const int n, ValueType *KOKKOS_RESTRICT A, const int as0, const int as1, |
| 86 |
const typename MagnitudeScalarType<ValueType>::type /*tiny*/) { |
| 87 |
constexpr int mbAlgo = Algo::LU::Blocked::mb(); |
| 88 |
const typename MagnitudeScalarType<ValueType>::type one(1.0), minus_one(-1.0); |
| 89 |
|
| 90 |
const int k = (m < n ? m : n); |
| 91 |
if (k <= 0) return 0; |
| 92 |
|
| 93 |
InnerLU<mbAlgo> lu(as0, as1); |
| 94 |
|
| 95 |
InnerTrsmLeftLowerUnitDiag<mbAlgo> trsm_llu(as0, as1, as0, as1); |
| 96 |
InnerTrsmLeftLowerNonUnitDiag<mbAlgo> trsm_run(as1, as0, as1, as0); |
| 97 |
|
| 98 |
auto lu_factorize = [&](const int ib, const int jb, ValueType *KOKKOS_RESTRICT AA) { |
| 99 |
const int mb = mbAlgo; |
| 100 |
const int kb = ib < jb ? ib : jb; |
| 101 |
for (int p = 0; p < kb; p += mb) { |
| 102 |
const int pb = (p + mb) > kb ? (kb - p) : mb; |
| 103 |
|
| 104 |
// diagonal block |
| 105 |
ValueType *KOKKOS_RESTRICT Ap = AA + p * as0 + p * as1; |
| 106 |
|
| 107 |
// lu on a block |
| 108 |
lu.serial_invoke(pb, Ap); |
| 109 |
|
| 110 |
// dimension ABR |
| 111 |
const int m_abr = ib - p - mb, n_abr = jb - p - mb; |
| 112 |
|
| 113 |
// trsm update |
| 114 |
trsm_llu.serial_invoke(Ap, pb, n_abr, Ap + mb * as1); |
| 115 |
trsm_run.serial_invoke(Ap, pb, m_abr, Ap + mb * as0); |
| 116 |
|
| 117 |
// gemm update |
| 118 |
SerialGemmInternal<Algo::Gemm::Blocked>::invoke(m_abr, n_abr, pb, minus_one, Ap + mb * as0, as0, as1, |
| 119 |
Ap + mb * as1, as0, as1, one, Ap + mb * as0 + mb * as1, as0, as1); |
| 120 |
} |
| 121 |
}; |
| 122 |
|
| 123 |
const bool is_small = true; //(m*n <= 64*64); |
| 124 |
if (is_small) { |
| 125 |
lu_factorize(m, n, A); |
| 126 |
} else { |
| 127 |
// // some cache blocking may need (not priority yet); |
| 128 |
// lu_factorize(m, n, A); |
| 129 |
} |
| 130 |
|
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
} // namespace KokkosBatched |
| 135 |
|
| 136 |
#endif |