| 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_APPLY_HOUSEHOLDER_SERIAL_INTERNAL_HPP |
| 17 |
#define KOKKOSBATCHED_APPLY_HOUSEHOLDER_SERIAL_INTERNAL_HPP |
| 18 |
|
| 19 |
/// \author Kyungjoo Kim ([email protected]) |
| 20 |
|
| 21 |
#include "KokkosBatched_Util.hpp" |
| 22 |
|
| 23 |
namespace KokkosBatched { |
| 24 |
|
| 25 |
/// |
| 26 |
/// Serial Internal Impl |
| 27 |
/// ==================== |
| 28 |
/// |
| 29 |
/// this impl follows the flame interface of householder transformation |
| 30 |
/// |
| 31 |
struct SerialApplyLeftHouseholderInternal { |
| 32 |
template <typename ValueType> |
| 33 |
KOKKOS_INLINE_FUNCTION static int invoke(const int m, const int n, const ValueType* tau, |
| 34 |
/* */ ValueType* u2, const int u2s, |
| 35 |
/* */ ValueType* a1t, const int a1ts, |
| 36 |
/* */ ValueType* A2, const int as0, const int as1, |
| 37 |
/* */ ValueType* w1t) { |
| 38 |
typedef ValueType value_type; |
| 39 |
|
| 40 |
/// u2 m x 1 |
| 41 |
/// a1t 1 x n |
| 42 |
/// A2 m x n |
| 43 |
|
| 44 |
// apply a single householder transform H from the left to a row vector a1t |
| 45 |
// and a matrix A2 |
| 46 |
const value_type inv_tau = value_type(1) / (*tau); |
| 47 |
|
| 48 |
// compute the followings: |
| 49 |
// a1t -= inv(tau)(a1t + u2'A2) |
| 50 |
// A2 -= u2 inv(tau)(a1t + u2'A2) |
| 51 |
|
| 52 |
// w1t = a1t + u2'A2 = A2^T conj(u2) |
| 53 |
// w1t /= tau |
| 54 |
for (int j = 0; j < n; ++j) { |
| 55 |
value_type tmp = a1t[j * a1ts]; |
| 56 |
for (int i = 0; i < m; ++i) tmp += Kokkos::ArithTraits<value_type>::conj(u2[i * u2s]) * A2[i * as0 + j * as1]; |
| 57 |
w1t[j] = tmp * inv_tau; // /= (*tau); |
| 58 |
} |
| 59 |
|
| 60 |
// a1t -= w1t (axpy) |
| 61 |
for (int j = 0; j < n; ++j) a1t[j * a1ts] -= w1t[j]; |
| 62 |
|
| 63 |
// A2 -= u2 w1t (ger) |
| 64 |
for (int j = 0; j < n; ++j) |
| 65 |
for (int i = 0; i < m; ++i) A2[i * as0 + j * as1] -= u2[i * u2s] * w1t[j]; |
| 66 |
|
| 67 |
return 0; |
| 68 |
} |
| 69 |
}; |
| 70 |
|
| 71 |
struct SerialApplyRightHouseholderInternal { |
| 72 |
template <typename ValueType> |
| 73 |
KOKKOS_INLINE_FUNCTION static int invoke(const int m, const int n, const ValueType* tau, |
| 74 |
/* */ ValueType* u2, const int u2s, |
| 75 |
/* */ ValueType* a1, const int a1s, |
| 76 |
/* */ ValueType* A2, const int as0, const int as1, |
| 77 |
/* */ ValueType* w1) { |
| 78 |
typedef ValueType value_type; |
| 79 |
/// u2 n x 1 |
| 80 |
/// a1 m x 1 |
| 81 |
/// A2 m x n |
| 82 |
|
| 83 |
// apply a single householder transform H from the left to a row vector a1t |
| 84 |
// and a matrix A2 |
| 85 |
const value_type inv_tau = value_type(1) / (*tau); |
| 86 |
|
| 87 |
// compute the followings: |
| 88 |
// a1 -= inv(tau)(a1 + A2 u2) |
| 89 |
// A2 -= inv(tau)(a1 + A2 u2) u2' |
| 90 |
|
| 91 |
// w1 = a1 + A2 u2 |
| 92 |
// w1 /= tau |
| 93 |
for (int i = 0; i < m; ++i) { |
| 94 |
value_type tmp = a1[i * a1s]; |
| 95 |
for (int j = 0; j < n; ++j) tmp += A2[i * as0 + j * as1] * u2[j * u2s]; |
| 96 |
w1[i] = tmp * inv_tau; // \= (*tau); |
| 97 |
} |
| 98 |
|
| 99 |
// a1 -= w1 (axpy) |
| 100 |
for (int i = 0; i < m; ++i) a1[i * a1s] -= w1[i]; |
| 101 |
|
| 102 |
// A2 -= w1 * u2' (ger with conjugate) |
| 103 |
for (int j = 0; j < n; ++j) |
| 104 |
for (int i = 0; i < m; ++i) A2[i * as0 + j * as1] -= w1[i] * Kokkos::ArithTraits<ValueType>::conj(u2[j * u2s]); |
| 105 |
|
| 106 |
return 0; |
| 107 |
} |
| 108 |
}; |
| 109 |
|
| 110 |
} // end namespace KokkosBatched |
| 111 |
|
| 112 |
#endif |