Debian Codegraph @

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