| 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 |
/// \author Kim Liegeois ([email protected]) |
| 17 |
|
| 18 |
#include "gtest/gtest.h" |
| 19 |
#include "Kokkos_Core.hpp" |
| 20 |
#include "Kokkos_Random.hpp" |
| 21 |
|
| 22 |
#include "KokkosBatched_Gesv.hpp" |
| 23 |
#include "KokkosBatched_Dot.hpp" |
| 24 |
#include "KokkosBlas2_serial_gemv_impl.hpp" |
| 25 |
|
| 26 |
#include "KokkosKernels_TestUtils.hpp" |
| 27 |
|
| 28 |
#include "Test_Batched_DenseUtils.hpp" |
| 29 |
|
| 30 |
using namespace KokkosBatched; |
| 31 |
|
| 32 |
namespace Test { |
| 33 |
namespace TeamVectorGesv { |
| 34 |
|
| 35 |
template <typename DeviceType, typename MatrixType, typename VectorType, typename AlgoTagType> |
| 36 |
struct Functor_TestBatchedTeamVectorGesv { |
| 37 |
using execution_space = typename DeviceType::execution_space; |
| 38 |
const MatrixType _A; |
| 39 |
const VectorType _X; |
| 40 |
const VectorType _B; |
| 41 |
|
| 42 |
KOKKOS_INLINE_FUNCTION |
| 43 |
Functor_TestBatchedTeamVectorGesv(const MatrixType &A, const VectorType &X, const VectorType &B) |
| 44 |
: _A(A), _X(X), _B(B) {} |
| 45 |
|
| 46 |
template <typename MemberType> |
| 47 |
KOKKOS_INLINE_FUNCTION void operator()(const MemberType &member) const { |
| 48 |
const int matrix_id = static_cast<int>(member.league_rank()); |
| 49 |
auto A = Kokkos::subview(_A, matrix_id, Kokkos::ALL, Kokkos::ALL); |
| 50 |
auto x = Kokkos::subview(_X, matrix_id, Kokkos::ALL); |
| 51 |
auto b = Kokkos::subview(_B, matrix_id, Kokkos::ALL); |
| 52 |
|
| 53 |
member.team_barrier(); |
| 54 |
KokkosBatched::TeamVectorGesv<MemberType, AlgoTagType>::invoke(member, A, x, b); |
| 55 |
member.team_barrier(); |
| 56 |
} |
| 57 |
|
| 58 |
inline void run() { |
| 59 |
typedef typename VectorType::value_type value_type; |
| 60 |
std::string name_region("KokkosBatched::Test::TeamVectorGesv"); |
| 61 |
const std::string name_value_type = Test::value_type_name<value_type>(); |
| 62 |
std::string name = name_region + name_value_type; |
| 63 |
Kokkos::Profiling::pushRegion(name.c_str()); |
| 64 |
Kokkos::TeamPolicy<execution_space> policy(_X.extent(0), Kokkos::AUTO(), Kokkos::AUTO()); |
| 65 |
|
| 66 |
using MatrixViewType = Kokkos::View<typename MatrixType::non_const_value_type **, typename MatrixType::array_layout, |
| 67 |
typename MatrixType::execution_space>; |
| 68 |
|
| 69 |
const int n = _A.extent(1); |
| 70 |
size_t bytes_0 = MatrixViewType::shmem_size(n, n + 4); |
| 71 |
policy.set_scratch_size(0, Kokkos::PerTeam(bytes_0)); |
| 72 |
|
| 73 |
Kokkos::parallel_for(name.c_str(), policy, *this); |
| 74 |
Kokkos::Profiling::popRegion(); |
| 75 |
} |
| 76 |
}; |
| 77 |
|
| 78 |
template <typename DeviceType, typename MatrixType, typename VectorType, typename AlgoTagType> |
| 79 |
void impl_test_batched_gesv(const int N, const int BlkSize) { |
| 80 |
typedef typename MatrixType::value_type value_type; |
| 81 |
typedef Kokkos::ArithTraits<value_type> ats; |
| 82 |
|
| 83 |
using MagnitudeType = typename Kokkos::ArithTraits<value_type>::mag_type; |
| 84 |
using NormViewType = Kokkos::View<MagnitudeType *, Kokkos::LayoutLeft, DeviceType>; |
| 85 |
|
| 86 |
NormViewType sqr_norm_j("sqr_norm_j", N); |
| 87 |
auto sqr_norm_j_host = Kokkos::create_mirror_view(sqr_norm_j); |
| 88 |
|
| 89 |
MatrixType A("A", N, BlkSize, BlkSize), A2("A", N, BlkSize, BlkSize); |
| 90 |
VectorType B("b", N, BlkSize), B2("b", N, BlkSize), X("x", N, BlkSize); |
| 91 |
|
| 92 |
create_tridiagonal_batched_matrices(A, B); |
| 93 |
Kokkos::deep_copy(A2, A); |
| 94 |
Kokkos::deep_copy(B2, B); |
| 95 |
|
| 96 |
auto A_host = Kokkos::create_mirror_view(A2); |
| 97 |
auto B_host = Kokkos::create_mirror_view(B2); |
| 98 |
auto X_host = Kokkos::create_mirror_view(X); |
| 99 |
|
| 100 |
Kokkos::deep_copy(A_host, A2); |
| 101 |
Kokkos::deep_copy(B_host, B2); |
| 102 |
|
| 103 |
Kokkos::fence(); |
| 104 |
|
| 105 |
Functor_TestBatchedTeamVectorGesv<DeviceType, MatrixType, VectorType, AlgoTagType>(A, X, B).run(); |
| 106 |
|
| 107 |
Kokkos::fence(); |
| 108 |
|
| 109 |
Kokkos::deep_copy(X_host, X); |
| 110 |
|
| 111 |
for (int l = 0; l < N; ++l) |
| 112 |
KokkosBlas::SerialGemv<Trans::NoTranspose, KokkosBlas::Algo::Gemv::Unblocked>::invoke( |
| 113 |
-1, Kokkos::subview(A_host, l, Kokkos::ALL, Kokkos::ALL), Kokkos::subview(X_host, l, Kokkos::ALL), 1, |
| 114 |
Kokkos::subview(B_host, l, Kokkos::ALL)); |
| 115 |
|
| 116 |
KokkosBatched::SerialDot<Trans::NoTranspose>::invoke(B_host, B_host, sqr_norm_j_host); |
| 117 |
|
| 118 |
const MagnitudeType eps = 1.0e3 * ats::epsilon(); |
| 119 |
|
| 120 |
for (int l = 0; l < N; ++l) EXPECT_NEAR_KK(sqr_norm_j_host(l), 0, eps); |
| 121 |
} |
| 122 |
} // namespace TeamVectorGesv |
| 123 |
} // namespace Test |
| 124 |
|
| 125 |
template <typename DeviceType, typename ValueType, typename AlgoTagType> |
| 126 |
int test_batched_teamvector_gesv() { |
| 127 |
#if defined(KOKKOSKERNELS_INST_LAYOUTLEFT) |
| 128 |
{ |
| 129 |
typedef Kokkos::View<ValueType ***, Kokkos::LayoutLeft, DeviceType> MatrixType; |
| 130 |
typedef Kokkos::View<ValueType **, Kokkos::LayoutLeft, DeviceType> VectorType; |
| 131 |
|
| 132 |
for (int i = 3; i < 10; ++i) { |
| 133 |
Test::TeamVectorGesv::impl_test_batched_gesv<DeviceType, MatrixType, VectorType, AlgoTagType>(1024, i); |
| 134 |
} |
| 135 |
} |
| 136 |
#endif |
| 137 |
#if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT) |
| 138 |
{ |
| 139 |
typedef Kokkos::View<ValueType ***, Kokkos::LayoutRight, DeviceType> MatrixType; |
| 140 |
typedef Kokkos::View<ValueType **, Kokkos::LayoutRight, DeviceType> VectorType; |
| 141 |
|
| 142 |
for (int i = 3; i < 10; ++i) { |
| 143 |
Test::TeamVectorGesv::impl_test_batched_gesv<DeviceType, MatrixType, VectorType, AlgoTagType>(1024, i); |
| 144 |
} |
| 145 |
} |
| 146 |
#endif |
| 147 |
|
| 148 |
return 0; |
| 149 |
} |