Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_TeamVectorQR.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 Kyungjoo Kim ([email protected])
17
18 #include "gtest/gtest.h"
19 #include "Kokkos_Core.hpp"
20 #include "Kokkos_Random.hpp"
21
22 #include "KokkosBlas1_set.hpp"
23 #include "KokkosBatched_Copy_Decl.hpp"
24 #include "KokkosBlas2_team_gemv_spec.hpp"
25 #include "KokkosBatched_Trsv_Decl.hpp"
26 #include "KokkosBatched_QR_Decl.hpp"
27 #include "KokkosBatched_ApplyQ_Decl.hpp"
28
29 #include "KokkosKernels_TestUtils.hpp"
30
31 using namespace KokkosBatched;
32
33 namespace Test {
34
35 template <typename DeviceType, typename MatrixViewType, typename VectorViewType, typename WorkViewType,
36 typename AlgoTagType>
37 struct Functor_TestBatchedTeamVectorQR {
38 using execution_space = typename DeviceType::execution_space;
39 MatrixViewType _a;
40 VectorViewType _x, _b, _t;
41 WorkViewType _w;
42
43 KOKKOS_INLINE_FUNCTION
44 Functor_TestBatchedTeamVectorQR(const MatrixViewType &a, const VectorViewType &x, const VectorViewType &b,
45 const VectorViewType &t, const WorkViewType &w)
46 : _a(a), _x(x), _b(b), _t(t), _w(w) {}
47
48 template <typename MemberType>
49 KOKKOS_INLINE_FUNCTION void operator()(const MemberType &member) const {
50 typedef typename MatrixViewType::non_const_value_type value_type;
51 const value_type one(1), zero(0), add_this(10);
52
53 const int k = member.league_rank();
54 auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL());
55 auto bb = Kokkos::subview(_b, k, Kokkos::ALL());
56 auto xx = Kokkos::subview(_x, k, Kokkos::ALL());
57 auto tt = Kokkos::subview(_t, k, Kokkos::ALL());
58 auto ww = Kokkos::subview(_w, k, Kokkos::ALL());
59
60 // make diagonal dominant
61 Kokkos::parallel_for(Kokkos::TeamVectorRange(member, aa.extent(0)), [&](const int &i) { aa(i, i) += add_this; });
62
63 /// xx = 1
64 KokkosBlas::TeamVectorSet<MemberType>::invoke(member, one, xx);
65 member.team_barrier();
66
67 /// bb = AA*xx
68 KokkosBlas::TeamVectorGemv<MemberType, Trans::NoTranspose, Algo::Gemv::Unblocked>::invoke(member, one, aa, xx, zero,
69 bb);
70 member.team_barrier();
71
72 /// AA = QR
73 TeamVectorQR<MemberType, AlgoTagType>::invoke(member, aa, tt, ww);
74 member.team_barrier();
75
76 /// xx = bb;
77 TeamVectorCopy<MemberType, Trans::NoTranspose, 1>::invoke(member, bb, xx);
78 member.team_barrier();
79
80 /// xx = Q^{T}xx;
81 TeamVectorApplyQ<MemberType, Side::Left, Trans::Transpose, Algo::ApplyQ::Unblocked>::invoke(member, aa, tt, xx, ww);
82 member.team_barrier();
83
84 /// xx = R^{-1} xx
85 TeamVectorTrsv<MemberType, Uplo::Upper, Trans::NoTranspose, Diag::NonUnit, Algo::Trsv::Unblocked>::invoke(
86 member, one, aa, xx);
87 }
88
89 inline void run() {
90 typedef typename MatrixViewType::non_const_value_type value_type;
91 std::string name_region("KokkosBatched::Test::TeamVectorQR");
92 const std::string name_value_type = Test::value_type_name<value_type>();
93 std::string name = name_region + name_value_type;
94 Kokkos::Profiling::pushRegion(name.c_str());
95
96 const int league_size = _a.extent(0);
97 Kokkos::TeamPolicy<execution_space> policy(league_size, Kokkos::AUTO);
98
99 Kokkos::parallel_for(name.c_str(), policy, *this);
100 Kokkos::Profiling::popRegion();
101 }
102 };
103
104 template <typename DeviceType, typename MatrixViewType, typename VectorViewType, typename WorkViewType,
105 typename AlgoTagType>
106 void impl_test_batched_qr(const int N, const int BlkSize) {
107 typedef typename MatrixViewType::non_const_value_type value_type;
108 typedef Kokkos::ArithTraits<value_type> ats;
109 const value_type one(1);
110 /// randomized input testing views
111 MatrixViewType a("a", N, BlkSize, BlkSize);
112 VectorViewType x("x", N, BlkSize);
113 VectorViewType b("b", N, BlkSize);
114 VectorViewType t("t", N, BlkSize);
115 WorkViewType w("w", N, BlkSize);
116
117 Kokkos::fence();
118
119 Kokkos::Random_XorShift64_Pool<typename DeviceType::execution_space> random(13718);
120 Kokkos::fill_random(a, random, value_type(1.0));
121
122 Kokkos::fence();
123
124 Functor_TestBatchedTeamVectorQR<DeviceType, MatrixViewType, VectorViewType, WorkViewType, AlgoTagType>(a, x, b, t, w)
125 .run();
126
127 Kokkos::fence();
128
129 /// for comparison send it to host
130 typename VectorViewType::HostMirror x_host = Kokkos::create_mirror_view(x);
131 Kokkos::deep_copy(x_host, x);
132
133 /// check x = 1; this eps is about 1e-14
134 typedef typename ats::mag_type mag_type;
135 const mag_type eps = 1e3 * ats::epsilon();
136
137 for (int k = 0; k < N; ++k) {
138 for (int i = 0; i < BlkSize; ++i) {
139 const mag_type sum = ats::abs(x_host(k, i));
140 const mag_type diff = ats::abs(x_host(k, i) - one);
141 EXPECT_NEAR_KK(diff / sum, mag_type(0), eps);
142 // printf("k = %d, i = %d, sum %e diff %e \n", k, i, sum, diff );
143 }
144 }
145 }
146 } // namespace Test
147
148 template <typename DeviceType, typename ValueType, typename AlgoTagType>
149 int test_batched_qr() {
150 #if defined(KOKKOSKERNELS_INST_LAYOUTLEFT)
151 {
152 typedef Kokkos::View<ValueType ***, Kokkos::LayoutLeft, DeviceType> MatrixViewType;
153 typedef Kokkos::View<ValueType **, Kokkos::LayoutLeft, DeviceType> VectorViewType;
154 typedef Kokkos::View<ValueType **, Kokkos::LayoutRight, DeviceType> WorkViewType;
155 Test::impl_test_batched_qr<DeviceType, MatrixViewType, VectorViewType, WorkViewType, AlgoTagType>(0, 10);
156 for (int i = 1; i < 10; ++i) {
157 // printf("Testing: LayoutLeft, Blksize %d\n", i);
158 Test::impl_test_batched_qr<DeviceType, MatrixViewType, VectorViewType, WorkViewType, AlgoTagType>(1024, i);
159 }
160 }
161 #endif
162 #if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT)
163 {
164 typedef Kokkos::View<ValueType ***, Kokkos::LayoutRight, DeviceType> MatrixViewType;
165 typedef Kokkos::View<ValueType **, Kokkos::LayoutRight, DeviceType> VectorViewType;
166 typedef Kokkos::View<ValueType **, Kokkos::LayoutRight, DeviceType> WorkViewType;
167 Test::impl_test_batched_qr<DeviceType, MatrixViewType, VectorViewType, WorkViewType, AlgoTagType>(0, 10);
168 for (int i = 1; i < 10; ++i) {
169 // printf("Testing: LayoutRight, Blksize %d\n", i);
170 Test::impl_test_batched_qr<DeviceType, MatrixViewType, VectorViewType, WorkViewType, AlgoTagType>(1024, i);
171 }
172 }
173 #endif
174
175 return 0;
176 }