Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_TeamGesv.hpp

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