Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_TeamVectorSolveUTV.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_UTV_Decl.hpp"
27 #include "KokkosBatched_SolveUTV_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 PivViewType,
36 typename WorkViewType, typename AlgoTagType>
37 struct Functor_TestBatchedTeamVectorSolveUTV {
38 using execution_space = typename DeviceType::execution_space;
39 MatrixViewType _r, _a, _acopy, _u, _v;
40 PivViewType _p;
41 VectorViewType _x, _b;
42 WorkViewType _w;
43
44 KOKKOS_INLINE_FUNCTION
45 Functor_TestBatchedTeamVectorSolveUTV(const MatrixViewType &r, const MatrixViewType &a, const MatrixViewType &acopy,
46 const MatrixViewType &u, const MatrixViewType &v, const PivViewType &p,
47 const VectorViewType &x, const VectorViewType &b, const WorkViewType &w)
48 : _r(r), _a(a), _acopy(acopy), _u(u), _v(v), _p(p), _x(x), _b(b), _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 rr = Kokkos::subview(_r, k, Kokkos::ALL(), Kokkos::ALL());
57 auto ac = Kokkos::subview(_acopy, k, Kokkos::ALL(), Kokkos::ALL());
58
59 auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL());
60 auto uu = Kokkos::subview(_u, k, Kokkos::ALL(), Kokkos::ALL());
61 auto vv = Kokkos::subview(_v, k, Kokkos::ALL(), Kokkos::ALL());
62
63 auto pp = Kokkos::subview(_p, k, Kokkos::ALL());
64
65 auto bb = Kokkos::subview(_b, k, Kokkos::ALL());
66 auto xx = Kokkos::subview(_x, k, Kokkos::ALL());
67
68 auto ww = Kokkos::subview(_w, k, Kokkos::ALL());
69
70 // make diagonal dominant and set xx = 1,2,3,4,5
71 const int m = aa.extent(0), r = rr.extent(1);
72 if (m <= r) {
73 Kokkos::parallel_for(Kokkos::TeamVectorRange(member, m), [&](const int &i) {
74 aa(i, i) += add_this;
75 xx(i) = (i + 1);
76 });
77 } else {
78 Kokkos::parallel_for(Kokkos::TeamVectorRange(member, m * m), [=](const int &ij) {
79 const int i = ij / m, j = ij % m;
80 value_type tmp(0);
81 for (int l = 0; l < r; ++l) tmp += rr(i, l) * rr(j, l);
82 aa(i, j) = tmp;
83 });
84 Kokkos::parallel_for(Kokkos::TeamVectorRange(member, m), [&](const int &i) { xx(i) = (i + 1); });
85 }
86 member.team_barrier(); // finish writing aa, xx
87
88 /// copy for verification
89 TeamVectorCopy<MemberType, Trans::NoTranspose>::invoke(member, aa, ac);
90
91 /// bb = AA*xx
92 KokkosBlas::TeamVectorGemv<MemberType, Trans::NoTranspose, Algo::Gemv::Unblocked>::invoke(member, one, aa, xx, zero,
93 bb);
94 member.team_barrier();
95
96 /// Solving Ax = b using UTV transformation
97 /// A P^T P x = b
98 /// UTV P x = b;
99
100 /// UTV = A P^T
101 int matrix_rank(0);
102 TeamVectorUTV<MemberType, AlgoTagType>::invoke(member, aa, pp, uu, vv, ww, matrix_rank);
103 member.team_barrier();
104
105 TeamVectorSolveUTV<MemberType, AlgoTagType>::invoke(member, matrix_rank, uu, aa, vv, pp, xx, bb, ww);
106 }
107
108 inline void run() {
109 typedef typename MatrixViewType::non_const_value_type value_type;
110 std::string name_region("KokkosBatched::Test::TeamVectorSolveUTV");
111 const std::string name_value_type = Test::value_type_name<value_type>();
112 std::string name = name_region + name_value_type;
113 Kokkos::Profiling::pushRegion(name.c_str());
114
115 const int league_size = _a.extent(0);
116 Kokkos::TeamPolicy<execution_space> policy(league_size, Kokkos::AUTO);
117
118 Kokkos::parallel_for(name.c_str(), policy, *this);
119 Kokkos::Profiling::popRegion();
120 }
121 };
122
123 template <typename DeviceType, typename MatrixViewType, typename VectorViewType, typename PivViewType,
124 typename WorkViewType, typename AlgoTagType>
125 void impl_test_batched_solve_utv(const int N, const int BlkSize) {
126 typedef typename MatrixViewType::non_const_value_type value_type;
127 typedef Kokkos::ArithTraits<value_type> ats;
128 // const value_type one(1);
129 /// randomized input testing views
130 MatrixViewType r("r", N, BlkSize, 3);
131 MatrixViewType a("a", N, BlkSize, BlkSize);
132 MatrixViewType acopy("copy", N, BlkSize, BlkSize);
133 MatrixViewType u("u", N, BlkSize, BlkSize);
134 MatrixViewType v("v", N, BlkSize, BlkSize);
135 PivViewType p("p", N, BlkSize);
136 VectorViewType x("x", N, BlkSize);
137 VectorViewType b("b", N, BlkSize);
138 WorkViewType w("w", N, 3 * BlkSize);
139
140 Kokkos::fence();
141
142 Kokkos::Random_XorShift64_Pool<typename DeviceType::execution_space> random(13718);
143 if (BlkSize <= 3)
144 Kokkos::fill_random(a, random, value_type(1.0));
145 else
146 Kokkos::fill_random(r, random, value_type(1.0));
147
148 Kokkos::fence();
149
150 Functor_TestBatchedTeamVectorSolveUTV<DeviceType, MatrixViewType, VectorViewType, PivViewType, WorkViewType,
151 AlgoTagType>(r, a, acopy, u, v, p, x, b, w)
152 .run();
153
154 Kokkos::fence();
155
156 /// for comparison send it to host
157 auto a_host = Kokkos::create_mirror_view(acopy);
158 auto x_host = Kokkos::create_mirror_view(x);
159 auto b_host = Kokkos::create_mirror_view(b);
160 auto w_host = Kokkos::create_mirror_view(w);
161
162 Kokkos::deep_copy(a_host, acopy);
163 Kokkos::deep_copy(x_host, x);
164 Kokkos::deep_copy(b_host, b);
165
166 /// this is least square; we cannot expect high
167 typedef typename ats::mag_type mag_type;
168 const mag_type eps = 1e3 * ats::epsilon();
169
170 for (int k = 0; k < N; ++k) {
171 mag_type residual(0), norm(0);
172 for (int i = 0; i < BlkSize; ++i) {
173 value_type tmp(0);
174 for (int j = 0; j < BlkSize; ++j) {
175 tmp += a_host(k, i, j) * x_host(k, j);
176 }
177 w_host(k, i) = tmp - b_host(k, i);
178 }
179 for (int i = 0; i < BlkSize; ++i) {
180 value_type tmp(0);
181 for (int j = 0; j < BlkSize; ++j) {
182 tmp += a_host(k, j, i) * w_host(k, j);
183 }
184 residual += ats::abs(tmp) * ats::abs(tmp);
185 norm += ats::abs(b_host(k, i)) * ats::abs(b_host(k, i));
186 }
187 // printf("norm %e, residual %e, rel res %e\n", norm, residual,
188 // residual/norm);
189 EXPECT_NEAR_KK(residual / norm, mag_type(0), eps);
190 }
191 }
192 } // namespace Test
193
194 template <typename DeviceType, typename ValueType, typename IntType, typename AlgoTagType>
195 int test_batched_solve_utv() {
196 #if defined(KOKKOSKERNELS_INST_LAYOUTLEFT)
197 {
198 typedef Kokkos::View<ValueType ***, Kokkos::LayoutLeft, DeviceType> MatrixViewType;
199 typedef Kokkos::View<ValueType **, Kokkos::LayoutLeft, DeviceType> VectorViewType;
200 typedef Kokkos::View<IntType **, Kokkos::LayoutLeft, DeviceType> PivViewType;
201 typedef Kokkos::View<ValueType **, Kokkos::LayoutRight, DeviceType> WorkViewType;
202 Test::impl_test_batched_solve_utv<DeviceType, MatrixViewType, VectorViewType, PivViewType, WorkViewType,
203 AlgoTagType>(0, 10);
204 for (int i = 1; i < 10; ++i) {
205 // printf("Testing: LayoutLeft, Blksize %d\n", i);
206 Test::impl_test_batched_solve_utv<DeviceType, MatrixViewType, VectorViewType, PivViewType, WorkViewType,
207 AlgoTagType>(1024, i);
208 }
209 }
210 #endif
211 #if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT)
212 {
213 typedef Kokkos::View<ValueType ***, Kokkos::LayoutRight, DeviceType> MatrixViewType;
214 typedef Kokkos::View<ValueType **, Kokkos::LayoutRight, DeviceType> VectorViewType;
215 typedef Kokkos::View<IntType **, Kokkos::LayoutRight, DeviceType> PivViewType;
216 typedef Kokkos::View<ValueType **, Kokkos::LayoutRight, DeviceType> WorkViewType;
217 Test::impl_test_batched_solve_utv<DeviceType, MatrixViewType, VectorViewType, PivViewType, WorkViewType,
218 AlgoTagType>(0, 10);
219 for (int i = 1; i < 10; ++i) {
220 // printf("Testing: LayoutRight, Blksize %d\n", i);
221 Test::impl_test_batched_solve_utv<DeviceType, MatrixViewType, VectorViewType, PivViewType, WorkViewType,
222 AlgoTagType>(1024, i);
223 }
224 }
225 #endif
226
227 return 0;
228 }